[JPS][IC] Register lookups as usages in jps dependency graph
^KT-65429 In Progress Merge-request: KT-MR-14485 Merged-by: Evgenii Mazhukin <evgenii.mazhukin@jetbrains.com>
This commit is contained in:
committed by
Space Team
parent
85cfa252c3
commit
cc2dcf0078
@@ -13,6 +13,7 @@ import org.jetbrains.jps.builders.java.JavaBuilderUtil
|
||||
import org.jetbrains.jps.builders.java.dependencyView.Callbacks
|
||||
import org.jetbrains.jps.builders.java.dependencyView.Callbacks.Backend
|
||||
import org.jetbrains.jps.builders.storage.BuildDataPaths
|
||||
import org.jetbrains.jps.dependency.java.LookupNameUsage
|
||||
import org.jetbrains.jps.incremental.*
|
||||
import org.jetbrains.jps.model.java.JpsJavaExtensionService
|
||||
import org.jetbrains.jps.model.module.JpsSdkDependency
|
||||
@@ -23,6 +24,7 @@ import org.jetbrains.kotlin.build.JvmBuildMetaInfo
|
||||
import org.jetbrains.kotlin.build.JvmSourceRoot
|
||||
import org.jetbrains.kotlin.cli.common.arguments.CommonCompilerArguments
|
||||
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity
|
||||
import org.jetbrains.kotlin.cli.common.messages.MessageCollector
|
||||
import org.jetbrains.kotlin.compilerRunner.JpsCompilerEnvironment
|
||||
import org.jetbrains.kotlin.compilerRunner.JpsKotlinCompilerRunner
|
||||
import org.jetbrains.kotlin.config.IncrementalCompilation
|
||||
@@ -37,6 +39,7 @@ import org.jetbrains.kotlin.jps.incremental.JpsIncrementalJvmCache
|
||||
import org.jetbrains.kotlin.jps.model.k2JvmCompilerArguments
|
||||
import org.jetbrains.kotlin.jps.model.kotlinCompilerSettings
|
||||
import org.jetbrains.kotlin.jps.statistic.JpsBuilderMetricReporter
|
||||
import org.jetbrains.kotlin.jps.targets.impl.LookupUsageRegistrar
|
||||
import org.jetbrains.kotlin.load.kotlin.incremental.components.IncrementalCache
|
||||
import org.jetbrains.kotlin.load.kotlin.incremental.components.IncrementalCompilationComponents
|
||||
import org.jetbrains.kotlin.modules.KotlinModuleXmlBuilder
|
||||
@@ -385,7 +388,7 @@ class KotlinJvmModuleBuildTarget(kotlinContext: KotlinCompileContext, jpsModuleB
|
||||
if (!cache.isMultifileFacade(className)) return emptySet()
|
||||
|
||||
// In case of graph implementation of JPS
|
||||
if (previousMappings == null) return emptySet()
|
||||
if (KotlinBuilder.useDependencyGraph || previousMappings == null) return emptySet()
|
||||
|
||||
val name = previousMappings.getName(className.internalName)
|
||||
return previousMappings.getClassSources(name).toSet()
|
||||
@@ -413,6 +416,13 @@ class KotlinJvmModuleBuildTarget(kotlinContext: KotlinCompileContext, jpsModuleB
|
||||
)
|
||||
}
|
||||
}
|
||||
if (KotlinBuilder.useDependencyGraph) {
|
||||
LookupUsageRegistrar().processLookupTracker(
|
||||
environment.services[LookupTracker::class.java],
|
||||
callback,
|
||||
environment.messageCollector
|
||||
)
|
||||
}
|
||||
|
||||
val allCompiled = dirtyFilesHolder.allDirtyFiles
|
||||
JavaBuilderUtil.registerFilesToCompile(localContext, allCompiled)
|
||||
@@ -447,4 +457,4 @@ class KotlinJvmModuleBuildTarget(kotlinContext: KotlinCompileContext, jpsModuleB
|
||||
|
||||
callback.registerImports(output.outputClass.className.internalName, importedFqNames ?: listOf(), enumFqNameClasses ?: listOf())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
* Copyright 2010-2024 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.jps.targets.impl
|
||||
|
||||
import org.jetbrains.jps.builders.java.dependencyView.Callbacks.Backend
|
||||
import org.jetbrains.jps.dependency.java.LookupNameUsage
|
||||
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity
|
||||
import org.jetbrains.kotlin.cli.common.messages.MessageCollector
|
||||
import org.jetbrains.kotlin.incremental.LookupTrackerImpl
|
||||
import org.jetbrains.kotlin.incremental.components.LookupTracker
|
||||
import java.nio.file.Paths
|
||||
|
||||
class LookupUsageRegistrar {
|
||||
fun processLookupTracker(lookupTracker: LookupTracker?, callback: Backend, messageCollector: MessageCollector) {
|
||||
if (!checkRequiredJpsBuildApi()) {
|
||||
messageCollector.report(
|
||||
CompilerMessageSeverity.WARNING,
|
||||
"Can't register lookup usages with this version of JPS. $HOW_TO_FIX"
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
when (lookupTracker) {
|
||||
is LookupTrackerImpl -> registerLookupTrackerImplEntries(lookupTracker, callback)
|
||||
else -> {
|
||||
// could be DO_NOTHING tracker, TestLookupTracker, RemoteLookupTrackerClient - the last two are not visible, and the first one is irrelevant
|
||||
messageCollector.report(
|
||||
CompilerMessageSeverity.WARNING,
|
||||
"Can't register lookup usages with this compilation setup. lookupTracker is $lookupTracker. $HOW_TO_FIX"
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Kotlin plugin can be used with older versions of jps-build, so we check for the availability of APIs.
|
||||
private fun checkRequiredJpsBuildApi(): Boolean {
|
||||
try {
|
||||
Class.forName("org.jetbrains.jps.dependency.java.LookupNameUsage")
|
||||
} catch (_: Throwable) {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
private fun registerLookupTrackerImplEntries(lookupTracker: LookupTrackerImpl, callback: Backend) {
|
||||
for ((lookupKey, fileList) in lookupTracker.lookups.entrySet()) {
|
||||
val symbolOwner = lookupKey.scope.replace('.', '/')
|
||||
val symbolName = lookupKey.name
|
||||
val usage = LookupNameUsage(symbolOwner, symbolName)
|
||||
for (file in fileList) {
|
||||
callback.registerUsage(Paths.get(file), usage)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private companion object {
|
||||
// these branches should only be reachable if the jps is in the dependency graph mode
|
||||
private const val HOW_TO_FIX =
|
||||
"Kotlin incremental compilation might be incorrect. Consider using build option -Djps.use.dependency.graph=false"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user