Adding support for JS compilation on daemon

This commit is contained in:
Ilya Chernikov
2015-10-07 12:28:33 +02:00
parent 391c292b8d
commit 99b638a58b
7 changed files with 61 additions and 25 deletions
@@ -111,12 +111,12 @@ public object KotlinCompilerClient {
}
public fun compile(compiler: CompileService, args: Array<out String>, out: OutputStream): Int {
public fun compile(compilerService: CompileService, targetPlatform: CompileService.TargetPlatform, args: Array<out String>, out: OutputStream): Int {
val outStrm = RemoteOutputStreamServer(out)
val servicesFacade = CompilerCallbackServicesFacadeServer()
try {
return compiler.remoteCompile(args, servicesFacade, outStrm, CompileService.OutputFormat.PLAIN, outStrm)
return compilerService.remoteCompile(targetPlatform, args, servicesFacade, outStrm, CompileService.OutputFormat.PLAIN, outStrm)
}
finally {
servicesFacade.disconnect()
@@ -125,13 +125,23 @@ public object KotlinCompilerClient {
}
public fun incrementalCompile(compiler: CompileService, args: Array<out String>, services: CompilationServices, compilerOut: OutputStream, daemonOut: OutputStream, profiler: Profiler = DummyProfiler()): Int {
public fun incrementalCompile(compileService: CompileService,
targetPlatform: CompileService.TargetPlatform,
args: Array<out String>,
callbackServices: CompilationServices,
compilerOut: OutputStream,
daemonOut: OutputStream,
profiler: Profiler = DummyProfiler()
): Int {
val compilerOutStreamServer = RemoteOutputStreamServer(compilerOut)
val daemonOutStreamServer = RemoteOutputStreamServer(daemonOut)
val servicesFacade = CompilerCallbackServicesFacadeServer(incrementalCompilationComponents = services.incrementalCompilationComponents, compilationCancelledStatus = services.compilationCanceledStatus)
val servicesFacade = CompilerCallbackServicesFacadeServer(incrementalCompilationComponents = callbackServices.incrementalCompilationComponents,
compilationCancelledStatus = callbackServices.compilationCanceledStatus)
try {
return profiler.withMeasure(this) { compiler.remoteIncrementalCompile(args, servicesFacade, compilerOutStreamServer, CompileService.OutputFormat.XML, daemonOutStreamServer) }
return profiler.withMeasure(this) {
compileService.remoteIncrementalCompile(targetPlatform, args, servicesFacade, compilerOutStreamServer, CompileService.OutputFormat.XML, daemonOutStreamServer)
}
}
finally {
servicesFacade.disconnect()
@@ -202,7 +212,7 @@ public object KotlinCompilerClient {
val memBefore = daemon.getUsedMemory() / 1024
val startTime = System.nanoTime()
val res = daemon.remoteCompile(filteredArgs.toArrayList().toTypedArray(), servicesFacade, outStrm, CompileService.OutputFormat.PLAIN, outStrm)
val res = daemon.remoteCompile(CompileService.TargetPlatform.JVM, filteredArgs.toArrayList().toTypedArray(), servicesFacade, outStrm, CompileService.OutputFormat.PLAIN, outStrm)
val endTime = System.nanoTime()
println("Compilation result code: $res")
@@ -26,6 +26,11 @@ public interface CompileService : Remote {
XML
}
public enum class TargetPlatform : java.io.Serializable {
JVM,
JS
}
@Throws(RemoteException::class)
public fun getCompilerId(): CompilerId
@@ -37,15 +42,16 @@ public interface CompileService : Remote {
@Throws(RemoteException::class)
public fun remoteCompile(
targetPlatform: TargetPlatform,
args: Array<out String>,
servicesFacade: CompilerCallbackServicesFacade,
compilerOutputStream: RemoteOutputStream,
outputFormat: OutputFormat,
serviceOutputStream: RemoteOutputStream
outputFormat: OutputFormat, serviceOutputStream: RemoteOutputStream
): Int
@Throws(RemoteException::class)
public fun remoteIncrementalCompile(
targetPlatform: TargetPlatform,
args: Array<out String>,
servicesFacade: CompilerCallbackServicesFacade,
compilerOutputStream: RemoteOutputStream,
@@ -16,6 +16,8 @@
package org.jetbrains.kotlin.rmi.service
import org.jetbrains.kotlin.cli.common.CLICompiler
import org.jetbrains.kotlin.cli.js.K2JSCompiler
import org.jetbrains.kotlin.cli.jvm.K2JVMCompiler
import org.jetbrains.kotlin.rmi.*
import java.io.File
@@ -133,8 +135,15 @@ public object CompileDaemon {
}
runFile.deleteOnExit()
val compiler = K2JVMCompiler()
val compilerService = CompileServiceImpl(registry, compiler, compilerId, daemonOptions, port)
val compilerSelector = object : CompilerSelector {
private val jvm by lazy { K2JVMCompiler() }
private val js by lazy { K2JSCompiler() }
override fun get(targetPlatform: CompileService.TargetPlatform): CLICompiler<*> = when (targetPlatform) {
CompileService.TargetPlatform.JVM -> jvm
CompileService.TargetPlatform.JS -> js
}
}
val compilerService = CompileServiceImpl(registry, compilerSelector, compilerId, daemonOptions, port)
if (daemonOptions.runFilesPath.isNotEmpty())
println(daemonOptions.runFilesPath)
@@ -35,9 +35,13 @@ import kotlin.concurrent.write
fun nowSeconds() = TimeUnit.NANOSECONDS.toSeconds(System.nanoTime())
class CompileServiceImpl<Compiler: CLICompiler<*>>(
interface CompilerSelector {
operator fun get(targetPlatform: CompileService.TargetPlatform): CLICompiler<*>
}
class CompileServiceImpl(
val registry: Registry,
val compiler: Compiler,
val compiler: CompilerSelector,
val selfCompilerId: CompilerId,
val daemonOptions: DaemonOptions,
port: Int
@@ -58,20 +62,21 @@ class CompileServiceImpl<Compiler: CLICompiler<*>>(
}
}
override fun remoteCompile(args: Array<out String>,
override fun remoteCompile(targetPlatform: CompileService.TargetPlatform,
args: Array<out String>,
servicesFacade: CompilerCallbackServicesFacade,
compilerOutputStream: RemoteOutputStream,
outputFormat: CompileService.OutputFormat,
serviceOutputStream: RemoteOutputStream
outputFormat: CompileService.OutputFormat, serviceOutputStream: RemoteOutputStream
): Int =
doCompile(args, compilerOutputStream, serviceOutputStream) { printStream, profiler ->
when (outputFormat) {
CompileService.OutputFormat.PLAIN -> compiler.exec(printStream, *args)
CompileService.OutputFormat.XML -> compiler.execAndOutputXml(printStream, createCompileServices(servicesFacade, profiler), *args)
CompileService.OutputFormat.PLAIN -> compiler[targetPlatform].exec(printStream, *args)
CompileService.OutputFormat.XML -> compiler[targetPlatform].execAndOutputXml(printStream, createCompileServices(servicesFacade, profiler), *args)
}
}
override fun remoteIncrementalCompile(args: Array<out String>,
override fun remoteIncrementalCompile(targetPlatform: CompileService.TargetPlatform,
args: Array<out String>,
servicesFacade: CompilerCallbackServicesFacade,
compilerOutputStream: RemoteOutputStream,
compilerOutputFormat: CompileService.OutputFormat,
@@ -80,7 +85,7 @@ class CompileServiceImpl<Compiler: CLICompiler<*>>(
doCompile(args, compilerOutputStream, serviceOutputStream) { printStream, profiler ->
when (compilerOutputFormat) {
CompileService.OutputFormat.PLAIN -> throw NotImplementedError("Only XML output is supported in remote incremental compilation")
CompileService.OutputFormat.XML -> compiler.execAndOutputXml(printStream, createCompileServices(servicesFacade, profiler), *args)
CompileService.OutputFormat.XML -> compiler[targetPlatform].execAndOutputXml(printStream, createCompileServices(servicesFacade, profiler), *args)
}
}
@@ -38,7 +38,7 @@ public class CompilerDaemonTest : KotlinIntegrationTestBase() {
val daemon = KotlinCompilerClient.connectToCompileService(compilerId, daemonJVMOptions, daemonOptions, DaemonReportingTargets(out = System.err), autostart = true, checkId = true)
TestCase.assertNotNull("failed to connect daemon", daemon)
val strm = ByteArrayOutputStream()
val code = KotlinCompilerClient.compile(daemon!!, args, strm)
val code = KotlinCompilerClient.compile(daemon!!, CompileService.TargetPlatform.JVM, args, strm)
return CompilerResults(code, strm.toString())
}
@@ -105,7 +105,7 @@ public object KotlinCompilerRunner {
val argsArray = argumentsList.toTypedArray()
if (!tryCompileWithDaemon(messageCollector, collector, environment, argsArray)) {
if (!tryCompileWithDaemon(compilerClassName, argsArray, environment, messageCollector, collector)) {
// otherwise fallback to in-process
val stream = ByteArrayOutputStream()
@@ -125,10 +125,10 @@ public object KotlinCompilerRunner {
}
private fun tryCompileWithDaemon(messageCollector: MessageCollector,
collector: OutputItemsCollector,
private fun tryCompileWithDaemon(compilerClassName: String,
argsArray: Array<String>,
environment: CompilerEnvironment,
argsArray: Array<String>): Boolean {
messageCollector: MessageCollector, collector: OutputItemsCollector): Boolean {
if (isDaemonEnabled()) {
val libPath = CompilerRunnerUtil.getLibPath(environment.kotlinPaths, messageCollector)
@@ -158,7 +158,12 @@ public object KotlinCompilerRunner {
val profiler = if (daemonOptions.reportPerf) WallAndThreadTotalProfiler() else DummyProfiler()
val res = KotlinCompilerClient.incrementalCompile(daemon, argsArray, services, compilerOut, daemonOut, profiler)
val targetPlatform = when (compilerClassName) {
K2JVM_COMPILER -> CompileService.TargetPlatform.JVM
K2JS_COMPILER -> CompileService.TargetPlatform.JS
else -> throw IllegalArgumentException("Unknown compiler type $compilerClassName")
}
val res = KotlinCompilerClient.incrementalCompile(daemon, targetPlatform, argsArray, services, compilerOut, daemonOut, profiler)
processCompilerOutput(messageCollector, collector, compilerOut, res.toString())
BufferedReader(StringReader(daemonOut.toString())).forEachLine {
@@ -67,6 +67,7 @@ public class SimpleKotlinJpsBuildTest : AbstractKotlinJpsBuildTestCase() {
rebuildAll()
}
// TODO: add JS tests
public fun testDaemon() {
System.setProperty(COMPILE_DAEMON_ENABLED_PROPERTY,"")
System.setProperty(COMPILE_DAEMON_VERBOSE_REPORT_PROPERTY, "")