From 3a12dc22d6ec3924067c7b32582cb6a1ed113344 Mon Sep 17 00:00:00 2001 From: Jonas Herzig Date: Fri, 26 May 2023 16:15:06 +0200 Subject: Allow JDK_HOME to be set explicitly, required for Kotlin 1.8+ --- src/main/kotlin/com/replaymod/gradle/remap/Transformer.kt | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'src/main') diff --git a/src/main/kotlin/com/replaymod/gradle/remap/Transformer.kt b/src/main/kotlin/com/replaymod/gradle/remap/Transformer.kt index 7fbec6f..6e8dae1 100644 --- a/src/main/kotlin/com/replaymod/gradle/remap/Transformer.kt +++ b/src/main/kotlin/com/replaymod/gradle/remap/Transformer.kt @@ -12,6 +12,7 @@ import org.jetbrains.kotlin.cli.jvm.compiler.EnvironmentConfigFiles import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment import org.jetbrains.kotlin.cli.jvm.config.JavaSourceRoot import org.jetbrains.kotlin.cli.jvm.config.JvmClasspathRoot +import org.jetbrains.kotlin.cli.jvm.modules.CoreJrtFileSystem import org.jetbrains.kotlin.com.intellij.codeInsight.CustomExceptionHandler import org.jetbrains.kotlin.com.intellij.mock.MockProject import org.jetbrains.kotlin.com.intellij.openapi.Disposable @@ -26,6 +27,7 @@ import org.jetbrains.kotlin.config.CommonConfigurationKeys import org.jetbrains.kotlin.config.CompilerConfiguration import org.jetbrains.kotlin.config.JVMConfigurationKeys import org.jetbrains.kotlin.psi.KtFile +import org.jetbrains.kotlin.utils.PathUtil import java.io.BufferedReader import java.io.File import java.io.IOException @@ -41,6 +43,8 @@ import kotlin.system.exitProcess class Transformer(private val map: MappingSet) { var classpath: Array? = null var remappedClasspath: Array? = null + var jdkHome: File? = null + var remappedJdkHome: File? = null var patternAnnotation: String? = null var manageImports = false @@ -67,6 +71,7 @@ class Transformer(private val map: MappingSet) { val config = CompilerConfiguration() config.put(CommonConfigurationKeys.MODULE_NAME, "main") + jdkHome?.let {config.setupJdk(it) } config.add(CLIConfigurationKeys.CONTENT_ROOTS, JavaSourceRoot(tmpDir.toFile(), "")) config.add(CLIConfigurationKeys.CONTENT_ROOTS, KotlinSourceRoot(tmpDir.toAbsolutePath().toString(), false)) config.addAll(CLIConfigurationKeys.CONTENT_ROOTS, classpath!!.map { JvmClasspathRoot(File(it)) }) @@ -152,8 +157,18 @@ class Transformer(private val map: MappingSet) { } } + private fun CompilerConfiguration.setupJdk(jdkHome: File) { + put(JVMConfigurationKeys.JDK_HOME, jdkHome) + + if (!CoreJrtFileSystem.isModularJdk(jdkHome)) { + val roots = PathUtil.getJdkClassesRoots(jdkHome).map { JvmClasspathRoot(it, true) } + addAll(CLIConfigurationKeys.CONTENT_ROOTS, 0, roots) + } + } + private fun setupRemappedProject(disposable: Disposable, classpath: Array, sourceRoot: Path): KotlinCoreEnvironment { val config = CompilerConfiguration() + (remappedJdkHome ?: jdkHome)?.let { config.setupJdk(it) } config.put(CommonConfigurationKeys.MODULE_NAME, "main") config.addAll(CLIConfigurationKeys.CONTENT_ROOTS, classpath.map { JvmClasspathRoot(File(it)) }) if (manageImports) { -- cgit