Repository: JetBrains/compose-multiplatform
File: gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/ComposeCompilerKotlinSupportPlugin.kt (Kotlin)

Code before:

package org.jetbrains.compose

import org.gradle.api.Project
import org.gradle.api.provider.Provider
import org.jetbrains.compose.internal.ComposeCompilerArtifactProvider
import org.jetbrains.compose.internal.mppExtOrNull
import org.jetbrains.compose.internal.webExt
import org.jetbrains.kotlin.gradle.plugin.*
import org.jetbrains.kotlin.gradle.targets.js.ir.KotlinJsIrTarget

class ComposeCompilerKotlinSupportPlugin : KotlinCompilerPluginSupportPlugin {
    private lateinit var composeCompilerArtifactProvider: ComposeCompilerArtifactProvider

    override fun apply(target: Project) {
        super.apply(target)
        target.plugins.withType(ComposePlugin::class.java) {
            val composeExt = target.extensions.getByType(ComposeExtension::class.java)

            composeCompilerArtifactProvider = ComposeCompilerArtifactProvider {
                composeExt.kotlinCompilerPlugin.orNull ?:
                    ComposeCompilerCompatibility.compilerVersionFor(target.getKotlinPluginVersion())
            }
        }
        target.afterEvaluate {
            warnAboutJetpackComposeCompilerUsageForNonJvm(it)
        }
    }

    @Suppress("NON_EXHAUSTIVE_WHEN")
    private fun warnAboutJetpackComposeCompilerUsageForNonJvm(target: Project) {
        val groupId = composeCompilerArtifactProvider.compilerArtifact.groupId
        val isUsingNonJBComposeCompiler = !groupId.startsWith("org.jetbrains.compose.compiler")

        if (isUsingNonJBComposeCompiler) {
            target.mppExtOrNull?.targets?.forEach {
                when (it.platformType) {
                    KotlinPlatformType.native,
                    KotlinPlatformType.js,
                    KotlinPlatformType.wasm -> target.logger.warn(createWarningAboutNonCompatibleCompiler(groupId))
                }
            }
        }
    }

    override fun getCompilerPluginId(): String =
        "androidx.compose.compiler.plugins.kotlin"

    override fun getPluginArtifact(): SubpluginArtifact =
        composeCompilerArtifactProvider.compilerArtifact


Review comment (performance):
This will be shown multiple times, as for the error (https://github.com/JetBrains/compose-multiplatform/issues/2866).

The fix is probably using some global AtomicBoolean, but I am not sure where to place it (Gradle can cache confiugurations).

So, i just mentioned it to not forget to fix it in the future.

Code after:

package org.jetbrains.compose

import org.gradle.api.Project
import org.gradle.api.provider.Provider
import org.gradle.build.event.BuildEventsListenerRegistry
import org.jetbrains.compose.internal.ComposeCompilerArtifactProvider
import org.jetbrains.compose.internal.mppExtOrNull
import org.jetbrains.compose.internal.webExt
import org.jetbrains.kotlin.gradle.plugin.*
import org.jetbrains.kotlin.gradle.targets.js.ir.KotlinJsIrTarget
import javax.inject.Inject

@Suppress("UnstableApiUsage")
class ComposeCompilerKotlinSupportPlugin @Inject constructor(
    @Suppress("UnstableApiUsage") private val buildEventsListenerRegistry: BuildEventsListenerRegistry
) : KotlinCompilerPluginSupportPlugin {
    private lateinit var composeCompilerArtifactProvider: ComposeCompilerArtifactProvider

    override fun apply(target: Project) {
        super.apply(target)
        target.plugins.withType(ComposePlugin::class.java) {
            val composeExt = target.extensions.getByType(ComposeExtension::class.java)

            composeCompilerArtifactProvider = ComposeCompilerArtifactProvider {
                composeExt.kotlinCompilerPlugin.orNull ?:
                    ComposeCompilerCompatibility.compilerVersionFor(target.getKotlinPluginVersion())
            }

            collectUnsupportedCompilerPluginUsages(target)
        }
    }

    private fun collectUnsupportedCompilerPluginUsages(target: Project) {
        fun Project.hasNonJvmTargets(): Boolean {
            val nonJvmTargets = setOf(KotlinPlatformType.native, KotlinPlatformType.js, KotlinPlatformType.wasm)
            return mppExtOrNull?.targets?.any {
                it.platformType in nonJvmTargets
            } ?: false
        }

        fun SubpluginArtifact.isNonJBComposeCompiler(): Boolean {
            return !groupId.startsWith("org.jetbrains.compose.compiler")
        }

        val service = ComposeMultiplatformBuildService.provider(target)
        buildEventsListenerRegistry.onTaskCompletion(service)

        service.get().parameters.unsupportedCompilerPlugins.add(
            target.provider {
                composeCompilerArtifactProvider.compilerArtifact.takeIf {