Skip to content

Fulfill the new requirements for Kotlin User Projects #4392

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
}
50 changes: 49 additions & 1 deletion buildSrc/src/main/kotlin/CommunityProjectsBuild.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably, should be used API compatible with isolated projects?

Suggested change
when (val prop = project.rootProject.properties["kotlin_Werror_override"] as? String) {
when (val prop = project.providers.gradleProperty("kotlin_Werror_override").orNull) {

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, but I don't know what the difference is. rootProject.properties is already used throughout our build scripts, and I haven't heard of any issues because of that. Could provide/link to a clear explanation of the downsides of the existing approach?

Copy link
Member

@osipxd osipxd Mar 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure!
Calling Project.properties is quite expensive because it collects not only properties from gradle.properties and CLI arguments, but also tasks and their fields, extensions, extras, conventions, etc (see gradle/gradle#25053)
getProperty and hasProperty are better, but they're not compatible with isolated projects because of the access to properties of other projects.

Since you're getting the property from the root project, it should be safe to use providers.gradleProperty which looks for Gradle properties in the root project. As a bonus, this API is compatible with isolated projects (which I hope will be stable someday) as there is no need in calling methods of the rootProject

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

However, as soon as this syntax is used everywhere in this file, this change is indeed out of the scope of the current PR.

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 <https://github.com/Kotlin/kotlinx.coroutines/pull/4392#issuecomment-2775630200>
*/
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)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -33,6 +38,7 @@ configure(subprojects) {
"kotlin.experimental.ExperimentalNativeApi",
)
}
addExtraCompilerFlags(project)
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
Expand Down