diff --git a/compiler/rmi/kotlinr/kotlinr.iml b/compiler/rmi/kotlinr/kotlinr.iml index 240b6471a57..36eff60cdf2 100644 --- a/compiler/rmi/kotlinr/kotlinr.iml +++ b/compiler/rmi/kotlinr/kotlinr.iml @@ -9,5 +9,6 @@ + \ No newline at end of file diff --git a/compiler/rmi/kotlinr/src/KotlinCompilerClient.kt b/compiler/rmi/kotlinr/src/KotlinCompilerClient.kt index e37a030ec69..e8be605c362 100644 --- a/compiler/rmi/kotlinr/src/KotlinCompilerClient.kt +++ b/compiler/rmi/kotlinr/src/KotlinCompilerClient.kt @@ -16,14 +16,17 @@ package org.jetbrains.kotlin.rmi.kotlinr +import org.jetbrains.kotlin.load.kotlin.incremental.components.IncrementalCache import org.jetbrains.kotlin.rmi.CompilerFacade +import java.io.OutputStream import java.rmi.registry.LocateRegistry import kotlin.platform.platformStatic public class KotlinCompilerClient { companion object { - platformStatic public fun main(vararg args: String) { + + public fun connectToCompilerServer(): CompilerFacade? { val compilerObj = LocateRegistry.getRegistry("localhost", 17031).lookup("KotlinJvmCompilerService") if (compilerObj == null) println("Unable to find compiler service") @@ -31,19 +34,39 @@ public class KotlinCompilerClient { val compiler = compilerObj as? CompilerFacade if (compiler == null) println("Unable to cast compiler service: ${compilerObj.javaClass}") - else { + return compiler + } + return null + } + + public fun incrementalCompile(compiler: CompilerFacade, args: Array, caches: Map, out: OutputStream): Int { + + val outStrm = RemoteOutputStreamServer(out) + val cacheServers = hashMapOf() + try { + caches.forEach { cacheServers.put( it.getKey(), RemoteIncrementalCacheServer( it.getValue())) } + val res = compiler.remoteIncrementalCompile(args, cacheServers, outStrm, CompilerFacade.OutputFormat.XML) + return res + } + finally { + cacheServers.forEach { it.getValue().disconnect() } + outStrm.disconnect() + } + } + + platformStatic public fun main(vararg args: String) { + connectToCompilerServer()?.let { println("Executing daemon compilation with args: " + args.joinToString(" ")) val outStrm = RemoteOutputStreamServer(System.out) try { - val res = compiler.remoteCompile(args, outStrm, CompilerFacade.OutputFormat.PLAIN) + val res = it.remoteCompile(args, outStrm, CompilerFacade.OutputFormat.PLAIN) println("Compilation result code: $res") } finally { - outStrm.close() + outStrm.disconnect() } } } - } } } diff --git a/compiler/rmi/kotlinr/src/RemoteIncrementalCacheServer.kt b/compiler/rmi/kotlinr/src/RemoteIncrementalCacheServer.kt new file mode 100644 index 00000000000..a924ad64f40 --- /dev/null +++ b/compiler/rmi/kotlinr/src/RemoteIncrementalCacheServer.kt @@ -0,0 +1,41 @@ +/* + * Copyright 2010-2015 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.rmi.kotlinr + +import org.jetbrains.kotlin.rmi.CompilerFacade +import org.jetbrains.kotlin.load.kotlin.incremental.components.IncrementalCache; +import java.rmi.server.UnicastRemoteObject + + +public class RemoteIncrementalCacheServer(val cache: IncrementalCache) : CompilerFacade.RemoteIncrementalCache { + + init { + UnicastRemoteObject.exportObject(this, 0) + } + + override fun getObsoletePackageParts(): Collection = cache.getObsoletePackageParts() + + override fun getPackageData(fqName: String): ByteArray? = cache.getPackageData(fqName) + + override fun close() { + cache.close() + } + + public fun disconnect() { + UnicastRemoteObject.unexportObject(this, true) + } +} \ No newline at end of file diff --git a/compiler/rmi/kotlinr/src/RemoteOutputStreamServer.kt b/compiler/rmi/kotlinr/src/RemoteOutputStreamServer.kt index 9e3ed540291..7a96d0dc927 100644 --- a/compiler/rmi/kotlinr/src/RemoteOutputStreamServer.kt +++ b/compiler/rmi/kotlinr/src/RemoteOutputStreamServer.kt @@ -27,9 +27,12 @@ class RemoteOutputStreamServer(val out: OutputStream) : RemoteOutputStream { UnicastRemoteObject.exportObject(this, 0) } + public fun disconnect() { + UnicastRemoteObject.unexportObject(this, true) + } + override fun close() { out.close() - UnicastRemoteObject.unexportObject(this, true) } override fun write(data: ByteArray, start: Int, length: Int) { diff --git a/compiler/rmi/rmi-interface/rmi-interface.iml b/compiler/rmi/rmi-interface/rmi-interface.iml index 0bd974ce23a..50c2d073f1a 100644 --- a/compiler/rmi/rmi-interface/rmi-interface.iml +++ b/compiler/rmi/rmi-interface/rmi-interface.iml @@ -7,6 +7,7 @@ + diff --git a/jps-plugin/jps-plugin.iml b/jps-plugin/jps-plugin.iml index bae91cce456..e092109dfa7 100644 --- a/jps-plugin/jps-plugin.iml +++ b/jps-plugin/jps-plugin.iml @@ -21,5 +21,7 @@ + + \ No newline at end of file diff --git a/jps-plugin/src/org/jetbrains/kotlin/compilerRunner/KotlinCompilerRunner.java b/jps-plugin/src/org/jetbrains/kotlin/compilerRunner/KotlinCompilerRunner.java index 887f515087a..5da5119d20a 100644 --- a/jps-plugin/src/org/jetbrains/kotlin/compilerRunner/KotlinCompilerRunner.java +++ b/jps-plugin/src/org/jetbrains/kotlin/compilerRunner/KotlinCompilerRunner.java @@ -30,6 +30,9 @@ import org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments; 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.rmi.CompilerFacade; +import org.jetbrains.kotlin.rmi.kotlinr.KotlinCompilerClient; import org.jetbrains.kotlin.utils.UtilsPackage; import java.io.*; @@ -38,6 +41,7 @@ import java.lang.reflect.Modifier; import java.util.ArrayList; import java.util.Collection; import java.util.List; +import java.util.Map; import static org.jetbrains.kotlin.cli.common.messages.CompilerMessageLocation.NO_LOCATION; import static org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity.ERROR; @@ -54,13 +58,14 @@ public class KotlinCompilerRunner { CompilerSettings compilerSettings, MessageCollector messageCollector, CompilerEnvironment environment, - File moduleFile, + Map incrementalCaches, File moduleFile, OutputItemsCollector collector ) { K2JVMCompilerArguments arguments = mergeBeans(commonArguments, k2jvmArguments); setupK2JvmArguments(moduleFile, arguments); - runCompiler(K2JVM_COMPILER, arguments, compilerSettings.getAdditionalArguments(), messageCollector, collector, environment); + runCompiler(K2JVM_COMPILER, arguments, compilerSettings.getAdditionalArguments(), messageCollector, collector, environment, + incrementalCaches); } public static void runK2JsCompiler( @@ -69,7 +74,7 @@ public class KotlinCompilerRunner { @NotNull CompilerSettings compilerSettings, @NotNull MessageCollector messageCollector, @NotNull CompilerEnvironment environment, - @NotNull OutputItemsCollector collector, + Map incrementalCaches, @NotNull OutputItemsCollector collector, @NotNull Collection sourceFiles, @NotNull List libraryFiles, @NotNull File outputFile @@ -77,7 +82,8 @@ public class KotlinCompilerRunner { K2JSCompilerArguments arguments = mergeBeans(commonArguments, k2jsArguments); setupK2JsArguments(outputFile, sourceFiles, libraryFiles, arguments); - runCompiler(K2JS_COMPILER, arguments, compilerSettings.getAdditionalArguments(), messageCollector, collector, environment); + runCompiler(K2JS_COMPILER, arguments, compilerSettings.getAdditionalArguments(), messageCollector, collector, environment, + incrementalCaches); } private static void runCompiler( @@ -86,12 +92,13 @@ public class KotlinCompilerRunner { String additionalArguments, MessageCollector messageCollector, OutputItemsCollector collector, - CompilerEnvironment environment + CompilerEnvironment environment, + Map incrementalCaches ) { ByteArrayOutputStream stream = new ByteArrayOutputStream(); PrintStream out = new PrintStream(stream); - String exitCode = execCompiler(compilerClassName, arguments, additionalArguments, environment, out, messageCollector); + String exitCode = execCompiler(compilerClassName, arguments, additionalArguments, environment, incrementalCaches, out, messageCollector); BufferedReader reader = new BufferedReader(new StringReader(stream.toString())); CompilerOutputParser.parseCompilerMessagesFromReader(messageCollector, reader, collector); @@ -107,6 +114,7 @@ public class KotlinCompilerRunner { CommonCompilerArguments arguments, String additionalArguments, CompilerEnvironment environment, + Map incrementalCaches, PrintStream out, MessageCollector messageCollector ) { @@ -116,13 +124,27 @@ public class KotlinCompilerRunner { List argumentsList = ArgumentUtils.convertArgumentsToStringList(arguments); argumentsList.addAll(StringUtil.split(additionalArguments, " ")); + String[] argsArray = ArrayUtil.toStringArray(argumentsList); + + // trying the daemon first + if (incrementalCaches != null) { + CompilerFacade daemon = KotlinCompilerClient.Companion.connectToCompilerServer(); + if (daemon != null) { + Integer res = KotlinCompilerClient.Companion.incrementalCompile(daemon, argsArray, incrementalCaches, out); + return res.toString(); + } + } + + // otherwise fallback to in-process + Object rc = CompilerRunnerUtil.invokeExecMethod( - compilerClassName, ArrayUtil.toStringArray(argumentsList), environment, messageCollector, out + compilerClassName, argsArray, environment, messageCollector, out ); // exec() returns an ExitCode object, class of which is loaded with a different class loader, // so we take it's contents through reflection return getReturnCodeFromObject(rc); + } catch (Throwable e) { MessageCollectorUtil.reportException(messageCollector, e); diff --git a/jps-plugin/src/org/jetbrains/kotlin/jps/build/KotlinBuilder.kt b/jps-plugin/src/org/jetbrains/kotlin/jps/build/KotlinBuilder.kt index 4606b72da80..e207b2e7200 100644 --- a/jps-plugin/src/org/jetbrains/kotlin/jps/build/KotlinBuilder.kt +++ b/jps-plugin/src/org/jetbrains/kotlin/jps/build/KotlinBuilder.kt @@ -247,9 +247,10 @@ public class KotlinBuilder : ModuleLevelBuilder(BuilderCategory.SOURCE_PROCESSOR filesToCompile: MultiMap, incrementalCaches: Map, messageCollector: MessageCollectorAdapter, project: JpsProject ): OutputItemsCollectorImpl? { + if (JpsUtils.isJsKotlinModule(chunk.representativeTarget())) { LOG.debug("Compiling to JS ${filesToCompile.values().size()} files in " + filesToCompile.keySet().map { it.getPresentableName() }.join()) - return compileToJs(chunk, commonArguments, environment, messageCollector, project) + return compileToJs(chunk, commonArguments, environment, null, messageCollector, project) } if (IncrementalCompilation.ENABLED) { @@ -279,7 +280,9 @@ public class KotlinBuilder : ModuleLevelBuilder(BuilderCategory.SOURCE_PROCESSOR ) } - return compileToJvm(allCompiledFiles, chunk, commonArguments, context, dirtyFilesHolder, environment, filesToCompile, messageCollector) + return compileToJvm(allCompiledFiles, chunk, commonArguments, context, dirtyFilesHolder, environment, + incrementalCaches.mapKeysTo(HashMap(incrementalCaches.size()), { it.getKey().id }), + filesToCompile, messageCollector) } private fun createCompileEnvironment( @@ -436,8 +439,8 @@ public class KotlinBuilder : ModuleLevelBuilder(BuilderCategory.SOURCE_PROCESSOR private fun compileToJs(chunk: ModuleChunk, commonArguments: CommonCompilerArguments, environment: CompilerEnvironment, - messageCollector: KotlinBuilder.MessageCollectorAdapter, - project: JpsProject + incrementalCaches: MutableMap?, + messageCollector: MessageCollectorAdapter, project: JpsProject ): OutputItemsCollectorImpl? { val outputItemCollector = OutputItemsCollectorImpl() @@ -468,7 +471,7 @@ public class KotlinBuilder : ModuleLevelBuilder(BuilderCategory.SOURCE_PROCESSOR val compilerSettings = JpsKotlinCompilerSettings.getCompilerSettings(project) val k2JsArguments = JpsKotlinCompilerSettings.getK2JsCompilerArguments(project) - runK2JsCompiler(commonArguments, k2JsArguments, compilerSettings, messageCollector, environment, outputItemCollector, sourceFiles, libraryFiles, outputFile) + runK2JsCompiler(commonArguments, k2JsArguments, compilerSettings, messageCollector, environment, incrementalCaches, outputItemCollector, sourceFiles, libraryFiles, outputFile) return outputItemCollector } @@ -491,8 +494,8 @@ public class KotlinBuilder : ModuleLevelBuilder(BuilderCategory.SOURCE_PROCESSOR context: CompileContext, dirtyFilesHolder: DirtyFilesHolder, environment: CompilerEnvironment, - filesToCompile: MultiMap, - messageCollector: KotlinBuilder.MessageCollectorAdapter + incrementalCaches: MutableMap?, + filesToCompile: MultiMap, messageCollector: MessageCollectorAdapter ): OutputItemsCollectorImpl? { val outputItemCollector = OutputItemsCollectorImpl() @@ -535,7 +538,7 @@ public class KotlinBuilder : ModuleLevelBuilder(BuilderCategory.SOURCE_PROCESSOR + (if (totalRemovedFiles == 0) "" else " ($totalRemovedFiles removed files)") + " in " + filesToCompile.keySet().map { it.getPresentableName() }.join()) - runK2JvmCompiler(commonArguments, k2JvmArguments, compilerSettings, messageCollector, environment, moduleFile, outputItemCollector) + runK2JvmCompiler(commonArguments, k2JvmArguments, compilerSettings, messageCollector, environment, incrementalCaches, moduleFile, outputItemCollector) moduleFile.delete() return outputItemCollector