-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Set Kotlin/Native cache kind based on Kotlin version
- Loading branch information
1 parent
44376fd
commit 1939de0
Showing
12 changed files
with
243 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
...main/kotlin/org/jetbrains/compose/experimental/internal/configureNativeCompilerCaching.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* Copyright 2020-2023 JetBrains s.r.o. and respective authors and developers. | ||
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE.txt file. | ||
*/ | ||
|
||
package org.jetbrains.compose.experimental.internal | ||
|
||
import org.gradle.api.Project | ||
import org.jetbrains.compose.internal.KOTLIN_MPP_PLUGIN_ID | ||
import org.jetbrains.compose.internal.mppExt | ||
import org.jetbrains.compose.internal.utils.KGPPropertyFinder | ||
import org.jetbrains.compose.internal.utils.configureEachWithType | ||
import org.jetbrains.kotlin.gradle.plugin.getKotlinPluginVersion | ||
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget | ||
import org.jetbrains.kotlin.konan.target.presetName | ||
|
||
private const val KOTLIN_NATIVE_CACHE_KIND = "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 propertyFinders = listOf( | ||
KGPPropertyFinder.GradleProperties(this), | ||
KGPPropertyFinder.LocalProperties(this) | ||
) | ||
|
||
val (majorKotlinVer, minorKotlinVer) = kotlinVersionNumbers(this) | ||
|
||
val isKotlinVersionAtLeast19 = majorKotlinVer > 1 || (majorKotlinVer == 1 && minorKotlinVer >= 9) | ||
val isKotlinVersionBefore19 = !isKotlinVersionAtLeast19 | ||
|
||
mppExt.targets.configureEachWithType<KotlinNativeTarget> { | ||
configureCompilerCache(propertyFinders, isKotlinVersionBefore19) | ||
} | ||
} | ||
} | ||
|
||
private fun KotlinNativeTarget.configureCompilerCache( | ||
propertyProviders: List<KGPPropertyFinder>, | ||
isKotlinVersionBefore19: Boolean | ||
) { | ||
fun String?.isNone(): Boolean = | ||
NONE_VALUE.equals(this, ignoreCase = true) | ||
|
||
val targetCacheKindProperty = "$KOTLIN_NATIVE_CACHE_KIND.${konanTarget.presetName}" | ||
for (cacheKindProperty in listOf(targetCacheKindProperty, KOTLIN_NATIVE_CACHE_KIND)) { | ||
for (provider in propertyProviders) { | ||
val value = provider.findProperty(cacheKindProperty) | ||
if (value.isNone()) { | ||
project.logger.warn( | ||
""" | ||
|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' from ${provider.location}. | ||
| * Alternative action: if you are sure you need '$cacheKindProperty=none', disable | ||
|this warning by adding '$COMPOSE_NATIVE_MANAGE_CACHE_KIND=false' to your 'gradle.properties'. | ||
""".trimMargin() | ||
) | ||
} | ||
|
||
if (value != null) break | ||
} | ||
} | ||
|
||
if (isKotlinVersionBefore19) { | ||
if (project.hasProperty(targetCacheKindProperty)) { | ||
project.setProperty(targetCacheKindProperty, NONE_VALUE) | ||
} else { | ||
project.extensions.extraProperties.set(targetCacheKindProperty, NONE_VALUE) | ||
} | ||
} | ||
} | ||
|
||
private data class KotlinVersionNumbers(val major: Int, val minor: Int) | ||
|
||
private fun kotlinVersionNumbers(project: Project): KotlinVersionNumbers { | ||
val version = project.getKotlinPluginVersion() | ||
val versionNumbers = version.trim() | ||
.split(".") | ||
.take(2) | ||
val majorPart = versionNumbers.getOrNull(0) | ||
val minorPart = versionNumbers.getOrNull(1) | ||
return KotlinVersionNumbers( | ||
major = majorPart?.toIntOrNull() ?: error("Could not parse major part '$majorPart' of Kotlin plugin version: '$version'"), | ||
minor = minorPart?.toIntOrNull() ?: error("Could not parse minor part '$minorPart' of Kotlin plugin version: '$version'"), | ||
) | ||
} |
35 changes: 35 additions & 0 deletions
35
...plugins/compose/src/main/kotlin/org/jetbrains/compose/internal/utils/KGPPropertyFinder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright 2020-2023 JetBrains s.r.o. and respective authors and developers. | ||
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE.txt file. | ||
*/ | ||
|
||
package org.jetbrains.compose.internal.utils | ||
|
||
import org.gradle.api.Project | ||
import java.util.* | ||
|
||
/** | ||
* Reads Kotlin Gradle plugin properties. | ||
* | ||
* Kotlin Gradle plugin supports reading property from two sources: | ||
* 1. Gradle properties. Normally located in gradle.properties file, | ||
* but can also be provided via command-line, <GRADLE_HOME>/gradle.properties | ||
* or can be set via Gradle API. | ||
* 2. local.properties file. local.properties file is not supported by Gradle out-of-the-box. | ||
* Nevertheless, it became a widespread convention. | ||
*/ | ||
internal abstract class KGPPropertyFinder { | ||
abstract fun findProperty(propertyName: String): String? | ||
abstract val location: String | ||
|
||
class GradleProperties(private val project: Project) : KGPPropertyFinder() { | ||
override fun findProperty(propertyName: String): String? = project.findProperty(propertyName)?.toString() | ||
override val location: String = "gradle.properties" | ||
} | ||
|
||
class LocalProperties(project: Project) : KGPPropertyFinder() { | ||
private val localProperties: Properties by lazyLoadProperties(project.localPropertiesFile) | ||
override fun findProperty(propertyName: String): String? = localProperties.getProperty(propertyName) | ||
override val location: String = "local.properties" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
gradle-plugins/compose/src/test/test-projects/misc/nativeCacheKind/build.gradle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
plugins { | ||
id "org.jetbrains.kotlin.multiplatform" | ||
id "org.jetbrains.compose" | ||
} | ||
|
||
kotlin { | ||
iosX64 { | ||
binaries.framework { | ||
isStatic = true | ||
baseName = "shared" | ||
} | ||
} | ||
iosArm64 { | ||
binaries.framework { | ||
isStatic = true | ||
baseName = "shared" | ||
} | ||
} | ||
|
||
sourceSets { | ||
commonMain { | ||
dependencies { | ||
implementation(compose.runtime) | ||
implementation(compose.material) | ||
implementation(compose.foundation) | ||
} | ||
} | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
gradle-plugins/compose/src/test/test-projects/misc/nativeCacheKind/gradle.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
org.jetbrains.compose.experimental.uikit.enabled=true |
12 changes: 12 additions & 0 deletions
12
gradle-plugins/compose/src/test/test-projects/misc/nativeCacheKind/settings.gradle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
pluginManagement { | ||
plugins { | ||
id 'org.jetbrains.kotlin.multiplatform' version 'KOTLIN_VERSION_PLACEHOLDER' | ||
id 'org.jetbrains.compose' version 'COMPOSE_GRADLE_PLUGIN_VERSION_PLACEHOLDER' | ||
} | ||
repositories { | ||
mavenLocal() | ||
gradlePluginPortal() | ||
maven { url "https://maven.pkg.jetbrains.space/kotlin/p/kotlin/dev" } | ||
} | ||
} | ||
rootProject.name = "nativeCacheKind" |
21 changes: 21 additions & 0 deletions
21
...-plugins/compose/src/test/test-projects/misc/nativeCacheKind/src/commonMain/kotlin/App.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import androidx.compose.material.Button | ||
import androidx.compose.material.MaterialTheme | ||
import androidx.compose.material.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.setValue | ||
|
||
@Composable | ||
fun App() { | ||
var text by remember { mutableStateOf("Hello, World!") } | ||
|
||
MaterialTheme { | ||
Button(onClick = { | ||
text = "Hello, Desktop!" | ||
}) { | ||
Text(text) | ||
} | ||
} | ||
} |