Reformat jps-plugin module

Original commit: 455fe7fe61
This commit is contained in:
Alexey Tsvetkov
2018-03-27 16:30:07 +03:00
parent 78ee6ba693
commit d849d47e22
23 changed files with 380 additions and 335 deletions
@@ -25,7 +25,9 @@ import org.jetbrains.kotlin.load.java.NULLABLE_ANNOTATIONS
import java.util.* import java.util.*
internal class NullabilityAnnotationsTracker : AnnotationsChangeTracker() { internal class NullabilityAnnotationsTracker : AnnotationsChangeTracker() {
private val annotations = (NULLABLE_ANNOTATIONS + JAVAX_NONNULL_ANNOTATION + NOT_NULL_ANNOTATIONS).mapTo(HashSet()) { it.internalNameWithoutInnerClasses }.toTypedArray() private val annotations =
(NULLABLE_ANNOTATIONS + JAVAX_NONNULL_ANNOTATION + NOT_NULL_ANNOTATIONS).mapTo(HashSet()) { it.internalNameWithoutInnerClasses }
.toTypedArray()
override fun methodAnnotationsChanged( override fun methodAnnotationsChanged(
context: DependencyContext, context: DependencyContext,
@@ -48,7 +50,11 @@ internal class NullabilityAnnotationsTracker : AnnotationsChangeTracker() {
return handleNullAnnotationsChanges(context, field, annotationsDiff.addedOrRemoved()) return handleNullAnnotationsChanges(context, field, annotationsDiff.addedOrRemoved())
} }
private fun handleNullAnnotationsChanges(context: NamingContext, protoMember: ProtoMember, annotations: Sequence<TypeRepr.ClassType>): Set<Recompile> { private fun handleNullAnnotationsChanges(
context: NamingContext,
protoMember: ProtoMember,
annotations: Sequence<TypeRepr.ClassType>
): Set<Recompile> {
val nullabilityAnnotations = TIntHashSet(this.annotations.toIntArray { context.get(it) }) val nullabilityAnnotations = TIntHashSet(this.annotations.toIntArray { context.get(it) })
val changedNullAnnotation = annotations.firstOrNull { nullabilityAnnotations.contains(it.className) } val changedNullAnnotation = annotations.firstOrNull { nullabilityAnnotations.contains(it.className) }
@@ -68,6 +74,6 @@ internal class NullabilityAnnotationsTracker : AnnotationsChangeTracker() {
private fun <T> Difference.Specifier<T, Difference>.addedOrRemoved(): Sequence<T> = private fun <T> Difference.Specifier<T, Difference>.addedOrRemoved(): Sequence<T> =
added().asSequence() + removed().asSequence() added().asSequence() + removed().asSequence()
private inline fun <T> Array<T>.toIntArray(fn: (T)->Int): IntArray = private inline fun <T> Array<T>.toIntArray(fn: (T) -> Int): IntArray =
IntArray(size) { i -> fn(get(i)) } IntArray(size) { i -> fn(get(i)) }
} }
@@ -125,5 +125,4 @@ public class CompilerRunnerUtil {
return null; return null;
} }
} }
@@ -28,15 +28,18 @@ import java.io.Serializable
internal class JpsCompilerServicesFacadeImpl( internal class JpsCompilerServicesFacadeImpl(
private val env: JpsCompilerEnvironment, private val env: JpsCompilerEnvironment,
port: Int = SOCKET_ANY_FREE_PORT port: Int = SOCKET_ANY_FREE_PORT
) : CompilerCallbackServicesFacadeServer(env.services.get(IncrementalCompilationComponents::class.java), ) : CompilerCallbackServicesFacadeServer(
env.services.get(IncrementalCompilationComponents::class.java),
env.services.get(LookupTracker::class.java), env.services.get(LookupTracker::class.java),
env.services.get(CompilationCanceledStatus::class.java), env.services.get(CompilationCanceledStatus::class.java),
port), port
),
JpsCompilerServicesFacade { JpsCompilerServicesFacade {
override fun report(category: Int, severity: Int, message: String?, attachment: Serializable?) { override fun report(category: Int, severity: Int, message: String?, attachment: Serializable?) {
env.messageCollector.reportFromDaemon( env.messageCollector.reportFromDaemon(
{ outFile, srcFiles -> env.outputItemsCollector.add(srcFiles, outFile) }, { outFile, srcFiles -> env.outputItemsCollector.add(srcFiles, outFile) },
category, severity, message, attachment) category, severity, message, attachment
)
} }
} }
@@ -39,13 +39,12 @@ class JpsKotlinCompilerRunner : KotlinCompilerRunner<JpsCompilerEnvironment>() {
private var compilerSettings: CompilerSettings? = null private var compilerSettings: CompilerSettings? = null
private inline fun withCompilerSettings(settings: CompilerSettings, fn: ()->Unit) { private inline fun withCompilerSettings(settings: CompilerSettings, fn: () -> Unit) {
val old = compilerSettings val old = compilerSettings
try { try {
compilerSettings = settings compilerSettings = settings
fn() fn()
} } finally {
finally {
compilerSettings = old compilerSettings = old
} }
} }
@@ -55,14 +54,14 @@ class JpsKotlinCompilerRunner : KotlinCompilerRunner<JpsCompilerEnvironment>() {
private var _jpsCompileServiceSession: CompileServiceSession? = null private var _jpsCompileServiceSession: CompileServiceSession? = null
@Synchronized @Synchronized
private fun getOrCreateDaemonConnection(newConnection: ()-> CompileServiceSession?): CompileServiceSession? { private fun getOrCreateDaemonConnection(newConnection: () -> CompileServiceSession?): CompileServiceSession? {
// TODO: consider adding state "ping" to the daemon interface // TODO: consider adding state "ping" to the daemon interface
if (_jpsCompileServiceSession == null || _jpsCompileServiceSession!!.compileService.getDaemonOptions() !is CompileService.CallResult.Good<DaemonOptions>) { if (_jpsCompileServiceSession == null || _jpsCompileServiceSession!!.compileService.getDaemonOptions() !is CompileService.CallResult.Good<DaemonOptions>) {
_jpsCompileServiceSession?. let { _jpsCompileServiceSession?.let {
try { try {
it.compileService.releaseCompileSession(it.sessionId) it.compileService.releaseCompileSession(it.sessionId)
} catch (_: Throwable) {
} }
catch (_: Throwable) {}
} }
_jpsCompileServiceSession = newConnection() _jpsCompileServiceSession = newConnection()
} }
@@ -126,8 +125,7 @@ class JpsKotlinCompilerRunner : KotlinCompilerRunner<JpsCompilerEnvironment>() {
return if (isDaemonEnabled()) { return if (isDaemonEnabled()) {
val daemonExitCode = compileWithDaemon(compilerClassName, compilerArgs, environment) val daemonExitCode = compileWithDaemon(compilerClassName, compilerArgs, environment)
daemonExitCode ?: fallbackCompileStrategy(compilerArgs, compilerClassName, environment) daemonExitCode ?: fallbackCompileStrategy(compilerArgs, compilerClassName, environment)
} } else {
else {
fallbackCompileStrategy(compilerArgs, compilerClassName, environment) fallbackCompileStrategy(compilerArgs, compilerClassName, environment)
} }
} }
@@ -154,8 +152,15 @@ class JpsKotlinCompilerRunner : KotlinCompilerRunner<JpsCompilerEnvironment>() {
val (daemon, sessionId) = connection val (daemon, sessionId) = connection
val compilerMode = CompilerMode.JPS_COMPILER val compilerMode = CompilerMode.JPS_COMPILER
val verbose = compilerArgs.verbose val verbose = compilerArgs.verbose
val options = CompilationOptions(compilerMode, targetPlatform, reportCategories(verbose), reportSeverity(verbose), requestedCompilationResults = emptyArray()) val options = CompilationOptions(
val res = daemon.compile(sessionId, withAdditionalCompilerArgs(compilerArgs), options, JpsCompilerServicesFacadeImpl(environment), null) compilerMode,
targetPlatform,
reportCategories(verbose),
reportSeverity(verbose),
requestedCompilationResults = emptyArray()
)
val res =
daemon.compile(sessionId, withAdditionalCompilerArgs(compilerArgs), options, JpsCompilerServicesFacadeImpl(environment), null)
// TODO: consider implementing connection retry, instead of fallback here // TODO: consider implementing connection retry, instead of fallback here
return res.takeUnless { it is CompileService.CallResult.Dying }?.let { exitCodeFromProcessExitCode(it.get()) } return res.takeUnless { it is CompileService.CallResult.Dying }?.let { exitCodeFromProcessExitCode(it.get()) }
} }
@@ -170,8 +175,7 @@ class JpsKotlinCompilerRunner : KotlinCompilerRunner<JpsCompilerEnvironment>() {
val categories = val categories =
if (!verbose) { if (!verbose) {
arrayOf(ReportCategory.COMPILER_MESSAGE, ReportCategory.EXCEPTION) arrayOf(ReportCategory.COMPILER_MESSAGE, ReportCategory.EXCEPTION)
} } else {
else {
ReportCategory.values() ReportCategory.values()
} }
@@ -182,8 +186,7 @@ class JpsKotlinCompilerRunner : KotlinCompilerRunner<JpsCompilerEnvironment>() {
private fun reportSeverity(verbose: Boolean): Int = private fun reportSeverity(verbose: Boolean): Int =
if (!verbose) { if (!verbose) {
ReportSeverity.INFO.code ReportSeverity.INFO.code
} } else {
else {
ReportSeverity.DEBUG.code ReportSeverity.DEBUG.code
} }
@@ -227,7 +230,13 @@ class JpsKotlinCompilerRunner : KotlinCompilerRunner<JpsCompilerEnvironment>() {
} }
} }
private fun setupK2JsArguments(_outputFile: File, sourceFiles: Collection<File>, _libraries: List<String>, _friendModules: List<String>, settings: K2JSCompilerArguments) { private fun setupK2JsArguments(
_outputFile: File,
sourceFiles: Collection<File>,
_libraries: List<String>,
_friendModules: List<String>,
settings: K2JSCompilerArguments
) {
with(settings) { with(settings) {
noStdlib = true noStdlib = true
freeArgs = sourceFiles.map { it.path }.toMutableList() freeArgs = sourceFiles.map { it.path }.toMutableList()
@@ -249,7 +258,7 @@ class JpsKotlinCompilerRunner : KotlinCompilerRunner<JpsCompilerEnvironment>() {
val libPath = CompilerRunnerUtil.getLibPath(environment.kotlinPaths, environment.messageCollector) val libPath = CompilerRunnerUtil.getLibPath(environment.kotlinPaths, environment.messageCollector)
val compilerPath = File(libPath, "kotlin-compiler.jar") val compilerPath = File(libPath, "kotlin-compiler.jar")
val toolsJarPath = CompilerRunnerUtil.getJdkToolsJar() val toolsJarPath = CompilerRunnerUtil.getJdkToolsJar()
val compilerId = CompilerId.makeCompilerId(listOfNotNull(compilerPath, toolsJarPath) ) val compilerId = CompilerId.makeCompilerId(listOfNotNull(compilerPath, toolsJarPath))
val daemonOptions = configureDaemonOptions() val daemonOptions = configureDaemonOptions()
val clientFlagFile = KotlinCompilerClient.getOrCreateClientFlagFile(daemonOptions) val clientFlagFile = KotlinCompilerClient.getOrCreateClientFlagFile(daemonOptions)
@@ -53,8 +53,7 @@ class FSOperationsHelper(
if (recursively) { if (recursively) {
FSOperations.markDirtyRecursively(compileContext, CompilationRound.NEXT, chunk, ::shouldMark) FSOperations.markDirtyRecursively(compileContext, CompilationRound.NEXT, chunk, ::shouldMark)
} } else {
else {
FSOperations.markDirty(compileContext, CompilationRound.NEXT, chunk, ::shouldMark) FSOperations.markDirty(compileContext, CompilationRound.NEXT, chunk, ::shouldMark)
} }
} }
@@ -67,7 +66,7 @@ class FSOperationsHelper(
markFilesImpl(files) { it !in excludeFiles && it.exists() && moduleBasedFilter.accept(it) } markFilesImpl(files) { it !in excludeFiles && it.exists() && moduleBasedFilter.accept(it) }
} }
private inline fun markFilesImpl(files: Iterable<File>, shouldMark: (File)->Boolean) { private inline fun markFilesImpl(files: Iterable<File>, shouldMark: (File) -> Boolean) {
val filesToMark = files.filterTo(HashSet(), shouldMark) val filesToMark = files.filterTo(HashSet(), shouldMark)
if (filesToMark.isEmpty()) return if (filesToMark.isEmpty()) return
@@ -85,7 +84,7 @@ class FSOperationsHelper(
private class ModulesBasedFileFilter( private class ModulesBasedFileFilter(
private val context: CompileContext, private val context: CompileContext,
chunk: ModuleChunk chunk: ModuleChunk
): Mappings.DependentFilesFilter { ) : Mappings.DependentFilesFilter {
private val chunkTargets = chunk.targets private val chunkTargets = chunk.targets
private val buildRootIndex = context.projectDescriptor.buildRootIndex private val buildRootIndex = context.projectDescriptor.buildRootIndex
private val buildTargetIndex = context.projectDescriptor.buildTargetIndex private val buildTargetIndex = context.projectDescriptor.buildTargetIndex
@@ -70,24 +70,25 @@ object JpsJsModuleUtils {
@JvmStatic @JvmStatic
fun getOutputMetaFile(module: JpsModule, isTests: Boolean): File { fun getOutputMetaFile(module: JpsModule, isTests: Boolean): File {
val moduleBuildTarget = ModuleBuildTarget(module, if (isTests) JavaModuleBuildTargetType.TEST else JavaModuleBuildTargetType.PRODUCTION) val moduleBuildTarget =
ModuleBuildTarget(module, if (isTests) JavaModuleBuildTargetType.TEST else JavaModuleBuildTargetType.PRODUCTION)
val outputDir = KotlinBuilderModuleScriptGenerator.getOutputDirSafe(moduleBuildTarget) val outputDir = KotlinBuilderModuleScriptGenerator.getOutputDirSafe(moduleBuildTarget)
return getOutputMetaFile(outputDir, module.name, isTests) return getOutputMetaFile(outputDir, module.name, isTests)
} }
@JvmStatic @JvmStatic
fun getOutputFile(outputDir: File, moduleName: String, isTests: Boolean) fun getOutputFile(outputDir: File, moduleName: String, isTests: Boolean) =
= File(outputDir, moduleName + suffix(isTests) + KotlinJavascriptMetadataUtils.JS_EXT) File(outputDir, moduleName + suffix(isTests) + KotlinJavascriptMetadataUtils.JS_EXT)
@JvmStatic @JvmStatic
fun getOutputMetaFile(outputDir: File, moduleName: String, isTests: Boolean) fun getOutputMetaFile(outputDir: File, moduleName: String, isTests: Boolean) =
= File(outputDir, moduleName + suffix(isTests) + KotlinJavascriptMetadataUtils.META_JS_SUFFIX) File(outputDir, moduleName + suffix(isTests) + KotlinJavascriptMetadataUtils.META_JS_SUFFIX)
private fun suffix(isTests: Boolean) = if (isTests) "_test" else "" private fun suffix(isTests: Boolean) = if (isTests) "_test" else ""
} }
val JpsModule.hasProductionSourceRoot val JpsModule.hasProductionSourceRoot
get() = sourceRoots.any { it.rootType == JavaSourceRootType.SOURCE} get() = sourceRoots.any { it.rootType == JavaSourceRootType.SOURCE }
val JpsModule.hasTestSourceRoot val JpsModule.hasTestSourceRoot
get() = sourceRoots.any { it.rootType == JavaSourceRootType.TEST_SOURCE} get() = sourceRoots.any { it.rootType == JavaSourceRootType.TEST_SOURCE }
@@ -37,7 +37,9 @@ import java.util.concurrent.ConcurrentHashMap;
class JpsUtils { class JpsUtils {
private static final Map<ModuleBuildTarget, Boolean> IS_KOTLIN_JS_MODULE_CACHE = new ConcurrentHashMap<ModuleBuildTarget, Boolean>(); private static final Map<ModuleBuildTarget, Boolean> IS_KOTLIN_JS_MODULE_CACHE = new ConcurrentHashMap<ModuleBuildTarget, Boolean>();
private static final Map<String, Boolean> IS_KOTLIN_JS_STDLIB_JAR_CACHE = new ConcurrentHashMap<String, Boolean>(); private static final Map<String, Boolean> IS_KOTLIN_JS_STDLIB_JAR_CACHE = new ConcurrentHashMap<String, Boolean>();
private JpsUtils() {}
private JpsUtils() {
}
@NotNull @NotNull
static JpsJavaDependenciesEnumerator getAllDependencies(@NotNull ModuleBuildTarget target) { static JpsJavaDependenciesEnumerator getAllDependencies(@NotNull ModuleBuildTarget target) {
@@ -66,8 +68,12 @@ class JpsUtils {
Boolean cachedValue = IS_KOTLIN_JS_STDLIB_JAR_CACHE.get(url); Boolean cachedValue = IS_KOTLIN_JS_STDLIB_JAR_CACHE.get(url);
if (cachedValue != null) { if (cachedValue != null) {
if (cachedValue.booleanValue()) return true; if (cachedValue.booleanValue()) {
else continue; return true;
}
else {
continue;
}
} }
boolean isKotlinJavascriptStdLibrary = LibraryUtils.isKotlinJavascriptStdLibrary(JpsPathUtil.urlToFile(url)); boolean isKotlinJavascriptStdLibrary = LibraryUtils.isKotlinJavascriptStdLibrary(JpsPathUtil.urlToFile(url));
@@ -29,7 +29,6 @@ import org.jetbrains.jps.builders.java.JavaBuilderUtil
import org.jetbrains.jps.builders.java.JavaSourceRootDescriptor import org.jetbrains.jps.builders.java.JavaSourceRootDescriptor
import org.jetbrains.jps.incremental.* import org.jetbrains.jps.incremental.*
import org.jetbrains.jps.incremental.ModuleLevelBuilder.ExitCode.* import org.jetbrains.jps.incremental.ModuleLevelBuilder.ExitCode.*
import org.jetbrains.jps.incremental.fs.CompilationRound
import org.jetbrains.jps.incremental.java.JavaBuilder import org.jetbrains.jps.incremental.java.JavaBuilder
import org.jetbrains.jps.incremental.messages.BuildMessage import org.jetbrains.jps.incremental.messages.BuildMessage
import org.jetbrains.jps.incremental.messages.CompilerMessage import org.jetbrains.jps.incremental.messages.CompilerMessage
@@ -70,10 +69,11 @@ import java.io.File
import java.io.IOException import java.io.IOException
import java.net.URI import java.net.URI
import java.util.* import java.util.*
import kotlin.collections.HashSet
class KotlinBuilder : ModuleLevelBuilder(BuilderCategory.SOURCE_PROCESSOR) { class KotlinBuilder : ModuleLevelBuilder(BuilderCategory.SOURCE_PROCESSOR) {
companion object { companion object {
@JvmField val KOTLIN_BUILDER_NAME: String = "Kotlin Builder" const val KOTLIN_BUILDER_NAME: String = "Kotlin Builder"
val LOG = Logger.getInstance("#org.jetbrains.kotlin.jps.build.KotlinBuilder") 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 JVM_BUILD_META_INFO_FILE_NAME = "jvm-build-meta-info.txt"
@@ -176,16 +176,17 @@ class KotlinBuilder : ModuleLevelBuilder(BuilderCategory.SOURCE_PROCESSOR) {
val lastBuildMetaInfo = val lastBuildMetaInfo =
try { try {
JvmBuildMetaInfo.deserializeFromString(file.readText()) ?: continue JvmBuildMetaInfo.deserializeFromString(file.readText()) ?: continue
} } catch (e: Exception) {
catch (e: Exception) {
LOG.error("Could not deserialize jvm build meta info", e) LOG.error("Could not deserialize jvm build meta info", e)
continue continue
} }
val lastBuildLangVersion = LanguageVersion.fromVersionString(lastBuildMetaInfo.languageVersionString) val lastBuildLangVersion = LanguageVersion.fromVersionString(lastBuildMetaInfo.languageVersionString)
val lastBuildApiVersion = ApiVersion.parse(lastBuildMetaInfo.apiVersionString) val lastBuildApiVersion = ApiVersion.parse(lastBuildMetaInfo.apiVersionString)
val currentLangVersion = args.languageVersion?.let { LanguageVersion.fromVersionString(it) } ?: LanguageVersion.LATEST_STABLE val currentLangVersion =
val currentApiVersion = args.apiVersion?.let { ApiVersion.parse(it) } ?: ApiVersion.createByLanguageVersion(currentLangVersion) args.languageVersion?.let { LanguageVersion.fromVersionString(it) } ?: LanguageVersion.LATEST_STABLE
val currentApiVersion =
args.apiVersion?.let { ApiVersion.parse(it) } ?: ApiVersion.createByLanguageVersion(currentLangVersion)
val reasonToRebuild = when { val reasonToRebuild = when {
currentLangVersion != lastBuildLangVersion -> { currentLangVersion != lastBuildLangVersion -> {
@@ -235,18 +236,16 @@ class KotlinBuilder : ModuleLevelBuilder(BuilderCategory.SOURCE_PROCESSOR) {
val actualExitCode = if (proposedExitCode == OK && fsOperations.hasMarkedDirty) ADDITIONAL_PASS_REQUIRED else proposedExitCode val actualExitCode = if (proposedExitCode == OK && fsOperations.hasMarkedDirty) ADDITIONAL_PASS_REQUIRED else proposedExitCode
LOG.debug("Build result: " + actualExitCode) LOG.debug("Build result: $actualExitCode")
context.testingContext?.buildLogger?.buildFinished(actualExitCode) context.testingContext?.buildLogger?.buildFinished(actualExitCode)
return actualExitCode return actualExitCode
} } catch (e: StopBuildException) {
catch (e: StopBuildException) { LOG.info("Caught exception: $e")
LOG.info("Caught exception: " + e)
throw e throw e
} } catch (e: Throwable) {
catch (e: Throwable) { LOG.info("Caught exception: $e")
LOG.info("Caught exception: " + e)
MessageCollectorUtil.reportException(messageCollector, e) MessageCollectorUtil.reportException(messageCollector, e)
return ABORT return ABORT
} }
@@ -279,8 +278,7 @@ class KotlinBuilder : ModuleLevelBuilder(BuilderCategory.SOURCE_PROCESSOR) {
targets.forEach { rebuildAfterCacheVersionChanged[it] = true } targets.forEach { rebuildAfterCacheVersionChanged[it] = true }
return CHUNK_REBUILD_REQUIRED return CHUNK_REBUILD_REQUIRED
} }
} } else {
else {
if (isChunkRebuilding) { if (isChunkRebuilding) {
targets.forEach { hasKotlin[it] = false } targets.forEach { hasKotlin[it] = false }
} }
@@ -311,8 +309,10 @@ class KotlinBuilder : ModuleLevelBuilder(BuilderCategory.SOURCE_PROCESSOR) {
LOG.debug("Compiling files: ${filesToCompile.values()}") LOG.debug("Compiling files: ${filesToCompile.values()}")
val start = System.nanoTime() val start = System.nanoTime()
val outputItemCollector = doCompileModuleChunk(allCompiledFiles, chunk, commonArguments, context, dirtyFilesHolder, val outputItemCollector = doCompileModuleChunk(
environment, filesToCompile, incrementalCaches, project) allCompiledFiles, chunk, commonArguments, context, dirtyFilesHolder,
environment, filesToCompile, incrementalCaches, project
)
statisticsLogger.registerStatistic(chunk, System.nanoTime() - start) statisticsLogger.registerStatistic(chunk, System.nanoTime() - start)
@@ -324,8 +324,7 @@ class KotlinBuilder : ModuleLevelBuilder(BuilderCategory.SOURCE_PROCESSOR) {
if (compilationErrors) { if (compilationErrors) {
LOG.info("Compiled with errors") LOG.info("Compiled with errors")
return ABORT return ABORT
} } else {
else {
LOG.info("Compiled successfully") LOG.info("Compiled successfully")
} }
@@ -478,11 +477,15 @@ class KotlinBuilder : ModuleLevelBuilder(BuilderCategory.SOURCE_PROCESSOR) {
for (argumentProvider in ServiceLoader.load(KotlinJpsCompilerArgumentsProvider::class.java)) { for (argumentProvider in ServiceLoader.load(KotlinJpsCompilerArgumentsProvider::class.java)) {
// appending to pluginOptions // appending to pluginOptions
commonArguments.pluginOptions = concatenate(commonArguments.pluginOptions, commonArguments.pluginOptions = concatenate(
argumentProvider.getExtraArguments(representativeTarget, context)) commonArguments.pluginOptions,
argumentProvider.getExtraArguments(representativeTarget, context)
)
// appending to classpath // appending to classpath
commonArguments.pluginClasspaths = concatenate(commonArguments.pluginClasspaths, commonArguments.pluginClasspaths = concatenate(
argumentProvider.getClasspath(representativeTarget, context)) commonArguments.pluginClasspaths,
argumentProvider.getClasspath(representativeTarget, context)
)
LOG.debug("Plugin loaded: ${argumentProvider::class.java.simpleName}") LOG.debug("Plugin loaded: ${argumentProvider::class.java.simpleName}")
} }
@@ -511,8 +514,10 @@ class KotlinBuilder : ModuleLevelBuilder(BuilderCategory.SOURCE_PROCESSOR) {
): JpsCompilerEnvironment? { ): JpsCompilerEnvironment? {
val compilerServices = with(Services.Builder()) { val compilerServices = with(Services.Builder()) {
register(LookupTracker::class.java, lookupTracker) register(LookupTracker::class.java, lookupTracker)
register(IncrementalCompilationComponents::class.java, register(
IncrementalCompilationComponentsImpl(incrementalCaches.mapKeys { TargetId(it.key) })) IncrementalCompilationComponents::class.java,
IncrementalCompilationComponentsImpl(incrementalCaches.mapKeys { TargetId(it.key) })
)
register(CompilationCanceledStatus::class.java, object : CompilationCanceledStatus { register(CompilationCanceledStatus::class.java, object : CompilationCanceledStatus {
override fun checkCanceled() { override fun checkCanceled() {
if (context.cancelStatus.isCanceled) throw CompilationCanceledException() if (context.cancelStatus.isCanceled) throw CompilationCanceledException()
@@ -523,8 +528,10 @@ class KotlinBuilder : ModuleLevelBuilder(BuilderCategory.SOURCE_PROCESSOR) {
val paths = computeKotlinPathsForJpsPlugin() val paths = computeKotlinPathsForJpsPlugin()
if (paths == null || !paths.homePath.exists()) { if (paths == null || !paths.homePath.exists()) {
messageCollector.report(ERROR, "Cannot find kotlinc home. Make sure the plugin is properly installed, " + messageCollector.report(
"or specify $JPS_KOTLIN_HOME_PROPERTY system property") ERROR, "Cannot find kotlinc home. Make sure the plugin is properly installed, " +
"or specify $JPS_KOTLIN_HOME_PROPERTY system property"
)
return null return null
} }
@@ -567,16 +574,20 @@ class KotlinBuilder : ModuleLevelBuilder(BuilderCategory.SOURCE_PROCESSOR) {
if (chunk.targets.size > 1) { if (chunk.targets.size > 1) {
for (target in chunk.targets) { for (target in chunk.targets) {
for (file in KotlinSourceFileCollector.getAllKotlinSourceFiles(target)) { for (file in KotlinSourceFileCollector.getAllKotlinSourceFiles(target)) {
sourceToTarget.put(file, target) sourceToTarget[file] = target
} }
} }
} }
val representativeTarget = chunk.representativeTarget() val representativeTarget = chunk.representativeTarget()
fun SimpleOutputItem.target() = fun SimpleOutputItem.target() =
sourceFiles.firstOrNull()?.let { sourceToTarget[it] } ?: sourceFiles.firstOrNull()?.let { sourceToTarget[it] } ?: chunk.targets.singleOrNull {
chunk.targets.singleOrNull { it.outputDir?.let { outputFile.startsWith(it) } ?: false } ?: it.outputDir?.let {
representativeTarget outputFile.startsWith(
it
)
} ?: false
} ?: representativeTarget
return outputItemCollector.outputs.groupBy(SimpleOutputItem::target, SimpleOutputItem::toGeneratedFile) return outputItemCollector.outputs.groupBy(SimpleOutputItem::target, SimpleOutputItem::toGeneratedFile)
} }
@@ -646,7 +657,8 @@ class KotlinBuilder : ModuleLevelBuilder(BuilderCategory.SOURCE_PROCESSOR) {
dirtyFilesHolder: DirtyFilesHolder<JavaSourceRootDescriptor, ModuleBuildTarget>, dirtyFilesHolder: DirtyFilesHolder<JavaSourceRootDescriptor, ModuleBuildTarget>,
filesToCompile: MultiMap<ModuleBuildTarget, File> filesToCompile: MultiMap<ModuleBuildTarget, File>
) { ) {
if (lookupTracker !is LookupTrackerImpl) throw AssertionError("Lookup tracker is expected to be LookupTrackerImpl, got ${lookupTracker::class.java}") if (lookupTracker !is LookupTrackerImpl)
throw AssertionError("Lookup tracker is expected to be LookupTrackerImpl, got ${lookupTracker::class.java}")
val removedFiles = chunk.targets.flatMap { KotlinSourceFileCollector.getRemovedKotlinFiles(dirtyFilesHolder, it) } val removedFiles = chunk.targets.flatMap { KotlinSourceFileCollector.getRemovedKotlinFiles(dirtyFilesHolder, it) }
dataManager.withLookupStorage { lookupStorage -> dataManager.withLookupStorage { lookupStorage ->
@@ -656,7 +668,8 @@ class KotlinBuilder : ModuleLevelBuilder(BuilderCategory.SOURCE_PROCESSOR) {
} }
// if null is returned, nothing was done // if null is returned, nothing was done
private fun compileToJs(chunk: ModuleChunk, private fun compileToJs(
chunk: ModuleChunk,
commonArguments: CommonCompilerArguments, commonArguments: CommonCompilerArguments,
environment: JpsCompilerEnvironment, environment: JpsCompilerEnvironment,
project: JpsProject project: JpsProject
@@ -698,8 +711,7 @@ class KotlinBuilder : ModuleLevelBuilder(BuilderCategory.SOURCE_PROCESSOR) {
// If prefix is not specified (empty) in UI, we want to produce paths relative to source maps location // If prefix is not specified (empty) in UI, we want to produce paths relative to source maps location
val sourceRoots = if (k2JsArguments.sourceMapPrefix.isNullOrBlank()) { val sourceRoots = if (k2JsArguments.sourceMapPrefix.isNullOrBlank()) {
emptyList() emptyList()
} } else {
else {
representativeModule.contentRootsList.urls representativeModule.contentRootsList.urls
.map { URI.create(it) } .map { URI.create(it) }
.filter { it.scheme == "file" } .filter { it.scheme == "file" }
@@ -712,8 +724,10 @@ class KotlinBuilder : ModuleLevelBuilder(BuilderCategory.SOURCE_PROCESSOR) {
} }
val compilerRunner = JpsKotlinCompilerRunner() val compilerRunner = JpsKotlinCompilerRunner()
compilerRunner.runK2JsCompiler(commonArguments, k2JsArguments, compilerSettings, environment, sourceFiles, sourceRoots, compilerRunner.runK2JsCompiler(
libraries, friendPaths, outputFile) commonArguments, k2JsArguments, compilerSettings, environment, sourceFiles, sourceRoots,
libraries, friendPaths, outputFile
)
return environment.outputItemsCollector return environment.outputItemsCollector
} }
@@ -726,13 +740,16 @@ class KotlinBuilder : ModuleLevelBuilder(BuilderCategory.SOURCE_PROCESSOR) {
val outputLibraryRuntimeDirectory = File(outputDir, compilerSettings.outputDirectoryForJsLibraryFiles).absolutePath val outputLibraryRuntimeDirectory = File(outputDir, compilerSettings.outputDirectoryForJsLibraryFiles).absolutePath
val libraryFilesToCopy = arrayListOf<String>() val libraryFilesToCopy = arrayListOf<String>()
JpsJsModuleUtils.getLibraryFiles(representativeTarget, libraryFilesToCopy) JpsJsModuleUtils.getLibraryFiles(representativeTarget, libraryFilesToCopy)
JsLibraryUtils.copyJsFilesFromLibraries(libraryFilesToCopy, outputLibraryRuntimeDirectory, JsLibraryUtils.copyJsFilesFromLibraries(
copySourceMap = k2jsCompilerSettings.sourceMap) libraryFilesToCopy, outputLibraryRuntimeDirectory,
copySourceMap = k2jsCompilerSettings.sourceMap
)
} }
} }
// if null is returned, nothing was done // if null is returned, nothing was done
private fun compileToJvm(allCompiledFiles: MutableSet<File>, private fun compileToJvm(
allCompiledFiles: MutableSet<File>,
chunk: ModuleChunk, chunk: ModuleChunk,
commonArguments: CommonCompilerArguments, commonArguments: CommonCompilerArguments,
context: CompileContext, context: CompileContext,
@@ -763,7 +780,8 @@ class KotlinBuilder : ModuleLevelBuilder(BuilderCategory.SOURCE_PROCESSOR) {
} }
} }
val moduleFile = KotlinBuilderModuleScriptGenerator.generateModuleDescription(context, chunk, filesToCompile, totalRemovedFiles != 0) val moduleFile =
KotlinBuilderModuleScriptGenerator.generateModuleDescription(context, chunk, filesToCompile, totalRemovedFiles != 0)
if (moduleFile == null) { if (moduleFile == null) {
KotlinBuilder.LOG.debug("Not compiling, because no files affected: " + filesToCompile.keySet().joinToString { it.presentableName }) KotlinBuilder.LOG.debug("Not compiling, because no files affected: " + filesToCompile.keySet().joinToString { it.presentableName })
// No Kotlin sources found // No Kotlin sources found
@@ -781,8 +799,7 @@ class KotlinBuilder : ModuleLevelBuilder(BuilderCategory.SOURCE_PROCESSOR) {
try { try {
val compilerRunner = JpsKotlinCompilerRunner() val compilerRunner = JpsKotlinCompilerRunner()
compilerRunner.runK2JvmCompiler(commonArguments, k2JvmArguments, compilerSettings, environment, moduleFile) compilerRunner.runK2JvmCompiler(commonArguments, k2JvmArguments, compilerSettings, environment, moduleFile)
} } finally {
finally {
if (System.getProperty("kotlin.jps.delete.module.file.after.build") != "false") { if (System.getProperty("kotlin.jps.delete.module.file.after.build") != "false") {
moduleFile.delete() moduleFile.delete()
} }
@@ -802,7 +819,8 @@ class KotlinBuilder : ModuleLevelBuilder(BuilderCategory.SOURCE_PROCESSOR) {
} }
val kind = kind(severity) val kind = kind(severity)
if (kind != null) { if (kind != null) {
context.processMessage(CompilerMessage( context.processMessage(
CompilerMessage(
CompilerRunnerConstants.KOTLIN_COMPILER_NAME, CompilerRunnerConstants.KOTLIN_COMPILER_NAME,
kind, kind,
prefix + message, prefix + message,
@@ -810,9 +828,9 @@ class KotlinBuilder : ModuleLevelBuilder(BuilderCategory.SOURCE_PROCESSOR) {
-1, -1, -1, -1, -1, -1,
location?.line?.toLong() ?: -1, location?.line?.toLong() ?: -1,
location?.column?.toLong() ?: -1 location?.column?.toLong() ?: -1
)) )
} )
else { } else {
val path = if (location != null) "${location.path}:${location.line}:${location.column}: " else "" val path = if (location != null) "${location.path}:${location.line}:${location.column}: " else ""
KotlinBuilder.LOG.debug(path + message) KotlinBuilder.LOG.debug(path + message)
} }
@@ -830,7 +848,7 @@ class KotlinBuilder : ModuleLevelBuilder(BuilderCategory.SOURCE_PROCESSOR) {
ERROR, EXCEPTION -> BuildMessage.Kind.ERROR ERROR, EXCEPTION -> BuildMessage.Kind.ERROR
WARNING, STRONG_WARNING -> BuildMessage.Kind.WARNING WARNING, STRONG_WARNING -> BuildMessage.Kind.WARNING
LOGGING -> null LOGGING -> null
else -> throw IllegalArgumentException("Unsupported severity: " + severity) else -> throw IllegalArgumentException("Unsupported severity: $severity")
} }
} }
} }
@@ -926,6 +944,7 @@ private fun getDependenciesRecursively(module: JpsModule, kind: JpsJavaClasspath
// TODO: investigate thread safety // TODO: investigate thread safety
private val ALL_COMPILED_FILES_KEY = Key.create<MutableSet<File>>("_all_kotlin_compiled_files_") private val ALL_COMPILED_FILES_KEY = Key.create<MutableSet<File>>("_all_kotlin_compiled_files_")
private fun getAllCompiledFilesContainer(context: CompileContext): MutableSet<File> { private fun getAllCompiledFilesContainer(context: CompileContext): MutableSet<File> {
var allCompiledFiles = ALL_COMPILED_FILES_KEY.get(context) var allCompiledFiles = ALL_COMPILED_FILES_KEY.get(context)
if (allCompiledFiles == null) { if (allCompiledFiles == null) {
@@ -937,10 +956,11 @@ private fun getAllCompiledFilesContainer(context: CompileContext): MutableSet<Fi
// TODO: investigate thread safety // TODO: investigate thread safety
private val PROCESSED_TARGETS_WITH_REMOVED_FILES = Key.create<MutableSet<ModuleBuildTarget>>("_processed_targets_with_removed_files_") private val PROCESSED_TARGETS_WITH_REMOVED_FILES = Key.create<MutableSet<ModuleBuildTarget>>("_processed_targets_with_removed_files_")
private fun getProcessedTargetsWithRemovedFilesContainer(context: CompileContext): MutableSet<ModuleBuildTarget> { private fun getProcessedTargetsWithRemovedFilesContainer(context: CompileContext): MutableSet<ModuleBuildTarget> {
var set = PROCESSED_TARGETS_WITH_REMOVED_FILES.get(context) var set = PROCESSED_TARGETS_WITH_REMOVED_FILES.get(context)
if (set == null) { if (set == null) {
set = HashSet<ModuleBuildTarget>() set = HashSet()
PROCESSED_TARGETS_WITH_REMOVED_FILES.set(context, set) PROCESSED_TARGETS_WITH_REMOVED_FILES.set(context, set)
} }
return set return set
@@ -62,10 +62,10 @@ object KotlinBuilderModuleScriptGenerator {
val getProductionModuleMethod: Method val getProductionModuleMethod: Method
try { try {
getTestModulePropertiesMethod = JpsJavaExtensionService::class.java.getDeclaredMethod("getTestModuleProperties", JpsModule::class.java) getTestModulePropertiesMethod =
JpsJavaExtensionService::class.java.getDeclaredMethod("getTestModuleProperties", JpsModule::class.java)
getProductionModuleMethod = klass.getDeclaredMethod("getProductionModule") getProductionModuleMethod = klass.getDeclaredMethod("getProductionModule")
} } catch (e: NoSuchMethodException) {
catch (e: NoSuchMethodException) {
return@run alwaysNull() return@run alwaysNull()
} }
@@ -100,7 +100,8 @@ object KotlinBuilderModuleScriptGenerator {
if (IncrementalCompilation.isEnabled()) if (IncrementalCompilation.isEnabled())
sourceFiles.get(target) sourceFiles.get(target)
else else
KotlinSourceFileCollector.getAllKotlinSourceFiles(target)) KotlinSourceFileCollector.getAllKotlinSourceFiles(target)
)
if (moduleSources.size > 0 || hasRemovedFiles) { if (moduleSources.size > 0 || hasRemovedFiles) {
noSources = false noSources = false
@@ -124,7 +125,8 @@ object KotlinBuilderModuleScriptGenerator {
(targetType as JavaModuleBuildTargetType).isTests, (targetType as JavaModuleBuildTargetType).isTests,
// this excludes the output directories from the class path, to be removed for true incremental compilation // this excludes the output directories from the class path, to be removed for true incremental compilation
outputDirs, outputDirs,
friendDirs) friendDirs
)
} }
if (noSources) return null if (noSources) return null
@@ -144,14 +146,12 @@ object KotlinBuilderModuleScriptGenerator {
val dir = System.getProperty("kotlin.jps.dir.for.module.files")?.let { File(it) }?.takeIf { it.isDirectory } val dir = System.getProperty("kotlin.jps.dir.for.module.files")?.let { File(it) }?.takeIf { it.isDirectory }
return try { return try {
File.createTempFile("kjps", readableSuffix + ".script.xml", dir) File.createTempFile("kjps", readableSuffix + ".script.xml", dir)
} } catch (e: IOException) {
catch (e: IOException) {
// sometimes files cannot be created, because file name is too long (Windows, Mac OS) // sometimes files cannot be created, because file name is too long (Windows, Mac OS)
// see https://bugs.openjdk.java.net/browse/JDK-8148023 // see https://bugs.openjdk.java.net/browse/JDK-8148023
try { try {
File.createTempFile("kjps", ".script.xml", dir) File.createTempFile("kjps", ".script.xml", dir)
} } catch (e: IOException) {
catch (e: IOException) {
val message = buildString { val message = buildString {
append("Could not create module file when building chunk $chunk") append("Could not create module file when building chunk $chunk")
if (dir != null) { if (dir != null) {
@@ -42,8 +42,7 @@ public class KotlinSourceFileCollector {
// For incremental compilation // For incremental compilation
@NotNull @NotNull
public static MultiMap<ModuleBuildTarget, File> getDirtySourceFiles(DirtyFilesHolder<JavaSourceRootDescriptor, ModuleBuildTarget> dirtyFilesHolder) public static MultiMap<ModuleBuildTarget, File> getDirtySourceFiles(DirtyFilesHolder<JavaSourceRootDescriptor, ModuleBuildTarget> dirtyFilesHolder)
throws IOException throws IOException {
{
final MultiMap<ModuleBuildTarget, File> result = new MultiMap<ModuleBuildTarget, File>(); final MultiMap<ModuleBuildTarget, File> result = new MultiMap<ModuleBuildTarget, File>();
dirtyFilesHolder.processDirtyFiles(new FileProcessor<JavaSourceRootDescriptor, ModuleBuildTarget>() { dirtyFilesHolder.processDirtyFiles(new FileProcessor<JavaSourceRootDescriptor, ModuleBuildTarget>() {
@@ -84,7 +83,8 @@ public class KotlinSourceFileCollector {
@NotNull @NotNull
public static List<File> getAllKotlinSourceFiles(@NotNull ModuleBuildTarget target) { public static List<File> getAllKotlinSourceFiles(@NotNull ModuleBuildTarget target) {
final List<File> moduleExcludes = ContainerUtil.map(target.getModule().getExcludeRootsList().getUrls(), new Function<String, File>() { final List<File> moduleExcludes =
ContainerUtil.map(target.getModule().getExcludeRootsList().getUrls(), new Function<String, File>() {
@Override @Override
public File fun(String url) { public File fun(String url) {
return JpsPathUtil.urlToFile(url); return JpsPathUtil.urlToFile(url);
@@ -92,7 +92,8 @@ public class KotlinSourceFileCollector {
}); });
final JpsCompilerExcludes compilerExcludes = final JpsCompilerExcludes compilerExcludes =
JpsJavaExtensionService.getInstance().getOrCreateCompilerConfiguration(target.getModule().getProject()).getCompilerExcludes(); JpsJavaExtensionService.getInstance().getOrCreateCompilerConfiguration(target.getModule().getProject())
.getCompilerExcludes();
final List<File> result = ContainerUtil.newArrayList(); final List<File> result = ContainerUtil.newArrayList();
for (JpsModuleSourceRoot sourceRoot : getRelevantSourceRoots(target)) { for (JpsModuleSourceRoot sourceRoot : getRelevantSourceRoots(target)) {
@@ -135,5 +136,6 @@ public class KotlinSourceFileCollector {
return FileUtilRt.extensionEquals(file.getName(), "kt"); return FileUtilRt.extensionEquals(file.getName(), "kt");
} }
private KotlinSourceFileCollector() {} private KotlinSourceFileCollector() {
}
} }
@@ -57,4 +57,5 @@ abstract class MarkerFile(private val fileName: String, private val paths: Build
} }
class HasKotlinMarker(dataManager: BuildDataManager) : MarkerFile(HAS_KOTLIN_MARKER_FILE_NAME, dataManager.dataPaths) class HasKotlinMarker(dataManager: BuildDataManager) : MarkerFile(HAS_KOTLIN_MARKER_FILE_NAME, dataManager.dataPaths)
class RebuildAfterCacheVersionChangeMarker(dataManager: BuildDataManager) : MarkerFile(REBUILD_AFTER_CACHE_VERSION_CHANGE_MARKER, dataManager.dataPaths) class RebuildAfterCacheVersionChangeMarker(dataManager: BuildDataManager) :
MarkerFile(REBUILD_AFTER_CACHE_VERSION_CHANGE_MARKER, dataManager.dataPaths)
@@ -47,8 +47,7 @@ class TeamcityStatisticsLogger {
val escChar = escapedChar(c) val escChar = escapedChar(c)
if (escChar == 0.toChar()) { if (escChar == 0.toChar()) {
escaped.append(c) escaped.append(c)
} } else {
else {
escaped.append('|').append(escChar) escaped.append('|').append(escChar)
} }
} }
@@ -21,7 +21,7 @@ import com.intellij.openapi.util.io.FileUtil
class PathFunctionPair( class PathFunctionPair(
val path: String, val path: String,
val function: String val function: String
): Comparable<PathFunctionPair> { ) : Comparable<PathFunctionPair> {
override fun compareTo(other: PathFunctionPair): Int { override fun compareTo(other: PathFunctionPair): Int {
val pathComp = FileUtil.comparePaths(path, other.path) val pathComp = FileUtil.comparePaths(path, other.path)
@@ -26,7 +26,7 @@ import org.jetbrains.kotlin.config.SettingConstants
abstract class BaseJpsCompilerSettingsSerializer<T : Any>( abstract class BaseJpsCompilerSettingsSerializer<T : Any>(
componentName: String, componentName: String,
private val settingsFactory: () -> T private val settingsFactory: () -> T
): JpsProjectExtensionSerializer(SettingConstants.KOTLIN_COMPILER_SETTINGS_FILE, componentName) { ) : JpsProjectExtensionSerializer(SettingConstants.KOTLIN_COMPILER_SETTINGS_FILE, componentName) {
protected abstract fun onLoad(project: JpsProject, settings: T) protected abstract fun onLoad(project: JpsProject, settings: T)
override fun loadExtension(project: JpsProject, componentTag: Element) { override fun loadExtension(project: JpsProject, componentTag: Element) {