Skip to content

Commit

Permalink
Fix AbstractKotlinCompile.ownModuleName usage (#2567)
Browse files Browse the repository at this point in the history
* Fix usage of deprecated method on creating a standalone KotlinJvmCompile task

* Use compilerOptions DSL for standalone KotlinJvmCompile task

* Add a workaround for AbstractKotlinCompile.ownModuleName property usage

The workaround tried to configure it only when it is available as via KT-64504 it will be removed in Kotlin 2.0.0 release.
  • Loading branch information
Tapchicoma authored Feb 12, 2024
1 parent 54840e0 commit 729fc24
Showing 1 changed file with 30 additions and 16 deletions.
46 changes: 30 additions & 16 deletions buildSrc/src/main/kotlin/Java9Modularity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@ import org.jetbrains.kotlin.gradle.plugin.*
import org.jetbrains.kotlin.gradle.plugin.mpp.*
import org.jetbrains.kotlin.gradle.plugin.mpp.pm20.util.*
import org.jetbrains.kotlin.gradle.targets.jvm.*
import org.jetbrains.kotlin.gradle.tasks.*
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import org.jetbrains.kotlin.gradle.tasks.KotlinJvmCompile
import org.jetbrains.kotlin.tooling.core.*
import java.io.*
import kotlin.reflect.*
import kotlin.reflect.full.*

object Java9Modularity {

Expand Down Expand Up @@ -108,12 +112,14 @@ object Java9Modularity {
compileTask: KotlinCompile,
sourceFile: File
): TaskProvider<out KotlinJvmCompile> {
apply<KotlinBaseApiPlugin>()
apply<KotlinApiPlugin>()
val verifyModuleTaskName = "verify${compileTask.name.removePrefix("compile").capitalize()}Module"
// work-around for https://youtrack.jetbrains.com/issue/KT-60542
val verifyModuleTask = plugins
.findPlugin(KotlinBaseApiPlugin::class)!!
.registerKotlinJvmCompileTask(verifyModuleTaskName)
val kotlinApiPlugin = plugins.getPlugin(KotlinApiPlugin::class)
val verifyModuleTask = kotlinApiPlugin.registerKotlinJvmCompileTask(
verifyModuleTaskName,
compileTask.compilerOptions.moduleName.get()
)
verifyModuleTask {
group = VERIFICATION_GROUP
description = "Verify Kotlin sources for JPMS problems"
Expand All @@ -126,13 +132,14 @@ object Java9Modularity {
source(sourceFile)
destinationDirectory.set(temporaryDir)
multiPlatformEnabled.set(compileTask.multiPlatformEnabled)
kotlinOptions {
moduleName = compileTask.kotlinOptions.moduleName
jvmTarget = "9"
compilerOptions {
jvmTarget.set(JvmTarget.JVM_9)
// To support LV override when set in aggregate builds
languageVersion = compileTask.kotlinOptions.languageVersion
freeCompilerArgs += listOf("-Xjdk-release=9", "-Xsuppress-version-warnings", "-Xexpect-actual-classes")
options.optIn.addAll(compileTask.kotlinOptions.options.optIn)
languageVersion.set(compileTask.compilerOptions.languageVersion)
freeCompilerArgs.addAll(
listOf("-Xjdk-release=9", "-Xsuppress-version-warnings", "-Xexpect-actual-classes")
)
optIn.addAll(compileTask.kotlinOptions.options.optIn)
}
// work-around for https://youtrack.jetbrains.com/issue/KT-60583
inputs.files(
Expand All @@ -145,12 +152,19 @@ object Java9Modularity {
}
).withPropertyName("moduleInfosOfLibraries")
this as KotlinCompile
// part of work-around for https://youtrack.jetbrains.com/issue/KT-60541
@Suppress("DEPRECATION")
ownModuleName.set(compileTask.kotlinOptions.moduleName)
// part of work-around for https://youtrack.jetbrains.com/issue/KT-60541
@Suppress("INVISIBLE_MEMBER")
commonSourceSet.from(compileTask.commonSourceSet)
val kotlinPluginVersion = KotlinToolingVersion(kotlinApiPlugin.pluginVersion)
if (kotlinPluginVersion <= KotlinToolingVersion("1.9.255")) {
// part of work-around for https://youtrack.jetbrains.com/issue/KT-60541
@Suppress("UNCHECKED_CAST")
val ownModuleNameProp = (this::class.superclasses.first() as KClass<AbstractKotlinCompile<*>>)
.declaredMemberProperties
.find { it.name == "ownModuleName" }
?.get(this) as? Property<String>
ownModuleNameProp?.set(compileTask.kotlinOptions.moduleName)
// part of work-around for https://youtrack.jetbrains.com/issue/KT-60541
@Suppress("INVISIBLE_MEMBER")
commonSourceSet.from(compileTask.commonSourceSet)
}
@OptIn(InternalKotlinGradlePluginApi::class)
apply {
multiplatformStructure.refinesEdges.set(compileTask.multiplatformStructure.refinesEdges)
Expand Down

0 comments on commit 729fc24

Please sign in to comment.