From 5617110373a45a8e0205615cf83fa31afd84fd75 Mon Sep 17 00:00:00 2001 From: mvicsokolova Date: Tue, 9 Aug 2022 13:12:23 +0200 Subject: [PATCH 1/8] Support a new compiler plugin extension with flags, allowing to apply the plugin separately to JS ans JVM targets --- .../plugin/gradle/AtomicFUGradlePlugin.kt | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/atomicfu-gradle-plugin/src/main/kotlin/kotlinx/atomicfu/plugin/gradle/AtomicFUGradlePlugin.kt b/atomicfu-gradle-plugin/src/main/kotlin/kotlinx/atomicfu/plugin/gradle/AtomicFUGradlePlugin.kt index 4a532514..922a8680 100644 --- a/atomicfu-gradle-plugin/src/main/kotlin/kotlinx/atomicfu/plugin/gradle/AtomicFUGradlePlugin.kt +++ b/atomicfu-gradle-plugin/src/main/kotlin/kotlinx/atomicfu/plugin/gradle/AtomicFUGradlePlugin.kt @@ -29,15 +29,20 @@ private const val ORIGINAL_DIR_NAME = "originalClassesDir" private const val COMPILE_ONLY_CONFIGURATION = "compileOnly" private const val IMPLEMENTATION_CONFIGURATION = "implementation" private const val TEST_IMPLEMENTATION_CONFIGURATION = "testImplementation" -private const val ENABLE_IR_TRANSFORMATION = "kotlinx.atomicfu.enableIrTransformation" +private const val ENABLE_JS_IR_TRANSFORMATION = "kotlinx.atomicfu.enableJsIrTransformation" +private const val ENABLE_JVM_IR_TRANSFORMATION = "kotlinx.atomicfu.enableJvmIrTransformation" open class AtomicFUGradlePlugin : Plugin { override fun apply(project: Project) = project.run { val pluginVersion = rootProject.buildscript.configurations.findByName("classpath") ?.allDependencies?.find { it.name == "atomicfu-gradle-plugin" }?.version extensions.add(EXTENSION_NAME, AtomicFUPluginExtension(pluginVersion)) - if (rootProject.getBooleanProperty(ENABLE_IR_TRANSFORMATION) && isCompilerPluginAvailable()) { + if (isCompilerPluginAvailable()) { plugins.apply(AtomicfuKotlinGradleSubplugin::class.java) + extensions.getByType(AtomicfuKotlinGradleSubplugin.AtomicfuKotlinGradleExtension::class.java).apply { + isJsIrTransformationEnabled = rootProject.getBooleanProperty(ENABLE_JS_IR_TRANSFORMATION) + isJvmIrTransformationEnabled = rootProject.getBooleanProperty(ENABLE_JVM_IR_TRANSFORMATION) + } } configureDependencies() configureTasks() @@ -69,6 +74,8 @@ private fun Project.configureTasks() { val config = config withPluginWhenEvaluated("kotlin") { if (config.transformJvm) { + // skip transformation task if ir transformation is enabled + if (rootProject.getBooleanProperty(ENABLE_JVM_IR_TRANSFORMATION)) return@withPluginWhenEvaluated configureJvmTransformation("compileTestKotlin") { sourceSet, transformedDir, originalDir -> createJvmTransformTask(sourceSet).configureJvmTask( sourceSet.compileClasspath, @@ -109,7 +116,7 @@ private fun String.toBooleanStrict(): Boolean = when (this) { } private fun Project.needsJsIrTransformation(target: KotlinTarget): Boolean = - rootProject.getBooleanProperty(ENABLE_IR_TRANSFORMATION) && target.isJsIrTarget() + rootProject.getBooleanProperty(ENABLE_JS_IR_TRANSFORMATION) && target.isJsIrTarget() private fun KotlinTarget.isJsIrTarget() = (this is KotlinJsTarget && this.irTarget != null) || this is KotlinJsIrTarget @@ -234,7 +241,8 @@ private fun Project.configureTransformationForTarget(target: KotlinTarget) { project.buildDir.resolve("classes/atomicfu/${target.name}/${compilation.name}") val transformTask = when (target.platformType) { KotlinPlatformType.jvm, KotlinPlatformType.androidJvm -> { - if (!config.transformJvm) return@compilations // skip when transformation is turned off + // skip transformation task if transformation is turned off or ir transformation is enabled + if (!config.transformJvm || rootProject.getBooleanProperty(ENABLE_JVM_IR_TRANSFORMATION)) return@compilations project.createJvmTransformTask(compilation).configureJvmTask( compilation.compileDependencyFiles, compilation.compileAllTaskName, From 6da61f3800b657d7362199ffebe8c21dcd450913 Mon Sep 17 00:00:00 2001 From: mvicsokolova Date: Wed, 14 Sep 2022 18:43:52 +0200 Subject: [PATCH 2/8] Update Kotlin to 1.7.20-RC --- gradle.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gradle.properties b/gradle.properties index de1cd791..60d476e2 100644 --- a/gradle.properties +++ b/gradle.properties @@ -5,7 +5,7 @@ version=0.18.4-SNAPSHOT group=org.jetbrains.kotlinx -kotlin_version=1.7.0 +kotlin_version=1.7.20-RC asm_version=9.3 slf4j_version=1.8.0-alpha2 junit_version=4.12 From 99a2e30635791839d52ae87608a601b52cea5075 Mon Sep 17 00:00:00 2001 From: Margarita Bobova Date: Thu, 7 Jul 2022 11:00:29 +0200 Subject: [PATCH 3/8] Use kotlin.targets in dsl --- atomicfu/build.gradle | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/atomicfu/build.gradle b/atomicfu/build.gradle index 6ef8b0bf..063c9a32 100644 --- a/atomicfu/build.gradle +++ b/atomicfu/build.gradle @@ -18,8 +18,10 @@ ext { } kotlin { - targets.metaClass.addTarget = { preset -> - addNative(delegate.fromPreset(preset, preset.name)) + targets { + delegate.metaClass.addTarget = { preset -> + addNative(delegate.fromPreset(preset, preset.name)) + } } // JS -- always From 17bd06f8067c5de30e310f2c015571a919476155 Mon Sep 17 00:00:00 2001 From: mvicsokolova Date: Wed, 5 Oct 2022 11:11:23 +0200 Subject: [PATCH 4/8] Update Kotlin to 1.7.20 --- gradle.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gradle.properties b/gradle.properties index 60d476e2..988e974a 100644 --- a/gradle.properties +++ b/gradle.properties @@ -5,7 +5,7 @@ version=0.18.4-SNAPSHOT group=org.jetbrains.kotlinx -kotlin_version=1.7.20-RC +kotlin_version=1.7.20 asm_version=9.3 slf4j_version=1.8.0-alpha2 junit_version=4.12 From 85bebfc235ee3794b02111d28bf388fcfda50c98 Mon Sep 17 00:00:00 2001 From: mvicsokolova Date: Wed, 5 Oct 2022 12:07:42 +0200 Subject: [PATCH 5/8] Support old JS IR atomicfu compiler plugin for KGP >= 1.6.20 && KGP <= 1.7.20 --- .../plugin/gradle/AtomicFUGradlePlugin.kt | 50 ++++++++++++++----- 1 file changed, 38 insertions(+), 12 deletions(-) diff --git a/atomicfu-gradle-plugin/src/main/kotlin/kotlinx/atomicfu/plugin/gradle/AtomicFUGradlePlugin.kt b/atomicfu-gradle-plugin/src/main/kotlin/kotlinx/atomicfu/plugin/gradle/AtomicFUGradlePlugin.kt index 922a8680..78a2acee 100644 --- a/atomicfu-gradle-plugin/src/main/kotlin/kotlinx/atomicfu/plugin/gradle/AtomicFUGradlePlugin.kt +++ b/atomicfu-gradle-plugin/src/main/kotlin/kotlinx/atomicfu/plugin/gradle/AtomicFUGradlePlugin.kt @@ -29,6 +29,7 @@ private const val ORIGINAL_DIR_NAME = "originalClassesDir" private const val COMPILE_ONLY_CONFIGURATION = "compileOnly" private const val IMPLEMENTATION_CONFIGURATION = "implementation" private const val TEST_IMPLEMENTATION_CONFIGURATION = "testImplementation" +private const val ENABLE_JS_IR_TRANSFORMATION_LEGACY = "kotlinx.atomicfu.enableIrTransformation" private const val ENABLE_JS_IR_TRANSFORMATION = "kotlinx.atomicfu.enableJsIrTransformation" private const val ENABLE_JVM_IR_TRANSFORMATION = "kotlinx.atomicfu.enableJvmIrTransformation" @@ -37,13 +38,7 @@ open class AtomicFUGradlePlugin : Plugin { val pluginVersion = rootProject.buildscript.configurations.findByName("classpath") ?.allDependencies?.find { it.name == "atomicfu-gradle-plugin" }?.version extensions.add(EXTENSION_NAME, AtomicFUPluginExtension(pluginVersion)) - if (isCompilerPluginAvailable()) { - plugins.apply(AtomicfuKotlinGradleSubplugin::class.java) - extensions.getByType(AtomicfuKotlinGradleSubplugin.AtomicfuKotlinGradleExtension::class.java).apply { - isJsIrTransformationEnabled = rootProject.getBooleanProperty(ENABLE_JS_IR_TRANSFORMATION) - isJvmIrTransformationEnabled = rootProject.getBooleanProperty(ENABLE_JVM_IR_TRANSFORMATION) - } - } + applyAtomicfuCompilerPlugin() configureDependencies() configureTasks() } @@ -95,15 +90,45 @@ private fun Project.configureTasks() { } } -private fun Project.isCompilerPluginAvailable(): Boolean { - // kotlinx-atomicfu compiler plugin is available for KGP >= 1.6.20 +private data class KotlinVersion(val major: Int, val minor: Int, val patch: Int) + +private fun Project.getKotlinVersion(): KotlinVersion { val kotlinVersion = getKotlinPluginVersion() - val (majorVersion, minorVersion) = kotlinVersion + val (major, minor) = kotlinVersion .split('.') .take(2) .map { it.toInt() } val patch = kotlinVersion.substringAfterLast('.').substringBefore('-').toInt() - return majorVersion == 1 && (minorVersion == 6 && patch >= 20 || minorVersion > 6) + return KotlinVersion(major, minor, patch) +} + +private fun Project.isCompilerPluginAvailable(): Boolean { + // kotlinx-atomicfu compiler plugin is available for KGP >= 1.6.20 + val kotlinVersion = getKotlinVersion() + return kotlinVersion.major == 1 && (kotlinVersion.minor == 6 && kotlinVersion.patch >= 20 || kotlinVersion.minor > 6) +} + +private fun Project.applyAtomicfuCompilerPlugin() { + val kotlinVersion = getKotlinVersion() + // for KGP >= 1.7.20: + // compiler plugin for JS IR is applied via the property `kotlinx.atomicfu.enableJsIrTransformation` + // compiler plugin for JVM IR is applied via the property `kotlinx.atomicfu.enableJvmIrTransformation` + if (kotlinVersion.major == 1 && (kotlinVersion.minor == 7 && kotlinVersion.patch >= 20 || kotlinVersion.minor > 7)) { + plugins.apply(AtomicfuKotlinGradleSubplugin::class.java) + extensions.getByType(AtomicfuKotlinGradleSubplugin.AtomicfuKotlinGradleExtension::class.java).apply { + isJsIrTransformationEnabled = rootProject.getBooleanProperty(ENABLE_JS_IR_TRANSFORMATION) + isJvmIrTransformationEnabled = rootProject.getBooleanProperty(ENABLE_JVM_IR_TRANSFORMATION) + } + } else { + // for KGP >= 1.6.20 && KGP <= 1.7.20: + // compiler plugin for JS IR is applied via the property `kotlinx.atomicfu.enableIrTransformation` + // compiler plugin for JVM IR is not supported yet + if (kotlinVersion.major == 1 && (kotlinVersion.minor == 6 && kotlinVersion.patch >= 20 || kotlinVersion.minor > 6)) { + if (rootProject.getBooleanProperty(ENABLE_JS_IR_TRANSFORMATION_LEGACY)) { + plugins.apply(AtomicfuKotlinGradleSubplugin::class.java) + } + } + } } private fun Project.getBooleanProperty(name: String) = @@ -116,7 +141,8 @@ private fun String.toBooleanStrict(): Boolean = when (this) { } private fun Project.needsJsIrTransformation(target: KotlinTarget): Boolean = - rootProject.getBooleanProperty(ENABLE_JS_IR_TRANSFORMATION) && target.isJsIrTarget() + (rootProject.getBooleanProperty(ENABLE_JS_IR_TRANSFORMATION) || rootProject.getBooleanProperty(ENABLE_JS_IR_TRANSFORMATION_LEGACY)) + && target.isJsIrTarget() private fun KotlinTarget.isJsIrTarget() = (this is KotlinJsTarget && this.irTarget != null) || this is KotlinJsIrTarget From fee1ec09a78d7a7c4316e807eb00386f3338006f Mon Sep 17 00:00:00 2001 From: mvicsokolova Date: Sun, 16 Oct 2022 23:00:20 +0200 Subject: [PATCH 6/8] Comment on the old property for JS IR compiler plugin --- .../kotlinx/atomicfu/plugin/gradle/AtomicFUGradlePlugin.kt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/atomicfu-gradle-plugin/src/main/kotlin/kotlinx/atomicfu/plugin/gradle/AtomicFUGradlePlugin.kt b/atomicfu-gradle-plugin/src/main/kotlin/kotlinx/atomicfu/plugin/gradle/AtomicFUGradlePlugin.kt index 78a2acee..b09b0862 100644 --- a/atomicfu-gradle-plugin/src/main/kotlin/kotlinx/atomicfu/plugin/gradle/AtomicFUGradlePlugin.kt +++ b/atomicfu-gradle-plugin/src/main/kotlin/kotlinx/atomicfu/plugin/gradle/AtomicFUGradlePlugin.kt @@ -29,6 +29,8 @@ private const val ORIGINAL_DIR_NAME = "originalClassesDir" private const val COMPILE_ONLY_CONFIGURATION = "compileOnly" private const val IMPLEMENTATION_CONFIGURATION = "implementation" private const val TEST_IMPLEMENTATION_CONFIGURATION = "testImplementation" +// If the project uses KGP <= 1.6.20, only JS IR compiler plugin is available, and it is turned on via setting this property. +// The property is supported for backwards compatibility. private const val ENABLE_JS_IR_TRANSFORMATION_LEGACY = "kotlinx.atomicfu.enableIrTransformation" private const val ENABLE_JS_IR_TRANSFORMATION = "kotlinx.atomicfu.enableJsIrTransformation" private const val ENABLE_JVM_IR_TRANSFORMATION = "kotlinx.atomicfu.enableJvmIrTransformation" From 5dc15b2e6ff240411bbd083174cbdca8a915dd1a Mon Sep 17 00:00:00 2001 From: mvicsokolova Date: Sun, 16 Oct 2022 23:00:57 +0200 Subject: [PATCH 7/8] KotlinVersion.atLeast(major, minor, patch) --- .../atomicfu/plugin/gradle/AtomicFUGradlePlugin.kt | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/atomicfu-gradle-plugin/src/main/kotlin/kotlinx/atomicfu/plugin/gradle/AtomicFUGradlePlugin.kt b/atomicfu-gradle-plugin/src/main/kotlin/kotlinx/atomicfu/plugin/gradle/AtomicFUGradlePlugin.kt index b09b0862..de0f7f04 100644 --- a/atomicfu-gradle-plugin/src/main/kotlin/kotlinx/atomicfu/plugin/gradle/AtomicFUGradlePlugin.kt +++ b/atomicfu-gradle-plugin/src/main/kotlin/kotlinx/atomicfu/plugin/gradle/AtomicFUGradlePlugin.kt @@ -104,18 +104,18 @@ private fun Project.getKotlinVersion(): KotlinVersion { return KotlinVersion(major, minor, patch) } -private fun Project.isCompilerPluginAvailable(): Boolean { - // kotlinx-atomicfu compiler plugin is available for KGP >= 1.6.20 - val kotlinVersion = getKotlinVersion() - return kotlinVersion.major == 1 && (kotlinVersion.minor == 6 && kotlinVersion.patch >= 20 || kotlinVersion.minor > 6) -} +private fun KotlinVersion.atLeast(major: Int, minor: Int, patch: Int) = + this.major == major && (this.minor == minor && this.patch >= patch || this.minor > minor) || this.major > major + +// kotlinx-atomicfu compiler plugin is available for KGP >= 1.6.20 +private fun Project.isCompilerPluginAvailable() = getKotlinVersion().atLeast(1, 6, 20) private fun Project.applyAtomicfuCompilerPlugin() { val kotlinVersion = getKotlinVersion() // for KGP >= 1.7.20: // compiler plugin for JS IR is applied via the property `kotlinx.atomicfu.enableJsIrTransformation` // compiler plugin for JVM IR is applied via the property `kotlinx.atomicfu.enableJvmIrTransformation` - if (kotlinVersion.major == 1 && (kotlinVersion.minor == 7 && kotlinVersion.patch >= 20 || kotlinVersion.minor > 7)) { + if (kotlinVersion.atLeast(1, 7, 20)) { plugins.apply(AtomicfuKotlinGradleSubplugin::class.java) extensions.getByType(AtomicfuKotlinGradleSubplugin.AtomicfuKotlinGradleExtension::class.java).apply { isJsIrTransformationEnabled = rootProject.getBooleanProperty(ENABLE_JS_IR_TRANSFORMATION) @@ -125,7 +125,7 @@ private fun Project.applyAtomicfuCompilerPlugin() { // for KGP >= 1.6.20 && KGP <= 1.7.20: // compiler plugin for JS IR is applied via the property `kotlinx.atomicfu.enableIrTransformation` // compiler plugin for JVM IR is not supported yet - if (kotlinVersion.major == 1 && (kotlinVersion.minor == 6 && kotlinVersion.patch >= 20 || kotlinVersion.minor > 6)) { + if (kotlinVersion.atLeast(1, 6, 20)) { if (rootProject.getBooleanProperty(ENABLE_JS_IR_TRANSFORMATION_LEGACY)) { plugins.apply(AtomicfuKotlinGradleSubplugin::class.java) } From e1844cd72c9798fc8e3d994dfd28b4a4a3efbb9a Mon Sep 17 00:00:00 2001 From: mvicsokolova Date: Sun, 16 Oct 2022 23:16:01 +0200 Subject: [PATCH 8/8] Fixup --- .../atomicfu/plugin/gradle/AtomicFUGradlePlugin.kt | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/atomicfu-gradle-plugin/src/main/kotlin/kotlinx/atomicfu/plugin/gradle/AtomicFUGradlePlugin.kt b/atomicfu-gradle-plugin/src/main/kotlin/kotlinx/atomicfu/plugin/gradle/AtomicFUGradlePlugin.kt index de0f7f04..b77e95bc 100644 --- a/atomicfu-gradle-plugin/src/main/kotlin/kotlinx/atomicfu/plugin/gradle/AtomicFUGradlePlugin.kt +++ b/atomicfu-gradle-plugin/src/main/kotlin/kotlinx/atomicfu/plugin/gradle/AtomicFUGradlePlugin.kt @@ -154,19 +154,13 @@ private fun Project.addCompilerPluginDependency() { if (needsJsIrTransformation(target)) { target.compilations.forEach { kotlinCompilation -> kotlinCompilation.dependencies { - val kotlinVersion = getKotlinPluginVersion() - val (majorVersion, minorVersion) = kotlinVersion - .split('.') - .take(2) - .map { it.toInt() } - val patch = kotlinVersion.substringAfterLast('.').substringBefore('-').toInt() - if (majorVersion == 1 && (minorVersion == 7 && patch >= 10 || minorVersion > 7)) { + if (getKotlinVersion().atLeast(1, 7, 10)) { // since Kotlin 1.7.10 we can add `atomicfu-runtime` dependency directly - implementation("org.jetbrains.kotlin:kotlinx-atomicfu-runtime:$kotlinVersion") + implementation("org.jetbrains.kotlin:kotlinx-atomicfu-runtime:${getKotlinPluginVersion()}") } else { // add atomicfu compiler plugin dependency // to provide the `atomicfu-runtime` library used during compiler plugin transformation - implementation("org.jetbrains.kotlin:atomicfu:$kotlinVersion") + implementation("org.jetbrains.kotlin:atomicfu:${getKotlinPluginVersion()}") } } }