From d0a26d1d856f52b2d4e1b11b1c4ad19ccf3b0971 Mon Sep 17 00:00:00 2001 From: Svyatoslav Scherbina Date: Fri, 11 Feb 2022 12:28:41 +0300 Subject: [PATCH] [Gradle, Native] Enable embeddable compiler jar by default For Kotlin/Native compilations, apply generic (backend-agnostic) compiler plugin artifacts instead of Native-specific ones by default. `kotlin.native.useEmbeddableCompilerJar=false` project property allows switching to the previous legacy behaviour. This flag will be removed in a future release. ^KT-48595 Fixed --- .../gradle/plugin/KotlinGradleSubplugin.kt | 9 +-- .../kotlin/gradle/native/GeneralNativeIT.kt | 2 +- .../native/NativeEmbeddableCompilerJarIT.kt | 14 ++-- .../mpp/CompilationSpecificPluginPath.kt | 65 +++++++++++++------ .../kotlin/gradle/plugin/KotlinProperties.kt | 2 +- 5 files changed, 60 insertions(+), 32 deletions(-) diff --git a/libraries/tools/kotlin-gradle-plugin-api/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/KotlinGradleSubplugin.kt b/libraries/tools/kotlin-gradle-plugin-api/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/KotlinGradleSubplugin.kt index 01acde22405..82ab39c0e4b 100644 --- a/libraries/tools/kotlin-gradle-plugin-api/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/KotlinGradleSubplugin.kt +++ b/libraries/tools/kotlin-gradle-plugin-api/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/KotlinGradleSubplugin.kt @@ -136,11 +136,12 @@ interface KotlinCompilerPluginSupportPlugin : Plugin { fun getPluginArtifact(): SubpluginArtifact /** - * Kotlin/Native-specific plugin artifact. + * Legacy Kotlin/Native-specific plugin artifact. * - * If Gradle is configured to use Kotlin/Native embeddable compiler jar - * (with `kotlin.native.useEmbeddableCompilerJar=true` project property), - * then [getPluginArtifact] is used instead. + * It is used only if Gradle is configured not to use Kotlin/Native embeddable compiler jar + * (with `kotlin.native.useEmbeddableCompilerJar=false` project property). + * + * Otherwise, [getPluginArtifact] is used by default. */ fun getPluginArtifactForNative(): SubpluginArtifact? = null } diff --git a/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/native/GeneralNativeIT.kt b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/native/GeneralNativeIT.kt index 41ebf22de33..95d6a8ecf39 100644 --- a/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/native/GeneralNativeIT.kt +++ b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/native/GeneralNativeIT.kt @@ -875,7 +875,7 @@ class GeneralNativeIT : BaseGradleIT() { } // Check that changing K/N version lead to tasks rerun - build(*compileTasksArray, "-Porg.jetbrains.kotlin.native.version=1.5.30") { + build(*compileTasksArray, "-Porg.jetbrains.kotlin.native.version=1.6.10") { assertSuccessful() assertTasksExecuted(compileTasks) } diff --git a/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/native/NativeEmbeddableCompilerJarIT.kt b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/native/NativeEmbeddableCompilerJarIT.kt index 3322782474d..bfcb9e9f26b 100644 --- a/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/native/NativeEmbeddableCompilerJarIT.kt +++ b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/native/NativeEmbeddableCompilerJarIT.kt @@ -28,8 +28,8 @@ class NativeEmbeddableCompilerJarIT : BaseGradleIT() { build(":linkDebugExecutableHost") { assertSuccessful() withNativeCompilerClasspath(":linkDebugExecutableHost", ":compileKotlinHost") { - assertTrue(it.includesRegularJar()) - assertFalse(it.includesEmbeddableJar()) + assertFalse(it.includesRegularJar()) + assertTrue(it.includesEmbeddableJar()) } } } @@ -61,8 +61,8 @@ class NativeEmbeddableCompilerJarIT : BaseGradleIT() { build(":linkDebugExecutableHost") { assertSuccessful() withNativeCompilerClasspath(":linkDebugExecutableHost", ":compileKotlinHost") { - assertTrue(it.includesRegularJar()) - assertFalse(it.includesEmbeddableJar()) + assertFalse(it.includesRegularJar()) + assertTrue(it.includesEmbeddableJar()) } } @@ -70,12 +70,12 @@ class NativeEmbeddableCompilerJarIT : BaseGradleIT() { assertTasksUpToDate(":linkDebugExecutableHost", ":compileKotlinHost") } - build(":linkDebugExecutableHost", "-Pkotlin.native.useEmbeddableCompilerJar=true") { + build(":linkDebugExecutableHost", "-Pkotlin.native.useEmbeddableCompilerJar=false") { assertSuccessful() assertTasksExecuted(":linkDebugExecutableHost", ":compileKotlinHost") withNativeCompilerClasspath(":linkDebugExecutableHost", ":compileKotlinHost") { - assertFalse(it.includesRegularJar()) - assertTrue(it.includesEmbeddableJar()) + assertTrue(it.includesRegularJar()) + assertFalse(it.includesEmbeddableJar()) } } } diff --git a/libraries/tools/kotlin-gradle-plugin/src/functionalTest/kotlin/org/jetbrains/kotlin/gradle/mpp/CompilationSpecificPluginPath.kt b/libraries/tools/kotlin-gradle-plugin/src/functionalTest/kotlin/org/jetbrains/kotlin/gradle/mpp/CompilationSpecificPluginPath.kt index 0fca89ba7a8..ca3c86a0936 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/functionalTest/kotlin/org/jetbrains/kotlin/gradle/mpp/CompilationSpecificPluginPath.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/functionalTest/kotlin/org/jetbrains/kotlin/gradle/mpp/CompilationSpecificPluginPath.kt @@ -20,8 +20,8 @@ import kotlin.test.* internal class CompilationSpecificPluginPath { @Test - fun `native common sourceset should be compiled with native plugins`() { - // Given plugin but with native-specific artifact + fun `native common sourceset should be compiled with common plugins`() { + // Given plugin but with legacy-native-specific artifact class NativeSpecificPlugin : FakeSubPlugin("common", "native", { true }) val project = buildProject { @@ -53,12 +53,12 @@ internal class CompilationSpecificPluginPath { } project.evaluate() - // Then expect native artifact to be used for nativeMain metadata compilation - assertEquals(setOf("native"), project.compileTaskSubplugins("compileNativeMainKotlinMetadata")) + // Then expect common artifact to be used for all compilations, including nativeMain metadata + assertEquals(setOf("common"), project.compileTaskSubplugins("compileNativeMainKotlinMetadata")) assertEquals(setOf("common"), project.compileTaskSubplugins("compileKotlinMetadata")) assertEquals(setOf("common"), project.compileTaskSubplugins("compileCommonMainKotlinMetadata")) assertEquals(setOf("common"), project.compileTaskSubplugins("compileKotlinJvm")) - assertEquals(setOf("native"), project.compileTaskSubplugins("compileKotlinLinuxX64")) + assertEquals(setOf("common"), project.compileTaskSubplugins("compileKotlinLinuxX64")) } @Test @@ -110,11 +110,11 @@ internal class CompilationSpecificPluginPath { } @Test - fun `only native artifact should be taken for native platforms`() { - // Given plugin but with native-specific artifact + fun `only common artifacts should be taken for native platforms`() { + // Given plugin but with legacy-native-specific artifact class NativeSpecificPlugin : FakeSubPlugin("common1", "native", { true }) - // And plugin without native artifact but applicable on all platforms + // And plugin without legacy native artifact but applicable on all platforms class RegularPluginWithoutNativeArtifact : FakeSubPlugin("common2", null, { true }) // When applying these plugins @@ -129,15 +129,13 @@ internal class CompilationSpecificPluginPath { } project.evaluate() - // Expect jvm target have both artifacts + // Expect jvm and native targets have both common artifacts and no legacy native artifacts assertEquals(listOf("common1", "common2"), project.subplugins("jvm")) - - // And native target should have only NativeSpecificPlugin artifacts - assertEquals(listOf("native"), project.subplugins("linuxX64")) + assertEquals(listOf("common1", "common2"), project.subplugins("linuxX64")) } @Test - fun `native should use regular artifact when embeddable compiler jar is used`() { + fun `native platform and common sourcesets should be compiled with native plugin artifacts when embeddable compiler jar is not used`() { // Given plugin but with native-specific artifact class NativeSpecificPlugin : FakeSubPlugin("common1", "native", { true }) @@ -149,17 +147,46 @@ internal class CompilationSpecificPluginPath { plugins.apply(NativeSpecificPlugin::class.java) plugins.apply(RegularPluginWithoutNativeArtifact::class.java) - // With kotlin.native.useEmbeddableCompilerJar=true - extensions.getByType(ExtraPropertiesExtension::class.java).set("kotlin.native.useEmbeddableCompilerJar", "true") + extensions.getByType(ExtraPropertiesExtension::class.java).set("kotlin.mpp.enableGranularSourceSetsMetadata", "true") + + // With kotlin.native.useEmbeddableCompilerJar=false + extensions.getByType(ExtraPropertiesExtension::class.java).set("kotlin.native.useEmbeddableCompilerJar", "false") kotlin { linuxX64() + mingwX64() + jvm() + + sourceSets.apply { + val commonMain = getByName("commonMain") + val nativeMain = create("nativeMain") + val linuxX64 = getByName("linuxX64Main") + val mingwX64 = getByName("mingwX64Main") + val jvm = getByName("jvmMain") + + // Make nativeMain be common source set for linuxX64 and mingwX64 + nativeMain.dependsOn(commonMain) + linuxX64.dependsOn(nativeMain) + mingwX64.dependsOn(nativeMain) + jvm.dependsOn(commonMain) + } } } project.evaluate() - // Expect native target have both regular artifacts - assertEquals(listOf("common1", "common2"), project.subplugins("linuxX64")) + // Then expect native artifact to be used for nativeMain metadata compilation + assertEquals(setOf("native"), project.compileTaskSubplugins("compileNativeMainKotlinMetadata")) + assertEquals(setOf("common1", "common2"), project.compileTaskSubplugins("compileKotlinMetadata")) + assertEquals(setOf("common1", "common2"), project.compileTaskSubplugins("compileCommonMainKotlinMetadata")) + assertEquals(setOf("common1", "common2"), project.compileTaskSubplugins("compileKotlinJvm")) + assertEquals(setOf("native"), project.compileTaskSubplugins("compileKotlinLinuxX64")) + + // Expect jvm target have both artifacts + assertEquals(listOf("common1", "common2"), project.subplugins("jvm")) + + // And native target should have only NativeSpecificPlugin artifacts + assertEquals(listOf("native"), project.subplugins("linuxX64")) + assertEquals(listOf("native"), project.subplugins("mingwX64")) } @Test @@ -228,7 +255,7 @@ internal class CompilationSpecificPluginPath { private abstract class FakeSubPlugin( val id: String, - val idNative: String? = null, + val idLegacyNative: String? = null, val isApplicablePredicate: KotlinCompilation<*>.() -> Boolean ) : KotlinCompilerPluginSupportPlugin { override fun isApplicable(kotlinCompilation: KotlinCompilation<*>): Boolean = kotlinCompilation.isApplicablePredicate() @@ -243,7 +270,7 @@ internal class CompilationSpecificPluginPath { id ) - override fun getPluginArtifactForNative(): SubpluginArtifact? = idNative?.let { + override fun getPluginArtifactForNative(): SubpluginArtifact? = idLegacyNative?.let { SubpluginArtifact( "test", it 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 2348afb93ef..e77a32f4e62 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 @@ -316,7 +316,7 @@ internal class PropertiesProvider private constructor(private val project: Proje * Will be default after proper migration. */ val nativeUseEmbeddableCompilerJar: Boolean - get() = booleanProperty("kotlin.native.useEmbeddableCompilerJar") ?: false + get() = booleanProperty("kotlin.native.useEmbeddableCompilerJar") ?: true /** * Allows a user to set project-wide options that will be passed to the K/N compiler via -Xbinary flag.