From 9dcd40d7b76a36b18ae9c7ef9bbd9dd86bed0267 Mon Sep 17 00:00:00 2001 From: Sebastian Sellmair Date: Fri, 31 Mar 2023 10:30:59 +0200 Subject: [PATCH] [CLI] K2JVMCompilerArguments: Model classpath as Array to allow interning individual file-path arguments on the IDE KTIJ-24976 --- .../arguments/K2JVMCompilerArgumentsCopyGenerated.kt | 2 +- .../cli/common/arguments/K2JVMCompilerArguments.kt | 5 +++-- .../kotlin/cli/jvm/compiler/cliCompilerUtils.kt | 2 +- .../src/org/jetbrains/kotlin/cli/jvm/jvmArguments.kt | 2 +- .../incremental/IncrementalFirJvmCompilerRunner.kt | 2 +- .../kotlin/incremental/IncrementalJvmCompilerRunner.kt | 10 ++++++---- .../AbstractIncrementalJvmCompilerRunnerTest.kt | 8 ++++---- .../org/jetbrains/kotlin/config/KotlinFacetSettings.kt | 2 +- .../org/jetbrains/kotlin/config/facetSerialization.kt | 4 ++-- .../test/CompilerArgumentsSerializationTest.kt | 2 +- .../jetbrains/kotlin/jps/build/KotlinJpsBuildTest.kt | 2 +- .../org/jetbrains/kotlin/maven/K2JVMCompileMojo.java | 8 ++++---- .../AbstractIncrementalFirJvmCompilerRunnerTest.kt | 2 +- .../org/jetbrains/kotlin/jvm/abi/BaseJvmAbiTest.kt | 2 +- 14 files changed, 28 insertions(+), 25 deletions(-) diff --git a/compiler/cli/cli-common/gen/org/jetbrains/kotlin/cli/common/arguments/K2JVMCompilerArgumentsCopyGenerated.kt b/compiler/cli/cli-common/gen/org/jetbrains/kotlin/cli/common/arguments/K2JVMCompilerArgumentsCopyGenerated.kt index 9fcb5308036..33ece639ca0 100644 --- a/compiler/cli/cli-common/gen/org/jetbrains/kotlin/cli/common/arguments/K2JVMCompilerArgumentsCopyGenerated.kt +++ b/compiler/cli/cli-common/gen/org/jetbrains/kotlin/cli/common/arguments/K2JVMCompilerArgumentsCopyGenerated.kt @@ -17,7 +17,7 @@ fun copyK2JVMCompilerArguments(from: K2JVMCompilerArguments, to: K2JVMCompilerAr to.assertionsMode = from.assertionsMode to.backendThreads = from.backendThreads to.buildFile = from.buildFile - to.classpath = from.classpath + to.classpath = from.classpath?.copyOf() to.compileJava = from.compileJava to.declarationsOutputPath = from.declarationsOutputPath to.defaultScriptExtension = from.defaultScriptExtension diff --git a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/K2JVMCompilerArguments.kt b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/K2JVMCompilerArguments.kt index 6eec916e764..fbcd3cb3bda 100644 --- a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/K2JVMCompilerArguments.kt +++ b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/K2JVMCompilerArguments.kt @@ -26,9 +26,10 @@ class K2JVMCompilerArguments : CommonCompilerArguments() { value = "-classpath", shortName = "-cp", valueDescription = "", - description = "List of directories and JAR/ZIP archives to search for user class files" + description = "List of directories and JAR/ZIP archives to search for user class files", + delimiter = Argument.Delimiters.pathSeparator ) - var classpath: String? = null + var classpath: Array? = null set(value) { checkFrozen() field = if (value.isNullOrEmpty()) null else value diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/cliCompilerUtils.kt b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/cliCompilerUtils.kt index bcdfa4924ca..c7de0dea25e 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/cliCompilerUtils.kt +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/cliCompilerUtils.kt @@ -174,7 +174,7 @@ fun writeOutputsIfNeeded( fun ModuleBuilder.configureFromArgs(args: K2JVMCompilerArguments) { args.friendPaths?.forEach { addFriendDir(it) } - args.classpath?.split(File.pathSeparator)?.forEach { addClasspathEntry(it) } + args.classpath?.forEach { addClasspathEntry(it) } args.javaSourceRoots?.forEach { addJavaSourceRoot(JavaRootPath(it, args.javaPackagePrefix)) } diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/jvmArguments.kt b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/jvmArguments.kt index 9f521787714..d316deff58c 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/jvmArguments.kt +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/jvmArguments.kt @@ -179,7 +179,7 @@ fun CompilerConfiguration.configureJavaModulesContentRoots(arguments: K2JVMCompi } fun CompilerConfiguration.configureContentRootsFromClassPath(arguments: K2JVMCompilerArguments) { - for (path in arguments.classpath?.split(File.pathSeparatorChar).orEmpty()) { + for (path in arguments.classpath.orEmpty()) { add(CLIConfigurationKeys.CONTENT_ROOTS, JvmClasspathRoot(File(path))) } } diff --git a/compiler/incremental-compilation-impl/src/org/jetbrains/kotlin/incremental/IncrementalFirJvmCompilerRunner.kt b/compiler/incremental-compilation-impl/src/org/jetbrains/kotlin/incremental/IncrementalFirJvmCompilerRunner.kt index c316105ce94..aa002fec30f 100644 --- a/compiler/incremental-compilation-impl/src/org/jetbrains/kotlin/incremental/IncrementalFirJvmCompilerRunner.kt +++ b/compiler/incremental-compilation-impl/src/org/jetbrains/kotlin/incremental/IncrementalFirJvmCompilerRunner.kt @@ -342,7 +342,7 @@ fun CompilerConfiguration.configureBaseRoots(args: K2JVMCompilerArguments) { } } - args.classpath?.split(File.pathSeparator)?.forEach { classpathRoot -> + args.classpath?.forEach { classpathRoot -> add( CLIConfigurationKeys.CONTENT_ROOTS, if (isJava9Module) JvmModulePathRoot(File(classpathRoot)) else JvmClasspathRoot(File(classpathRoot)) diff --git a/compiler/incremental-compilation-impl/src/org/jetbrains/kotlin/incremental/IncrementalJvmCompilerRunner.kt b/compiler/incremental-compilation-impl/src/org/jetbrains/kotlin/incremental/IncrementalJvmCompilerRunner.kt index d62b050e269..a04f7ee898f 100644 --- a/compiler/incremental-compilation-impl/src/org/jetbrains/kotlin/incremental/IncrementalJvmCompilerRunner.kt +++ b/compiler/incremental-compilation-impl/src/org/jetbrains/kotlin/incremental/IncrementalJvmCompilerRunner.kt @@ -174,7 +174,8 @@ open class IncrementalJvmCompilerRunner( private val psiFileFactory: PsiFileFactory by lazy { val rootDisposable = Disposer.newDisposable() val configuration = compilerConfiguration - val environment = KotlinCoreEnvironment.createForProduction(rootDisposable, configuration, EnvironmentConfigFiles.JVM_CONFIG_FILES) + val environment = + KotlinCoreEnvironment.createForProduction(rootDisposable, configuration, EnvironmentConfigFiles.JVM_CONFIG_FILES) val project = environment.project PsiFileFactory.getInstance(project) } @@ -281,7 +282,8 @@ open class IncrementalJvmCompilerRunner( if (!lastBuildInfoFile.exists()) { return CompilationMode.Rebuild(BuildAttribute.NO_LAST_BUILD_INFO) } - val lastBuildInfo = BuildInfo.read(lastBuildInfoFile, messageCollector) ?: return CompilationMode.Rebuild(BuildAttribute.INVALID_LAST_BUILD_INFO) + val lastBuildInfo = BuildInfo.read(lastBuildInfoFile, messageCollector) + ?: return CompilationMode.Rebuild(BuildAttribute.INVALID_LAST_BUILD_INFO) reporter.debug { "Last Kotlin Build info -- $lastBuildInfo" } val scopes = caches.lookupCache.lookupSymbols.map { it.scope.ifBlank { it.name } }.distinct() @@ -548,7 +550,7 @@ var K2JVMCompilerArguments.destinationAsFile: File } var K2JVMCompilerArguments.classpathAsList: List - get() = classpath.orEmpty().split(File.pathSeparator).map(::File) + get() = classpath.orEmpty().map(::File) set(value) { - classpath = value.joinToString(separator = File.pathSeparator, transform = { it.path }) + classpath = value.map { it.path }.toTypedArray() } diff --git a/compiler/incremental-compilation-impl/test/org/jetbrains/kotlin/incremental/AbstractIncrementalJvmCompilerRunnerTest.kt b/compiler/incremental-compilation-impl/test/org/jetbrains/kotlin/incremental/AbstractIncrementalJvmCompilerRunnerTest.kt index b93cda46c1f..4f3f02314da 100644 --- a/compiler/incremental-compilation-impl/test/org/jetbrains/kotlin/incremental/AbstractIncrementalJvmCompilerRunnerTest.kt +++ b/compiler/incremental-compilation-impl/test/org/jetbrains/kotlin/incremental/AbstractIncrementalJvmCompilerRunnerTest.kt @@ -48,14 +48,14 @@ abstract class AbstractIncrementalJvmCompilerRunnerTest : AbstractIncrementalCom } if (javaSources.isEmpty()) return TestCompilationResult(ExitCode.OK, emptyList(), emptyList()) - val javaClasspath = compileClasspath + File.pathSeparator + kotlinClassesPath + val javaClasspath = compileClasspath + kotlinClassesPath val javaDestinationDir = File(workingDir, "java-classes").apply { if (exists()) { deleteRecursively() } mkdirs() } - val args = arrayOf("-cp", javaClasspath, + val args = arrayOf("-cp", javaClasspath.joinToString(File.pathSeparator), "-d", javaDestinationDir.canonicalPath, *javaSources.map { it.canonicalPath }.toTypedArray() ) @@ -73,12 +73,12 @@ abstract class AbstractIncrementalJvmCompilerRunnerTest : AbstractIncrementalCom K2JVMCompilerArguments().apply { moduleName = testDir.name destination = destinationDir.path - classpath = compileClasspath + classpath = compileClasspath.toTypedArray() } private val compileClasspath = listOf( kotlinStdlibJvm, KtTestUtil.getAnnotationsJar() - ).joinToString(File.pathSeparator) { it.canonicalPath } + ).map { it.canonicalPath } } diff --git a/jps/jps-common/src/org/jetbrains/kotlin/config/KotlinFacetSettings.kt b/jps/jps-common/src/org/jetbrains/kotlin/config/KotlinFacetSettings.kt index 8fa5d30940d..e2ec4cbc1b8 100644 --- a/jps/jps-common/src/org/jetbrains/kotlin/config/KotlinFacetSettings.kt +++ b/jps/jps-common/src/org/jetbrains/kotlin/config/KotlinFacetSettings.kt @@ -181,7 +181,7 @@ class KotlinFacetSettings { if (compilerSettings != null) { parseCommandLineArguments(compilerSettings.additionalArgumentsAsList, this) } - if (this is K2JVMCompilerArguments) this.classpath = "" + if (this is K2JVMCompilerArguments) this.classpath = null } } else null } diff --git a/jps/jps-common/src/org/jetbrains/kotlin/config/facetSerialization.kt b/jps/jps-common/src/org/jetbrains/kotlin/config/facetSerialization.kt index 434a11680e3..0cf68c50015 100644 --- a/jps/jps-common/src/org/jetbrains/kotlin/config/facetSerialization.kt +++ b/jps/jps-common/src/org/jetbrains/kotlin/config/facetSerialization.kt @@ -230,7 +230,7 @@ fun CommonCompilerArguments.convertPathsToSystemIndependent() { when (this) { is K2JVMCompilerArguments -> { destination = destination?.let(FileUtilRt::toSystemIndependentName) - classpath = classpath?.let(FileUtilRt::toSystemIndependentName) + classpath?.forEachIndexed { index, s -> classpath!![index] = FileUtilRt.toSystemIndependentName(s) } jdkHome = jdkHome?.let(FileUtilRt::toSystemIndependentName) kotlinHome = kotlinHome?.let(FileUtilRt::toSystemIndependentName) friendPaths?.forEachIndexed { index, s -> friendPaths!![index] = FileUtilRt.toSystemIndependentName(s) } @@ -334,7 +334,7 @@ private fun KotlinFacetSettings.writeConfig(element: Element) { element.addContent( Element("externalSystemTestTasks").apply { externalSystemRunTasks.forEach { task -> - when(task) { + when (task) { is ExternalSystemTestRunTask -> { addContent( Element("externalSystemTestTask").apply { addContent(task.toStringRepresentation()) } diff --git a/jps/jps-common/test/CompilerArgumentsSerializationTest.kt b/jps/jps-common/test/CompilerArgumentsSerializationTest.kt index 135e2657f2b..7a88a5034ce 100644 --- a/jps/jps-common/test/CompilerArgumentsSerializationTest.kt +++ b/jps/jps-common/test/CompilerArgumentsSerializationTest.kt @@ -34,7 +34,7 @@ class CompilerArgumentsSerializationTest { @Test fun testLongClasspathArgumentJVM() { doSerializeDeserializeAndCompareTest { - classpath = generateSequence { generateRandomString(50) }.take(10).toList().joinToString(File.pathSeparator) + classpath = generateSequence { generateRandomString(50) }.take(10).toList().toTypedArray() } } diff --git a/jps/jps-plugin/jps-tests/test/org/jetbrains/kotlin/jps/build/KotlinJpsBuildTest.kt b/jps/jps-plugin/jps-tests/test/org/jetbrains/kotlin/jps/build/KotlinJpsBuildTest.kt index 5402b2fa92b..2d26715b33e 100644 --- a/jps/jps-plugin/jps-tests/test/org/jetbrains/kotlin/jps/build/KotlinJpsBuildTest.kt +++ b/jps/jps-plugin/jps-tests/test/org/jetbrains/kotlin/jps/build/KotlinJpsBuildTest.kt @@ -958,7 +958,7 @@ open class KotlinJpsBuildTest : KotlinJpsBuildTestBase() { Files.copy(libraryJar.toPath(), module1Lib.toPath(), StandardCopyOption.REPLACE_EXISTING) assert(module1Lib.exists()) - (facet.compilerArguments as K2JVMCompilerArguments).classpath = module1Lib.path + (facet.compilerArguments as K2JVMCompilerArguments).classpath = arrayOf(module1Lib.path) it.container.setChild( JpsKotlinFacetModuleExtension.KIND, 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 80d6abdb5f6..7b16aee52a1 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 @@ -171,7 +171,7 @@ public class K2JVMCompileMojo extends KotlinCompileMojoBase filteredClasspath = new ArrayList<>(); - for (String path : classpath.split(File.pathSeparator)) { + for (String path : classpath) { if (!classesDir.equals(new File(path))) { filteredClasspath.add(path); } } - arguments.setClasspath(StringUtil.join(filteredClasspath, File.pathSeparator)); + arguments.setClasspath(filteredClasspath.toArray(new String[0])); } IncrementalJvmCompilerRunnerKt.makeIncrementally(cachesDir, sourceRoots, arguments, messageCollector, icReporter); diff --git a/plugins/fir-plugin-prototype/fir-plugin-ic-test/tests/org/jetbrains/kotlin/incremental/AbstractIncrementalFirJvmCompilerRunnerTest.kt b/plugins/fir-plugin-prototype/fir-plugin-ic-test/tests/org/jetbrains/kotlin/incremental/AbstractIncrementalFirJvmCompilerRunnerTest.kt index 20b91f74c95..160a4f1e720 100644 --- a/plugins/fir-plugin-prototype/fir-plugin-ic-test/tests/org/jetbrains/kotlin/incremental/AbstractIncrementalFirJvmCompilerRunnerTest.kt +++ b/plugins/fir-plugin-prototype/fir-plugin-ic-test/tests/org/jetbrains/kotlin/incremental/AbstractIncrementalFirJvmCompilerRunnerTest.kt @@ -35,7 +35,7 @@ abstract class AbstractIncrementalFirJvmWithPluginCompilerRunnerTest : AbstractI val annotationsJar = findJar(ANNOTATIONS_JAR_DIR, ANNOTATIONS_JAR_NAME, ":plugins:fir-plugin-prototype:plugin-annotations:jar") val pluginJar = findJar(PLUGIN_JAR_DIR, PLUGIN_JAR_NAME, ":plugins:fir-plugin-prototype:jar") - classpath += "${File.pathSeparator}$annotationsJar" + classpath = classpath?.plus(annotationsJar) pluginClasspaths = arrayOf(pluginJar) } diff --git a/plugins/jvm-abi-gen/test/org/jetbrains/kotlin/jvm/abi/BaseJvmAbiTest.kt b/plugins/jvm-abi-gen/test/org/jetbrains/kotlin/jvm/abi/BaseJvmAbiTest.kt index f25f4750b34..59b7ca0b942 100644 --- a/plugins/jvm-abi-gen/test/org/jetbrains/kotlin/jvm/abi/BaseJvmAbiTest.kt +++ b/plugins/jvm-abi-gen/test/org/jetbrains/kotlin/jvm/abi/BaseJvmAbiTest.kt @@ -74,7 +74,7 @@ abstract class BaseJvmAbiTest : TestCase() { val compiler = K2JVMCompiler() val args = compiler.createArguments().apply { freeArgs = listOf(compilation.srcDir.canonicalPath) - classpath = (abiDependencies + kotlinJvmStdlib).joinToString(File.pathSeparator) { it.canonicalPath } + classpath = (abiDependencies + kotlinJvmStdlib).map { it.canonicalPath }.toTypedArray() pluginClasspaths = arrayOf(abiPluginJar.canonicalPath) pluginOptions = listOfNotNull( abiOption(JvmAbiCommandLineProcessor.OUTPUT_PATH_OPTION.optionName, compilation.abiDir.canonicalPath),