From 6ad9af972e506d3c437141e85a96b3ad4dd46a40 Mon Sep 17 00:00:00 2001 From: Ilya Matveev Date: Thu, 26 Aug 2021 16:15:44 +0700 Subject: [PATCH] [Gradle] Move K/N binary options retrieving to PropertiesProvider --- .../kotlin/gradle/plugin/KotlinProperties.kt | 30 ++++++++++++++++++- .../targets/native/tasks/KotlinNativeTasks.kt | 14 ++------- 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/KotlinProperties.kt b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/KotlinProperties.kt index 2030e0b9de2..3c0a44df8ad 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/KotlinProperties.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/KotlinProperties.kt @@ -262,6 +262,16 @@ internal fun PropertiesProvider.mapKotlinDaemonProperties(task: CompileUsingKotl val nativeUseEmbeddableCompilerJar: Boolean get() = booleanProperty("kotlin.native.useEmbeddableCompilerJar") ?: false + /** + * Allows a user to set project-wide options that will be passed to the K/N compiler via -Xbinary flag. + * E.g. setting kotlin.native.binary.memoryModel=experimental results in passing -Xbinary=memoryModel=experimental to the compiler. + * @return a map: property name without `kotlin.native.binary.` prefix -> property value + */ + val nativeBinaryOptions: Map + get() = propertiesWithPrefix(KOTLIN_NATIVE_BINARY_OPTION_PREFIX).mapKeys { (key, _) -> + key.removePrefix(KOTLIN_NATIVE_BINARY_OPTION_PREFIX) + } + /** * Allows a user to specify additional arguments of a JVM executing KLIB commonizer. */ @@ -391,7 +401,23 @@ internal fun PropertiesProvider.mapKotlinDaemonProperties(task: CompileUsingKotl localProperties.getProperty(propName) } - object PropertyNames { + private fun propertiesWithPrefix(prefix: String): Map { + val result = mutableMapOf() + project.properties.forEach { (name, value) -> + if (name.startsWith(prefix) && value is String) { + result.put(name, value) + } + } + localProperties.forEach { (name, value) -> + if (name is String && name.startsWith(prefix) && value is String) { + // Project properties have higher priority. + result.putIfAbsent(name, value) + } + } + return result + } + + object PropertyNames { const val KOTLIN_MPP_ENABLE_GRANULAR_SOURCE_SETS_METADATA = "kotlin.mpp.enableGranularSourceSetsMetadata" const val KOTLIN_MPP_HIERARCHICAL_STRUCTURE_BY_DEFAULT = "kotlin.internal.mpp.hierarchicalStructureByDefault" const val KOTLIN_MPP_HIERARCHICAL_STRUCTURE_SUPPORT = "kotlin.mpp.hierarchicalStructureSupport" @@ -405,6 +431,8 @@ internal fun PropertiesProvider.mapKotlinDaemonProperties(task: CompileUsingKotl internal const val KOTLIN_NATIVE_IGNORE_INCORRECT_DEPENDENCIES = "kotlin.native.ignoreIncorrectDependencies" + private const val KOTLIN_NATIVE_BINARY_OPTION_PREFIX = "kotlin.native.binary." + operator fun invoke(project: Project): PropertiesProvider = with(project.extensions.extraProperties) { if (!has(CACHED_PROVIDER_EXT_NAME)) { diff --git a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/targets/native/tasks/KotlinNativeTasks.kt b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/targets/native/tasks/KotlinNativeTasks.kt index f830f72fccb..3e946d3ab99 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/targets/native/tasks/KotlinNativeTasks.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/targets/native/tasks/KotlinNativeTasks.kt @@ -28,6 +28,7 @@ import org.jetbrains.kotlin.gradle.dsl.KotlinCompile import org.jetbrains.kotlin.gradle.dsl.NativeCacheKind import org.jetbrains.kotlin.gradle.internal.ensureParentDirsCreated import org.jetbrains.kotlin.gradle.plugin.KotlinCompilation +import org.jetbrains.kotlin.gradle.plugin.PropertiesProvider import org.jetbrains.kotlin.gradle.plugin.cocoapods.asValidFrameworkName import org.jetbrains.kotlin.gradle.plugin.mpp.* import org.jetbrains.kotlin.gradle.plugin.mpp.pm20.KotlinNativeCompilationData @@ -593,14 +594,7 @@ constructor( @Input get() = binary.binaryOptions val projectWideBinaryOptions: Map - @Input get() = project.properties.mapNotNull { (name, value) -> - val prefix = KOTLIN_NATIVE_BINARY_OPTION_PREFIX - if (name.startsWith(prefix) && value is String) { - name.removePrefix(prefix) to value - } else { - null - } - }.toMap() + @Input get() = PropertiesProvider(project).nativeBinaryOptions val processTests: Boolean @Input get() = binary is TestExecutable @@ -709,10 +703,6 @@ constructor( validatedExportedLibraries() super.compile() } - - private companion object { - const val KOTLIN_NATIVE_BINARY_OPTION_PREFIX = "kotlin.native.binary." - } } private class ExternalDependenciesBuilder(