From bdae0b5efb03b7f31b2236e35611dd6e9ad47d85 Mon Sep 17 00:00:00 2001 From: Alexey Tsvetkov Date: Fri, 13 Jan 2017 00:00:17 +0300 Subject: [PATCH] Fix the build --- .../cli/common/arguments/argumentUtils.kt | 8 ++--- .../kotlin/daemon/common/CompileService.kt | 2 +- .../kotlin/daemon/CompileServiceImpl.kt | 30 ++++++++++------- .../org/jetbrains/kotlin/utils/jvmUtils.kt | 27 +++++++++++++++ jps-plugin/jps-plugin.iml | 1 + .../compilerRunner/JpsKotlinCompilerRunner.kt | 33 +++++++++++++------ .../GradleKotlinCompilerRunner.kt | 6 ++-- 7 files changed, 78 insertions(+), 29 deletions(-) create mode 100644 compiler/util/src/org/jetbrains/kotlin/utils/jvmUtils.kt diff --git a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/argumentUtils.kt b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/argumentUtils.kt index a0202bc33d5..4e14e89e91b 100644 --- a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/argumentUtils.kt +++ b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/argumentUtils.kt @@ -23,10 +23,10 @@ import java.lang.reflect.Modifier import java.util.* fun parseArguments(args: Array, arguments: A) { - val unparsedArgs = Args.parse(arguments, args, false).partition { it.startsWith("-X") } - - arguments.unknownExtraFlags = unparsedArgs.first - arguments.freeArgs = unparsedArgs.second + val unparsedArgs = Args.parse(arguments, args, false) + val (unknownExtraArgs, unknownArgs) = unparsedArgs.partition { it.startsWith("-X") } + arguments.unknownExtraFlags = unknownExtraArgs + arguments.freeArgs = unknownArgs for (argument in arguments.freeArgs) { if (argument.startsWith("-")) { diff --git a/compiler/daemon/daemon-common/src/org/jetbrains/kotlin/daemon/common/CompileService.kt b/compiler/daemon/daemon-common/src/org/jetbrains/kotlin/daemon/common/CompileService.kt index 2cff4f0fb07..e972a2d4082 100644 --- a/compiler/daemon/daemon-common/src/org/jetbrains/kotlin/daemon/common/CompileService.kt +++ b/compiler/daemon/daemon-common/src/org/jetbrains/kotlin/daemon/common/CompileService.kt @@ -132,7 +132,7 @@ interface CompileService : Remote { @Throws(RemoteException::class) fun compile( sessionId: Int, - compilerArguments: CommonCompilerArguments, + compilerArguments: Array, compilationOptions: CompilationOptions, servicesFacade: CompilerServicesFacadeBase, compilationResultsSink: CompilationResultsSink? diff --git a/compiler/daemon/src/org/jetbrains/kotlin/daemon/CompileServiceImpl.kt b/compiler/daemon/src/org/jetbrains/kotlin/daemon/CompileServiceImpl.kt index 6e067f94f51..7014c585ff6 100644 --- a/compiler/daemon/src/org/jetbrains/kotlin/daemon/CompileServiceImpl.kt +++ b/compiler/daemon/src/org/jetbrains/kotlin/daemon/CompileServiceImpl.kt @@ -23,10 +23,7 @@ import com.intellij.openapi.vfs.impl.jar.CoreJarFileSystem import org.jetbrains.kotlin.cli.common.CLICompiler import org.jetbrains.kotlin.cli.common.ExitCode import org.jetbrains.kotlin.cli.common.KOTLIN_COMPILER_ENVIRONMENT_KEEPALIVE_PROPERTY -import org.jetbrains.kotlin.cli.common.arguments.CommonCompilerArguments -import org.jetbrains.kotlin.cli.common.arguments.K2JSCompilerArguments -import org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments -import org.jetbrains.kotlin.cli.common.arguments.K2MetadataCompilerArguments +import org.jetbrains.kotlin.cli.common.arguments.* import org.jetbrains.kotlin.cli.common.messages.* import org.jetbrains.kotlin.cli.common.modules.ModuleXmlParser import org.jetbrains.kotlin.cli.common.repl.ReplCheckResult @@ -46,10 +43,8 @@ import org.jetbrains.kotlin.incremental.* import org.jetbrains.kotlin.load.kotlin.incremental.components.IncrementalCompilationComponents import org.jetbrains.kotlin.progress.CompilationCanceledStatus import org.jetbrains.kotlin.utils.addToStdlib.check -import java.io.BufferedOutputStream -import java.io.ByteArrayOutputStream -import java.io.File -import java.io.PrintStream +import org.jetbrains.kotlin.utils.stackTraceStr +import java.io.* import java.rmi.NoSuchObjectException import java.rmi.registry.Registry import java.rmi.server.UnicastRemoteObject @@ -321,7 +316,7 @@ class CompileServiceImpl( override fun compile( sessionId: Int, - compilerArguments: CommonCompilerArguments, + compilerArguments: Array, compilationOptions: CompilationOptions, servicesFacade: CompilerServicesFacadeBase, compilationResultsSink: CompilationResultsSink? @@ -330,6 +325,17 @@ class CompileServiceImpl( val daemonReporter = DaemonMessageReporter(servicesFacade, compilationOptions) val compilerMode = compilationOptions.compilerMode val targetPlatform = compilationOptions.targetPlatform + val k2PlatformArgs = try { + when (targetPlatform) { + CompileService.TargetPlatform.JVM -> K2JVMCompilerArguments().apply { K2JVMCompiler().parseArguments(compilerArguments, this) } + CompileService.TargetPlatform.JS -> K2JSCompilerArguments().apply { K2JSCompiler().parseArguments(compilerArguments, this) } + CompileService.TargetPlatform.METADATA -> K2MetadataCompilerArguments().apply { K2MetadataCompiler().parseArguments(compilerArguments, this) } + } + } + catch (e: IllegalArgumentException) { + messageCollector.report(CompilerMessageSeverity.EXCEPTION, e.stackTraceStr, CompilerMessageLocation.NO_LOCATION) + return CompileService.CallResult.Error("Could not deserialize compiler arguments") + } return when (compilerMode) { CompilerMode.JPS_COMPILER -> { @@ -337,12 +343,12 @@ class CompileServiceImpl( doCompile(sessionId, daemonReporter, tracer = null) { eventManger, profiler -> val services = createCompileServices(jpsServicesFacade, eventManger, profiler) - execCompiler(compilationOptions.targetPlatform, services, compilerArguments, messageCollector) + execCompiler(compilationOptions.targetPlatform, services, k2PlatformArgs, messageCollector) } } CompilerMode.NON_INCREMENTAL_COMPILER -> { doCompile(sessionId, daemonReporter, tracer = null) { eventManger, profiler -> - execCompiler(targetPlatform, Services.EMPTY, compilerArguments, messageCollector) + execCompiler(targetPlatform, Services.EMPTY, k2PlatformArgs, messageCollector) } } CompilerMode.INCREMENTAL_COMPILER -> { @@ -350,7 +356,7 @@ class CompileServiceImpl( throw IllegalStateException("Incremental compilation is not supported for target platform: $targetPlatform") } - val k2jvmArgs = compilerArguments as K2JVMCompilerArguments + val k2jvmArgs = k2PlatformArgs as K2JVMCompilerArguments val gradleIncrementalArgs = compilationOptions as IncrementalCompilationOptions val gradleIncrementalServicesFacade = servicesFacade as IncrementalCompilerServicesFacade diff --git a/compiler/util/src/org/jetbrains/kotlin/utils/jvmUtils.kt b/compiler/util/src/org/jetbrains/kotlin/utils/jvmUtils.kt new file mode 100644 index 00000000000..6756f321502 --- /dev/null +++ b/compiler/util/src/org/jetbrains/kotlin/utils/jvmUtils.kt @@ -0,0 +1,27 @@ +/* + * Copyright 2010-2017 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.kotlin.utils + +import java.io.PrintWriter +import java.io.StringWriter + +val Exception.stackTraceStr: String + get() { + val sw = StringWriter() + PrintWriter(sw).use { printStackTrace(it) } + return sw.toString() + } \ No newline at end of file diff --git a/jps-plugin/jps-plugin.iml b/jps-plugin/jps-plugin.iml index 59e6dad560b..f5fcf5cec86 100644 --- a/jps-plugin/jps-plugin.iml +++ b/jps-plugin/jps-plugin.iml @@ -21,5 +21,6 @@ + \ No newline at end of file diff --git a/jps-plugin/src/org/jetbrains/kotlin/compilerRunner/JpsKotlinCompilerRunner.kt b/jps-plugin/src/org/jetbrains/kotlin/compilerRunner/JpsKotlinCompilerRunner.kt index 3d207d68a12..df4af961c7c 100644 --- a/jps-plugin/src/org/jetbrains/kotlin/compilerRunner/JpsKotlinCompilerRunner.kt +++ b/jps-plugin/src/org/jetbrains/kotlin/compilerRunner/JpsKotlinCompilerRunner.kt @@ -33,7 +33,20 @@ import java.rmi.server.UnicastRemoteObject import java.util.* class JpsKotlinCompilerRunner : KotlinCompilerRunner() { - override val log: KotlinLogger = JpsKotlinLogger(KotlinBuilder.LOG) + override val log: KotlinLogger = JpsKotlinLogger(KotlinBuilder.LOG) + + private var compilerSettings: CompilerSettings? = null + + private inline fun withCompilerSettings(settings: CompilerSettings, fn: ()->Unit) { + val old = compilerSettings + try { + compilerSettings = settings + fn() + } + finally { + compilerSettings = old + } + } companion object { private @Volatile var jpsDaemonConnection: DaemonConnection? = null @@ -48,10 +61,9 @@ class JpsKotlinCompilerRunner : KotlinCompilerRunner() { ) { val arguments = mergeBeans(commonArguments, k2jvmArguments) setupK2JvmArguments(moduleFile, arguments) - val additionalArguments = compilerSettings.additionalArguments.split(" ").toTypedArray() - parseArguments(additionalArguments, arguments) - - runCompiler(K2JVM_COMPILER, arguments, environment) + withCompilerSettings(compilerSettings) { + runCompiler(K2JVM_COMPILER, arguments, environment) + } } fun runK2JsCompiler( @@ -65,10 +77,9 @@ class JpsKotlinCompilerRunner : KotlinCompilerRunner() { ) { val arguments = mergeBeans(commonArguments, k2jsArguments) setupK2JsArguments(outputFile, sourceFiles, libraryFiles, arguments) - val additionalArguments = compilerSettings.additionalArguments.split(" ").toTypedArray() - parseArguments(additionalArguments, arguments) - - runCompiler(K2JS_COMPILER, arguments, environment) + withCompilerSettings(compilerSettings) { + runCompiler(K2JS_COMPILER, arguments, environment) + } } override fun compileWithDaemonOrFallback( @@ -104,7 +115,9 @@ class JpsKotlinCompilerRunner : KotlinCompilerRunner() { val compilerMode = CompilerMode.JPS_COMPILER val verbose = compilerArgs.verbose val options = CompilationOptions(compilerMode, targetPlatform, reportCategories(verbose), reportSeverity(verbose), requestedCompilationResults = emptyArray()) - daemon.compile(sessionId, compilerArgs, options, JpsCompilerServicesFacadeImpl(environment), null) + val allArgs = ArgumentUtils.convertArgumentsToStringList(compilerArgs) + + (compilerSettings?.additionalArguments?.split(" ") ?: emptyList()) + daemon.compile(sessionId, allArgs.toTypedArray(), options, JpsCompilerServicesFacadeImpl(environment), null) } return res?.get()?.let { exitCodeFromProcessExitCode(it) } diff --git a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/compilerRunner/GradleKotlinCompilerRunner.kt b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/compilerRunner/GradleKotlinCompilerRunner.kt index 556b5a58d30..553781a3994 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/compilerRunner/GradleKotlinCompilerRunner.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/compilerRunner/GradleKotlinCompilerRunner.kt @@ -172,7 +172,8 @@ internal class GradleCompilerRunner(private val project: Project) : KotlinCompil val servicesFacade = GradleCompilerServicesFacadeImpl(project, environment.messageCollector) val res = withDaemon(environment, retryOnConnectionError = true) { daemon, sessionId -> - daemon.compile(sessionId, environment.compilerArgs, compilationOptions, servicesFacade, compilationResultsSink = null) + val argsArray = ArgumentUtils.convertArgumentsToStringList(environment.compilerArgs).toTypedArray() + daemon.compile(sessionId, argsArray, compilationOptions, servicesFacade, compilationResultsSink = null) } val exitCode = res?.get()?.let { exitCodeFromProcessExitCode(it) } @@ -203,7 +204,8 @@ internal class GradleCompilerRunner(private val project: Project) : KotlinCompil val servicesFacade = GradleIncrementalCompilerServicesFacadeImpl(project, environment) val res = withDaemon(environment, retryOnConnectionError = true) { daemon, sessionId -> - daemon.compile(sessionId, environment.compilerArgs, compilationOptions, servicesFacade, GradleCompilationResultsSink(project)) + val argsArray = ArgumentUtils.convertArgumentsToStringList(environment.compilerArgs).toTypedArray() + daemon.compile(sessionId, argsArray, compilationOptions, servicesFacade, GradleCompilationResultsSink(project)) } val exitCode = res?.get()?.let { exitCodeFromProcessExitCode(it) }