Refactoring: pass MessageCollector, OutputItemsCollector in CompilerEnvironment

Original commit: 9654607f42
This commit is contained in:
Alexey Tsvetkov
2016-11-29 12:57:50 +03:00
parent 5f0b827f0c
commit ee9621ab2a
4 changed files with 38 additions and 45 deletions
@@ -74,10 +74,9 @@ public class CompilerRunnerUtil {
@NotNull String compilerClassName, @NotNull String compilerClassName,
@NotNull String[] arguments, @NotNull String[] arguments,
@NotNull JpsCompilerEnvironment environment, @NotNull JpsCompilerEnvironment environment,
@NotNull MessageCollector messageCollector,
@NotNull PrintStream out @NotNull PrintStream out
) throws Exception { ) throws Exception {
File libPath = getLibPath(environment.getKotlinPaths(), messageCollector); File libPath = getLibPath(environment.getKotlinPaths(), environment.getMessageCollector());
if (libPath == null) return null; if (libPath == null) return null;
ClassLoader classLoader = getOrCreateClassLoader(environment, libPath); ClassLoader classLoader = getOrCreateClassLoader(environment, libPath);
@@ -27,8 +27,10 @@ import org.jetbrains.kotlin.utils.PathUtil
class JpsCompilerEnvironment( class JpsCompilerEnvironment(
val kotlinPaths: KotlinPaths, val kotlinPaths: KotlinPaths,
services: Services, services: Services,
val classesToLoadByParent: ClassCondition val classesToLoadByParent: ClassCondition,
) : CompilerEnvironment(services) { messageCollector: MessageCollector,
override val outputItemsCollector: OutputItemsCollectorImpl
) : CompilerEnvironment(services, messageCollector, outputItemsCollector) {
fun success(): Boolean { fun success(): Boolean {
return kotlinPaths.homePath.exists() return kotlinPaths.homePath.exists()
} }
@@ -44,24 +44,20 @@ class JpsKotlinCompilerRunner : KotlinCompilerRunner<JpsCompilerEnvironment>() {
commonArguments: CommonCompilerArguments, commonArguments: CommonCompilerArguments,
k2jvmArguments: K2JVMCompilerArguments, k2jvmArguments: K2JVMCompilerArguments,
compilerSettings: CompilerSettings, compilerSettings: CompilerSettings,
messageCollector: MessageCollector,
environment: JpsCompilerEnvironment, environment: JpsCompilerEnvironment,
moduleFile: File, moduleFile: File
collector: OutputItemsCollector
) { ) {
val arguments = mergeBeans(commonArguments, k2jvmArguments) val arguments = mergeBeans(commonArguments, k2jvmArguments)
setupK2JvmArguments(moduleFile, arguments) setupK2JvmArguments(moduleFile, arguments)
runCompiler(K2JVM_COMPILER, arguments, compilerSettings.additionalArguments, messageCollector, collector, environment) runCompiler(K2JVM_COMPILER, arguments, compilerSettings.additionalArguments, environment)
} }
fun runK2JsCompiler( fun runK2JsCompiler(
commonArguments: CommonCompilerArguments, commonArguments: CommonCompilerArguments,
k2jsArguments: K2JSCompilerArguments, k2jsArguments: K2JSCompilerArguments,
compilerSettings: CompilerSettings, compilerSettings: CompilerSettings,
messageCollector: MessageCollector,
environment: JpsCompilerEnvironment, environment: JpsCompilerEnvironment,
collector: OutputItemsCollector,
sourceFiles: Collection<File>, sourceFiles: Collection<File>,
libraryFiles: List<String>, libraryFiles: List<String>,
outputFile: File outputFile: File
@@ -69,27 +65,25 @@ class JpsKotlinCompilerRunner : KotlinCompilerRunner<JpsCompilerEnvironment>() {
val arguments = mergeBeans(commonArguments, k2jsArguments) val arguments = mergeBeans(commonArguments, k2jsArguments)
setupK2JsArguments(outputFile, sourceFiles, libraryFiles, arguments) setupK2JsArguments(outputFile, sourceFiles, libraryFiles, arguments)
runCompiler(K2JS_COMPILER, arguments, compilerSettings.additionalArguments, messageCollector, collector, environment) runCompiler(K2JS_COMPILER, arguments, compilerSettings.additionalArguments, environment)
} }
override fun doRunCompiler(compilerClassName: String, argsArray: Array<String>, environment: JpsCompilerEnvironment, messageCollector: MessageCollector, collector: OutputItemsCollector): ExitCode { override fun doRunCompiler(compilerClassName: String, argsArray: Array<String>, environment: JpsCompilerEnvironment): ExitCode {
messageCollector.report(CompilerMessageSeverity.INFO, "Using kotlin-home = " + environment.kotlinPaths.homePath, CompilerMessageLocation.NO_LOCATION) environment.messageCollector.report(CompilerMessageSeverity.INFO, "Using kotlin-home = " + environment.kotlinPaths.homePath, CompilerMessageLocation.NO_LOCATION)
return if (isDaemonEnabled()) { return if (isDaemonEnabled()) {
val daemonExitCode = compileWithDaemon(compilerClassName, argsArray, environment, messageCollector, collector) val daemonExitCode = compileWithDaemon(compilerClassName, argsArray, environment)
daemonExitCode ?: fallbackCompileStrategy(argsArray, collector, compilerClassName, environment, messageCollector) daemonExitCode ?: fallbackCompileStrategy(argsArray, compilerClassName, environment)
} }
else { else {
fallbackCompileStrategy(argsArray, collector, compilerClassName, environment, messageCollector) fallbackCompileStrategy(argsArray, compilerClassName, environment)
} }
} }
private fun fallbackCompileStrategy( private fun fallbackCompileStrategy(
argsArray: Array<String>, argsArray: Array<String>,
collector: OutputItemsCollector,
compilerClassName: String, compilerClassName: String,
environment: JpsCompilerEnvironment, environment: JpsCompilerEnvironment
messageCollector: MessageCollector
): ExitCode { ): ExitCode {
// otherwise fallback to in-process // otherwise fallback to in-process
log.info("Compile in-process") log.info("Compile in-process")
@@ -103,12 +97,12 @@ class JpsKotlinCompilerRunner : KotlinCompilerRunner<JpsCompilerEnvironment>() {
if (System.getProperty(GlobalOptions.COMPILE_PARALLEL_OPTION, "false").toBoolean()) if (System.getProperty(GlobalOptions.COMPILE_PARALLEL_OPTION, "false").toBoolean())
System.setProperty(KOTLIN_COMPILER_ENVIRONMENT_KEEPALIVE_PROPERTY, "true") System.setProperty(KOTLIN_COMPILER_ENVIRONMENT_KEEPALIVE_PROPERTY, "true")
val rc = CompilerRunnerUtil.invokeExecMethod(compilerClassName, argsArray, environment, messageCollector, out) val rc = CompilerRunnerUtil.invokeExecMethod(compilerClassName, argsArray, environment, out)
// exec() returns an ExitCode object, class of which is loaded with a different class loader, // exec() returns an ExitCode object, class of which is loaded with a different class loader,
// so we take it's contents through reflection // so we take it's contents through reflection
val exitCode = ExitCode.valueOf(getReturnCodeFromObject(rc)) val exitCode = ExitCode.valueOf(getReturnCodeFromObject(rc))
processCompilerOutput(messageCollector, collector, stream, exitCode) processCompilerOutput(environment, stream, exitCode)
return exitCode return exitCode
} }
@@ -140,14 +134,14 @@ class JpsKotlinCompilerRunner : KotlinCompilerRunner<JpsCompilerEnvironment>() {
} }
@Synchronized @Synchronized
override fun getDaemonConnection(environment: JpsCompilerEnvironment, messageCollector: MessageCollector): DaemonConnection { override fun getDaemonConnection(environment: JpsCompilerEnvironment): DaemonConnection {
if (jpsDaemonConnection == null) { if (jpsDaemonConnection == null) {
val libPath = CompilerRunnerUtil.getLibPath(environment.kotlinPaths, messageCollector) val libPath = CompilerRunnerUtil.getLibPath(environment.kotlinPaths, environment.messageCollector)
val compilerPath = File(libPath, "kotlin-compiler.jar") val compilerPath = File(libPath, "kotlin-compiler.jar")
val flagFile = File.createTempFile("kotlin-compiler-jps-session-", "-is-running").apply { val flagFile = File.createTempFile("kotlin-compiler-jps-session-", "-is-running").apply {
deleteOnExit() deleteOnExit()
} }
newDaemonConnection(compilerPath, messageCollector, flagFile) newDaemonConnection(compilerPath, flagFile, environment)
} }
return jpsDaemonConnection!! return jpsDaemonConnection!!
} }
@@ -205,7 +205,7 @@ class KotlinBuilder : ModuleLevelBuilder(BuilderCategory.SOURCE_PROCESSOR) {
val project = projectDescriptor.project val project = projectDescriptor.project
val lookupTracker = getLookupTracker(project) val lookupTracker = getLookupTracker(project)
val incrementalCaches = getIncrementalCaches(chunk, context) val incrementalCaches = getIncrementalCaches(chunk, context)
val environment = createCompileEnvironment(incrementalCaches, lookupTracker, context) val environment = createCompileEnvironment(incrementalCaches, lookupTracker, context, messageCollector)
if (!environment.success()) { if (!environment.success()) {
environment.reportErrorsTo(messageCollector) environment.reportErrorsTo(messageCollector)
return ABORT return ABORT
@@ -238,7 +238,7 @@ class KotlinBuilder : ModuleLevelBuilder(BuilderCategory.SOURCE_PROCESSOR) {
LOG.info("Compiled successfully") LOG.info("Compiled successfully")
} }
val generatedFiles = getGeneratedFiles(chunk, outputItemCollector) val generatedFiles = getGeneratedFiles(chunk, environment.outputItemsCollector)
registerOutputItems(outputConsumer, generatedFiles) registerOutputItems(outputConsumer, generatedFiles)
saveVersions(context, chunk) saveVersions(context, chunk)
@@ -368,11 +368,11 @@ class KotlinBuilder : ModuleLevelBuilder(BuilderCategory.SOURCE_PROCESSOR) {
dirtyFilesHolder: DirtyFilesHolder<JavaSourceRootDescriptor, ModuleBuildTarget>, environment: JpsCompilerEnvironment, dirtyFilesHolder: DirtyFilesHolder<JavaSourceRootDescriptor, ModuleBuildTarget>, environment: JpsCompilerEnvironment,
filesToCompile: MultiMap<ModuleBuildTarget, File>, incrementalCaches: Map<ModuleBuildTarget, IncrementalCacheImpl<*>>, filesToCompile: MultiMap<ModuleBuildTarget, File>, incrementalCaches: Map<ModuleBuildTarget, IncrementalCacheImpl<*>>,
messageCollector: MessageCollectorAdapter, project: JpsProject messageCollector: MessageCollectorAdapter, project: JpsProject
): OutputItemsCollectorImpl? { ): OutputItemsCollector? {
if (JpsUtils.isJsKotlinModule(chunk.representativeTarget())) { if (JpsUtils.isJsKotlinModule(chunk.representativeTarget())) {
LOG.debug("Compiling to JS ${filesToCompile.values().size} files in ${filesToCompile.keySet().joinToString { it.presentableName }}") LOG.debug("Compiling to JS ${filesToCompile.values().size} files in ${filesToCompile.keySet().joinToString { it.presentableName }}")
return compileToJs(chunk, commonArguments, environment, messageCollector, project) return compileToJs(chunk, commonArguments, environment, project)
} }
if (IncrementalCompilation.isEnabled()) { if (IncrementalCompilation.isEnabled()) {
@@ -402,13 +402,14 @@ class KotlinBuilder : ModuleLevelBuilder(BuilderCategory.SOURCE_PROCESSOR) {
) )
} }
return compileToJvm(allCompiledFiles, chunk, commonArguments, context, dirtyFilesHolder, environment, filesToCompile, messageCollector) return compileToJvm(allCompiledFiles, chunk, commonArguments, context, dirtyFilesHolder, environment, filesToCompile)
} }
private fun createCompileEnvironment( private fun createCompileEnvironment(
incrementalCaches: Map<ModuleBuildTarget, IncrementalCache>, incrementalCaches: Map<ModuleBuildTarget, IncrementalCache>,
lookupTracker: LookupTracker, lookupTracker: LookupTracker,
context: CompileContext context: CompileContext,
messageCollector: MessageCollectorAdapter
): JpsCompilerEnvironment { ): JpsCompilerEnvironment {
val compilerServices = with(Services.Builder()) { val compilerServices = with(Services.Builder()) {
register(IncrementalCompilationComponents::class.java, register(IncrementalCompilationComponents::class.java,
@@ -433,7 +434,9 @@ class KotlinBuilder : ModuleLevelBuilder(BuilderCategory.SOURCE_PROCESSOR) {
|| className == "org.jetbrains.kotlin.progress.CompilationCanceledStatus" || className == "org.jetbrains.kotlin.progress.CompilationCanceledStatus"
|| className == "org.jetbrains.kotlin.progress.CompilationCanceledException" || className == "org.jetbrains.kotlin.progress.CompilationCanceledException"
|| className == "org.jetbrains.kotlin.modules.TargetId" || className == "org.jetbrains.kotlin.modules.TargetId"
} },
messageCollector,
OutputItemsCollectorImpl()
) )
} }
@@ -585,16 +588,13 @@ class KotlinBuilder : ModuleLevelBuilder(BuilderCategory.SOURCE_PROCESSOR) {
private fun compileToJs(chunk: ModuleChunk, private fun compileToJs(chunk: ModuleChunk,
commonArguments: CommonCompilerArguments, commonArguments: CommonCompilerArguments,
environment: JpsCompilerEnvironment, environment: JpsCompilerEnvironment,
messageCollector: MessageCollectorAdapter,
project: JpsProject project: JpsProject
): OutputItemsCollectorImpl? { ): OutputItemsCollector? {
val outputItemCollector = OutputItemsCollectorImpl()
val representativeTarget = chunk.representativeTarget() val representativeTarget = chunk.representativeTarget()
if (chunk.modules.size > 1) { if (chunk.modules.size > 1) {
// We do not support circular dependencies, but if they are present, we do our best should not break the build, // We do not support circular dependencies, but if they are present, we do our best should not break the build,
// so we simply yield a warning and report NOTHING_DONE // so we simply yield a warning and report NOTHING_DONE
messageCollector.report( environment.messageCollector.report(
WARNING, WARNING,
"Circular dependencies are not supported. The following JS modules depend on each other: " "Circular dependencies are not supported. The following JS modules depend on each other: "
+ chunk.modules.map { it.name }.joinToString(", ") + ". " + chunk.modules.map { it.name }.joinToString(", ") + ". "
@@ -619,8 +619,8 @@ class KotlinBuilder : ModuleLevelBuilder(BuilderCategory.SOURCE_PROCESSOR) {
val k2JsArguments = JpsKotlinCompilerSettings.getK2JsCompilerArguments(representativeModule) val k2JsArguments = JpsKotlinCompilerSettings.getK2JsCompilerArguments(representativeModule)
val compilerRunner = JpsKotlinCompilerRunner() val compilerRunner = JpsKotlinCompilerRunner()
compilerRunner.runK2JsCompiler(commonArguments, k2JsArguments, compilerSettings, messageCollector, environment, outputItemCollector, sourceFiles, libraryFiles, outputFile) compilerRunner.runK2JsCompiler(commonArguments, k2JsArguments, compilerSettings, environment, sourceFiles, libraryFiles, outputFile)
return outputItemCollector return environment.outputItemsCollector
} }
private fun copyJsLibraryFilesIfNeeded(chunk: ModuleChunk, project: JpsProject) { private fun copyJsLibraryFilesIfNeeded(chunk: ModuleChunk, project: JpsProject) {
@@ -642,12 +642,10 @@ class KotlinBuilder : ModuleLevelBuilder(BuilderCategory.SOURCE_PROCESSOR) {
context: CompileContext, context: CompileContext,
dirtyFilesHolder: DirtyFilesHolder<JavaSourceRootDescriptor, ModuleBuildTarget>, dirtyFilesHolder: DirtyFilesHolder<JavaSourceRootDescriptor, ModuleBuildTarget>,
environment: JpsCompilerEnvironment, environment: JpsCompilerEnvironment,
filesToCompile: MultiMap<ModuleBuildTarget, File>, messageCollector: MessageCollectorAdapter filesToCompile: MultiMap<ModuleBuildTarget, File>
): OutputItemsCollectorImpl? { ): OutputItemsCollector? {
val outputItemCollector = OutputItemsCollectorImpl()
if (chunk.modules.size > 1) { if (chunk.modules.size > 1) {
messageCollector.report( environment.messageCollector.report(
WARNING, WARNING,
"Circular dependencies are only partially supported. The following modules depend on each other: " "Circular dependencies are only partially supported. The following modules depend on each other: "
+ chunk.modules.map { it.name }.joinToString(", ") + ". " + chunk.modules.map { it.name }.joinToString(", ") + ". "
@@ -686,10 +684,10 @@ class KotlinBuilder : ModuleLevelBuilder(BuilderCategory.SOURCE_PROCESSOR) {
+ " in " + filesToCompile.keySet().joinToString { it.presentableName }) + " in " + filesToCompile.keySet().joinToString { it.presentableName })
val compilerRunner = JpsKotlinCompilerRunner() val compilerRunner = JpsKotlinCompilerRunner()
compilerRunner.runK2JvmCompiler(commonArguments, k2JvmArguments, compilerSettings, messageCollector, environment, moduleFile, outputItemCollector) compilerRunner.runK2JvmCompiler(commonArguments, k2JvmArguments, compilerSettings, environment, moduleFile)
moduleFile.delete() moduleFile.delete()
return outputItemCollector return environment.outputItemsCollector
} }
class MessageCollectorAdapter(private val context: CompileContext) : MessageCollector { class MessageCollectorAdapter(private val context: CompileContext) : MessageCollector {