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 b071b1dd39a..14422e9788c 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/K2JVMCompiler.kt +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/K2JVMCompiler.kt @@ -94,17 +94,15 @@ class K2JVMCompiler : CLICompiler() { 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() { } 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 diff --git a/compiler/testData/javaModules/withBuildFile/build.xml b/compiler/testData/javaModules/withBuildFile/build.xml new file mode 100644 index 00000000000..feaa430f36d --- /dev/null +++ b/compiler/testData/javaModules/withBuildFile/build.xml @@ -0,0 +1,6 @@ + + + + + + diff --git a/compiler/testData/javaModules/withBuildFile/usage.txt b/compiler/testData/javaModules/withBuildFile/usage.txt new file mode 100644 index 00000000000..4b7bbf7ffa6 --- /dev/null +++ b/compiler/testData/javaModules/withBuildFile/usage.txt @@ -0,0 +1,2 @@ +warning: the '-d' option with a directory destination is ignored because '-Xbuild-file' is specified +OK diff --git a/compiler/testData/javaModules/withBuildFile/usage/module-info.java b/compiler/testData/javaModules/withBuildFile/usage/module-info.java new file mode 100644 index 00000000000..ecbcee60df4 --- /dev/null +++ b/compiler/testData/javaModules/withBuildFile/usage/module-info.java @@ -0,0 +1,3 @@ +module usage { + requires kotlin.stdlib; +} diff --git a/compiler/testData/javaModules/withBuildFile/usage/usage.kt b/compiler/testData/javaModules/withBuildFile/usage/usage.kt new file mode 100644 index 00000000000..200cb181bc5 --- /dev/null +++ b/compiler/testData/javaModules/withBuildFile/usage/usage.kt @@ -0,0 +1,3 @@ +class Test { + fun test() = KotlinVersion.CURRENT +} diff --git a/compiler/tests-common/tests/org/jetbrains/kotlin/cli/AbstractCliTest.java b/compiler/tests-common/tests/org/jetbrains/kotlin/cli/AbstractCliTest.java index ab5fc3e38b0..c78443f45b6 100644 --- a/compiler/tests-common/tests/org/jetbrains/kotlin/cli/AbstractCliTest.java +++ b/compiler/tests-common/tests/org/jetbrains/kotlin/cli/AbstractCliTest.java @@ -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( diff --git a/compiler/tests/org/jetbrains/kotlin/jvm/compiler/Java9ModulesIntegrationTest.kt b/compiler/tests/org/jetbrains/kotlin/jvm/compiler/Java9ModulesIntegrationTest.kt index dd8016fdea3..c71b0c9e693 100644 --- a/compiler/tests/org/jetbrains/kotlin/jvm/compiler/Java9ModulesIntegrationTest.kt +++ b/compiler/tests/org/jetbrains/kotlin/jvm/compiler/Java9ModulesIntegrationTest.kt @@ -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 = emptyList(), addModules: List = emptyList(), + additionalKotlinArguments: List = 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)) + } }