Skip to content

Commit 6bbfc2b

Browse files
committed
Fulfill the new requirements for Kotlin User Projects
1 parent 8627cc3 commit 6bbfc2b

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

buildSrc/src/main/kotlin/CommunityProjectsBuild.kt

+34
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
@@ -147,3 +148,36 @@ 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+
}

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,13 @@ 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) {

0 commit comments

Comments
 (0)