From 292563451caca0d473608677b108fc6fcb8b812c Mon Sep 17 00:00:00 2001 From: Sergey Igushkin Date: Wed, 6 May 2020 20:43:09 +0300 Subject: [PATCH] Fix falsely skipped shared-native source sets in HMPP (KT-38746) Issue #KT-38746 Fixed --- .../jetbrains/kotlin/gradle/KlibBasedMppIT.kt | 24 ++++++++++++ .../hierarchical-all-native/build.gradle.kts | 37 +++++++++++++++++++ .../hierarchical-all-native/gradle.properties | 1 + .../settings.gradle.kts | 12 ++++++ .../src/allNative/kotlin/Foo.kt | 10 +++++ .../src/currentHostAndLinux/kotlin/UseFoo.kt | 10 +++++ .../targets/native/KotlinNativeCompilation.kt | 3 +- 7 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/resources/testProject/hierarchical-all-native/build.gradle.kts create mode 100644 libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/resources/testProject/hierarchical-all-native/gradle.properties create mode 100644 libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/resources/testProject/hierarchical-all-native/settings.gradle.kts create mode 100644 libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/resources/testProject/hierarchical-all-native/src/allNative/kotlin/Foo.kt create mode 100644 libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/resources/testProject/hierarchical-all-native/src/currentHostAndLinux/kotlin/UseFoo.kt diff --git a/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/KlibBasedMppIT.kt b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/KlibBasedMppIT.kt index ffd1118e374..7160afb1109 100644 --- a/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/KlibBasedMppIT.kt +++ b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/KlibBasedMppIT.kt @@ -224,4 +224,28 @@ class KlibBasedMppIT : BaseGradleIT() { assertSuccessful() } } + + @Test + fun testAvoidSkippingSharedNativeSourceSetKt38746() = with(Project("hierarchical-all-native")) { + val targetNames = listOf( + // Try different alphabetical ordering of the targets to ensure that the behavior doesn't depend on it, as with 'first target' + listOf("a1", "a2", "a3"), + listOf("a3", "a1", "a2"), + listOf("a2", "a3", "a1"), + ) + val targetParamNames = listOf("mingwTargetName", "linuxTargetName", "macosTargetName", "currentHostTargetName") + for (names in targetNames) { + val currentHostTargetName = when { + HostManager.hostIsMingw -> names[0] + HostManager.hostIsLinux -> names[1] + HostManager.hostIsMac -> names[2] + else -> error("unexpected host") + } + val params = targetParamNames.zip(names + currentHostTargetName) { k, v -> "-P$k=$v" } + build(":clean", ":compileCurrentHostAndLinuxKotlinMetadata", *params.toTypedArray()) { + assertSuccessful() + assertTasksExecuted(":compileCurrentHostAndLinuxKotlinMetadata", ":compileAllNativeKotlinMetadata") + } + } + } } \ No newline at end of file diff --git a/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/resources/testProject/hierarchical-all-native/build.gradle.kts b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/resources/testProject/hierarchical-all-native/build.gradle.kts new file mode 100644 index 00000000000..a1907a939da --- /dev/null +++ b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/resources/testProject/hierarchical-all-native/build.gradle.kts @@ -0,0 +1,37 @@ +plugins { + kotlin("multiplatform") +} + +repositories { + mavenLocal() + jcenter() +} + +kotlin { + val mingwTargetName: String by project + val linuxTargetName: String by project + val macosTargetName: String by project + val currentHostTargetName: String by project + + val mingw = mingwX64(mingwTargetName) { } + val linux = linuxX64(linuxTargetName) { } + val macos = macosX64(macosTargetName) { } + val linuxArm = linuxArm64() + + sourceSets { + val allNative by creating { + dependsOn(getByName("commonMain")) + listOf(mingw, linux, macos).forEach { + it.compilations["main"].defaultSourceSet.dependsOn(this@creating) + } + } + + val currentHostAndLinux by creating { + dependsOn(allNative) + } + + configure(listOf(linuxArm, targets.getByName(currentHostTargetName))) { + compilations["main"].defaultSourceSet.dependsOn(currentHostAndLinux) + } + } +} \ No newline at end of file diff --git a/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/resources/testProject/hierarchical-all-native/gradle.properties b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/resources/testProject/hierarchical-all-native/gradle.properties new file mode 100644 index 00000000000..2a391fe78b0 --- /dev/null +++ b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/resources/testProject/hierarchical-all-native/gradle.properties @@ -0,0 +1 @@ +kotlin.mpp.enableGranularSourceSetsMetadata=true \ No newline at end of file diff --git a/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/resources/testProject/hierarchical-all-native/settings.gradle.kts b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/resources/testProject/hierarchical-all-native/settings.gradle.kts new file mode 100644 index 00000000000..0f81f6b3861 --- /dev/null +++ b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/resources/testProject/hierarchical-all-native/settings.gradle.kts @@ -0,0 +1,12 @@ +pluginManagement { + repositories { + mavenLocal() + gradlePluginPortal() + } + val kotlin_version: String by settings + plugins { + kotlin("multiplatform").version(kotlin_version) + } +} + +rootProject.name = "my-lib-foo" \ No newline at end of file diff --git a/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/resources/testProject/hierarchical-all-native/src/allNative/kotlin/Foo.kt b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/resources/testProject/hierarchical-all-native/src/allNative/kotlin/Foo.kt new file mode 100644 index 00000000000..c30ca7a066d --- /dev/null +++ b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/resources/testProject/hierarchical-all-native/src/allNative/kotlin/Foo.kt @@ -0,0 +1,10 @@ +/* + * Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. + */ + +package com.example.foo + +expect fun foo(): String + +fun fooCommon() = "foo" \ No newline at end of file diff --git a/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/resources/testProject/hierarchical-all-native/src/currentHostAndLinux/kotlin/UseFoo.kt b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/resources/testProject/hierarchical-all-native/src/currentHostAndLinux/kotlin/UseFoo.kt new file mode 100644 index 00000000000..9ca2386f4b8 --- /dev/null +++ b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/resources/testProject/hierarchical-all-native/src/currentHostAndLinux/kotlin/UseFoo.kt @@ -0,0 +1,10 @@ +/* + * Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. + */ + +package com.example.foo + +actual fun foo(): String = "foo" + +fun useFooCommon() = fooCommon() \ No newline at end of file diff --git a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/targets/native/KotlinNativeCompilation.kt b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/targets/native/KotlinNativeCompilation.kt index 4de8401a500..3201d28cc1f 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/targets/native/KotlinNativeCompilation.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/targets/native/KotlinNativeCompilation.kt @@ -102,7 +102,8 @@ class KotlinSharedNativeCompilation(override val target: KotlinMetadataTarget, v target, // TODO: this will end up as '-target' argument passed to K2Native, which is wrong. // Rewrite this when we'll compile native-shared source-sets against commonized platform libs - konanTargets.first(), + // We find any konan target that is enabled on the current host in order to pass the checks that avoid compiling the code otherwise. + konanTargets.find { it.enabledOnCurrentHost } ?: konanTargets.first(), name ), KotlinMetadataCompilation {