From 6911131b83e1ac634fb00b0427327be661df8355 Mon Sep 17 00:00:00 2001 From: Konstantin Tskhovrebov Date: Wed, 4 Sep 2024 12:16:59 +0200 Subject: [PATCH] [gradle] Set custom freeCompilerArgs only for the old compiler plugin (Kotlin < 2.0) --- .../ComposeCompilerKotlinSupportPlugin.kt | 32 +++++++++++++++- .../org/jetbrains/compose/ComposePlugin.kt | 37 +++---------------- 2 files changed, 35 insertions(+), 34 deletions(-) diff --git a/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/ComposeCompilerKotlinSupportPlugin.kt b/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/ComposeCompilerKotlinSupportPlugin.kt index 37c87a3a0b6..629669ab72a 100644 --- a/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/ComposeCompilerKotlinSupportPlugin.kt +++ b/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/ComposeCompilerKotlinSupportPlugin.kt @@ -14,7 +14,9 @@ import org.jetbrains.compose.internal.KOTLIN_JVM_PLUGIN_ID import org.jetbrains.compose.internal.KOTLIN_MPP_PLUGIN_ID import org.jetbrains.compose.internal.Version import org.jetbrains.compose.internal.ideaIsInSyncProvider +import org.jetbrains.compose.internal.mppExtOrNull import org.jetbrains.compose.internal.webExt +import org.jetbrains.kotlin.gradle.dsl.KotlinJsCompile import org.jetbrains.kotlin.gradle.plugin.KotlinBasePlugin import org.jetbrains.kotlin.gradle.plugin.KotlinCompilation import org.jetbrains.kotlin.gradle.plugin.KotlinCompilerPluginSupportPlugin @@ -52,6 +54,32 @@ private fun Project.configureComposeCompilerPlugin(kgp: KotlinBasePlugin) { if (Version.fromString(kgpVersion) < Version.fromString(newCompilerIsAvailableVersion)) { logger.info("Apply ComposeCompilerKotlinSupportPlugin (KGP version = $kgpVersion)") project.plugins.apply(ComposeCompilerKotlinSupportPlugin::class.java) + + //legacy logic applied for Kotlin < 2.0 only + project.afterEvaluate { + val composeExtension = project.extensions.getByType(ComposeExtension::class.java) + project.tasks.withType(org.jetbrains.kotlin.gradle.dsl.KotlinCompile::class.java).configureEach { + it.kotlinOptions.apply { + freeCompilerArgs = freeCompilerArgs + + composeExtension.kotlinCompilerPluginArgs.get().flatMap { arg -> + listOf("-P", "plugin:androidx.compose.compiler.plugins.kotlin:$arg") + } + } + } + + val hasAnyWebTarget = project.mppExtOrNull?.targets?.firstOrNull { + it.platformType == KotlinPlatformType.js || + it.platformType == KotlinPlatformType.wasm + } != null + if (hasAnyWebTarget) { + // currently k/wasm compile task is covered by KotlinJsCompile type + project.tasks.withType(KotlinJsCompile::class.java).configureEach { + it.kotlinOptions.freeCompilerArgs += listOf( + "-Xklib-enable-signature-clash-checks=false", + ) + } + } + } } else { //There is no other way to check that the plugin WASN'T applied! afterEvaluate { @@ -76,8 +104,8 @@ class ComposeCompilerKotlinSupportPlugin : KotlinCompilerPluginSupportPlugin { val composeExt = target.extensions.getByType(ComposeExtension::class.java) composeCompilerArtifactProvider = ComposeCompilerArtifactProvider { - composeExt.kotlinCompilerPlugin.orNull ?: - ComposeCompilerCompatibility.compilerVersionFor(target.getKotlinPluginVersion()) + composeExt.kotlinCompilerPlugin.orNull + ?: ComposeCompilerCompatibility.compilerVersionFor(target.getKotlinPluginVersion()) } applicableForPlatformTypes = composeExt.platformTypes diff --git a/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/ComposePlugin.kt b/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/ComposePlugin.kt index 41e9afd7efc..0893a941399 100644 --- a/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/ComposePlugin.kt +++ b/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/ComposePlugin.kt @@ -19,17 +19,16 @@ import org.jetbrains.compose.desktop.DesktopExtension import org.jetbrains.compose.desktop.application.internal.configureDesktop import org.jetbrains.compose.desktop.preview.internal.initializePreview import org.jetbrains.compose.experimental.dsl.ExperimentalExtension -import org.jetbrains.compose.experimental.internal.* -import org.jetbrains.compose.internal.* +import org.jetbrains.compose.experimental.internal.configureExperimentalTargetsFlagsCheck +import org.jetbrains.compose.internal.KOTLIN_MPP_PLUGIN_ID +import org.jetbrains.compose.internal.mppExt import org.jetbrains.compose.internal.utils.currentTarget import org.jetbrains.compose.resources.ResourcesExtension import org.jetbrains.compose.resources.configureComposeResources import org.jetbrains.compose.web.WebExtension import org.jetbrains.compose.web.internal.configureWeb -import org.jetbrains.kotlin.com.github.gundy.semver4j.SemVer -import org.jetbrains.kotlin.gradle.dsl.KotlinCompile -import org.jetbrains.kotlin.gradle.dsl.KotlinJsCompile -import org.jetbrains.kotlin.gradle.plugin.* +import org.jetbrains.kotlin.gradle.plugin.KotlinDependencyHandler +import org.jetbrains.kotlin.gradle.plugin.getKotlinPluginVersion internal val composeVersion get() = ComposeBuildConfig.composeVersion @@ -61,32 +60,6 @@ abstract class ComposePlugin : Plugin { val mppExt = project.mppExt project.configureExperimentalTargetsFlagsCheck(mppExt) } - - project.tasks.withType(KotlinCompile::class.java).configureEach { - it.kotlinOptions.apply { - freeCompilerArgs = freeCompilerArgs + - composeExtension.kotlinCompilerPluginArgs.get().flatMap { arg -> - listOf("-P", "plugin:androidx.compose.compiler.plugins.kotlin:$arg") - } - } - } - - disableSignatureClashCheck(project) - } - } - - private fun disableSignatureClashCheck(project: Project) { - val hasAnyWebTarget = project.mppExtOrNull?.targets?.firstOrNull { - it.platformType == KotlinPlatformType.js || - it.platformType == KotlinPlatformType.wasm - } != null - if (hasAnyWebTarget) { - // currently k/wasm compile task is covered by KotlinJsCompile type - project.tasks.withType(KotlinJsCompile::class.java).configureEach { - it.kotlinOptions.freeCompilerArgs += listOf( - "-Xklib-enable-signature-clash-checks=false", - ) - } } }