[KT-54284][Native] Produce deterministic klibs from cinterop. (#4979)
Pass in temp-file C sources to clang via stdin instead of by name. This prevents absolute paths of temp files from being encoded in bitcode.
This commit is contained in:
+4
-4
@@ -16,10 +16,10 @@
|
|||||||
|
|
||||||
package org.jetbrains.kotlin.native.interop.indexer
|
package org.jetbrains.kotlin.native.interop.indexer
|
||||||
|
|
||||||
enum class Language(val sourceFileExtension: String) {
|
enum class Language(val sourceFileExtension: String, val clangLanguageName: String) {
|
||||||
C("c"),
|
C("c", "c"),
|
||||||
CPP("cpp"),
|
CPP("cpp", "c++"),
|
||||||
OBJECTIVE_C("m")
|
OBJECTIVE_C("m", "objective-c")
|
||||||
}
|
}
|
||||||
|
|
||||||
interface HeaderInclusionPolicy {
|
interface HeaderInclusionPolicy {
|
||||||
|
|||||||
+10
-5
@@ -105,9 +105,12 @@ private fun List<String>?.isTrue(): Boolean {
|
|||||||
return this?.last() == "true"
|
return this?.last() == "true"
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun runCmd(command: Array<String>, verbose: Boolean = false) {
|
private fun runCmd(command: Array<String>, verbose: Boolean = false, redirectInputFile: File? = null) {
|
||||||
if (verbose) println("COMMAND: " + command.joinToString(" "))
|
if (verbose) {
|
||||||
Command(*command).getOutputLines(true).let { lines ->
|
val redirect = if (redirectInputFile == null) "" else " < ${redirectInputFile.path}"
|
||||||
|
println("COMMAND: " + command.joinToString(" ") + redirect)
|
||||||
|
}
|
||||||
|
Command(command.toList(), redirectInputFile = redirectInputFile).getOutputLines(true).let { lines ->
|
||||||
if (verbose) lines.forEach(::println)
|
if (verbose) lines.forEach(::println)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -384,9 +387,11 @@ private fun processCLib(flavor: KotlinPlatform, cinteropArguments: CInteropArgum
|
|||||||
}
|
}
|
||||||
KotlinPlatform.NATIVE -> {
|
KotlinPlatform.NATIVE -> {
|
||||||
val outLib = File(nativeLibsDir, "$libName.bc")
|
val outLib = File(nativeLibsDir, "$libName.bc")
|
||||||
|
// Note that the output bitcode contains the source file path, which can lead to non-deterministc builds (see KT-54284).
|
||||||
|
// The source file is passed in via stdin to ensure the output library is deterministic.
|
||||||
val compilerCmd = arrayOf(compiler, *compilerArgs,
|
val compilerCmd = arrayOf(compiler, *compilerArgs,
|
||||||
"-emit-llvm", "-c", outCFile.absolutePath, "-o", outLib.absolutePath)
|
"-emit-llvm", "-x", library.language.clangLanguageName, "-c", "-", "-o", outLib.absolutePath)
|
||||||
runCmd(compilerCmd, verbose)
|
runCmd(compilerCmd, verbose, redirectInputFile = File(outCFile.absolutePath))
|
||||||
outLib.absolutePath
|
outLib.absolutePath
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+12
-3
@@ -18,12 +18,13 @@ package org.jetbrains.kotlin.konan.exec
|
|||||||
|
|
||||||
import org.jetbrains.kotlin.konan.KonanExternalToolFailure
|
import org.jetbrains.kotlin.konan.KonanExternalToolFailure
|
||||||
import java.io.BufferedReader
|
import java.io.BufferedReader
|
||||||
|
import java.io.File
|
||||||
import java.io.InputStreamReader
|
import java.io.InputStreamReader
|
||||||
import java.lang.ProcessBuilder.Redirect
|
import java.lang.ProcessBuilder.Redirect
|
||||||
import java.nio.file.Files
|
import java.nio.file.Files
|
||||||
|
|
||||||
|
|
||||||
open class Command(initialCommand: List<String>) {
|
open class Command(initialCommand: List<String>, val redirectInputFile: File? = null) {
|
||||||
|
|
||||||
constructor(tool: String) : this(listOf(tool))
|
constructor(tool: String) : this(listOf(tool))
|
||||||
constructor(vararg command: String) : this(command.toList<String>())
|
constructor(vararg command: String) : this(command.toList<String>())
|
||||||
@@ -58,7 +59,11 @@ open class Command(initialCommand: List<String>) {
|
|||||||
val builder = ProcessBuilder(command)
|
val builder = ProcessBuilder(command)
|
||||||
|
|
||||||
builder.redirectOutput(Redirect.INHERIT)
|
builder.redirectOutput(Redirect.INHERIT)
|
||||||
builder.redirectInput(Redirect.INHERIT)
|
if (redirectInputFile == null) {
|
||||||
|
builder.redirectInput(Redirect.INHERIT)
|
||||||
|
} else {
|
||||||
|
builder.redirectInput(redirectInputFile)
|
||||||
|
}
|
||||||
|
|
||||||
val process = builder.start()
|
val process = builder.start()
|
||||||
|
|
||||||
@@ -90,7 +95,11 @@ open class Command(initialCommand: List<String>) {
|
|||||||
try {
|
try {
|
||||||
val builder = ProcessBuilder(command)
|
val builder = ProcessBuilder(command)
|
||||||
|
|
||||||
builder.redirectInput(Redirect.INHERIT)
|
if (redirectInputFile == null) {
|
||||||
|
builder.redirectInput(Redirect.INHERIT)
|
||||||
|
} else {
|
||||||
|
builder.redirectInput(redirectInputFile)
|
||||||
|
}
|
||||||
builder.redirectError(Redirect.INHERIT)
|
builder.redirectError(Redirect.INHERIT)
|
||||||
builder.redirectOutput(Redirect.to(outputFile))
|
builder.redirectOutput(Redirect.to(outputFile))
|
||||||
.redirectErrorStream(withErrors)
|
.redirectErrorStream(withErrors)
|
||||||
|
|||||||
Reference in New Issue
Block a user