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

Code before:
private const val PROJECT_CACHE_KIND_PROPERTY_NAME = "kotlin.native.cacheKind"
private const val COMPOSE_NATIVE_MANAGE_CACHE_KIND = "compose.kotlin.native.manageCacheKind"
private const val NONE_VALUE = "none"

internal fun Project.configureNativeCompilerCaching() {
    if (findProperty(COMPOSE_NATIVE_MANAGE_CACHE_KIND) == "false") return

    plugins.withId(KOTLIN_MPP_PLUGIN_ID) {
        val kotlinVersion = kotlinVersionNumbers(this)
        mppExt.targets.configureEachWithType<KotlinNativeTarget> {
            assertCacheKindUserValueIsNotNone()
            configureTargetCompilerCache(kotlinVersion)
        }
    }
}

private fun KotlinNativeTarget.assertCacheKindUserValueIsNotNone() {
    // To determine cache kind KGP checks kotlin.native.cacheKind.<PRESET_NAME> first, then kotlin.native.cacheKind
    // For each property it tries to read Project.property, then checks local.properties
    // See https://github.com/JetBrains/kotlin/blob/d4d30dcfcf1afb083f09279c6f1ba05031efeabb/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/plugin/PropertiesProvider.kt#L416
    val cacheKindProperties = listOf(targetCacheKindPropertyName, PROJECT_CACHE_KIND_PROPERTY_NAME)
    val propertyProviders = listOf(
        KGPPropertyProvider.GradleProperties(project),
        KGPPropertyProvider.LocalProperties(project)
    )

    for (cacheKindProperty in cacheKindProperties) {
        for (provider in propertyProviders) {
            val value = provider.valueOrNull(cacheKindProperty)
            if (value != null) {
                if (value.equals(NONE_VALUE, ignoreCase = true)) {
                    project.logger.warn(cacheKindPropertyWarningMessage(cacheKindProperty, provider))
                }
                return
            }
        }
    }
}

private fun cacheKindPropertyWarningMessage(
    cacheKindProperty: String,
    provider: KGPPropertyProvider
) = """
    |Warning: '$cacheKindProperty' is explicitly set to `none`.
    |This option significantly slows the Kotlin/Native compiler.
    |Compose Multiplatform Gradle plugin can set this property automatically,
    |when it is necessary.
    |  * Recommended action: remove explicit '$cacheKindProperty=$NONE_VALUE' from ${provider.location}. 
    |  * Alternative action: if you are sure you need '$cacheKindProperty=$NONE_VALUE', disable
    |this warning by adding '$COMPOSE_NATIVE_MANAGE_CACHE_KIND=false' to your 'gradle.properties'.
""".trimMargin()

Review comment (performance):


When `kotlin.native.cacheKind` accidentally exists in local.properties - it is mistake in project configuraion.
I don't know how exactly better to behave.
I think we can just skip this case. But, I don't have a strong opinion.



Code after:

private const val PROJECT_CACHE_KIND_PROPERTY_NAME = "kotlin.native.cacheKind"
private const val COMPOSE_NATIVE_MANAGE_CACHE_KIND = "compose.kotlin.native.manageCacheKind"
private const val NONE_VALUE = "none"

internal fun Project.configureNativeCompilerCaching() {
    if (findProperty(COMPOSE_NATIVE_MANAGE_CACHE_KIND) == "false") return

    plugins.withId(KOTLIN_MPP_PLUGIN_ID) {
        val kotlinVersion = kotlinVersionNumbers(this)
        mppExt.targets.configureEachWithType<KotlinNativeTarget> {
            checkCacheKindUserValueIsNotNone()
            configureTargetCompilerCache(kotlinVersion)
        }
    }
}

private fun KotlinNativeTarget.checkCacheKindUserValueIsNotNone() {
    // To determine cache kind KGP checks kotlin.native.cacheKind.<PRESET_NAME> first, then kotlin.native.cacheKind
    // For each property it tries to read Project.property, then checks local.properties
    // See https://github.com/JetBrains/kotlin/blob/d4d30dcfcf1afb083f09279c6f1ba05031efeabb/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/plugin/PropertiesProvider.kt#L416
    val cacheKindProperties = listOf(targetCacheKindPropertyName, PROJECT_CACHE_KIND_PROPERTY_NAME)
    val propertyProviders = listOf(
        KGPPropertyProvider.GradleProperties(project),
        KGPPropertyProvider.LocalProperties(project)
    )

    for (cacheKindProperty in cacheKindProperties) {
        for (provider in propertyProviders) {
            val value = provider.valueOrNull(cacheKindProperty)
            if (value != null) {
                if (value.equals(NONE_VALUE, ignoreCase = true)) {
                    ComposeMultiplatformBuildService
                        .getInstance(project)
                        .warnOnceAfterBuild(cacheKindPropertyWarningMessage(cacheKindProperty, provider))
                }
                return
            }
        }
    }
}

private fun cacheKindPropertyWarningMessage(
    cacheKindProperty: String,
    provider: KGPPropertyProvider
) = """
    |Warning: '$cacheKindProperty' is explicitly set to `none`.
    |This option significantly slows the Kotlin/Native compiler.
    |Compose Multiplatform Gradle plugin can set this property automatically,
    |when it is necessary.
    |  * Recommended action: remove explicit '$cacheKindProperty=$NONE_VALUE' from ${provider.location}.