diff --git a/buildSrc/src/main/kotlin/KotlinConfiguration.kt b/buildSrc/src/main/kotlin/KotlinConfiguration.kt index 3d9aee1f..1412fe12 100644 --- a/buildSrc/src/main/kotlin/KotlinConfiguration.kt +++ b/buildSrc/src/main/kotlin/KotlinConfiguration.kt @@ -1,8 +1,8 @@ @file:JvmName("KotlinConfiguration") +import org.gradle.api.GradleException import org.gradle.api.Project -import org.gradle.api.artifacts.dsl.RepositoryHandler -import java.net.URI +import org.jetbrains.kotlin.gradle.dsl.KotlinCommonCompilerOptions import java.util.logging.Logger /* @@ -83,3 +83,49 @@ fun getOverridingKotlinApiVersion(project: Project): String? { */ fun irValidationMode(project: Project): String? = project.providers.gradleProperty("kotlin_ir_validation_mode").orNull + +/** + * Unconditional compiler flags required of Kotlin User Projects. See KT-75078. + */ +fun KotlinCommonCompilerOptions.addKotlinUserProjectFlags() { + freeCompilerArgs.addAll( + "-Xreport-all-warnings", + "-Xrender-internal-diagnostic-names", + ) +} + +/** + * Use `kotlin_Werror_override` property to override the state of -Werror: + * `true` means that warnings should be treated as errors,`false` means that they should not. + */ +private fun warningsAreErrorsOverride(project: Project): Boolean? = + project.providers.gradleProperty("kotlin_Werror_override").orNull.let { + when (it) { + "enable" -> true + "disable" -> false + null -> true + else -> throw GradleException("Invalid kotlin_Werror_override value. Use 'enable' or 'disable'") + } + } + +/** + * Set warnings as errors but allow the Kotlin User Project configuration to override. See KT-75078. + */ +fun KotlinCommonCompilerOptions.setWarningsAsErrors(project: Project) { + if (warningsAreErrorsOverride(project) == false) { + freeCompilerArgs.addAll("-Wextra", "-Xuse-fir-experimental-checkers") + } else { + allWarningsAsErrors.set(true) + } +} + +/** + * Pass additional CLI options to the Kotlin compiler. + */ +fun KotlinCommonCompilerOptions.addExtraCompilerFlags(project: Project) { + project.providers.gradleProperty("kotlin_additional_cli_options").orNull?.let { options -> + options.split(" ").filter { it.isNotBlank() }.forEach { + freeCompilerArgs.add(it) + } + } +} \ No newline at end of file diff --git a/buildSrc/src/main/kotlin/gradle-compatibility.gradle.kts b/buildSrc/src/main/kotlin/gradle-compatibility.gradle.kts index a6f2d5fc..8546f430 100644 --- a/buildSrc/src/main/kotlin/gradle-compatibility.gradle.kts +++ b/buildSrc/src/main/kotlin/gradle-compatibility.gradle.kts @@ -21,7 +21,7 @@ kotlin { // Gradle plugin must be compiled targeting the same Kotlin version as used by Gradle @Suppress("DEPRECATION", "DEPRECATION_ERROR") compilerOptions { - allWarningsAsErrors.set(true) + setWarningsAsErrors(project) freeCompilerArgs.add("-Xsuppress-version-warnings") languageVersion = getOverridingKotlinLanguageVersion(project)?.let { KotlinVersion.fromVersion(it) } diff --git a/buildSrc/src/main/kotlin/kotlin-multiplatform-conventions.gradle.kts b/buildSrc/src/main/kotlin/kotlin-multiplatform-conventions.gradle.kts index b78c8ffd..82dd9019 100644 --- a/buildSrc/src/main/kotlin/kotlin-multiplatform-conventions.gradle.kts +++ b/buildSrc/src/main/kotlin/kotlin-multiplatform-conventions.gradle.kts @@ -29,8 +29,17 @@ tasks.withType().configureEach { tasks.withType>().configureEach { compilerOptions { + addKotlinUserProjectFlags() + addExtraCompilerFlags(project) irValidationMode(project)?.let { freeCompilerArgs.addAll("-Xverify-ir=$it", "-Xverify-ir-visibility") } } } + +tasks.withType>().configureEach { + doFirst { + logger.info("Added Kotlin compiler flags: ${compilerOptions.freeCompilerArgs.get().joinToString(", ")}") + logger.info("allWarningsAsErrors=${compilerOptions.allWarningsAsErrors.get()}") + } +}