diff --git a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/K2JVMCompilerArguments.java b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/K2JVMCompilerArguments.java index 33f5968b5f7..442a8ba7df1 100644 --- a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/K2JVMCompilerArguments.java +++ b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/K2JVMCompilerArguments.java @@ -31,9 +31,9 @@ public class K2JVMCompilerArguments extends CommonCompilerArguments { @Argument(value = "include-runtime", description = "Include Kotlin runtime in to resulting .jar") public boolean includeRuntime; - @Argument(value = "jdk", description = "Path to JDK home to include into classpath, if differs from default %JAVA_HOME%") + @Argument(value = "jdk-home", description = "Path to JDK home directory to include into classpath, if differs from default JAVA_HOME") @ValueDescription("") - public String jdk; + public String jdkHome; @Argument(value = "no-jdk", description = "Don't include Java runtime into classpath") public boolean noJdk; diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/K2JVMCompiler.kt b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/K2JVMCompiler.kt index d4e5bf4283a..fb87133abaf 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/K2JVMCompiler.kt +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/K2JVMCompiler.kt @@ -60,26 +60,8 @@ class K2JVMCompiler : CLICompiler() { messageCollector.report(CompilerMessageSeverity.LOGGING, "Using Kotlin home directory " + paths.homePath, CompilerMessageLocation.NO_LOCATION) PerformanceCounter.setTimeCounterEnabled(arguments.reportPerf) - try { - if (!arguments.noJdk) { - if (arguments.jdk != null) { - configuration.addJvmClasspathRoots(PathUtil.getJdkClassesRoots(File(arguments.jdk))) - } - else { - configuration.addJvmClasspathRoots(PathUtil.getJdkClassesRoots()) - } - } - else { - if (arguments.jdk != null) { - messageCollector.report(CompilerMessageSeverity.WARNING, - "The '-jdk' option with a path to JDK is ignored because '-no-jdk' is specified", - CompilerMessageLocation.NO_LOCATION) - } - } - } - catch (t: Throwable) { - MessageCollectorUtil.reportException(messageCollector, t) - return INTERNAL_ERROR + setupJdkClasspathRoots(arguments, configuration, messageCollector).let { + if (it != OK) return it } try { @@ -325,6 +307,41 @@ class K2JVMCompiler : CLICompiler() { } return classpath } + + private fun setupJdkClasspathRoots(arguments: K2JVMCompilerArguments, configuration: CompilerConfiguration, messageCollector: MessageCollector): ExitCode { + try { + if (!arguments.noJdk) { + if (arguments.jdkHome != null) { + messageCollector.report(CompilerMessageSeverity.LOGGING, + "Using JDK home directory ${arguments.jdkHome}", + CompilerMessageLocation.NO_LOCATION) + val classesRoots = PathUtil.getJdkClassesRoots(File(arguments.jdkHome)) + if (classesRoots.isEmpty()) { + messageCollector.report(CompilerMessageSeverity.ERROR, + "No class roots are found in the JDK path: ${arguments.jdkHome}", + CompilerMessageLocation.NO_LOCATION) + return COMPILATION_ERROR + } + configuration.addJvmClasspathRoots(classesRoots) + } + else { + configuration.addJvmClasspathRoots(PathUtil.getJdkClassesRoots()) + } + } + else { + if (arguments.jdkHome != null) { + messageCollector.report(CompilerMessageSeverity.WARNING, + "The '-jdk-home' option is ignored because '-no-jdk' is specified", + CompilerMessageLocation.NO_LOCATION) + } + } + } + catch (t: Throwable) { + MessageCollectorUtil.reportException(messageCollector, t) + return INTERNAL_ERROR + } + return OK + } } } diff --git a/compiler/testData/cli/jvm/help.out b/compiler/testData/cli/jvm/help.out index b802088cd82..d691eea5a4a 100644 --- a/compiler/testData/cli/jvm/help.out +++ b/compiler/testData/cli/jvm/help.out @@ -3,7 +3,7 @@ where possible options include: -d Destination for generated class files -classpath (-cp) Paths where to find user class files -include-runtime Include Kotlin runtime in to resulting .jar - -jdk Path to JDK home to include into classpath, if differs from default %JAVA_HOME% + -jdk-home Path to JDK home directory to include into classpath, if differs from default JAVA_HOME -no-jdk Don't include Java runtime into classpath -no-stdlib Don't include Kotlin runtime into classpath -module Path to the module file to compile diff --git a/compiler/testData/cli/jvm/warningJdkWithNoJdk.args b/compiler/testData/cli/jvm/warningJdkWithNoJdk.args index 19536fe9476..54f1ae1ac2d 100644 --- a/compiler/testData/cli/jvm/warningJdkWithNoJdk.args +++ b/compiler/testData/cli/jvm/warningJdkWithNoJdk.args @@ -1,6 +1,6 @@ $TESTDATA_DIR$/simple.kt -d $TEMP_DIR$ --jdk +-jdk-home $TESTDATA_DIR$ -no-jdk diff --git a/compiler/testData/cli/jvm/warningJdkWithNoJdk.out b/compiler/testData/cli/jvm/warningJdkWithNoJdk.out index 1958ae1c5cb..4bd430c5677 100644 --- a/compiler/testData/cli/jvm/warningJdkWithNoJdk.out +++ b/compiler/testData/cli/jvm/warningJdkWithNoJdk.out @@ -1,2 +1,2 @@ -warning: the '-jdk' option with a path to JDK is ignored because '-no-jdk' is specified +warning: the '-jdk-home' option is ignored because '-no-jdk' is specified OK diff --git a/compiler/testData/cli/jvm/wrongArgument.out b/compiler/testData/cli/jvm/wrongArgument.out index 4ba88fde362..da2021e5faf 100644 --- a/compiler/testData/cli/jvm/wrongArgument.out +++ b/compiler/testData/cli/jvm/wrongArgument.out @@ -4,7 +4,7 @@ where possible options include: -d Destination for generated class files -classpath (-cp) Paths where to find user class files -include-runtime Include Kotlin runtime in to resulting .jar - -jdk Path to JDK home to include into classpath, if differs from default %JAVA_HOME% + -jdk-home Path to JDK home directory to include into classpath, if differs from default JAVA_HOME -no-jdk Don't include Java runtime into classpath -no-stdlib Don't include Kotlin runtime into classpath -module Path to the module file to compile diff --git a/compiler/testData/cli/jvm/wrongJdkPath.args b/compiler/testData/cli/jvm/wrongJdkPath.args new file mode 100644 index 00000000000..897676dc468 --- /dev/null +++ b/compiler/testData/cli/jvm/wrongJdkPath.args @@ -0,0 +1,5 @@ +$TESTDATA_DIR$/simple.kt +-d +$TEMP_DIR$ +-jdk-home +$TESTDATA_DIR$ diff --git a/compiler/testData/cli/jvm/wrongJdkPath.out b/compiler/testData/cli/jvm/wrongJdkPath.out new file mode 100644 index 00000000000..00cbb19e373 --- /dev/null +++ b/compiler/testData/cli/jvm/wrongJdkPath.out @@ -0,0 +1,2 @@ +error: no class roots are found in the JDK path: compiler/testData/cli/jvm +COMPILATION_ERROR diff --git a/compiler/tests/org/jetbrains/kotlin/cli/CliTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/cli/CliTestGenerated.java index ba199bc2156..55dfc8f2f84 100644 --- a/compiler/tests/org/jetbrains/kotlin/cli/CliTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/cli/CliTestGenerated.java @@ -295,6 +295,12 @@ public class CliTestGenerated extends AbstractCliTest { doJvmTest(fileName); } + @TestMetadata("wrongJdkPath.args") + public void testWrongJdkPath() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/cli/jvm/wrongJdkPath.args"); + doJvmTest(fileName); + } + @TestMetadata("wrongJvmTargetVersion.args") public void testWrongJvmTargetVersion() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/cli/jvm/wrongJvmTargetVersion.args"); diff --git a/libraries/tools/kotlin-gradle-plugin-core/src/main/kotlin/org/jetbrains/kotlin/gradle/tasks/Tasks.kt b/libraries/tools/kotlin-gradle-plugin-core/src/main/kotlin/org/jetbrains/kotlin/gradle/tasks/Tasks.kt index bc135dc3ddc..01f467a0c95 100644 --- a/libraries/tools/kotlin-gradle-plugin-core/src/main/kotlin/org/jetbrains/kotlin/gradle/tasks/Tasks.kt +++ b/libraries/tools/kotlin-gradle-plugin-core/src/main/kotlin/org/jetbrains/kotlin/gradle/tasks/Tasks.kt @@ -195,7 +195,7 @@ open class KotlinCompile() : AbstractKotlinCompile() { logger.kotlinDebug("args.pluginOptions = ${args.pluginOptions.joinToString(File.pathSeparator)}") args.noStdlib = true - args.jdk = kotlinOptions.jdk + args.jdkHome = kotlinOptions.jdkHome args.noJdk = kotlinOptions.noJdk args.noInline = kotlinOptions.noInline args.noOptimize = kotlinOptions.noOptimize diff --git a/libraries/tools/kotlin-gradle-plugin/src/test/resources/testProject/customJdk/build.gradle b/libraries/tools/kotlin-gradle-plugin/src/test/resources/testProject/customJdk/build.gradle index 689f054d6fc..d29afa03005 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/test/resources/testProject/customJdk/build.gradle +++ b/libraries/tools/kotlin-gradle-plugin/src/test/resources/testProject/customJdk/build.gradle @@ -32,7 +32,7 @@ dependencies { } compileKotlin { - kotlinOptions.jdk = System.getenv("JDK_17") + kotlinOptions.jdkHome = System.getenv("JDK_17") } task wrapper(type: Wrapper) { diff --git a/libraries/tools/kotlin-maven-plugin-test/src/it/test-customJdk/pom.xml b/libraries/tools/kotlin-maven-plugin-test/src/it/test-customJdk/pom.xml index 01dd2d363dc..aacc9e40031 100644 --- a/libraries/tools/kotlin-maven-plugin-test/src/it/test-customJdk/pom.xml +++ b/libraries/tools/kotlin-maven-plugin-test/src/it/test-customJdk/pom.xml @@ -22,7 +22,7 @@ - ${env.JDK_18} + ${env.JDK_17} diff --git a/libraries/tools/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/K2JVMCompileMojo.java b/libraries/tools/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/K2JVMCompileMojo.java index 76a9c7e0efd..094d65ab144 100644 --- a/libraries/tools/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/K2JVMCompileMojo.java +++ b/libraries/tools/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/K2JVMCompileMojo.java @@ -62,8 +62,8 @@ public class K2JVMCompileMojo extends KotlinCompileMojoBase