Skip to content

Commit

Permalink
[gradle] Set custom freeCompilerArgs only for the old compiler plugin…
Browse files Browse the repository at this point in the history
… (Kotlin < 2.0)
  • Loading branch information
terrakok committed Sep 4, 2024
1 parent aa52511 commit 6911131
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -61,32 +60,6 @@ abstract class ComposePlugin : Plugin<Project> {
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",
)
}
}
}

Expand Down

0 comments on commit 6911131

Please sign in to comment.