Rename -jdk compiler option to -jdk-home (jdkHome), add jdk path validation.

This commit is contained in:
Ilya Gorbunov
2016-06-23 16:33:25 +03:00
parent 13016cae93
commit 7c5c4610e8
13 changed files with 64 additions and 34 deletions
@@ -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("<path>")
public String jdk;
public String jdkHome;
@Argument(value = "no-jdk", description = "Don't include Java runtime into classpath")
public boolean noJdk;
@@ -60,26 +60,8 @@ class K2JVMCompiler : CLICompiler<K2JVMCompilerArguments>() {
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<K2JVMCompilerArguments>() {
}
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
}
}
}
+1 -1
View File
@@ -3,7 +3,7 @@ where possible options include:
-d <directory|jar> Destination for generated class files
-classpath (-cp) <path> Paths where to find user class files
-include-runtime Include Kotlin runtime in to resulting .jar
-jdk <path> Path to JDK home to include into classpath, if differs from default %JAVA_HOME%
-jdk-home <path> 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> Path to the module file to compile
+1 -1
View File
@@ -1,6 +1,6 @@
$TESTDATA_DIR$/simple.kt
-d
$TEMP_DIR$
-jdk
-jdk-home
$TESTDATA_DIR$
-no-jdk
+1 -1
View File
@@ -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
+1 -1
View File
@@ -4,7 +4,7 @@ where possible options include:
-d <directory|jar> Destination for generated class files
-classpath (-cp) <path> Paths where to find user class files
-include-runtime Include Kotlin runtime in to resulting .jar
-jdk <path> Path to JDK home to include into classpath, if differs from default %JAVA_HOME%
-jdk-home <path> 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> Path to the module file to compile
+5
View File
@@ -0,0 +1,5 @@
$TESTDATA_DIR$/simple.kt
-d
$TEMP_DIR$
-jdk-home
$TESTDATA_DIR$
+2
View File
@@ -0,0 +1,2 @@
error: no class roots are found in the JDK path: compiler/testData/cli/jvm
COMPILATION_ERROR
@@ -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");
@@ -195,7 +195,7 @@ open class KotlinCompile() : AbstractKotlinCompile<K2JVMCompilerArguments>() {
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
@@ -32,7 +32,7 @@ dependencies {
}
compileKotlin {
kotlinOptions.jdk = System.getenv("JDK_17")
kotlinOptions.jdkHome = System.getenv("JDK_17")
}
task wrapper(type: Wrapper) {
@@ -22,7 +22,7 @@
</dependencies>
<properties>
<kotlin.compiler.jdk>${env.JDK_18}</kotlin.compiler.jdk>
<kotlin.compiler.jdkHome>${env.JDK_17}</kotlin.compiler.jdkHome>
</properties>
<build>
@@ -62,8 +62,8 @@ public class K2JVMCompileMojo extends KotlinCompileMojoBase<K2JVMCompilerArgumen
@Parameter(property = "kotlin.compiler.jvmTarget", required = false, readonly = false)
protected String jvmTarget;
@Parameter(property = "kotlin.compiler.jdk", required = false, readonly = false)
protected String jdk;
@Parameter(property = "kotlin.compiler.jdkHome", required = false, readonly = false)
protected String jdkHome;
@NotNull
@Override
@@ -110,9 +110,9 @@ public class K2JVMCompileMojo extends KotlinCompileMojoBase<K2JVMCompilerArgumen
arguments.languageVersion = languageVersion;
arguments.jvmTarget = jvmTarget;
if (jdk != null) {
getLog().info("Overriding JDK path with: " + jdk);
arguments.jdk = jdk;
if (jdkHome != null) {
getLog().info("Overriding JDK home path with: " + jdkHome);
arguments.jdkHome = jdkHome;
}
}
}