incremental compilation via daemon

This commit is contained in:
ligee
2015-08-11 15:44:30 +02:00
committed by Ilya Chernikov
parent 06b3ca8343
commit b55894499f
8 changed files with 117 additions and 21 deletions
+1
View File
@@ -9,5 +9,6 @@
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="kotlin-runtime" level="project" />
<orderEntry type="module" module-name="rmi-interface" />
<orderEntry type="module" module-name="frontend.java" />
</component>
</module>
@@ -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<String>, caches: Map<String, IncrementalCache>, out: OutputStream): Int {
val outStrm = RemoteOutputStreamServer(out)
val cacheServers = hashMapOf<String, RemoteIncrementalCacheServer>()
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()
}
}
}
}
}
}
@@ -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<String> = cache.getObsoletePackageParts()
override fun getPackageData(fqName: String): ByteArray? = cache.getPackageData(fqName)
override fun close() {
cache.close()
}
public fun disconnect() {
UnicastRemoteObject.unexportObject(this, true)
}
}
@@ -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) {
@@ -7,6 +7,7 @@
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="kotlin-runtime" level="project" />
<orderEntry type="module" module-name="cli-common" />
<orderEntry type="module" module-name="frontend.java" />
</component>
+2
View File
@@ -21,5 +21,7 @@
<orderEntry type="library" name="asm" level="project" />
<orderEntry type="module" module-name="util" />
<orderEntry type="module" module-name="preloader" />
<orderEntry type="module" module-name="kotlinr" />
<orderEntry type="module" module-name="rmi-interface" />
</component>
</module>
@@ -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<String, IncrementalCache> 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<String, IncrementalCache> incrementalCaches, @NotNull OutputItemsCollector collector,
@NotNull Collection<File> sourceFiles,
@NotNull List<String> 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<String, IncrementalCache> 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<String, IncrementalCache> incrementalCaches,
PrintStream out,
MessageCollector messageCollector
) {
@@ -116,13 +124,27 @@ public class KotlinCompilerRunner {
List<String> 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);
@@ -247,9 +247,10 @@ public class KotlinBuilder : ModuleLevelBuilder(BuilderCategory.SOURCE_PROCESSOR
filesToCompile: MultiMap<ModuleBuildTarget, File>, incrementalCaches: Map<ModuleBuildTarget, IncrementalCacheImpl>,
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<String, IncrementalCache>(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<String, IncrementalCache>?,
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<JavaSourceRootDescriptor, ModuleBuildTarget>,
environment: CompilerEnvironment,
filesToCompile: MultiMap<ModuleBuildTarget, File>,
messageCollector: KotlinBuilder.MessageCollectorAdapter
incrementalCaches: MutableMap<String, IncrementalCache>?,
filesToCompile: MultiMap<ModuleBuildTarget, File>, 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