diff --git a/compiler/rmi/kotlinr/src/org/jetbrains/kotlin/rmi/kotlinr/KotlinCompilerClient.kt b/compiler/rmi/kotlinr/src/org/jetbrains/kotlin/rmi/kotlinr/KotlinCompilerClient.kt index 826e4b1284b..e7b857115c4 100644 --- a/compiler/rmi/kotlinr/src/org/jetbrains/kotlin/rmi/kotlinr/KotlinCompilerClient.kt +++ b/compiler/rmi/kotlinr/src/org/jetbrains/kotlin/rmi/kotlinr/KotlinCompilerClient.kt @@ -111,12 +111,12 @@ public object KotlinCompilerClient { } - public fun compile(compiler: CompileService, args: Array, out: OutputStream): Int { + public fun compile(compilerService: CompileService, targetPlatform: CompileService.TargetPlatform, args: Array, 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, services: CompilationServices, compilerOut: OutputStream, daemonOut: OutputStream, profiler: Profiler = DummyProfiler()): Int { + public fun incrementalCompile(compileService: CompileService, + targetPlatform: CompileService.TargetPlatform, + args: Array, + 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") diff --git a/compiler/rmi/rmi-interface/src/org/jetbrains/kotlin/rmi/CompileService.kt b/compiler/rmi/rmi-interface/src/org/jetbrains/kotlin/rmi/CompileService.kt index 77d8cf25c0e..c1331c0975b 100644 --- a/compiler/rmi/rmi-interface/src/org/jetbrains/kotlin/rmi/CompileService.kt +++ b/compiler/rmi/rmi-interface/src/org/jetbrains/kotlin/rmi/CompileService.kt @@ -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, servicesFacade: CompilerCallbackServicesFacade, compilerOutputStream: RemoteOutputStream, - outputFormat: OutputFormat, - serviceOutputStream: RemoteOutputStream + outputFormat: OutputFormat, serviceOutputStream: RemoteOutputStream ): Int @Throws(RemoteException::class) public fun remoteIncrementalCompile( + targetPlatform: TargetPlatform, args: Array, servicesFacade: CompilerCallbackServicesFacade, compilerOutputStream: RemoteOutputStream, diff --git a/compiler/rmi/rmi-server/src/org/jetbrains/kotlin/rmi/service/CompileDaemon.kt b/compiler/rmi/rmi-server/src/org/jetbrains/kotlin/rmi/service/CompileDaemon.kt index 8d7f7d26be4..efda358ec14 100644 --- a/compiler/rmi/rmi-server/src/org/jetbrains/kotlin/rmi/service/CompileDaemon.kt +++ b/compiler/rmi/rmi-server/src/org/jetbrains/kotlin/rmi/service/CompileDaemon.kt @@ -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) diff --git a/compiler/rmi/rmi-server/src/org/jetbrains/kotlin/rmi/service/CompileServiceImpl.kt b/compiler/rmi/rmi-server/src/org/jetbrains/kotlin/rmi/service/CompileServiceImpl.kt index 9ffcf44d214..8da7cbee995 100644 --- a/compiler/rmi/rmi-server/src/org/jetbrains/kotlin/rmi/service/CompileServiceImpl.kt +++ b/compiler/rmi/rmi-server/src/org/jetbrains/kotlin/rmi/service/CompileServiceImpl.kt @@ -35,9 +35,13 @@ import kotlin.concurrent.write fun nowSeconds() = TimeUnit.NANOSECONDS.toSeconds(System.nanoTime()) -class CompileServiceImpl>( +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>( } } - override fun remoteCompile(args: Array, + override fun remoteCompile(targetPlatform: CompileService.TargetPlatform, + args: Array, 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, + override fun remoteIncrementalCompile(targetPlatform: CompileService.TargetPlatform, + args: Array, servicesFacade: CompilerCallbackServicesFacade, compilerOutputStream: RemoteOutputStream, compilerOutputFormat: CompileService.OutputFormat, @@ -80,7 +85,7 @@ class CompileServiceImpl>( 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) } } diff --git a/compiler/tests/org/jetbrains/kotlin/daemon/CompilerDaemonTest.kt b/compiler/tests/org/jetbrains/kotlin/daemon/CompilerDaemonTest.kt index 5b1ea64392f..e819307ca78 100644 --- a/compiler/tests/org/jetbrains/kotlin/daemon/CompilerDaemonTest.kt +++ b/compiler/tests/org/jetbrains/kotlin/daemon/CompilerDaemonTest.kt @@ -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()) } diff --git a/jps-plugin/src/org/jetbrains/kotlin/compilerRunner/KotlinCompilerRunner.kt b/jps-plugin/src/org/jetbrains/kotlin/compilerRunner/KotlinCompilerRunner.kt index 5e69b53f678..ece6f5bdd3d 100644 --- a/jps-plugin/src/org/jetbrains/kotlin/compilerRunner/KotlinCompilerRunner.kt +++ b/jps-plugin/src/org/jetbrains/kotlin/compilerRunner/KotlinCompilerRunner.kt @@ -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, environment: CompilerEnvironment, - argsArray: Array): 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 { diff --git a/jps-plugin/test/org/jetbrains/kotlin/jps/build/SimpleKotlinJpsBuildTest.kt b/jps-plugin/test/org/jetbrains/kotlin/jps/build/SimpleKotlinJpsBuildTest.kt index f32820d9b48..920df5bb0b2 100644 --- a/jps-plugin/test/org/jetbrains/kotlin/jps/build/SimpleKotlinJpsBuildTest.kt +++ b/jps-plugin/test/org/jetbrains/kotlin/jps/build/SimpleKotlinJpsBuildTest.kt @@ -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, "")