xref: /aosp_15_r20/external/kotlinx.coroutines/buildSrc/src/main/kotlin/CommunityProjectsBuild.kt (revision 7a7160fed73afa6648ef8aa100d4a336fe921d9a)

<lambda>null1 @file:JvmName("CommunityProjectsBuild")
2 
3 import org.gradle.api.*
4 import org.gradle.api.artifacts.dsl.*
5 import org.gradle.api.tasks.testing.Test
6 import org.gradle.kotlin.dsl.*
7 import java.net.*
8 import java.util.logging.*
9 import org.jetbrains.kotlin.gradle.dsl.KotlinVersion
10 
11 private val LOGGER: Logger = Logger.getLogger("Kotlin settings logger")
12 
13 /**
14  * Functions in this file are responsible for configuring kotlinx.coroutines build against a custom dev version
15  * of Kotlin compiler.
16  * Such configuration is used in a composite community build of Kotlin in order to check whether not-yet-released changes
17  * are compatible with our libraries (aka "integration testing that substitutes lack of unit testing").
18  *
19  * When `build_snapshot_train` is set to true (and [isSnapshotTrainEnabled] returns `true`),
20  * - `kotlin_version property` is overridden with `kotlin_snapshot_version` (see [getOverriddenKotlinVersion]),
21  * - `atomicfu_version` is overwritten by TeamCity environment (AFU is built with snapshot and published to mavenLocal
22  *   as previous step or the snapshot build).
23  * Additionally, mavenLocal and Sonatype snapshots are added to repository list and stress tests are disabled
24  * (see [configureCommunityBuildTweaks]).
25  *
26  * DO NOT change the name of these properties without adapting the kotlinx.train build chain.
27 */
28 
29 /**
30  * Should be used for running against of non-released Kotlin compiler on a system test level.
31  *
32  * @return a Kotlin API version parametrized from command line nor gradle.properties, null otherwise
33  */
34 fun getOverriddenKotlinApiVersion(project: Project): KotlinVersion? {
35     val apiVersion = project.rootProject.properties["kotlin_api_version"] as? String
36     return if (apiVersion != null) {
37         LOGGER.info("""Configured Kotlin API version: '$apiVersion' for project $${project.name}""")
38         KotlinVersion.fromVersion(apiVersion)
39     } else {
40         null
41     }
42 }
43 
44 /**
45  * Should be used for running against of non-released Kotlin compiler on a system test level
46  *
47  * @return a Kotlin Language version parametrized from command line nor gradle.properties, null otherwise
48  */
getOverriddenKotlinLanguageVersionnull49 fun getOverriddenKotlinLanguageVersion(project: Project): KotlinVersion? {
50     val languageVersion = project.rootProject.properties["kotlin_language_version"] as? String
51     return if (languageVersion != null) {
52         LOGGER.info("""Configured Kotlin Language version: '$languageVersion' for project ${project.name}""")
53         KotlinVersion.fromVersion(languageVersion)
54     } else {
55         null
56     }
57 }
58 
59 /**
60  * Should be used for running against of non-released Kotlin compiler on a system test level
61  * Kotlin compiler artifacts are expected to be downloaded from maven central by default.
62  * In case of compiling with not-published into the MC kotlin compiler artifacts, a kotlin_repo_url gradle parameter should be specified.
63  * To reproduce a build locally, a kotlin/dev repo should be passed
64  *
65  * @return an url for a kotlin compiler repository parametrized from command line nor gradle.properties, empty string otherwise
66  */
getKotlinDevRepositoryUrlnull67 fun getKotlinDevRepositoryUrl(project: Project): URI? {
68     val url: String? = project.rootProject.properties["kotlin_repo_url"] as? String
69     if (url != null) {
70         LOGGER.info("""Configured Kotlin Compiler repository url: '$url' for project ${project.name}""")
71         return URI.create(url)
72     }
73     return null
74 }
75 
76 /**
77  * Adds a kotlin-dev space repository with dev versions of Kotlin if Kotlin aggregate build is enabled
78  */
addDevRepositoryIfEnablednull79 fun addDevRepositoryIfEnabled(rh: RepositoryHandler, project: Project) {
80     val devRepoUrl = getKotlinDevRepositoryUrl(project) ?: return
81     rh.maven {
82         url = devRepoUrl
83     }
84 }
85 
86 /**
87  * Changes the build config when 'build_snapshot_train' is enabled:
88  * Disables flaky and Kotlin-specific tests, prints the real version of Kotlin applied (to be sure overridden version of Kotlin is properly picked).
89  */
Projectnull90 fun Project.configureCommunityBuildTweaks() {
91     if (!isSnapshotTrainEnabled(this)) return
92     allprojects {
93         // Disable stress tests and tests that are flaky on Kotlin version specific
94         tasks.withType<Test>().configureEach {
95             exclude("**/*LinearizabilityTest*")
96             exclude("**/*LFTest*")
97             exclude("**/*StressTest*")
98             exclude("**/*scheduling*")
99             exclude("**/*Timeout*")
100             exclude("**/*definitely/not/kotlinx*")
101             exclude("**/*PrecompiledDebugProbesTest*")
102         }
103     }
104 
105     println("Manifest of kotlin-compiler-embeddable.jar for coroutines")
106     val coreProject = subprojects.single { it.name == coreModule }
107     configure(listOf(coreProject)) {
108         configurations.matching { it.name == "kotlinCompilerClasspath" }.configureEach {
109             val config = resolvedConfiguration.files.single { it.name.contains("kotlin-compiler-embeddable") }
110 
111             val manifest = zipTree(config).matching {
112                 include("META-INF/MANIFEST.MF")
113             }.files.single()
114 
115             manifest.readLines().forEach {
116                 println(it)
117             }
118         }
119     }
120 }
121 
122 /**
123  * Ensures that, if [isSnapshotTrainEnabled] is true, the project is built with a snapshot version of Kotlin compiler.
124  */
getOverriddenKotlinVersionnull125 fun getOverriddenKotlinVersion(project: Project): String? =
126     if (isSnapshotTrainEnabled(project)) {
127         val snapshotVersion = project.rootProject.properties["kotlin_snapshot_version"]
128             ?: error("'kotlin_snapshot_version' should be defined when building with a snapshot compiler")
129         snapshotVersion.toString()
130     } else {
131         null
132     }
133 
134 /**
135  * Checks if the project is built with a snapshot version of Kotlin compiler.
136  */
isSnapshotTrainEnablednull137 fun isSnapshotTrainEnabled(project: Project): Boolean =
138     when (project.rootProject.properties["build_snapshot_train"]) {
139         null -> false
140         "" -> false
141         else -> true
142     }
143 
shouldUseLocalMavennull144 fun shouldUseLocalMaven(project: Project): Boolean {
145     var someDependencyIsSnapshot = false
146     project.rootProject.properties.forEach { key, value ->
147         if (key.endsWith("_version") && value is String && value.endsWith("-SNAPSHOT")) {
148             println("NOTE: USING SNAPSHOT VERSION: $key=$value")
149             someDependencyIsSnapshot = true
150         }
151     }
152     return isSnapshotTrainEnabled(project) || someDependencyIsSnapshot
153 }
154