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[] arguments,
@NotNull JpsCompilerEnvironment environment,
@NotNull MessageCollector messageCollector,
@NotNull PrintStream out
) throws Exception {
File libPath = getLibPath(environment.getKotlinPaths(), messageCollector);
File libPath = getLibPath(environment.getKotlinPaths(), environment.getMessageCollector());
if (libPath == null) return null;
ClassLoader classLoader = getOrCreateClassLoader(environment, libPath);
@@ -27,8 +27,10 @@ import org.jetbrains.kotlin.utils.PathUtil
class JpsCompilerEnvironment(
val kotlinPaths: KotlinPaths,
services: Services,
val classesToLoadByParent: ClassCondition
) : CompilerEnvironment(services) {
val classesToLoadByParent: ClassCondition,
messageCollector: MessageCollector,
override val outputItemsCollector: OutputItemsCollectorImpl
) : CompilerEnvironment(services, messageCollector, outputItemsCollector) {
fun success(): Boolean {
return kotlinPaths.homePath.exists()
}
@@ -44,24 +44,20 @@ class JpsKotlinCompilerRunner : KotlinCompilerRunner<JpsCompilerEnvironment>() {
commonArguments: CommonCompilerArguments,
k2jvmArguments: K2JVMCompilerArguments,
compilerSettings: CompilerSettings,
messageCollector: MessageCollector,
environment: JpsCompilerEnvironment,
moduleFile: File,
collector: OutputItemsCollector
moduleFile: File
) {
val arguments = mergeBeans(commonArguments, k2jvmArguments)
setupK2JvmArguments(moduleFile, arguments)
runCompiler(K2JVM_COMPILER, arguments, compilerSettings.additionalArguments, messageCollector, collector, environment)
runCompiler(K2JVM_COMPILER, arguments, compilerSettings.additionalArguments, environment)
}
fun runK2JsCompiler(
commonArguments: CommonCompilerArguments,
k2jsArguments: K2JSCompilerArguments,
compilerSettings: CompilerSettings,
messageCollector: MessageCollector,
environment: JpsCompilerEnvironment,
collector: OutputItemsCollector,
sourceFiles: Collection<File>,
libraryFiles: List<String>,
outputFile: File
@@ -69,27 +65,25 @@ class JpsKotlinCompilerRunner : KotlinCompilerRunner<JpsCompilerEnvironment>() {
val arguments = mergeBeans(commonArguments, k2jsArguments)
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 {
messageCollector.report(CompilerMessageSeverity.INFO, "Using kotlin-home = " + environment.kotlinPaths.homePath, CompilerMessageLocation.NO_LOCATION)
override fun doRunCompiler(compilerClassName: String, argsArray: Array<String>, environment: JpsCompilerEnvironment): ExitCode {
environment.messageCollector.report(CompilerMessageSeverity.INFO, "Using kotlin-home = " + environment.kotlinPaths.homePath, CompilerMessageLocation.NO_LOCATION)
return if (isDaemonEnabled()) {
val daemonExitCode = compileWithDaemon(compilerClassName, argsArray, environment, messageCollector, collector)
daemonExitCode ?: fallbackCompileStrategy(argsArray, collector, compilerClassName, environment, messageCollector)
val daemonExitCode = compileWithDaemon(compilerClassName, argsArray, environment)
daemonExitCode ?: fallbackCompileStrategy(argsArray, compilerClassName, environment)
}
else {
fallbackCompileStrategy(argsArray, collector, compilerClassName, environment, messageCollector)
fallbackCompileStrategy(argsArray, compilerClassName, environment)
}
}
private fun fallbackCompileStrategy(
argsArray: Array<String>,
collector: OutputItemsCollector,
compilerClassName: String,
environment: JpsCompilerEnvironment,
messageCollector: MessageCollector
environment: JpsCompilerEnvironment
): ExitCode {
// otherwise fallback to in-process
log.info("Compile in-process")
@@ -103,12 +97,12 @@ class JpsKotlinCompilerRunner : KotlinCompilerRunner<JpsCompilerEnvironment>() {
if (System.getProperty(GlobalOptions.COMPILE_PARALLEL_OPTION, "false").toBoolean())
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,
// so we take it's contents through reflection
val exitCode = ExitCode.valueOf(getReturnCodeFromObject(rc))
processCompilerOutput(messageCollector, collector, stream, exitCode)
processCompilerOutput(environment, stream, exitCode)
return exitCode
}
@@ -140,14 +134,14 @@ class JpsKotlinCompilerRunner : KotlinCompilerRunner<JpsCompilerEnvironment>() {
}
@Synchronized
override fun getDaemonConnection(environment: JpsCompilerEnvironment, messageCollector: MessageCollector): DaemonConnection {
override fun getDaemonConnection(environment: JpsCompilerEnvironment): DaemonConnection {
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 flagFile = File.createTempFile("kotlin-compiler-jps-session-", "-is-running").apply {
deleteOnExit()
}
newDaemonConnection(compilerPath, messageCollector, flagFile)
newDaemonConnection(compilerPath, flagFile, environment)
}
return jpsDaemonConnection!!
}
@@ -205,7 +205,7 @@ class KotlinBuilder : ModuleLevelBuilder(BuilderCategory.SOURCE_PROCESSOR) {
val project = projectDescriptor.project
val lookupTracker = getLookupTracker(project)
val incrementalCaches = getIncrementalCaches(chunk, context)
val environment = createCompileEnvironment(incrementalCaches, lookupTracker, context)
val environment = createCompileEnvironment(incrementalCaches, lookupTracker, context, messageCollector)
if (!environment.success()) {
environment.reportErrorsTo(messageCollector)
return ABORT
@@ -238,7 +238,7 @@ class KotlinBuilder : ModuleLevelBuilder(BuilderCategory.SOURCE_PROCESSOR) {
LOG.info("Compiled successfully")
}
val generatedFiles = getGeneratedFiles(chunk, outputItemCollector)
val generatedFiles = getGeneratedFiles(chunk, environment.outputItemsCollector)
registerOutputItems(outputConsumer, generatedFiles)
saveVersions(context, chunk)
@@ -368,11 +368,11 @@ class KotlinBuilder : ModuleLevelBuilder(BuilderCategory.SOURCE_PROCESSOR) {
dirtyFilesHolder: DirtyFilesHolder<JavaSourceRootDescriptor, ModuleBuildTarget>, environment: JpsCompilerEnvironment,
filesToCompile: MultiMap<ModuleBuildTarget, File>, incrementalCaches: Map<ModuleBuildTarget, IncrementalCacheImpl<*>>,
messageCollector: MessageCollectorAdapter, project: JpsProject
): OutputItemsCollectorImpl? {
): OutputItemsCollector? {
if (JpsUtils.isJsKotlinModule(chunk.representativeTarget())) {
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()) {
@@ -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(
incrementalCaches: Map<ModuleBuildTarget, IncrementalCache>,
lookupTracker: LookupTracker,
context: CompileContext
context: CompileContext,
messageCollector: MessageCollectorAdapter
): JpsCompilerEnvironment {
val compilerServices = with(Services.Builder()) {
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.CompilationCanceledException"
|| className == "org.jetbrains.kotlin.modules.TargetId"
}
},
messageCollector,
OutputItemsCollectorImpl()
)
}
@@ -585,16 +588,13 @@ class KotlinBuilder : ModuleLevelBuilder(BuilderCategory.SOURCE_PROCESSOR) {
private fun compileToJs(chunk: ModuleChunk,
commonArguments: CommonCompilerArguments,
environment: JpsCompilerEnvironment,
messageCollector: MessageCollectorAdapter,
project: JpsProject
): OutputItemsCollectorImpl? {
val outputItemCollector = OutputItemsCollectorImpl()
): OutputItemsCollector? {
val representativeTarget = chunk.representativeTarget()
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,
// so we simply yield a warning and report NOTHING_DONE
messageCollector.report(
environment.messageCollector.report(
WARNING,
"Circular dependencies are not supported. The following JS modules depend on each other: "
+ chunk.modules.map { it.name }.joinToString(", ") + ". "
@@ -619,8 +619,8 @@ class KotlinBuilder : ModuleLevelBuilder(BuilderCategory.SOURCE_PROCESSOR) {
val k2JsArguments = JpsKotlinCompilerSettings.getK2JsCompilerArguments(representativeModule)
val compilerRunner = JpsKotlinCompilerRunner()
compilerRunner.runK2JsCompiler(commonArguments, k2JsArguments, compilerSettings, messageCollector, environment, outputItemCollector, sourceFiles, libraryFiles, outputFile)
return outputItemCollector
compilerRunner.runK2JsCompiler(commonArguments, k2JsArguments, compilerSettings, environment, sourceFiles, libraryFiles, outputFile)
return environment.outputItemsCollector
}
private fun copyJsLibraryFilesIfNeeded(chunk: ModuleChunk, project: JpsProject) {
@@ -642,12 +642,10 @@ class KotlinBuilder : ModuleLevelBuilder(BuilderCategory.SOURCE_PROCESSOR) {
context: CompileContext,
dirtyFilesHolder: DirtyFilesHolder<JavaSourceRootDescriptor, ModuleBuildTarget>,
environment: JpsCompilerEnvironment,
filesToCompile: MultiMap<ModuleBuildTarget, File>, messageCollector: MessageCollectorAdapter
): OutputItemsCollectorImpl? {
val outputItemCollector = OutputItemsCollectorImpl()
filesToCompile: MultiMap<ModuleBuildTarget, File>
): OutputItemsCollector? {
if (chunk.modules.size > 1) {
messageCollector.report(
environment.messageCollector.report(
WARNING,
"Circular dependencies are only partially supported. The following modules depend on each other: "
+ chunk.modules.map { it.name }.joinToString(", ") + ". "
@@ -686,10 +684,10 @@ class KotlinBuilder : ModuleLevelBuilder(BuilderCategory.SOURCE_PROCESSOR) {
+ " in " + filesToCompile.keySet().joinToString { it.presentableName })
val compilerRunner = JpsKotlinCompilerRunner()
compilerRunner.runK2JvmCompiler(commonArguments, k2JvmArguments, compilerSettings, messageCollector, environment, moduleFile, outputItemCollector)
compilerRunner.runK2JvmCompiler(commonArguments, k2JvmArguments, compilerSettings, environment, moduleFile)
moduleFile.delete()
return outputItemCollector
return environment.outputItemsCollector
}
class MessageCollectorAdapter(private val context: CompileContext) : MessageCollector {