Skip to content

Commit

Permalink
Explicitly set the JVM target
Browse files Browse the repository at this point in the history
  • Loading branch information
sheu committed Feb 7, 2023
1 parent 2d732ef commit d6bc1ca
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
13 changes: 9 additions & 4 deletions common/src/main/kotlin/component/KotlinEnvironment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
import org.jetbrains.kotlin.cli.jvm.config.addJvmClasspathRoots
import org.jetbrains.kotlin.cli.jvm.config.configureJdkClasspathRoots
import org.jetbrains.kotlin.cli.jvm.configureAdvancedJvmOptions
import org.jetbrains.kotlin.cli.jvm.setupJvmSpecificArguments
import org.jetbrains.kotlin.config.CommonConfigurationKeys
import org.jetbrains.kotlin.config.CompilerConfiguration
import org.jetbrains.kotlin.config.JVMConfigurationKeys
import org.jetbrains.kotlin.config.JvmTarget
import org.jetbrains.kotlin.config.languageVersionSettings
import org.jetbrains.kotlin.ir.backend.js.jsPhases
import org.jetbrains.kotlin.js.config.JSConfigurationKeys
Expand All @@ -28,7 +30,8 @@ import java.io.File

class KotlinEnvironment(
val classpath: List<File>,
additionalJsClasspath: List<File>
additionalJsClasspath: List<File>,
additionalCompilerFlags: List<String> = emptyList()
) {
companion object {
/**
Expand All @@ -46,7 +49,7 @@ class KotlinEnvironment(
"-opt-in=kotlin.contracts.ExperimentalContracts",
"-opt-in=kotlin.experimental.ExperimentalTypeInference",
"-Xcontext-receivers",
"-XXLanguage:+RangeUntilOperator"
"-XXLanguage:+RangeUntilOperator",
)
}

Expand All @@ -59,6 +62,7 @@ class KotlinEnvironment(
}

val JS_LIBRARIES = additionalJsClasspath.map { it.absolutePath }
val additionalCompilerOptions = additionalCompilerFlags + additionalCompilerArguments

@Synchronized
fun <T> environment(f: (KotlinCoreEnvironment) -> T): T {
Expand Down Expand Up @@ -96,7 +100,7 @@ class KotlinEnvironment(

private fun createConfiguration(): CompilerConfiguration {
val arguments = K2JVMCompilerArguments()
parseCommandLineArguments(additionalCompilerArguments, arguments)
parseCommandLineArguments(additionalCompilerOptions, arguments)
return CompilerConfiguration().apply {
addJvmClasspathRoots(classpath.filter { it.exists() && it.isFile && it.extension == "jar" })
val messageCollector = MessageCollector.NONE
Expand All @@ -109,9 +113,10 @@ class KotlinEnvironment(

// it uses languageVersionSettings that was set above
configureAdvancedJvmOptions(arguments)
setupJvmSpecificArguments(arguments)
put(JVMConfigurationKeys.DO_NOT_CLEAR_BINDING_CONTEXT, true)

configureJdkClasspathRoots()
}
}
}
}
20 changes: 20 additions & 0 deletions src/test/kotlin/com/compiler/server/JvmRunnerTest.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,29 @@
package com.compiler.server

import com.compiler.server.base.BaseExecutorTest
import component.KotlinEnvironment
import org.jetbrains.kotlin.config.JVMConfigurationKeys
import org.jetbrains.kotlin.config.JvmTarget
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test

class JvmRunnerTest : BaseExecutorTest() {

@Test
fun `jvm test setting java 11`() {
val kv = KotlinEnvironment(emptyList(), emptyList(), listOf("-jvm-target=11"))
kv.environment {
Assertions.assertEquals(JvmTarget.JVM_11, it.configuration.get(JVMConfigurationKeys.JVM_TARGET), "JVM target should be java 11")
}
}

@Test
fun `jvm test default`() {
val kv = KotlinEnvironment(emptyList(), emptyList(), emptyList())
kv.environment {
Assertions.assertEquals(null, it.configuration.get(JVMConfigurationKeys.JVM_TARGET), "JVM target is not set")
}
}
@Test
fun `base execute test JVM`() {
run(
Expand All @@ -13,6 +32,7 @@ class JvmRunnerTest : BaseExecutorTest() {
)
}


@Test
fun `no main class jvm test`() {
run(
Expand Down

0 comments on commit d6bc1ca

Please sign in to comment.