From 4b1601974f4f2c185cf5dc17d7668471a1085868 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 --- .../rmi/kotlinr/KotlinCompilerClient.kt | 24 ++++++-- .../RemoteCompilationCanceledStatusServer.kt | 35 ++++++++++++ ...eIncrementalCompilationComponentsServer.kt | 44 +++++++++++++++ .../rmi/kotlinr/RemoteLookupTrackerServer.kt | 40 ++++++++++++++ .../jetbrains/kotlin/rmi/CompileService.kt | 37 ++++++++++++- .../kotlin/rmi/service/CompileServiceImpl.kt | 33 ++++------- .../RemoteCompilationCanceledStatusClient.kt | 27 +++++++++ ...eIncrementalCompilationComponentsClient.kt | 31 +++++++++++ .../rmi/service/RemoteLookupTrackerClient.kt | 32 +++++++++++ .../compilerRunner/KotlinCompilerRunner.kt | 55 +++++++++---------- .../kotlin/jps/build/KotlinBuilder.kt | 10 +--- 11 files changed, 301 insertions(+), 67 deletions(-) create mode 100644 compiler/rmi/kotlinr/src/org/jetbrains/kotlin/rmi/kotlinr/RemoteCompilationCanceledStatusServer.kt create mode 100644 compiler/rmi/kotlinr/src/org/jetbrains/kotlin/rmi/kotlinr/RemoteIncrementalCompilationComponentsServer.kt create mode 100644 compiler/rmi/kotlinr/src/org/jetbrains/kotlin/rmi/kotlinr/RemoteLookupTrackerServer.kt create mode 100644 compiler/rmi/rmi-server/src/org/jetbrains/kotlin/rmi/service/RemoteCompilationCanceledStatusClient.kt create mode 100644 compiler/rmi/rmi-server/src/org/jetbrains/kotlin/rmi/service/RemoteIncrementalCompilationComponentsClient.kt create mode 100644 compiler/rmi/rmi-server/src/org/jetbrains/kotlin/rmi/service/RemoteLookupTrackerClient.kt 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 2b115f861e9..c44482d72e9 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 @@ -16,8 +16,9 @@ package org.jetbrains.kotlin.rmi.kotlinr -import org.jetbrains.kotlin.load.kotlin.incremental.components.IncrementalCache +import org.jetbrains.kotlin.load.kotlin.incremental.components.IncrementalCompilationComponents import org.jetbrains.kotlin.modules.TargetId +import org.jetbrains.kotlin.progress.CompilationCanceledStatus import org.jetbrains.kotlin.rmi.* import java.io.* import java.rmi.ConnectException @@ -27,6 +28,12 @@ import java.util.concurrent.TimeUnit import kotlin.concurrent.thread +public class CompilationServices( + val incrementalCompilationComponents: IncrementalCompilationComponents? = null, + val compilationCanceledStatus: CompilationCanceledStatus? = null +) + + public object KotlinCompilerClient { val DAEMON_DEFAULT_STARTUP_TIMEOUT_MS = 10000L @@ -108,7 +115,7 @@ public object KotlinCompilerClient { val outStrm = RemoteOutputStreamServer(out) try { - return compiler.remoteCompile(args, outStrm, CompileService.OutputFormat.PLAIN, outStrm) + return compiler.remoteCompile(args, makeRemoteServices(CompilationServices()), outStrm, CompileService.OutputFormat.PLAIN, outStrm) } finally { outStrm.disconnect() @@ -118,14 +125,13 @@ public object KotlinCompilerClient { // TODO: remove jvmStatic after all use sites will switch to kotlin @JvmStatic - public fun incrementalCompile(compiler: CompileService, args: Array, caches: Map, compilerOut: OutputStream, daemonOut: OutputStream): Int { + public fun incrementalCompile(compiler: CompileService, args: Array, services: CompilationServices, compilerOut: OutputStream, daemonOut: OutputStream): Int { val compilerOutStreamServer = RemoteOutputStreamServer(compilerOut) val daemonOutStreamServer = RemoteOutputStreamServer(daemonOut) val cacheServers = hashMapOf() try { - caches.mapValuesTo(cacheServers, { RemoteIncrementalCacheServer(it.getValue()) }) - return compiler.remoteIncrementalCompile(args, cacheServers, compilerOutStreamServer, CompileService.OutputFormat.XML, daemonOutStreamServer) + return compiler.remoteIncrementalCompile(args, makeRemoteServices(services), compilerOutStreamServer, CompileService.OutputFormat.XML, daemonOutStreamServer) } finally { cacheServers.forEach { it.getValue().disconnect() } @@ -135,6 +141,12 @@ public object KotlinCompilerClient { } + fun makeRemoteServices(services: CompilationServices): CompileService.RemoteCompilationServices = + CompileService.RemoteCompilationServices( + incrementalCompilationComponents = if (services.incrementalCompilationComponents == null) null else RemoteIncrementalCompilationComponentsServer(services.incrementalCompilationComponents), + compilationCanceledStatus = if (services.compilationCanceledStatus == null) null else RemoteCompilationCanceledStatusServer(services.compilationCanceledStatus) + ) + data class ClientOptions( public var stop: Boolean = false ) : OptionsGroup { @@ -196,7 +208,7 @@ public object KotlinCompilerClient { val memBefore = daemon.getUsedMemory() / 1024 val startTime = System.nanoTime() - val res = daemon.remoteCompile(filteredArgs.toArrayList().toTypedArray(), outStrm, CompileService.OutputFormat.PLAIN, outStrm) + val res = daemon.remoteCompile(filteredArgs.toArrayList().toTypedArray(), makeRemoteServices(CompilationServices()), outStrm, CompileService.OutputFormat.PLAIN, outStrm) val endTime = System.nanoTime() println("Compilation result code: $res") diff --git a/compiler/rmi/kotlinr/src/org/jetbrains/kotlin/rmi/kotlinr/RemoteCompilationCanceledStatusServer.kt b/compiler/rmi/kotlinr/src/org/jetbrains/kotlin/rmi/kotlinr/RemoteCompilationCanceledStatusServer.kt new file mode 100644 index 00000000000..1557d6dee22 --- /dev/null +++ b/compiler/rmi/kotlinr/src/org/jetbrains/kotlin/rmi/kotlinr/RemoteCompilationCanceledStatusServer.kt @@ -0,0 +1,35 @@ +/* + * 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.progress.CompilationCanceledStatus +import org.jetbrains.kotlin.rmi.CompileService +import org.jetbrains.kotlin.rmi.LoopbackNetworkInterface +import org.jetbrains.kotlin.rmi.SOCKET_ANY_FREE_PORT +import java.rmi.server.UnicastRemoteObject + + +public class RemoteCompilationCanceledStatusServer(val base: CompilationCanceledStatus, port: Int = SOCKET_ANY_FREE_PORT) : CompileService.RemoteCompilationCanceledStatus { + + init { + UnicastRemoteObject.exportObject(this, port, LoopbackNetworkInterface.clientLoopbackSocketFactory, LoopbackNetworkInterface.serverLoopbackSocketFactory) + } + + override fun checkCanceled() { + base.checkCanceled() + } +} \ No newline at end of file diff --git a/compiler/rmi/kotlinr/src/org/jetbrains/kotlin/rmi/kotlinr/RemoteIncrementalCompilationComponentsServer.kt b/compiler/rmi/kotlinr/src/org/jetbrains/kotlin/rmi/kotlinr/RemoteIncrementalCompilationComponentsServer.kt new file mode 100644 index 00000000000..52eddc4c2a8 --- /dev/null +++ b/compiler/rmi/kotlinr/src/org/jetbrains/kotlin/rmi/kotlinr/RemoteIncrementalCompilationComponentsServer.kt @@ -0,0 +1,44 @@ +/* + * 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.load.kotlin.incremental.components.IncrementalCompilationComponents +import org.jetbrains.kotlin.modules.TargetId +import org.jetbrains.kotlin.rmi.CompileService +import org.jetbrains.kotlin.rmi.LoopbackNetworkInterface +import org.jetbrains.kotlin.rmi.SOCKET_ANY_FREE_PORT +import java.rmi.server.UnicastRemoteObject + + +public class RemoteIncrementalCompilationComponentsServer(val base: IncrementalCompilationComponents, val port: Int = SOCKET_ANY_FREE_PORT) : CompileService.RemoteIncrementalCompilationComponents { + + init { + UnicastRemoteObject.exportObject(this, port, LoopbackNetworkInterface.clientLoopbackSocketFactory, LoopbackNetworkInterface.serverLoopbackSocketFactory) + } + + private val cacheServers = hashMapOf() + private val lookupTrackerServer by lazy { RemoteLookupTrackerServer(base.getLookupTracker()) } + + override fun getIncrementalCache(target: TargetId): CompileService.RemoteIncrementalCache = + cacheServers.get(target) ?: { + val newServer = RemoteIncrementalCacheServer(base.getIncrementalCache(target), port) + cacheServers.put(target, newServer) + newServer + }() + + override fun getLookupTracker(): CompileService.RemoteLookupTracker = lookupTrackerServer +} diff --git a/compiler/rmi/kotlinr/src/org/jetbrains/kotlin/rmi/kotlinr/RemoteLookupTrackerServer.kt b/compiler/rmi/kotlinr/src/org/jetbrains/kotlin/rmi/kotlinr/RemoteLookupTrackerServer.kt new file mode 100644 index 00000000000..197d8e817e6 --- /dev/null +++ b/compiler/rmi/kotlinr/src/org/jetbrains/kotlin/rmi/kotlinr/RemoteLookupTrackerServer.kt @@ -0,0 +1,40 @@ +/* + * 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.incremental.components.LookupTracker +import org.jetbrains.kotlin.incremental.components.ScopeKind +import org.jetbrains.kotlin.rmi.CompileService +import org.jetbrains.kotlin.rmi.LoopbackNetworkInterface +import org.jetbrains.kotlin.rmi.SOCKET_ANY_FREE_PORT +import java.rmi.server.UnicastRemoteObject + + +public class RemoteLookupTrackerServer(val base: LookupTracker, port: Int = SOCKET_ANY_FREE_PORT) : CompileService.RemoteLookupTracker { + + init { + UnicastRemoteObject.exportObject(this, port, LoopbackNetworkInterface.clientLoopbackSocketFactory, LoopbackNetworkInterface.serverLoopbackSocketFactory) + } + + override fun record(lookupContainingFile: String, lookupLine: Int?, lookupColumn: Int?, scopeFqName: String, scopeKind: ScopeKind, name: String) { + base.record(lookupContainingFile, lookupLine, lookupColumn, scopeFqName, scopeKind, name) + } + + private val _isDoNothing: Boolean = base == LookupTracker.DO_NOTHING + override fun isDoNothing(): Boolean = _isDoNothing +} + 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 6c01ffba748..302d2281c9a 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 @@ -16,7 +16,9 @@ package org.jetbrains.kotlin.rmi +import org.jetbrains.kotlin.incremental.components.ScopeKind import org.jetbrains.kotlin.modules.TargetId +import java.io.Serializable import java.rmi.Remote import java.rmi.RemoteException @@ -47,6 +49,38 @@ public interface CompileService : Remote { public fun close() } + public interface RemoteLookupTracker : Remote { + @Throws(RemoteException::class) + fun record( + lookupContainingFile: String, + lookupLine: Int?, + lookupColumn: Int?, + scopeFqName: String, + scopeKind: ScopeKind, + name: String + ) + @Throws(RemoteException::class) + fun isDoNothing(): Boolean + } + + public interface RemoteIncrementalCompilationComponents : Remote { + @Throws(RemoteException::class) + public fun getIncrementalCache(target: TargetId): RemoteIncrementalCache + + @Throws(RemoteException::class) + public fun getLookupTracker(): RemoteLookupTracker + } + + public interface RemoteCompilationCanceledStatus : Remote { + @Throws(RemoteException::class) + fun checkCanceled(): Unit + } + + public data class RemoteCompilationServices( + public val incrementalCompilationComponents: RemoteIncrementalCompilationComponents? = null, + public val compilationCanceledStatus: RemoteCompilationCanceledStatus? = null + ) : Serializable + @Throws(RemoteException::class) public fun getCompilerId(): CompilerId @@ -59,6 +93,7 @@ public interface CompileService : Remote { @Throws(RemoteException::class) public fun remoteCompile( args: Array, + services: RemoteCompilationServices, compilerOutputStream: RemoteOutputStream, outputFormat: OutputFormat, serviceOutputStream: RemoteOutputStream @@ -67,7 +102,7 @@ public interface CompileService : Remote { @Throws(RemoteException::class) public fun remoteIncrementalCompile( args: Array, - caches: Map, + services: RemoteCompilationServices, compilerOutputStream: RemoteOutputStream, compilerOutputFormat: OutputFormat, serviceOutputStream: RemoteOutputStream 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 872f3a3beed..2a239d0fdca 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 @@ -19,22 +19,17 @@ package org.jetbrains.kotlin.rmi.service import org.jetbrains.kotlin.cli.common.CLICompiler import org.jetbrains.kotlin.cli.common.ExitCode import org.jetbrains.kotlin.config.Services -import org.jetbrains.kotlin.incremental.components.LookupTracker -import org.jetbrains.kotlin.load.kotlin.incremental.components.IncrementalCache import org.jetbrains.kotlin.load.kotlin.incremental.components.IncrementalCompilationComponents -import org.jetbrains.kotlin.modules.TargetId +import org.jetbrains.kotlin.progress.CompilationCanceledStatus import org.jetbrains.kotlin.rmi.* -import java.io.IOException import java.io.PrintStream import java.lang.management.ManagementFactory import java.lang.management.ThreadMXBean -import java.net.URLClassLoader import java.rmi.NoSuchObjectException import java.rmi.registry.Registry import java.rmi.server.UnicastRemoteObject import java.util.concurrent.TimeUnit import java.util.concurrent.locks.ReentrantReadWriteLock -import java.util.jar.Manifest import java.util.logging.Logger import kotlin.concurrent.read import kotlin.concurrent.write @@ -65,6 +60,7 @@ class CompileServiceImpl>( } override fun remoteCompile(args: Array, + services: CompileService.RemoteCompilationServices, compilerOutputStream: RemoteOutputStream, outputFormat: CompileService.OutputFormat, serviceOutputStream: RemoteOutputStream @@ -77,7 +73,7 @@ class CompileServiceImpl>( } override fun remoteIncrementalCompile(args: Array, - caches: Map, + services: CompileService.RemoteCompilationServices, compilerOutputStream: RemoteOutputStream, compilerOutputFormat: CompileService.OutputFormat, serviceOutputStream: RemoteOutputStream @@ -85,7 +81,7 @@ class CompileServiceImpl>( doCompile(args, compilerOutputStream, serviceOutputStream) { printStream -> when (compilerOutputFormat) { CompileService.OutputFormat.PLAIN -> throw NotImplementedError("Only XML output is supported in remote incremental compilation") - CompileService.OutputFormat.XML -> compiler.execAndOutputXml(printStream, createCompileServices(caches), *args) + CompileService.OutputFormat.XML -> compiler.execAndOutputXml(printStream, createCompileServices(services), *args) } } @@ -127,13 +123,6 @@ class CompileServiceImpl>( alive = true } - private class IncrementalCompilationComponentsImpl(val targetToCache: Map): IncrementalCompilationComponents { - // perf: cheap object, but still the pattern may be costly if there are too many calls to cache with the same id (which seems not to be the case now) - override fun getIncrementalCache(target: TargetId): IncrementalCache = RemoteIncrementalCacheClient(targetToCache[target]!!) - // TODO: add appropriate proxy into interaction when lookup tracker is needed - override fun getLookupTracker(): LookupTracker = LookupTracker.DO_NOTHING - } - private fun doCompile(args: Array, compilerMessagesStreamProxy: RemoteOutputStream, serviceOutputStreamProxy: RemoteOutputStream, body: (PrintStream) -> ExitCode): Int = ifAlive { val compilerMessagesStream = PrintStream(RemoteOutputStreamClient(compilerMessagesStreamProxy)) @@ -145,14 +134,12 @@ class CompileServiceImpl>( } } - private fun createCompileServices(incrementalCaches: Map): Services = - Services.Builder() - .register(IncrementalCompilationComponents::class.java, IncrementalCompilationComponentsImpl(incrementalCaches)) - // TODO: add remote proxy for cancellation status tracking -// .register(javaClass(), object: CompilationCanceledStatus { -// override fun checkCanceled(): Unit = if (context.getCancelStatus().isCanceled()) throw CompilationCanceledException() -// }) - .build() + private fun createCompileServices(services: CompileService.RemoteCompilationServices): Services { + val builder = Services.Builder() + services.incrementalCompilationComponents?.let { builder.register(IncrementalCompilationComponents::class.java, RemoteIncrementalCompilationComponentsClient(it)) } + services.compilationCanceledStatus?.let { builder.register(CompilationCanceledStatus::class.java, RemoteCompilationCanceledStatusClient(it)) } + return builder.build() + } fun usedMemory(): Long { diff --git a/compiler/rmi/rmi-server/src/org/jetbrains/kotlin/rmi/service/RemoteCompilationCanceledStatusClient.kt b/compiler/rmi/rmi-server/src/org/jetbrains/kotlin/rmi/service/RemoteCompilationCanceledStatusClient.kt new file mode 100644 index 00000000000..423def3e8c0 --- /dev/null +++ b/compiler/rmi/rmi-server/src/org/jetbrains/kotlin/rmi/service/RemoteCompilationCanceledStatusClient.kt @@ -0,0 +1,27 @@ +/* + * 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.service + +import org.jetbrains.kotlin.progress.CompilationCanceledStatus +import org.jetbrains.kotlin.rmi.CompileService + + +class RemoteCompilationCanceledStatusClient(val proxy: CompileService.RemoteCompilationCanceledStatus): CompilationCanceledStatus { + override fun checkCanceled() { + proxy.checkCanceled() + } +} diff --git a/compiler/rmi/rmi-server/src/org/jetbrains/kotlin/rmi/service/RemoteIncrementalCompilationComponentsClient.kt b/compiler/rmi/rmi-server/src/org/jetbrains/kotlin/rmi/service/RemoteIncrementalCompilationComponentsClient.kt new file mode 100644 index 00000000000..e6ce460e0c3 --- /dev/null +++ b/compiler/rmi/rmi-server/src/org/jetbrains/kotlin/rmi/service/RemoteIncrementalCompilationComponentsClient.kt @@ -0,0 +1,31 @@ +/* + * 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.service + +import org.jetbrains.kotlin.incremental.components.LookupTracker +import org.jetbrains.kotlin.load.kotlin.incremental.components.IncrementalCache +import org.jetbrains.kotlin.load.kotlin.incremental.components.IncrementalCompilationComponents +import org.jetbrains.kotlin.modules.TargetId +import org.jetbrains.kotlin.rmi.CompileService + + +class RemoteIncrementalCompilationComponentsClient(val proxy: CompileService.RemoteIncrementalCompilationComponents) : IncrementalCompilationComponents { + + override fun getIncrementalCache(target: TargetId): IncrementalCache = RemoteIncrementalCacheClient(proxy.getIncrementalCache(target)) + + override fun getLookupTracker(): LookupTracker = RemoteLookupTrackerClient(proxy.getLookupTracker()) +} diff --git a/compiler/rmi/rmi-server/src/org/jetbrains/kotlin/rmi/service/RemoteLookupTrackerClient.kt b/compiler/rmi/rmi-server/src/org/jetbrains/kotlin/rmi/service/RemoteLookupTrackerClient.kt new file mode 100644 index 00000000000..e3d1fd2c0aa --- /dev/null +++ b/compiler/rmi/rmi-server/src/org/jetbrains/kotlin/rmi/service/RemoteLookupTrackerClient.kt @@ -0,0 +1,32 @@ +/* + * 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.service + +import org.jetbrains.kotlin.incremental.components.LookupTracker +import org.jetbrains.kotlin.incremental.components.ScopeKind +import org.jetbrains.kotlin.rmi.CompileService + + +class RemoteLookupTrackerClient(val proxy: CompileService.RemoteLookupTracker) : LookupTracker { + + private val isDoNothing = proxy.isDoNothing() + + override fun record(lookupContainingFile: String, lookupLine: Int?, lookupColumn: Int?, scopeFqName: String, scopeKind: ScopeKind, name: String) { + if (!isDoNothing) + proxy.record(lookupContainingFile, lookupLine, lookupColumn, scopeFqName, scopeKind, name) + } +} diff --git a/jps-plugin/src/org/jetbrains/kotlin/compilerRunner/KotlinCompilerRunner.kt b/jps-plugin/src/org/jetbrains/kotlin/compilerRunner/KotlinCompilerRunner.kt index 4ebfec35d54..0dd541b1b9a 100644 --- a/jps-plugin/src/org/jetbrains/kotlin/compilerRunner/KotlinCompilerRunner.kt +++ b/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-plugin/src/org/jetbrains/kotlin/jps/build/KotlinBuilder.kt b/jps-plugin/src/org/jetbrains/kotlin/jps/build/KotlinBuilder.kt index 81a96f10aa0..1c028a21d29 100644 --- a/jps-plugin/src/org/jetbrains/kotlin/jps/build/KotlinBuilder.kt +++ b/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