From aedcfe78e1e333676a56c69207e635c091ffe51c Mon Sep 17 00:00:00 2001 From: Martin Petrov Date: Wed, 10 Nov 2021 22:49:00 -0500 Subject: [PATCH] [Native] Add -Xoverride-konan-properties to cinterop. This aligns with the flag added to kotlinc in: https://youtrack.jetbrains.com/issue/KT-40670 Hermetic builds would benefit from being able to override dependency paths or airplaneMode using this mechanism: ``` $ bin/cinterop -Xoverride-konan-properties "apple-llvm-20200714-macos-x64-essentials.default=/tmp/llvm;airplaneMode=true" -def baz.def ``` --- .../native/interop/gen/jvm/CommandLine.kt | 4 ++++ .../native/interop/gen/jvm/ToolConfig.kt | 4 ++-- .../kotlin/native/interop/gen/jvm/main.kt | 18 +++++++++++++++--- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/kotlin-native/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/jvm/CommandLine.kt b/kotlin-native/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/jvm/CommandLine.kt index 0b4b9777d01..73f96e64f23 100644 --- a/kotlin-native/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/jvm/CommandLine.kt +++ b/kotlin-native/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/jvm/CommandLine.kt @@ -65,6 +65,10 @@ open class CommonInteropArguments(val argParser: ArgParser) { description = "save temporary files to the given directory") val kotlincOption by argParser.option(ArgType.String, "Xkotlinc-option", description = "additional kotlinc compiler option").multiple() + val overrideKonanProperties by argParser.option(ArgType.String, + fullName = "Xoverride-konan-properties", + description = "Override konan.properties.values" + ).multiple().delimiter(";") companion object { val DEFAULT_MODE = GenerationMode.METADATA diff --git a/kotlin-native/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/jvm/ToolConfig.kt b/kotlin-native/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/jvm/ToolConfig.kt index 30107817ee3..7520cad2c04 100644 --- a/kotlin-native/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/jvm/ToolConfig.kt +++ b/kotlin-native/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/jvm/ToolConfig.kt @@ -22,10 +22,10 @@ import org.jetbrains.kotlin.konan.util.defaultTargetSubstitutions import org.jetbrains.kotlin.native.interop.gen.jvm.KotlinPlatform import org.jetbrains.kotlin.native.interop.indexer.Language -class ToolConfig(userProvidedTargetName: String?, private val flavor: KotlinPlatform) { +class ToolConfig(userProvidedTargetName: String?, private val flavor: KotlinPlatform, private val propertyOverrides: Map) { private val konanHome = KonanHomeProvider.determineKonanHome() - private val distribution = customerDistribution(konanHome) + private val distribution = Distribution(konanHome, propertyOverrides = propertyOverrides) private val platformManager = PlatformManager(distribution) private val targetManager = platformManager.targetManager(userProvidedTargetName) private val host = HostManager.host diff --git a/kotlin-native/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/jvm/main.kt b/kotlin-native/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/jvm/main.kt index 229864a10b4..85833c45010 100644 --- a/kotlin-native/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/jvm/main.kt +++ b/kotlin-native/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/jvm/main.kt @@ -214,7 +214,7 @@ private fun processCLib(flavor: KotlinPlatform, cinteropArguments: CInteropArgum cinteropArguments.argParser.printError("-def or -pkg should be provided!") } - val tool = prepareTool(cinteropArguments.target, flavor) + val tool = prepareTool(cinteropArguments.target, flavor, parseKeyValuePairs(cinteropArguments.overrideKonanProperties)) val def = DefFile(defFile, tool.substitutions) val isLinkerOptsSetByUser = (cinteropArguments.linkerOpts.valueOrigin == ArgParser.ValueOrigin.SET_BY_USER) || @@ -453,8 +453,8 @@ private fun resolveDependencies( ).getFullList(TopologicalLibraryOrder) } -internal fun prepareTool(target: String?, flavor: KotlinPlatform): ToolConfig { - val tool = ToolConfig(target, flavor) +internal fun prepareTool(target: String?, flavor: KotlinPlatform, propertyOverrides: Map = emptyMap()): ToolConfig { + val tool = ToolConfig(target, flavor, propertyOverrides) tool.downloadDependencies() System.load(tool.libclang) @@ -527,3 +527,15 @@ internal fun buildNativeLibrary( headerFilter = headerFilter ) } + +private fun parseKeyValuePairs( + argumentValue: List, +): Map = argumentValue.mapNotNull { + val keyValueSeparatorIndex = it.indexOf('=') + if (keyValueSeparatorIndex > 0) { + it.substringBefore('=') to it.substringAfter('=') + } else { + warn("incorrect property format: expected '=', got '$it'") + null + } +}.toMap() \ No newline at end of file