diff --git a/build.gradle.kts b/build.gradle.kts index 7b9248ca49..503c6d1806 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -167,5 +167,7 @@ configure(subprojects.filter { AuxBuildConfiguration.configure(rootProject) rootProject.registerTopLevelDeployTask() -// Report Kotlin compiler version when building project -println("Using Kotlin compiler version: ${KotlinCompilerVersion.VERSION}") +if (isSnapshotTrainEnabled(rootProject)) { + // Report Kotlin compiler version when building project + println("Using Kotlin compiler version: ${KotlinCompilerVersion.VERSION}") +} diff --git a/buildSrc/src/main/kotlin/CommunityProjectsBuild.kt b/buildSrc/src/main/kotlin/CommunityProjectsBuild.kt index 0a6b90a5d7..fd99a2ed42 100644 --- a/buildSrc/src/main/kotlin/CommunityProjectsBuild.kt +++ b/buildSrc/src/main/kotlin/CommunityProjectsBuild.kt @@ -4,6 +4,7 @@ import org.gradle.api.* import org.gradle.api.artifacts.dsl.* import org.gradle.api.tasks.testing.Test import org.gradle.kotlin.dsl.* +import org.jetbrains.kotlin.gradle.dsl.KotlinCommonCompilerOptions import java.net.* import java.util.logging.* import org.jetbrains.kotlin.gradle.dsl.KotlinVersion @@ -102,7 +103,7 @@ fun Project.configureCommunityBuildTweaks() { } } - println("Manifest of kotlin-compiler-embeddable.jar for coroutines") + LOGGER.info("Manifest of kotlin-compiler-embeddable.jar for coroutines") val coreProject = subprojects.single { it.name == coreModule } configure(listOf(coreProject)) { configurations.matching { it.name == "kotlinCompilerClasspath" }.configureEach { @@ -147,3 +148,50 @@ fun shouldUseLocalMaven(project: Project): Boolean { } return hasSnapshotDependency || isSnapshotTrainEnabled(project) } + +/** + * Returns a non-null value if the CI needs to override the default behavior of treating warnings as errors. + * Then, `true` means that warnings should be treated as errors, `false` means that they should not. + */ +private fun warningsAreErrorsOverride(project: Project): Boolean? = + when (val prop = project.rootProject.properties["kotlin_Werror_override"] as? String) { + null -> null + "enable" -> true + "disable" -> false + else -> error("Unknown value for 'kotlin_Werror_override': $prop") + } + +/** + * Set warnings as errors, but allow the Kotlin User Project configuration to take over. See KT-75078. + */ +fun KotlinCommonCompilerOptions.setWarningsAsErrors(project: Project) { + if (warningsAreErrorsOverride(project) != false) { + allWarningsAsErrors = true + } else { + freeCompilerArgs.addAll("-Wextra", "-Xuse-fir-experimental-checkers") + } +} + +/** + * Compiler flags required of Kotlin User Projects. See KT-75078. + */ +fun KotlinCommonCompilerOptions.configureKotlinUserProject() { + freeCompilerArgs.addAll( + "-Xreport-all-warnings", // emit warnings even if there are also errors + "-Xrender-internal-diagnostic-names", // render the diagnostic names in CLI + ) +} + +/** + * Additional compiler flags passed on a case-by-case basis. Should be applied after the other flags. + * See + */ +fun KotlinCommonCompilerOptions.addExtraCompilerFlags(project: Project) { + val extraOptions = project.rootProject.properties["kotlin_additional_cli_options"] as? String + if (extraOptions != null) { + LOGGER.info("""Adding extra compiler flags '$extraOptions' for a compilation in the project $${project.name}""") + extraOptions.split(" ")?.forEach { + if (it.isNotEmpty()) freeCompilerArgs.add(it) + } + } +} diff --git a/buildSrc/src/main/kotlin/configure-compilation-conventions.gradle.kts b/buildSrc/src/main/kotlin/configure-compilation-conventions.gradle.kts index 26ee664c9c..42e7c01087 100644 --- a/buildSrc/src/main/kotlin/configure-compilation-conventions.gradle.kts +++ b/buildSrc/src/main/kotlin/configure-compilation-conventions.gradle.kts @@ -14,16 +14,21 @@ configure(subprojects) { apiVersion = it } if (isMainTaskName && !unpublished.contains(project.name)) { - allWarningsAsErrors = true - freeCompilerArgs.addAll("-Xexplicit-api=strict", "-Xdont-warn-on-error-suppression") + setWarningsAsErrors(project) + freeCompilerArgs.addAll( + "-Xexplicit-api=strict", + "-Xdont-warn-on-error-suppression", + ) } + configureKotlinUserProject() /* Coroutines do not interop with Java and these flags provide a significant * (i.e. close to double-digit) reduction in both bytecode and optimized dex size */ if (this@configureEach is KotlinJvmCompile) { freeCompilerArgs.addAll( "-Xno-param-assertions", "-Xno-call-assertions", - "-Xno-receiver-assertions" + "-Xno-receiver-assertions", + "-Xjvm-default=disable", ) } if (this@configureEach is KotlinNativeCompile) { @@ -33,6 +38,7 @@ configure(subprojects) { "kotlin.experimental.ExperimentalNativeApi", ) } + addExtraCompilerFlags(project) } } diff --git a/buildSrc/src/main/kotlin/kotlin-multiplatform-conventions.gradle.kts b/buildSrc/src/main/kotlin/kotlin-multiplatform-conventions.gradle.kts index f1845cc640..e2e1e66e2d 100644 --- a/buildSrc/src/main/kotlin/kotlin-multiplatform-conventions.gradle.kts +++ b/buildSrc/src/main/kotlin/kotlin-multiplatform-conventions.gradle.kts @@ -15,10 +15,7 @@ kotlin { jvm { compilations.all { compileTaskProvider.configure { - compilerOptions { - jvmTarget = JvmTarget.JVM_1_8 - freeCompilerArgs.addAll("-Xjvm-default=disable") - } + compilerOptions.jvmTarget = JvmTarget.JVM_1_8 } } }