Configure Java module path when compiler is invoked in -Xbuild-file mode
#KT-27626 Fixed
This commit is contained in:
@@ -94,17 +94,15 @@ class K2JVMCompiler : CLICompiler<K2JVMCompilerArguments>() {
|
||||
|
||||
configuration.put(CommonConfigurationKeys.MODULE_NAME, arguments.moduleName ?: JvmAbi.DEFAULT_MODULE_NAME)
|
||||
|
||||
if (arguments.buildFile == null) {
|
||||
configureContentRoots(paths, arguments, configuration)
|
||||
configureContentRoots(paths, arguments, configuration)
|
||||
|
||||
if (arguments.freeArgs.isEmpty() && !arguments.version) {
|
||||
if (arguments.script) {
|
||||
messageCollector.report(ERROR, "Specify script source path to evaluate")
|
||||
return COMPILATION_ERROR
|
||||
}
|
||||
ReplFromTerminal.run(rootDisposable, configuration)
|
||||
return ExitCode.OK
|
||||
if (arguments.buildFile == null && arguments.freeArgs.isEmpty() && !arguments.version) {
|
||||
if (arguments.script) {
|
||||
messageCollector.report(ERROR, "Specify script source path to evaluate")
|
||||
return COMPILATION_ERROR
|
||||
}
|
||||
ReplFromTerminal.run(rootDisposable, configuration)
|
||||
return ExitCode.OK
|
||||
}
|
||||
|
||||
if (arguments.includeRuntime) {
|
||||
@@ -411,15 +409,20 @@ class K2JVMCompiler : CLICompiler<K2JVMCompilerArguments>() {
|
||||
}
|
||||
|
||||
private fun configureContentRoots(paths: KotlinPaths?, arguments: K2JVMCompilerArguments, configuration: CompilerConfiguration) {
|
||||
for (modularRoot in arguments.javaModulePath?.split(File.pathSeparatorChar).orEmpty()) {
|
||||
configuration.add(CLIConfigurationKeys.CONTENT_ROOTS, JvmModulePathRoot(File(modularRoot)))
|
||||
}
|
||||
|
||||
if (arguments.buildFile != null) {
|
||||
// In the .xml compilation mode, all content roots except module path will be loaded from the .xml build file.
|
||||
return
|
||||
}
|
||||
|
||||
val messageCollector = configuration.getNotNull(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY)
|
||||
for (path in arguments.classpath?.split(File.pathSeparatorChar).orEmpty()) {
|
||||
configuration.add(CLIConfigurationKeys.CONTENT_ROOTS, JvmClasspathRoot(File(path)))
|
||||
}
|
||||
|
||||
for (modularRoot in arguments.javaModulePath?.split(File.pathSeparatorChar).orEmpty()) {
|
||||
configuration.add(CLIConfigurationKeys.CONTENT_ROOTS, JvmModulePathRoot(File(modularRoot)))
|
||||
}
|
||||
|
||||
val isModularJava = configuration.get(JVMConfigurationKeys.JDK_HOME).let { it != null && CoreJrtFileSystem.isModularJdk(it) }
|
||||
fun addRoot(moduleName: String, libraryName: String, getLibrary: (KotlinPaths) -> File, noLibraryArgument: String) {
|
||||
val file = getLibraryFromHome(paths, getLibrary, libraryName, messageCollector, noLibraryArgument) ?: return
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
<modules>
|
||||
<module name="app" type="java-production" outputDir="$TEMP_DIR$/app">
|
||||
<sources path="$TESTDATA_DIR$/usage/usage.kt"/>
|
||||
<javaSourceRoots path="$TESTDATA_DIR$/usage"/>
|
||||
</module>
|
||||
</modules>
|
||||
@@ -0,0 +1,2 @@
|
||||
warning: the '-d' option with a directory destination is ignored because '-Xbuild-file' is specified
|
||||
OK
|
||||
@@ -0,0 +1,3 @@
|
||||
module usage {
|
||||
requires kotlin.stdlib;
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
class Test {
|
||||
fun test() = KotlinVersion.CURRENT
|
||||
}
|
||||
@@ -204,7 +204,7 @@ public abstract class AbstractCliTest extends TestCaseWithTmpdir {
|
||||
String argWithTestPathsReplaced = replaceTestPaths(argWithColonsReplaced, testDataDir, tempDir);
|
||||
|
||||
if (arg.startsWith(BUILD_FILE_ARGUMENT_PREFIX)) {
|
||||
return createTempFileWithPathsReplaced(argWithTestPathsReplaced, BUILD_FILE_ARGUMENT_PREFIX, ".xml", testDataDir, tempDir);
|
||||
return replacePathsInBuildXml(argWithTestPathsReplaced, testDataDir, tempDir);
|
||||
}
|
||||
|
||||
if (arg.startsWith(ARGFILE_ARGUMENT)) {
|
||||
@@ -214,6 +214,11 @@ public abstract class AbstractCliTest extends TestCaseWithTmpdir {
|
||||
return argWithTestPathsReplaced;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static String replacePathsInBuildXml(@NotNull String argument, @NotNull String testDataDir, @NotNull String tempDir) {
|
||||
return createTempFileWithPathsReplaced(argument, BUILD_FILE_ARGUMENT_PREFIX, ".xml", testDataDir, tempDir);
|
||||
}
|
||||
|
||||
// Create new temporary file with all test paths replaced and return the new argument value with the new file path
|
||||
@NotNull
|
||||
private static String createTempFileWithPathsReplaced(
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
package org.jetbrains.kotlin.jvm.compiler
|
||||
|
||||
import com.intellij.openapi.util.io.FileUtil
|
||||
import org.jetbrains.kotlin.cli.AbstractCliTest
|
||||
import org.jetbrains.kotlin.codegen.forTestCompile.ForTestCompileRuntime
|
||||
import org.jetbrains.kotlin.test.KotlinTestUtils
|
||||
import java.io.File
|
||||
@@ -30,6 +31,7 @@ class Java9ModulesIntegrationTest : AbstractKotlinCompilerIntegrationTest() {
|
||||
name: String,
|
||||
modulePath: List<File> = emptyList(),
|
||||
addModules: List<String> = emptyList(),
|
||||
additionalKotlinArguments: List<String> = emptyList(),
|
||||
manifest: Manifest? = null
|
||||
): File {
|
||||
val paths = (modulePath + ForTestCompileRuntime.runtimeJarForTests()).joinToString(separator = File.pathSeparator) { it.path }
|
||||
@@ -42,6 +44,7 @@ class Java9ModulesIntegrationTest : AbstractKotlinCompilerIntegrationTest() {
|
||||
if (addModules.isNotEmpty()) {
|
||||
kotlinOptions += "-Xadd-modules=${addModules.joinToString()}"
|
||||
}
|
||||
kotlinOptions += additionalKotlinArguments
|
||||
|
||||
return compileLibrary(
|
||||
name,
|
||||
@@ -237,4 +240,15 @@ class Java9ModulesIntegrationTest : AbstractKotlinCompilerIntegrationTest() {
|
||||
fun testDependencyOnReflect() {
|
||||
module("usage", listOf(ForTestCompileRuntime.reflectJarForTests()))
|
||||
}
|
||||
|
||||
fun testWithBuildFile() {
|
||||
// This test checks that module path is configured correctly when the compiler is invoked in the '-Xbuild-file' mode. Note that
|
||||
// the "'-d' option is ignored" warning in this test is an artifact of the test infrastructure and is not a part of the test.
|
||||
val buildFile = AbstractCliTest.replacePathsInBuildXml(
|
||||
"-Xbuild-file=${File(testDataDirectory, "build.xml").path}",
|
||||
testDataDirectory.absolutePath,
|
||||
tmpdir.absolutePath
|
||||
)
|
||||
module("usage", additionalKotlinArguments = listOf("-no-stdlib", buildFile))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user