Skip to content

Commit fdec0de

Browse files
authored
Fulfill the new requirements for Kotlin User Projects (#4392)
Fixes KT-75078 for `kotlinx.coroutines`
1 parent 4ba95e0 commit fdec0de

4 files changed

+63
-10
lines changed

build.gradle.kts

+4-2
Original file line numberDiff line numberDiff line change
@@ -167,5 +167,7 @@ configure(subprojects.filter {
167167
AuxBuildConfiguration.configure(rootProject)
168168
rootProject.registerTopLevelDeployTask()
169169

170-
// Report Kotlin compiler version when building project
171-
println("Using Kotlin compiler version: ${KotlinCompilerVersion.VERSION}")
170+
if (isSnapshotTrainEnabled(rootProject)) {
171+
// Report Kotlin compiler version when building project
172+
println("Using Kotlin compiler version: ${KotlinCompilerVersion.VERSION}")
173+
}

buildSrc/src/main/kotlin/CommunityProjectsBuild.kt

+49-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import org.gradle.api.*
44
import org.gradle.api.artifacts.dsl.*
55
import org.gradle.api.tasks.testing.Test
66
import org.gradle.kotlin.dsl.*
7+
import org.jetbrains.kotlin.gradle.dsl.KotlinCommonCompilerOptions
78
import java.net.*
89
import java.util.logging.*
910
import org.jetbrains.kotlin.gradle.dsl.KotlinVersion
@@ -102,7 +103,7 @@ fun Project.configureCommunityBuildTweaks() {
102103
}
103104
}
104105

105-
println("Manifest of kotlin-compiler-embeddable.jar for coroutines")
106+
LOGGER.info("Manifest of kotlin-compiler-embeddable.jar for coroutines")
106107
val coreProject = subprojects.single { it.name == coreModule }
107108
configure(listOf(coreProject)) {
108109
configurations.matching { it.name == "kotlinCompilerClasspath" }.configureEach {
@@ -147,3 +148,50 @@ fun shouldUseLocalMaven(project: Project): Boolean {
147148
}
148149
return hasSnapshotDependency || isSnapshotTrainEnabled(project)
149150
}
151+
152+
/**
153+
* Returns a non-null value if the CI needs to override the default behavior of treating warnings as errors.
154+
* Then, `true` means that warnings should be treated as errors, `false` means that they should not.
155+
*/
156+
private fun warningsAreErrorsOverride(project: Project): Boolean? =
157+
when (val prop = project.rootProject.properties["kotlin_Werror_override"] as? String) {
158+
null -> null
159+
"enable" -> true
160+
"disable" -> false
161+
else -> error("Unknown value for 'kotlin_Werror_override': $prop")
162+
}
163+
164+
/**
165+
* Set warnings as errors, but allow the Kotlin User Project configuration to take over. See KT-75078.
166+
*/
167+
fun KotlinCommonCompilerOptions.setWarningsAsErrors(project: Project) {
168+
if (warningsAreErrorsOverride(project) != false) {
169+
allWarningsAsErrors = true
170+
} else {
171+
freeCompilerArgs.addAll("-Wextra", "-Xuse-fir-experimental-checkers")
172+
}
173+
}
174+
175+
/**
176+
* Compiler flags required of Kotlin User Projects. See KT-75078.
177+
*/
178+
fun KotlinCommonCompilerOptions.configureKotlinUserProject() {
179+
freeCompilerArgs.addAll(
180+
"-Xreport-all-warnings", // emit warnings even if there are also errors
181+
"-Xrender-internal-diagnostic-names", // render the diagnostic names in CLI
182+
)
183+
}
184+
185+
/**
186+
* Additional compiler flags passed on a case-by-case basis. Should be applied after the other flags.
187+
* See <https://github.com/Kotlin/kotlinx.coroutines/pull/4392#issuecomment-2775630200>
188+
*/
189+
fun KotlinCommonCompilerOptions.addExtraCompilerFlags(project: Project) {
190+
val extraOptions = project.rootProject.properties["kotlin_additional_cli_options"] as? String
191+
if (extraOptions != null) {
192+
LOGGER.info("""Adding extra compiler flags '$extraOptions' for a compilation in the project $${project.name}""")
193+
extraOptions.split(" ")?.forEach {
194+
if (it.isNotEmpty()) freeCompilerArgs.add(it)
195+
}
196+
}
197+
}

buildSrc/src/main/kotlin/configure-compilation-conventions.gradle.kts

+9-3
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,21 @@ configure(subprojects) {
1414
apiVersion = it
1515
}
1616
if (isMainTaskName && !unpublished.contains(project.name)) {
17-
allWarningsAsErrors = true
18-
freeCompilerArgs.addAll("-Xexplicit-api=strict", "-Xdont-warn-on-error-suppression")
17+
setWarningsAsErrors(project)
18+
freeCompilerArgs.addAll(
19+
"-Xexplicit-api=strict",
20+
"-Xdont-warn-on-error-suppression",
21+
)
1922
}
23+
configureKotlinUserProject()
2024
/* Coroutines do not interop with Java and these flags provide a significant
2125
* (i.e. close to double-digit) reduction in both bytecode and optimized dex size */
2226
if (this@configureEach is KotlinJvmCompile) {
2327
freeCompilerArgs.addAll(
2428
"-Xno-param-assertions",
2529
"-Xno-call-assertions",
26-
"-Xno-receiver-assertions"
30+
"-Xno-receiver-assertions",
31+
"-Xjvm-default=disable",
2732
)
2833
}
2934
if (this@configureEach is KotlinNativeCompile) {
@@ -33,6 +38,7 @@ configure(subprojects) {
3338
"kotlin.experimental.ExperimentalNativeApi",
3439
)
3540
}
41+
addExtraCompilerFlags(project)
3642
}
3743

3844
}

buildSrc/src/main/kotlin/kotlin-multiplatform-conventions.gradle.kts

+1-4
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,7 @@ kotlin {
1515
jvm {
1616
compilations.all {
1717
compileTaskProvider.configure {
18-
compilerOptions {
19-
jvmTarget = JvmTarget.JVM_1_8
20-
freeCompilerArgs.addAll("-Xjvm-default=disable")
21-
}
18+
compilerOptions.jvmTarget = JvmTarget.JVM_1_8
2219
}
2320
}
2421
}

0 commit comments

Comments
 (0)