From 1efdfbe6dbe4f682b77875ca6a087980e2315e3f Mon Sep 17 00:00:00 2001 From: Ilya Chernikov Date: Wed, 23 Sep 2015 15:07:48 +0200 Subject: [PATCH] Refactoring parts related to passing services to compiler in daemon, adding uniform support for cancellation check and lookup tracking Original commit: 4b1601974f4f2c185cf5dc17d7668471a1085868 --- .../compilerRunner/KotlinCompilerRunner.kt | 55 +++++++++---------- .../kotlin/jps/build/KotlinBuilder.kt | 10 +--- 2 files changed, 28 insertions(+), 37 deletions(-) diff --git a/jps/jps-plugin/src/org/jetbrains/kotlin/compilerRunner/KotlinCompilerRunner.kt b/jps/jps-plugin/src/org/jetbrains/kotlin/compilerRunner/KotlinCompilerRunner.kt index 4ebfec35d54..0dd541b1b9a 100644 --- a/jps/jps-plugin/src/org/jetbrains/kotlin/compilerRunner/KotlinCompilerRunner.kt +++ b/jps/jps-plugin/src/org/jetbrains/kotlin/compilerRunner/KotlinCompilerRunner.kt @@ -27,25 +27,23 @@ import org.jetbrains.kotlin.cli.common.arguments.K2JSCompilerArguments import org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments import org.jetbrains.kotlin.cli.common.messages.CompilerMessageLocation import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity +import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity.ERROR +import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity.INFO import org.jetbrains.kotlin.cli.common.messages.MessageCollector import org.jetbrains.kotlin.cli.common.messages.MessageCollectorUtil import org.jetbrains.kotlin.config.CompilerSettings -import org.jetbrains.kotlin.load.kotlin.incremental.components.IncrementalCache -import org.jetbrains.kotlin.modules.TargetId -import org.jetbrains.kotlin.rmi.* -import org.jetbrains.kotlin.rmi.kotlinr.DaemonReportCategory -import org.jetbrains.kotlin.rmi.kotlinr.DaemonReportMessage -import org.jetbrains.kotlin.rmi.kotlinr.DaemonReportingTargets -import org.jetbrains.kotlin.rmi.kotlinr.KotlinCompilerClient -import org.jetbrains.kotlin.utils.* - +import org.jetbrains.kotlin.load.kotlin.incremental.components.IncrementalCompilationComponents +import org.jetbrains.kotlin.progress.CompilationCanceledStatus +import org.jetbrains.kotlin.rmi.CompilerId +import org.jetbrains.kotlin.rmi.configureDaemonJVMOptions +import org.jetbrains.kotlin.rmi.configureDaemonOptions +import org.jetbrains.kotlin.rmi.isDaemonEnabled +import org.jetbrains.kotlin.rmi.kotlinr.* +import org.jetbrains.kotlin.utils.rethrow import java.io.* import java.lang.reflect.Field import java.lang.reflect.Modifier -import java.util.ArrayList - -import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity.ERROR -import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity.INFO +import java.util.* public object KotlinCompilerRunner { private val K2JVM_COMPILER = "org.jetbrains.kotlin.cli.jvm.K2JVMCompiler" @@ -58,14 +56,12 @@ public object KotlinCompilerRunner { compilerSettings: CompilerSettings, messageCollector: MessageCollector, environment: CompilerEnvironment, - incrementalCaches: Map?, moduleFile: File, collector: OutputItemsCollector) { val arguments = mergeBeans(commonArguments, k2jvmArguments) setupK2JvmArguments(moduleFile, arguments) - runCompiler(K2JVM_COMPILER, arguments, compilerSettings.additionalArguments, messageCollector, collector, environment, - incrementalCaches) + runCompiler(K2JVM_COMPILER, arguments, compilerSettings.additionalArguments, messageCollector, collector, environment) } public fun runK2JsCompiler( @@ -74,7 +70,6 @@ public object KotlinCompilerRunner { compilerSettings: CompilerSettings, messageCollector: MessageCollector, environment: CompilerEnvironment, - incrementalCaches: Map?, collector: OutputItemsCollector, sourceFiles: Collection, libraryFiles: List, @@ -82,8 +77,7 @@ public object KotlinCompilerRunner { val arguments = mergeBeans(commonArguments, k2jsArguments) setupK2JsArguments(outputFile, sourceFiles, libraryFiles, arguments) - runCompiler(K2JS_COMPILER, arguments, compilerSettings.additionalArguments, messageCollector, collector, environment, - incrementalCaches) + runCompiler(K2JS_COMPILER, arguments, compilerSettings.additionalArguments, messageCollector, collector, environment) } private fun processCompilerOutput( @@ -109,8 +103,7 @@ public object KotlinCompilerRunner { additionalArguments: String, messageCollector: MessageCollector, collector: OutputItemsCollector, - environment: CompilerEnvironment, - incrementalCaches: Map?) { + environment: CompilerEnvironment) { try { messageCollector.report(INFO, "Using kotlin-home = " + environment.kotlinPaths.homePath, CompilerMessageLocation.NO_LOCATION) @@ -119,7 +112,7 @@ public object KotlinCompilerRunner { val argsArray = argumentsList.toTypedArray() - if (!tryCompileWithDaemon(messageCollector, collector, environment, incrementalCaches, argsArray)) { + if (!tryCompileWithDaemon(messageCollector, collector, environment, argsArray)) { // otherwise fallback to in-process val stream = ByteArrayOutputStream() @@ -139,14 +132,12 @@ public object KotlinCompilerRunner { } - private fun tryCompileWithDaemon( - messageCollector: MessageCollector, - collector: OutputItemsCollector, - environment: CompilerEnvironment, - incrementalCaches: Map?, - argsArray: Array): Boolean { + private fun tryCompileWithDaemon(messageCollector: MessageCollector, + collector: OutputItemsCollector, + environment: CompilerEnvironment, + argsArray: Array): Boolean { - if (incrementalCaches != null && isDaemonEnabled()) { + if (isDaemonEnabled()) { val libPath = CompilerRunnerUtil.getLibPath(environment.kotlinPaths, messageCollector) // TODO: it may be a good idea to cache the compilerId, since making it means calculating digest over jar(s) and if \\ // the lifetime of JPS process is small anyway, we can neglect the probability of changed compiler @@ -173,7 +164,11 @@ public object KotlinCompilerRunner { val compilerOut = ByteArrayOutputStream() val daemonOut = ByteArrayOutputStream() - val res = KotlinCompilerClient.incrementalCompile(daemon, argsArray, incrementalCaches, compilerOut, daemonOut) + val services = CompilationServices( + incrementalCompilationComponents = environment.services.get(IncrementalCompilationComponents::class.java), + compilationCanceledStatus = environment.services.get(CompilationCanceledStatus::class.java)) + + val res = KotlinCompilerClient.incrementalCompile(daemon, argsArray, services, compilerOut, daemonOut) processCompilerOutput(messageCollector, collector, compilerOut, res.toString()) BufferedReader(StringReader(daemonOut.toString())).forEachLine { diff --git a/jps/jps-plugin/src/org/jetbrains/kotlin/jps/build/KotlinBuilder.kt b/jps/jps-plugin/src/org/jetbrains/kotlin/jps/build/KotlinBuilder.kt index 81a96f10aa0..1c028a21d29 100644 --- a/jps/jps-plugin/src/org/jetbrains/kotlin/jps/build/KotlinBuilder.kt +++ b/jps/jps-plugin/src/org/jetbrains/kotlin/jps/build/KotlinBuilder.kt @@ -318,10 +318,7 @@ public class KotlinBuilder : ModuleLevelBuilder(BuilderCategory.SOURCE_PROCESSOR ) } - return compileToJvm(allCompiledFiles, chunk, commonArguments, context, dirtyFilesHolder, environment, - incrementalCaches.mapKeysTo(HashMap(incrementalCaches.size()), - { TargetId(it.getKey()) }), - filesToCompile, messageCollector) + return compileToJvm(allCompiledFiles, chunk, commonArguments, context, dirtyFilesHolder, environment, filesToCompile, messageCollector) } private fun createCompileEnvironment( @@ -519,7 +516,7 @@ public class KotlinBuilder : ModuleLevelBuilder(BuilderCategory.SOURCE_PROCESSOR val compilerSettings = JpsKotlinCompilerSettings.getCompilerSettings(project) val k2JsArguments = JpsKotlinCompilerSettings.getK2JsCompilerArguments(project) - KotlinCompilerRunner.runK2JsCompiler(commonArguments, k2JsArguments, compilerSettings, messageCollector, environment, incrementalCaches, outputItemCollector, sourceFiles, libraryFiles, outputFile) + KotlinCompilerRunner.runK2JsCompiler(commonArguments, k2JsArguments, compilerSettings, messageCollector, environment, outputItemCollector, sourceFiles, libraryFiles, outputFile) return outputItemCollector } @@ -542,7 +539,6 @@ public class KotlinBuilder : ModuleLevelBuilder(BuilderCategory.SOURCE_PROCESSOR context: CompileContext, dirtyFilesHolder: DirtyFilesHolder, environment: CompilerEnvironment, - incrementalCaches: MutableMap?, filesToCompile: MultiMap, messageCollector: MessageCollectorAdapter ): OutputItemsCollectorImpl? { val outputItemCollector = OutputItemsCollectorImpl() @@ -586,7 +582,7 @@ public class KotlinBuilder : ModuleLevelBuilder(BuilderCategory.SOURCE_PROCESSOR + (if (totalRemovedFiles == 0) "" else " ($totalRemovedFiles removed files)") + " in " + filesToCompile.keySet().map { it.getPresentableName() }.join()) - KotlinCompilerRunner.runK2JvmCompiler(commonArguments, k2JvmArguments, compilerSettings, messageCollector, environment, incrementalCaches, moduleFile, outputItemCollector) + KotlinCompilerRunner.runK2JvmCompiler(commonArguments, k2JvmArguments, compilerSettings, messageCollector, environment, moduleFile, outputItemCollector) moduleFile.delete() return outputItemCollector