xref: /aosp_15_r20/external/kotlinx.coroutines/buildSrc/src/main/kotlin/AuxBuildConfiguration.kt (revision 7a7160fed73afa6648ef8aa100d4a336fe921d9a)
1 import CacheRedirector.configure
2 import org.gradle.api.Project
3 import org.gradle.api.tasks.*
4 import org.gradle.kotlin.dsl.*
5 
6 /**
7  * Auxiliary build configuration that is grouped in a single place for convenience:
8  * - Workarounds for Gradle/KGP issues
9  * - Cache redirector
10  */
11 object AuxBuildConfiguration {
12 
13     @JvmStatic
configurenull14     fun configure(rootProject: Project) {
15         rootProject.allprojects {
16             workaroundForCleanTask()
17             CacheRedirector.configure(this)
18             workaroundForCoroutinesLeakageToClassPath()
19         }
20 
21         CacheRedirector.configureJsPackageManagers(rootProject)
22         CacheRedirector.configureWasmNodeRepositories(rootProject)
23 
24         // Sigh, there is no BuildScanExtension in classpath when there is no --scan
25         rootProject.extensions.findByName("buildScan")?.withGroovyBuilder {
26             setProperty("termsOfServiceUrl", "https://gradle.com/terms-of-service")
27             setProperty("termsOfServiceAgree", "yes")
28         }
29     }
30 
Projectnull31     private fun Project.workaroundForCleanTask() {
32         // the 'clean' task cannot delete expanded.lock file on Windows as it is still held by Gradle, failing the build
33         // Gradle issue: https://github.com/gradle/gradle/issues/25752
34         tasks {
35             val clean by existing(Delete::class) {
36                 setDelete(fileTree(layout.buildDirectory) {
37                     exclude("tmp/.cache/expanded/expanded.lock")
38                 })
39             }
40         }
41     }
42 
43     /*
44      * 'kotlinx-coroutines-core' dependency leaks into test runtime classpath via 'kotlin-compiler-embeddable'
45      * and conflicts with our own test/runtime incompatibilities (e.g. when class is moved from a main to test),
46      * so we do substitution here.
47      * TODO figure out if it's still the problem
48      */
Projectnull49     private fun Project.workaroundForCoroutinesLeakageToClassPath() {
50         configurations
51             .matching {
52                 // Excluding substituted project itself because of circular dependencies, but still do it
53                 // for "*Test*" configurations
54                 name != coreModule || it.name.contains("Test")
55             }
56             .configureEach {
57                 resolutionStrategy.dependencySubstitution {
58                     substitute(module("org.jetbrains.kotlinx:$coreModule"))
59                         .using(project(":$coreModule"))
60                         .because(
61                             "Because Kotlin compiler embeddable leaks coroutines into the runtime classpath, " +
62                                 "triggering all sort of incompatible class changes errors"
63                         )
64                 }
65             }
66     }
67 }
68