Skip to content
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

[all] Set jdk-release #5731

Merged
merged 1 commit into from
Mar 18, 2024
Merged
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
use jdk-release
  • Loading branch information
martinbonnin committed Mar 15, 2024
commit e1d7ee1b204d5437b3e437d2f93daea91bb4fe0a
37 changes: 24 additions & 13 deletions build-logic/src/main/kotlin/CompilerOptions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ import org.jetbrains.kotlin.gradle.dsl.KotlinProjectExtension
import org.jetbrains.kotlin.gradle.dsl.KotlinVersion
import org.jetbrains.kotlin.gradle.plugin.KotlinPlatformType

fun KotlinCommonCompilerOptions.configure(baseJvmTarget: Int, isAndroid: Boolean) {
/**
* @param target the JVM version we want to be compatible with (bytecode + bootstrap classpath)
*/
fun KotlinCommonCompilerOptions.configure(target: Int) {
freeCompilerArgs.add("-Xexpect-actual-classes")

/**
Expand All @@ -37,16 +40,8 @@ fun KotlinCommonCompilerOptions.configure(baseJvmTarget: Int, isAndroid: Boolean
when (this) {
is KotlinJvmCompilerOptions -> {
freeCompilerArgs.add("-Xjvm-default=all")
val target = when {
isAndroid -> {
// https://blog.blundellapps.co.uk/setting-jdk-level-in-android-gradle-builds/
// D8 can dex Java17 bytecode
JvmTarget.JVM_17
}
baseJvmTarget == 8 -> JvmTarget.JVM_1_8
else -> JvmTarget.fromTarget(baseJvmTarget.toString())
}
jvmTarget.set(target)
freeCompilerArgs.add("-Xjdk-release=${target.toJvmTarget().target}")
jvmTarget.set(target.toJvmTarget())
}

is KotlinNativeCompilerOptions -> {
Expand All @@ -60,6 +55,13 @@ fun KotlinCommonCompilerOptions.configure(baseJvmTarget: Int, isAndroid: Boolean
}
}

private fun Int.toJvmTarget(): JvmTarget {
return when(this) {
8 -> JvmTarget.JVM_1_8
else -> JvmTarget.fromTarget(this.toString())
}
}

private fun KotlinProjectExtension.forEachCompilerOptions(block: KotlinCommonCompilerOptions.(isAndroid: Boolean) -> Unit) {
when (this) {
is KotlinJvmProjectExtension -> compilerOptions.block(false)
Expand Down Expand Up @@ -94,8 +96,17 @@ fun Project.configureJavaAndKotlinCompilers(jvmTarget: Int?) {
@Suppress("NAME_SHADOWING")
val jvmTarget = jvmTarget?: 8

kotlinExtensionOrNull?.forEachCompilerOptions {
configure(jvmTarget, it)
kotlinExtensionOrNull?.forEachCompilerOptions { isAndroid ->
val target = when {
isAndroid -> {
// https://blog.blundellapps.co.uk/setting-jdk-level-in-android-gradle-builds/
// D8 can dex Java17 bytecode
17
}
else -> jvmTarget
}

configure(target)
}
project.tasks.withType(JavaCompile::class.java).configureEach {
// For JVM only modules, this dictates the "org.gradle.jvm.version" Gradle attribute
Expand Down
Loading