Minimize usage of TargetId in IC
This commit is contained in:
@@ -47,10 +47,9 @@ import java.util.*
|
||||
|
||||
val KOTLIN_CACHE_DIRECTORY_NAME = "kotlin"
|
||||
|
||||
open class IncrementalCacheImpl<Target>(
|
||||
open class IncrementalCacheImpl(
|
||||
private val targetDataRoot: File,
|
||||
targetOutputDir: File?,
|
||||
target: Target
|
||||
targetOutputDir: File?
|
||||
) : IncrementalCacheCommon(File(targetDataRoot, KOTLIN_CACHE_DIRECTORY_NAME)), IncrementalCache {
|
||||
companion object {
|
||||
private val PROTO_MAP = "proto"
|
||||
@@ -77,7 +76,7 @@ open class IncrementalCacheImpl<Target>(
|
||||
// todo: try to use internal names only?
|
||||
private val internalNameToSource = registerMap(InternalNameToSourcesMap(INTERNAL_NAME_TO_SOURCE.storageFile))
|
||||
|
||||
private val outputDir by lazy(LazyThreadSafetyMode.NONE) { requireNotNull(targetOutputDir) { "Target is expected to have output directory: $target" } }
|
||||
private val outputDir by lazy(LazyThreadSafetyMode.NONE) { requireNotNull(targetOutputDir) { "Target is expected to have output directory" } }
|
||||
|
||||
protected open fun debugLog(message: String) {}
|
||||
|
||||
@@ -115,7 +114,7 @@ open class IncrementalCacheImpl<Target>(
|
||||
return CompilationResult.NO_CHANGES
|
||||
}
|
||||
|
||||
open fun saveFileToCache(generatedClass: GeneratedJvmClass<Target>): CompilationResult {
|
||||
open fun saveFileToCache(generatedClass: GeneratedJvmClass<*>): CompilationResult {
|
||||
val sourceFiles: Collection<File> = generatedClass.sourceFiles
|
||||
val kotlinClass: LocalFileKotlinClass = generatedClass.outputClass
|
||||
val className = kotlinClass.className
|
||||
|
||||
@@ -14,9 +14,6 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
// these functions are used in the kotlin gradle plugin
|
||||
@file:Suppress("unused")
|
||||
|
||||
package org.jetbrains.kotlin.incremental
|
||||
|
||||
import com.intellij.openapi.util.io.FileUtil
|
||||
@@ -25,8 +22,6 @@ import org.jetbrains.kotlin.build.GeneratedFile
|
||||
import org.jetbrains.kotlin.build.GeneratedJvmClass
|
||||
import org.jetbrains.kotlin.build.JvmSourceRoot
|
||||
import org.jetbrains.kotlin.build.isModuleMappingFile
|
||||
import org.jetbrains.kotlin.compilerRunner.OutputItemsCollectorImpl
|
||||
import org.jetbrains.kotlin.config.IncrementalCompilation
|
||||
import org.jetbrains.kotlin.config.Services
|
||||
import org.jetbrains.kotlin.incremental.components.LookupTracker
|
||||
import org.jetbrains.kotlin.load.kotlin.incremental.components.IncrementalCache
|
||||
@@ -35,14 +30,9 @@ import org.jetbrains.kotlin.modules.KotlinModuleXmlBuilder
|
||||
import org.jetbrains.kotlin.modules.TargetId
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import org.jetbrains.kotlin.progress.CompilationCanceledStatus
|
||||
import org.jetbrains.kotlin.utils.keysToMap
|
||||
import java.io.File
|
||||
import java.util.*
|
||||
|
||||
|
||||
fun Iterable<File>.javaSourceRoots(roots: Iterable<File>): Iterable<File> =
|
||||
filter(File::isJavaFile).mapNotNull { findSrcDirRoot(it, roots) }
|
||||
|
||||
fun makeModuleFile(
|
||||
name: String,
|
||||
isTest: Boolean,
|
||||
@@ -86,61 +76,22 @@ fun makeCompileServices(
|
||||
build()
|
||||
}
|
||||
|
||||
fun makeLookupTracker(parentLookupTracker: LookupTracker = LookupTracker.DO_NOTHING): LookupTracker =
|
||||
if (IncrementalCompilation.isEnabled()) LookupTrackerImpl(parentLookupTracker)
|
||||
else parentLookupTracker
|
||||
|
||||
fun<Target> makeIncrementalCachesMap(
|
||||
targets: Iterable<Target>,
|
||||
getDependencies: (Target) -> Iterable<Target>,
|
||||
getCache: (Target) -> IncrementalCacheImpl<Target>,
|
||||
getTargetId: Target.() -> TargetId
|
||||
): Map<TargetId, IncrementalCacheImpl<Target>>
|
||||
{
|
||||
val dependents = targets.keysToMap { hashSetOf<Target>() }
|
||||
val targetsWithDependents = targets.toHashSet()
|
||||
|
||||
for (target in targets) {
|
||||
for (dependency in getDependencies(target)) {
|
||||
if (dependency !in targets) continue
|
||||
|
||||
dependents[dependency]!!.add(target)
|
||||
targetsWithDependents.add(target)
|
||||
}
|
||||
}
|
||||
|
||||
val caches = targetsWithDependents.keysToMap { getCache(it) }
|
||||
|
||||
for ((target, cache) in caches) {
|
||||
dependents[target]?.forEach {
|
||||
cache.addDependentCache(caches[it]!!)
|
||||
}
|
||||
}
|
||||
|
||||
return caches.mapKeys { it.key.getTargetId() }
|
||||
}
|
||||
|
||||
fun<Target> updateIncrementalCaches(
|
||||
targets: Iterable<Target>,
|
||||
generatedFiles: List<GeneratedFile<Target>>,
|
||||
compiledWithErrors: Boolean,
|
||||
getIncrementalCache: (Target) -> IncrementalCacheImpl<Target>
|
||||
fun updateIncrementalCache(
|
||||
generatedFiles: List<GeneratedFile<*>>,
|
||||
cache: IncrementalCacheImpl,
|
||||
compiledWithErrors: Boolean
|
||||
): CompilationResult {
|
||||
|
||||
var changesInfo = CompilationResult.NO_CHANGES
|
||||
for (generatedFile in generatedFiles) {
|
||||
val ic = getIncrementalCache(generatedFile.target)
|
||||
when {
|
||||
generatedFile is GeneratedJvmClass<Target> -> changesInfo += ic.saveFileToCache(generatedFile)
|
||||
generatedFile.outputFile.isModuleMappingFile() -> changesInfo += ic.saveModuleMappingToCache(generatedFile.sourceFiles, generatedFile.outputFile)
|
||||
generatedFile is GeneratedJvmClass<*> -> changesInfo += cache.saveFileToCache(generatedFile)
|
||||
generatedFile.outputFile.isModuleMappingFile() -> changesInfo += cache.saveModuleMappingToCache(generatedFile.sourceFiles, generatedFile.outputFile)
|
||||
}
|
||||
}
|
||||
|
||||
if (!compiledWithErrors) {
|
||||
targets.forEach {
|
||||
val newChangesInfo = getIncrementalCache(it).clearCacheForRemovedClasses()
|
||||
changesInfo += newChangesInfo
|
||||
}
|
||||
val newChangesInfo = cache.clearCacheForRemovedClasses()
|
||||
changesInfo += newChangesInfo
|
||||
}
|
||||
|
||||
return changesInfo
|
||||
@@ -158,30 +109,6 @@ fun LookupStorage.update(
|
||||
addAll(lookupTracker.lookups.entrySet(), lookupTracker.pathInterner.values)
|
||||
}
|
||||
|
||||
fun<Target> OutputItemsCollectorImpl.generatedFiles(
|
||||
targets: Collection<Target>,
|
||||
representativeTarget: Target,
|
||||
getSources: (Target) -> Iterable<File>,
|
||||
getOutputDir: (Target) -> File?
|
||||
): List<GeneratedFile<Target>> {
|
||||
// If there's only one target, this map is empty: get() always returns null, and the representativeTarget will be used below
|
||||
val sourceToTarget =
|
||||
if (targets.size >1) targets.flatMap { target -> getSources(target).map { Pair(it, target) } }.toMap()
|
||||
else mapOf<File, Target>()
|
||||
|
||||
return outputs.map { outputItem ->
|
||||
val target =
|
||||
outputItem.sourceFiles.firstOrNull()?.let { sourceToTarget[it] } ?:
|
||||
targets.singleOrNull { getOutputDir(it)?.let { outputItem.outputFile.startsWith(it) } ?: false } ?:
|
||||
representativeTarget
|
||||
|
||||
when (outputItem.outputFile.extension) {
|
||||
"class" -> GeneratedJvmClass(target, outputItem.sourceFiles, outputItem.outputFile)
|
||||
else -> GeneratedFile(target, outputItem.sourceFiles, outputItem.outputFile)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
data class DirtyData(
|
||||
val dirtyLookupSymbols: Collection<LookupSymbol> = emptyList(),
|
||||
val dirtyClassesFqNames: Collection<FqName> = emptyList()
|
||||
@@ -262,9 +189,6 @@ fun mapClassesFqNamesToFiles(
|
||||
return dirtyFiles
|
||||
}
|
||||
|
||||
private fun findSrcDirRoot(file: File, roots: Iterable<File>): File? =
|
||||
roots.firstOrNull { FileUtil.isAncestor(it, file, false) }
|
||||
|
||||
fun withSubtypes(
|
||||
typeFqName: FqName,
|
||||
caches: Iterable<IncrementalCacheCommon>
|
||||
|
||||
Reference in New Issue
Block a user