From 33ef4452b742be1fdb74e9e57ad905dacc16f2be Mon Sep 17 00:00:00 2001 From: Sergey Igushkin Date: Mon, 2 Mar 2020 19:18:31 +0300 Subject: [PATCH] Always create shared-Native metadata compilations Create the compilations even when all of the targets that the shared-Native source set is compiled for are disabled on the current host. In that case, disable the compilation task Also clear the inputs of the disabled tasks so that when Gradle builds the task graph it doesn't resolve the dependencies. Otherwise, a Gradle build that includes the compile tasks of the disabled targets in the task graph would fail as follows, e.g. on a macOS host with a project containing a mingw target): ``` Could not determine the dependencies of task ':compileKotlinMingwX86' ... Could not resolve all task dependencies for configuration ':mingwX86CompileKlibraries' ... Could not find com.example:my-dependency-mingwx86:1.0 ... --- .../KotlinMetadataTargetConfigurator.kt | 20 ++++++++++++------- .../targets/native/tasks/KotlinNativeTasks.kt | 6 +++++- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/targets/metadata/KotlinMetadataTargetConfigurator.kt b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/targets/metadata/KotlinMetadataTargetConfigurator.kt index c9197c9ebf4..993f7f7e2a5 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/targets/metadata/KotlinMetadataTargetConfigurator.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/targets/metadata/KotlinMetadataTargetConfigurator.kt @@ -258,8 +258,9 @@ class KotlinMetadataTargetConfigurator(kotlinPluginVersion: String) : val compilationName = sourceSet.name - val isNativeSourceSet = CompilationSourceSetUtil.compilationsBySourceSets(project).getValue(sourceSet) - .all { compilation -> compilation.target is KotlinNativeTarget } + val platformCompilations = CompilationSourceSetUtil.compilationsBySourceSets(project).getValue(sourceSet) + + val isNativeSourceSet = platformCompilations.all { compilation -> compilation.target is KotlinNativeTarget } val compilationFactory: KotlinCompilationFactory> = when { isNativeSourceSet -> KotlinSharedNativeCompilationFactory(target) @@ -275,6 +276,14 @@ class KotlinMetadataTargetConfigurator(kotlinPluginVersion: String) : if (!isHostSpecific) { val metadataContent = project.filesWithUnpackedArchives(this@apply.output.allOutputs, setOf("klib")) allMetadataJar.from(metadataContent) { spec -> spec.into(this@apply.defaultSourceSet.name) } + } else { + if (platformCompilations.filterIsInstance().none { it.konanTarget.enabledOnCurrentHost }) { + // Then we don't have any platform module to put this compiled source set to, so disable the compilation task: + compileKotlinTaskHolder.configure { it.enabled = false } + // Also clear the dependency files (classpath) of the compilation so that the host-specific dependencies are + // not resolved: + compileDependencyFiles = project.files() + } } } } @@ -476,11 +485,8 @@ internal fun getPublishedCommonSourceSets(project: Project): Set compilations.map { it.target.platformType }.distinct().run { - size > 1 && toSet() != setOf(KotlinPlatformType.androidJvm, KotlinPlatformType.jvm) || ( - singleOrNull() == KotlinPlatformType.native && - compilations.map { it.target }.distinct().size > 1 && - compilations.any { (it as AbstractKotlinNativeCompilation).konanTarget.enabledOnCurrentHost } - ) + size > 1 && toSet() != setOf(KotlinPlatformType.androidJvm, KotlinPlatformType.jvm) || + singleOrNull() == KotlinPlatformType.native && compilations.map { it.target }.distinct().size > 1 // TODO: platform-shared source sets other than Kotlin/Native ones are not yet supported; support will be needed for JVM, JS } } 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 f3549a679a1..f5587de1fc3 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 @@ -128,7 +128,11 @@ abstract class AbstractKotlinNativeCompile : Abstra // Inputs and outputs val libraries: FileCollection - @InputFiles get() = compilation.compileDependencyFiles.filterOutPublishableInteropLibs(project) + @InputFiles get() = + // Avoid resolving these dependencies during task graph construction when we can't build the target: + if (compilation.konanTarget.enabledOnCurrentHost) + compilation.compileDependencyFiles.filterOutPublishableInteropLibs(project) + else project.files() override fun getClasspath(): FileCollection = libraries override fun setClasspath(configuration: FileCollection?) {