Skip to content

Commit e5bb191

Browse files
authored
Use providers.gradle property instead of rootProject.properties in the build scripts (#4421)
See <https://github.com/Kotlin/kotlinx.coroutines/pull/4392/files#r2016110128> for an explanation of the benefits of this. Also, fix a bug in `integration-testing` that used to always treat the build as one with snapshot dependencies, given that it was always using the snapshot version of `kotlinx.coroutines` by design.
1 parent e548509 commit e5bb191

File tree

8 files changed

+55
-30
lines changed

8 files changed

+55
-30
lines changed

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ allprojects {
4040
if (deployVersion != null) version = deployVersion
4141

4242
if (isSnapshotTrainEnabled(rootProject)) {
43-
val skipSnapshotChecks = rootProject.properties["skip_snapshot_checks"] != null
43+
val skipSnapshotChecks = providers.gradleProperty("skip_snapshot_checks").isPresent
4444
if (!skipSnapshotChecks && version != version("atomicfu")) {
4545
throw IllegalStateException("Current deploy version is $version, but atomicfu version is not overridden (${version("atomicfu")}) for $this")
4646
}

buildSrc/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ plugins {
66

77
val cacheRedirectorEnabled = System.getenv("CACHE_REDIRECTOR")?.toBoolean() == true
88
val buildSnapshotTrain = properties["build_snapshot_train"]?.toString()?.toBoolean() == true
9-
val kotlinDevUrl = project.rootProject.properties["kotlin_repo_url"] as? String
9+
val kotlinDevUrl = project.providers.gradleProperty("kotlin_repo_url").orNull
1010

1111
repositories {
1212
mavenCentral()

buildSrc/src/main/kotlin/CommunityProjectsBuild.kt

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ private val LOGGER: Logger = Logger.getLogger("Kotlin settings logger")
3333
* @return a Kotlin API version parametrized from command line nor gradle.properties, null otherwise
3434
*/
3535
fun getOverriddenKotlinApiVersion(project: Project): KotlinVersion? {
36-
val apiVersion = project.rootProject.properties["kotlin_api_version"] as? String
36+
val apiVersion = project.providers.gradleProperty("kotlin_api_version").orNull
3737
return if (apiVersion != null) {
3838
LOGGER.info("""Configured Kotlin API version: '$apiVersion' for project $${project.name}""")
3939
KotlinVersion.fromVersion(apiVersion)
@@ -48,7 +48,7 @@ fun getOverriddenKotlinApiVersion(project: Project): KotlinVersion? {
4848
* @return a Kotlin Language version parametrized from command line nor gradle.properties, null otherwise
4949
*/
5050
fun getOverriddenKotlinLanguageVersion(project: Project): KotlinVersion? {
51-
val languageVersion = project.rootProject.properties["kotlin_language_version"] as? String
51+
val languageVersion = project.providers.gradleProperty("kotlin_language_version").orNull
5252
return if (languageVersion != null) {
5353
LOGGER.info("""Configured Kotlin Language version: '$languageVersion' for project ${project.name}""")
5454
KotlinVersion.fromVersion(languageVersion)
@@ -66,7 +66,7 @@ fun getOverriddenKotlinLanguageVersion(project: Project): KotlinVersion? {
6666
* @return an url for a kotlin compiler repository parametrized from command line nor gradle.properties, empty string otherwise
6767
*/
6868
fun getKotlinDevRepositoryUrl(project: Project): URI? {
69-
val url: String? = project.rootProject.properties["kotlin_repo_url"] as? String
69+
val url: String? = project.providers.gradleProperty("kotlin_repo_url").orNull
7070
if (url != null) {
7171
LOGGER.info("""Configured Kotlin Compiler repository url: '$url' for project ${project.name}""")
7272
return URI.create(url)
@@ -114,7 +114,7 @@ fun Project.configureCommunityBuildTweaks() {
114114
}.files.single()
115115

116116
manifest.readLines().forEach {
117-
println(it)
117+
LOGGER.info(it)
118118
}
119119
}
120120
}
@@ -125,9 +125,8 @@ fun Project.configureCommunityBuildTweaks() {
125125
*/
126126
fun getOverriddenKotlinVersion(project: Project): String? =
127127
if (isSnapshotTrainEnabled(project)) {
128-
val snapshotVersion = project.rootProject.properties["kotlin_snapshot_version"]
128+
project.providers.gradleProperty("kotlin_snapshot_version").orNull
129129
?: error("'kotlin_snapshot_version' should be defined when building with a snapshot compiler")
130-
snapshotVersion.toString()
131130
} else {
132131
null
133132
}
@@ -136,14 +135,29 @@ fun getOverriddenKotlinVersion(project: Project): String? =
136135
* Checks if the project is built with a snapshot version of Kotlin compiler.
137136
*/
138137
fun isSnapshotTrainEnabled(project: Project): Boolean {
139-
val buildSnapshotTrain = project.rootProject.properties["build_snapshot_train"] as? String
138+
val buildSnapshotTrain = project.providers.gradleProperty("build_snapshot_train").orNull
140139
return !buildSnapshotTrain.isNullOrBlank()
141140
}
142141

142+
/**
143+
* The list of projects snapshot versions of which we may want to use with `kotlinx.coroutines`.
144+
*
145+
* In `gradle.properties`, these properties are defined as `<name>_version`, e.g. `kotlin_version`.
146+
*/
147+
val firstPartyDependencies = listOf(
148+
"kotlin",
149+
"atomicfu",
150+
)
151+
143152
fun shouldUseLocalMaven(project: Project): Boolean {
144-
val hasSnapshotDependency = project.rootProject.properties.any { (key, value) ->
145-
key.endsWith("_version") && value is String && value.endsWith("-SNAPSHOT").also {
146-
if (it) println("NOTE: USING SNAPSHOT VERSION: $key=$value")
153+
val hasSnapshotDependency = firstPartyDependencies.any { dependencyName ->
154+
val key = "${dependencyName}_version"
155+
val value = project.providers.gradleProperty(key).orNull
156+
if (value != null && value.endsWith("-SNAPSHOT")) {
157+
LOGGER.info("NOTE: USING SNAPSHOT VERSION: $key=$value")
158+
true
159+
} else {
160+
false
147161
}
148162
}
149163
return hasSnapshotDependency || isSnapshotTrainEnabled(project)
@@ -154,7 +168,7 @@ fun shouldUseLocalMaven(project: Project): Boolean {
154168
* Then, `true` means that warnings should be treated as errors, `false` means that they should not.
155169
*/
156170
private fun warningsAreErrorsOverride(project: Project): Boolean? =
157-
when (val prop = project.rootProject.properties["kotlin_Werror_override"] as? String) {
171+
when (val prop = project.providers.gradleProperty("kotlin_Werror_override").orNull) {
158172
null -> null
159173
"enable" -> true
160174
"disable" -> false
@@ -187,10 +201,10 @@ fun KotlinCommonCompilerOptions.configureKotlinUserProject() {
187201
* See <https://github.com/Kotlin/kotlinx.coroutines/pull/4392#issuecomment-2775630200>
188202
*/
189203
fun KotlinCommonCompilerOptions.addExtraCompilerFlags(project: Project) {
190-
val extraOptions = project.rootProject.properties["kotlin_additional_cli_options"] as? String
204+
val extraOptions = project.providers.gradleProperty("kotlin_additional_cli_options").orNull
191205
if (extraOptions != null) {
192206
LOGGER.info("""Adding extra compiler flags '$extraOptions' for a compilation in the project $${project.name}""")
193-
extraOptions.split(" ")?.forEach {
207+
extraOptions.split(" ").forEach {
194208
if (it.isNotEmpty()) freeCompilerArgs.add(it)
195209
}
196210
}

buildSrc/src/main/kotlin/Projects.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,16 @@ fun Project.version(target: String): String {
1010
return property("${target}_version") as String
1111
}
1212

13-
val Project.jdkToolchainVersion: Int get() = property("jdk_toolchain_version").toString().toInt()
13+
val Project.jdkToolchainVersion: Int get() =
14+
providers.gradleProperty("jdk_toolchain_version").get().toInt()
1415

1516
/**
1617
* TODO: check if this is still relevant.
1718
* It was introduced in <https://github.com/Kotlin/kotlinx.coroutines/pull/2389>, and the project for which this was
1819
* done is already long finished.
1920
*/
20-
val Project.nativeTargetsAreEnabled: Boolean get() = rootProject.properties["disable_native_targets"] == null
21+
val Project.nativeTargetsAreEnabled: Boolean get() =
22+
!providers.gradleProperty("disable_native_targets").isPresent
2123

2224
val Project.sourceSets: SourceSetContainer
2325
get() = extensions.getByName("sourceSets") as SourceSetContainer

gradle.properties

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
# Kotlin
21
version=1.10.2-SNAPSHOT
32
group=org.jetbrains.kotlinx
3+
4+
# First-party dependencies.
5+
# ONLY rename these properties alongside adapting kotlinx.train build chain
6+
# and the `firstPartyDependencies` list in `buildSrc`.
47
kotlin_version=2.1.20
5-
# DO NOT rename this property without adapting kotlinx.train build chain:
68
atomicfu_version=0.26.1
7-
benchmarks_version=0.4.13
8-
benchmarks_jmh_version=1.37
99

1010
# Dependencies
11+
benchmarks_version=0.4.13
12+
benchmarks_jmh_version=1.37
1113
junit_version=4.12
1214
junit5_version=5.7.0
1315
knit_version=0.5.0

integration-testing/build.gradle.kts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,23 @@ buildscript {
1515
DO NOT change the name of these properties without adapting kotlinx.train build chain.
1616
*/
1717
fun checkIsSnapshotTrainProperty(): Boolean {
18-
val buildSnapshotTrain = rootProject.properties["build_snapshot_train"]?.toString()
18+
val buildSnapshotTrain = providers.gradleProperty("build_snapshot_train").orNull
1919
return !buildSnapshotTrain.isNullOrEmpty()
2020
}
2121

22+
val firstPartyDependencies = listOf(
23+
"kotlin",
24+
"atomicfu",
25+
)
26+
2227
fun checkIsSnapshotVersion(): Boolean {
2328
var usingSnapshotVersion = checkIsSnapshotTrainProperty()
24-
rootProject.properties.forEach { (key, value) ->
25-
if (key.endsWith("_version") && value is String && value.endsWith("-SNAPSHOT")) {
26-
println("NOTE: USING SNAPSHOT VERSION: $key=$value")
27-
usingSnapshotVersion = true
28-
}
29+
for (key in firstPartyDependencies) {
30+
val value = providers.gradleProperty("${key}_version").orNull
31+
?.takeIf { it.endsWith("-SNAPSHOT") }
32+
?: continue
33+
println("NOTE: USING SNAPSHOT VERSION: $key=$value")
34+
usingSnapshotVersion = true
2935
}
3036
return usingSnapshotVersion
3137
}
@@ -64,10 +70,10 @@ java {
6470
}
6571

6672
val kotlinVersion = if (extra["build_snapshot_train"] == true) {
67-
rootProject.properties["kotlin_snapshot_version"]?.toString()
73+
providers.gradleProperty("kotlin_snapshot_version").orNull
6874
?: throw IllegalArgumentException("'kotlin_snapshot_version' should be defined when building with snapshot compiler")
6975
} else {
70-
rootProject.properties["kotlin_version"].toString()
76+
providers.gradleProperty("kotlin_version").get()
7177
}
7278

7379
val asmVersion = property("asm_version")

integration-testing/src/debugDynamicAgentTest/kotlin/DynamicAttachDebugTest.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import org.junit.Test
55
import java.io.*
66
import java.lang.IllegalStateException
77

8+
@OptIn(kotlinx.coroutines.ExperimentalCoroutinesApi::class)
89
class DynamicAttachDebugTest {
910

1011
@Test

kotlinx-coroutines-core/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ val jvmTest by tasks.getting(Test::class) {
140140
maxHeapSize = "1g"
141141
enableAssertions = true
142142
// 'stress' is required to be able to run all subpackage tests like ":jvmTests --tests "*channels*" -Pstress=true"
143-
if (!Idea.active && rootProject.properties["stress"] == null) {
143+
if (!Idea.active && !providers.gradleProperty("stress").isPresent) {
144144
exclude("**/*LincheckTest*")
145145
exclude("**/*StressTest.*")
146146
}

0 commit comments

Comments
 (0)