From 9332bd3b97f712748640b75bd66004de9d794a4c Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Wed, 5 Jul 2017 20:51:02 +0300 Subject: [PATCH] Move JPS-related kotlin-home detection code to jps-plugin Original commit: 4b42f9e0718ced0116da47a72cc0f3ac1fb95593 --- .../compilerRunner/JpsCompilerEnvironment.kt | 16 ++----- .../kotlin/jps/build/KotlinBuilder.kt | 43 ++++++++++++++----- 2 files changed, 36 insertions(+), 23 deletions(-) diff --git a/jps/jps-plugin/src/org/jetbrains/kotlin/compilerRunner/JpsCompilerEnvironment.kt b/jps/jps-plugin/src/org/jetbrains/kotlin/compilerRunner/JpsCompilerEnvironment.kt index 9579bc1a1e1..58cbe194574 100644 --- a/jps/jps-plugin/src/org/jetbrains/kotlin/compilerRunner/JpsCompilerEnvironment.kt +++ b/jps/jps-plugin/src/org/jetbrains/kotlin/compilerRunner/JpsCompilerEnvironment.kt @@ -16,28 +16,18 @@ package org.jetbrains.kotlin.compilerRunner -import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity.ERROR import org.jetbrains.kotlin.cli.common.messages.MessageCollector import org.jetbrains.kotlin.config.Services import org.jetbrains.kotlin.preloading.ClassCondition import org.jetbrains.kotlin.utils.KotlinPaths -import org.jetbrains.kotlin.utils.PathUtil class JpsCompilerEnvironment( val kotlinPaths: KotlinPaths, services: Services, val classesToLoadByParent: ClassCondition, messageCollector: MessageCollector, - override val outputItemsCollector: OutputItemsCollectorImpl + outputItemsCollector: OutputItemsCollectorImpl ) : CompilerEnvironment(services, messageCollector, outputItemsCollector) { - fun success(): Boolean { - return kotlinPaths.homePath.exists() - } - - fun reportErrorsTo(messageCollector: MessageCollector) { - if (!kotlinPaths.homePath.exists()) { - messageCollector.report(ERROR, "Cannot find kotlinc home: " + kotlinPaths.homePath + ". Make sure plugin is properly installed, " + - "or specify " + PathUtil.JPS_KOTLIN_HOME_PROPERTY + " system property") - } - } + override val outputItemsCollector: OutputItemsCollectorImpl + get() = super.outputItemsCollector as OutputItemsCollectorImpl } diff --git a/jps/jps-plugin/src/org/jetbrains/kotlin/jps/build/KotlinBuilder.kt b/jps/jps-plugin/src/org/jetbrains/kotlin/jps/build/KotlinBuilder.kt index 3cfc091740a..54399c116b9 100644 --- a/jps/jps-plugin/src/org/jetbrains/kotlin/jps/build/KotlinBuilder.kt +++ b/jps/jps-plugin/src/org/jetbrains/kotlin/jps/build/KotlinBuilder.kt @@ -68,9 +68,7 @@ import org.jetbrains.kotlin.modules.TargetId import org.jetbrains.kotlin.preloading.ClassCondition import org.jetbrains.kotlin.progress.CompilationCanceledException import org.jetbrains.kotlin.progress.CompilationCanceledStatus -import org.jetbrains.kotlin.utils.JsLibraryUtils -import org.jetbrains.kotlin.utils.PathUtil -import org.jetbrains.kotlin.utils.keysToMap +import org.jetbrains.kotlin.utils.* import org.jetbrains.org.objectweb.asm.ClassReader import java.io.File import java.util.* @@ -82,6 +80,7 @@ class KotlinBuilder : ModuleLevelBuilder(BuilderCategory.SOURCE_PROCESSOR) { val LOG = Logger.getInstance("#org.jetbrains.kotlin.jps.build.KotlinBuilder") const val JVM_BUILD_META_INFO_FILE_NAME = "jvm-build-meta-info.txt" const val SKIP_CACHE_VERSION_CHECK_PROPERTY = "kotlin.jps.skip.cache.version.check" + const val JPS_KOTLIN_HOME_PROPERTY = "jps.kotlin.home" } private val statisticsLogger = TeamcityStatisticsLogger() @@ -243,11 +242,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, messageCollector) - if (!environment.success()) { - environment.reportErrorsTo(messageCollector) - return ABORT - } + val environment = createCompileEnvironment(incrementalCaches, lookupTracker, context, messageCollector) ?: return ABORT val commonArguments = compilerArgumentsForChunk(chunk).apply { reportOutputFiles = true @@ -458,7 +453,7 @@ class KotlinBuilder : ModuleLevelBuilder(BuilderCategory.SOURCE_PROCESSOR) { lookupTracker: LookupTracker, context: CompileContext, messageCollector: MessageCollectorAdapter - ): JpsCompilerEnvironment { + ): JpsCompilerEnvironment? { val compilerServices = with(Services.Builder()) { register(IncrementalCompilationComponents::class.java, IncrementalCompilationComponentsImpl(incrementalCaches.mapKeys { TargetId(it.key) }, @@ -471,8 +466,15 @@ class KotlinBuilder : ModuleLevelBuilder(BuilderCategory.SOURCE_PROCESSOR) { build() } + val paths = computeKotlinPathsForJpsPlugin() + if (paths == null || !paths.homePath.exists()) { + messageCollector.report(ERROR, "Cannot find kotlinc home. Make sure the plugin is properly installed, " + + "or specify $JPS_KOTLIN_HOME_PROPERTY system property") + return null + } + return JpsCompilerEnvironment( - PathUtil.kotlinPathsForJpsPluginOrJpsTests, + paths, compilerServices, ClassCondition { className -> className.startsWith("org.jetbrains.kotlin.load.kotlin.incremental.components.") @@ -488,6 +490,27 @@ class KotlinBuilder : ModuleLevelBuilder(BuilderCategory.SOURCE_PROCESSOR) { ) } + // When JPS is run on TeamCity, it can not rely on Kotlin plugin layout, + // so the path to Kotlin is specified in a system property + private fun computeKotlinPathsForJpsPlugin(): KotlinPaths? { + if (System.getProperty("kotlin.jps.tests").equals("true", ignoreCase = true)) { + return PathUtil.kotlinPathsForDistDirectory + } + + val jpsKotlinHome = System.getProperty(JPS_KOTLIN_HOME_PROPERTY) + if (jpsKotlinHome != null) { + return KotlinPathsFromHomeDir(File(jpsKotlinHome)) + } + + val jar = PathUtil.pathUtilJar.takeIf(File::exists) + if (jar?.name == "kotlin-jps-plugin.jar") { + val pluginHome = jar.parentFile.parentFile.parentFile + return KotlinPathsFromHomeDir(File(pluginHome, PathUtil.HOME_FOLDER_NAME)) + } + + return null + } + private fun getGeneratedFiles( chunk: ModuleChunk, outputItemCollector: OutputItemsCollectorImpl