Refactoring: return removed dirty files from calculateSourcesToCompile
This commit is contained in:
+32
-34
@@ -252,7 +252,11 @@ open class KotlinCompile : AbstractKotlinCompile<K2JVMCompilerArguments>(), Kotl
|
|||||||
is ChangesEither.Unknown -> return rebuild("Could not get changes for java files")
|
is ChangesEither.Unknown -> return rebuild("Could not get changes for java files")
|
||||||
}
|
}
|
||||||
|
|
||||||
val dirtyFiles = changedFiles.modified.filter(File::isKotlinFile).toMutableSet()
|
val dirtyFiles = HashSet<File>(with(changedFiles) { modified.size + removed.size })
|
||||||
|
with(changedFiles) {
|
||||||
|
modified.asSequence() + removed.asSequence()
|
||||||
|
}.forEach { if (it.isKotlinFile()) dirtyFiles.add(it) }
|
||||||
|
|
||||||
val lookupSymbols = HashSet<LookupSymbol>()
|
val lookupSymbols = HashSet<LookupSymbol>()
|
||||||
lookupSymbols.addAll(affectedJavaSymbols)
|
lookupSymbols.addAll(affectedJavaSymbols)
|
||||||
lookupSymbols.addAll(classpathChanges.lookupSymbols)
|
lookupSymbols.addAll(classpathChanges.lookupSymbols)
|
||||||
@@ -295,16 +299,13 @@ open class KotlinCompile : AbstractKotlinCompile<K2JVMCompilerArguments>(), Kotl
|
|||||||
logger.kotlinDebug { "Last Kotlin Build info for task $path -- $lastBuildInfo" }
|
logger.kotlinDebug { "Last Kotlin Build info for task $path -- $lastBuildInfo" }
|
||||||
val caches = IncrementalCachesManager(targetId, cacheDirectory, outputDir)
|
val caches = IncrementalCachesManager(targetId, cacheDirectory, outputDir)
|
||||||
|
|
||||||
// TODO: decide what to do if no files are considered dirty - rebuild or skip the module
|
val (dirtyKotlinSources, isIncrementalDecided) = calculateSourcesToCompile(javaFilesProcessor, caches, lastBuildInfo, changedFiles)
|
||||||
var (sourcesToCompile, isIncrementalDecided) = calculateSourcesToCompile(javaFilesProcessor, caches, lastBuildInfo, changedFiles)
|
compileIncrementally(args, caches, isIncrementalDecided, javaFilesProcessor, logAction, lookupTracker, outputDir, relativePathOrCanonical, allKotlinSources, dirtyKotlinSources, targetId)
|
||||||
val currentRemoved = (changedFiles as? ChangedFiles.Known)?.removed?.filter(File::isKotlinFile) ?: emptyList()
|
|
||||||
compileIncrementally(args, caches, currentRemoved, isIncrementalDecided, javaFilesProcessor, logAction, lookupTracker, outputDir, relativePathOrCanonical, allKotlinSources, sourcesToCompile, targetId)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun compileIncrementally(
|
private fun compileIncrementally(
|
||||||
args: K2JVMCompilerArguments,
|
args: K2JVMCompilerArguments,
|
||||||
caches: IncrementalCachesManager,
|
caches: IncrementalCachesManager,
|
||||||
currentRemoved: List<File>,
|
|
||||||
isIncrementalDecided: Boolean,
|
isIncrementalDecided: Boolean,
|
||||||
javaFilesProcessor: ChangedJavaFilesProcessor,
|
javaFilesProcessor: ChangedJavaFilesProcessor,
|
||||||
logAction: (String) -> Unit,
|
logAction: (String) -> Unit,
|
||||||
@@ -312,13 +313,12 @@ open class KotlinCompile : AbstractKotlinCompile<K2JVMCompilerArguments>(), Kotl
|
|||||||
outputDir: File,
|
outputDir: File,
|
||||||
relativePathOrCanonical: (File) -> String,
|
relativePathOrCanonical: (File) -> String,
|
||||||
allKotlinSources: List<File>,
|
allKotlinSources: List<File>,
|
||||||
sourcesToCompile: Set<File>,
|
dirtyKotlinFiles: Set<File>,
|
||||||
targetId: TargetId
|
targetId: TargetId
|
||||||
) {
|
) {
|
||||||
val allGeneratedFiles = hashSetOf<GeneratedFile<TargetId>>()
|
val allGeneratedFiles = hashSetOf<GeneratedFile<TargetId>>()
|
||||||
var currentRemoved1 = currentRemoved
|
|
||||||
var isIncrementalDecided1 = isIncrementalDecided
|
var isIncrementalDecided1 = isIncrementalDecided
|
||||||
var sourcesToCompile1 = sourcesToCompile
|
val dirtySources: MutableList<File> = allKotlinSources.filterTo(ArrayList()) { it in dirtyKotlinFiles }
|
||||||
if (isIncrementalDecided1) {
|
if (isIncrementalDecided1) {
|
||||||
additionalClasspath.add(destinationDir)
|
additionalClasspath.add(destinationDir)
|
||||||
} else {
|
} else {
|
||||||
@@ -333,24 +333,20 @@ open class KotlinCompile : AbstractKotlinCompile<K2JVMCompilerArguments>(), Kotl
|
|||||||
val buildDirtyFqNames = HashSet<FqName>()
|
val buildDirtyFqNames = HashSet<FqName>()
|
||||||
|
|
||||||
var exitCode = ExitCode.OK
|
var exitCode = ExitCode.OK
|
||||||
while (sourcesToCompile1.any() || currentRemoved1.any()) {
|
while (dirtySources.any()) {
|
||||||
val removedAndModified = (sourcesToCompile1 + currentRemoved1).toList()
|
val outdatedClasses = caches.incrementalCache.classesBySources(dirtySources)
|
||||||
val outdatedClasses = caches.incrementalCache.classesBySources(removedAndModified)
|
caches.incrementalCache.markOutputClassesDirty(dirtySources)
|
||||||
caches.incrementalCache.markOutputClassesDirty(removedAndModified)
|
caches.incrementalCache.removeClassfilesBySources(dirtySources)
|
||||||
caches.incrementalCache.removeClassfilesBySources(removedAndModified)
|
|
||||||
|
|
||||||
// can be empty if only removed sources are present
|
val (sourcesToCompile, removedKotlinSources) = dirtySources.partition { it.isFile }
|
||||||
if (sourcesToCompile1.isNotEmpty()) {
|
if (sourcesToCompile.isNotEmpty()) {
|
||||||
logger.kotlinInfo("compile iteration: ${sourcesToCompile1.joinToString(transform = relativePathOrCanonical)}")
|
logger.kotlinDebug { "compile iteration: ${sourcesToCompile.joinToString(transform = relativePathOrCanonical)}" }
|
||||||
}
|
}
|
||||||
|
|
||||||
val (existingSource, nonExistingSource) = sourcesToCompile1.partition { it.isFile }
|
val text = sourcesToCompile.map { it.canonicalPath }.joinToString(separator = System.getProperty("line.separator"))
|
||||||
assert(nonExistingSource.isEmpty()) { "Trying to compile removed files: ${nonExistingSource.map(relativePathOrCanonical)}" }
|
|
||||||
|
|
||||||
val text = existingSource.map { it.canonicalPath }.joinToString(separator = System.getProperty("line.separator"))
|
|
||||||
dirtySourcesSinceLastTimeFile.writeText(text)
|
dirtySourcesSinceLastTimeFile.writeText(text)
|
||||||
|
|
||||||
val compilerOutput = compileChanged(listOf(targetId), existingSource.toSet(), outputDir, args, { caches.incrementalCache }, lookupTracker)
|
val compilerOutput = compileChanged(listOf(targetId), sourcesToCompile.toSet(), outputDir, args, { caches.incrementalCache }, lookupTracker)
|
||||||
exitCode = compilerOutput.exitCode
|
exitCode = compilerOutput.exitCode
|
||||||
|
|
||||||
if (exitCode == ExitCode.OK) {
|
if (exitCode == ExitCode.OK) {
|
||||||
@@ -361,12 +357,13 @@ open class KotlinCompile : AbstractKotlinCompile<K2JVMCompilerArguments>(), Kotl
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
allGeneratedFiles.addAll(compilerOutput.generatedFiles)
|
val generatedClassFiles = compilerOutput.generatedFiles
|
||||||
val compilationResult = updateIncrementalCaches(listOf(targetId), compilerOutput.generatedFiles,
|
allGeneratedFiles.addAll(generatedClassFiles)
|
||||||
|
val compilationResult = updateIncrementalCaches(listOf(targetId), generatedClassFiles,
|
||||||
compiledWithErrors = exitCode != ExitCode.OK,
|
compiledWithErrors = exitCode != ExitCode.OK,
|
||||||
getIncrementalCache = { caches.incrementalCache })
|
getIncrementalCache = { caches.incrementalCache })
|
||||||
|
|
||||||
caches.lookupCache.update(lookupTracker, sourcesToCompile1, currentRemoved1)
|
caches.lookupCache.update(lookupTracker, sourcesToCompile, removedKotlinSources)
|
||||||
|
|
||||||
val generatedJavaFiles = kapt2GeneratedSourcesDir.walk().filter { it.isJavaFile() }.toList()
|
val generatedJavaFiles = kapt2GeneratedSourcesDir.walk().filter { it.isJavaFile() }.toList()
|
||||||
val generatedJavaFilesDiff = caches.incrementalCache.compareAndUpdateFileSnapshots(generatedJavaFiles)
|
val generatedJavaFilesDiff = caches.incrementalCache.compareAndUpdateFileSnapshots(generatedJavaFiles)
|
||||||
@@ -378,6 +375,7 @@ open class KotlinCompile : AbstractKotlinCompile<K2JVMCompilerArguments>(), Kotl
|
|||||||
|
|
||||||
val (dirtyLookupSymbols, dirtyClassFqNames) = compilationResult.getDirtyData(listOf(caches.incrementalCache), logAction)
|
val (dirtyLookupSymbols, dirtyClassFqNames) = compilationResult.getDirtyData(listOf(caches.incrementalCache), logAction)
|
||||||
val generatedJavaFilesChanges = javaFilesProcessor.process(generatedJavaFilesDiff)
|
val generatedJavaFilesChanges = javaFilesProcessor.process(generatedJavaFilesDiff)
|
||||||
|
val compiledInThisIterationSet = sourcesToCompile.toHashSet()
|
||||||
val dirtyKotlinFilesFromJava = when (generatedJavaFilesChanges) {
|
val dirtyKotlinFilesFromJava = when (generatedJavaFilesChanges) {
|
||||||
is ChangesEither.Unknown -> {
|
is ChangesEither.Unknown -> {
|
||||||
logger.kotlinDebug { "Could not get changes for generated java files, recompiling all kotlin" }
|
logger.kotlinDebug { "Could not get changes for generated java files, recompiling all kotlin" }
|
||||||
@@ -385,21 +383,22 @@ open class KotlinCompile : AbstractKotlinCompile<K2JVMCompilerArguments>(), Kotl
|
|||||||
allKotlinSources.toSet()
|
allKotlinSources.toSet()
|
||||||
}
|
}
|
||||||
is ChangesEither.Known -> {
|
is ChangesEither.Known -> {
|
||||||
mapLookupSymbolsToFiles(caches.lookupCache, generatedJavaFilesChanges.lookupSymbols, logAction, relativePathOrCanonical, excludes = sourcesToCompile1)
|
mapLookupSymbolsToFiles(caches.lookupCache, generatedJavaFilesChanges.lookupSymbols, logAction, relativePathOrCanonical, excludes = compiledInThisIterationSet)
|
||||||
}
|
}
|
||||||
else -> throw IllegalStateException("Unknown ChangesEither implementation: $generatedJavaFiles")
|
else -> throw IllegalStateException("Unknown ChangesEither implementation: $generatedJavaFiles")
|
||||||
}
|
}
|
||||||
sourcesToCompile1 = dirtyKotlinFilesFromJava +
|
|
||||||
mapLookupSymbolsToFiles(caches.lookupCache, dirtyLookupSymbols, logAction, relativePathOrCanonical, excludes = sourcesToCompile1) +
|
with (dirtySources) {
|
||||||
mapClassesFqNamesToFiles(listOf(caches.incrementalCache), dirtyClassFqNames, logAction, relativePathOrCanonical, excludes = sourcesToCompile1)
|
clear()
|
||||||
|
addAll(dirtyKotlinFilesFromJava)
|
||||||
|
addAll(mapLookupSymbolsToFiles(caches.lookupCache, dirtyLookupSymbols, logAction, relativePathOrCanonical, excludes = compiledInThisIterationSet))
|
||||||
|
addAll(mapClassesFqNamesToFiles(listOf(caches.incrementalCache), dirtyClassFqNames, logAction, relativePathOrCanonical, excludes = compiledInThisIterationSet))
|
||||||
|
}
|
||||||
|
|
||||||
buildDirtyLookupSymbols.addAll(dirtyLookupSymbols)
|
buildDirtyLookupSymbols.addAll(dirtyLookupSymbols)
|
||||||
buildDirtyFqNames.addAll(dirtyClassFqNames)
|
buildDirtyFqNames.addAll(dirtyClassFqNames)
|
||||||
|
|
||||||
if (currentRemoved1.any()) {
|
anyClassesCompiled = anyClassesCompiled || generatedClassFiles.isNotEmpty() || removedKotlinSources.isNotEmpty()
|
||||||
anyClassesCompiled = true
|
|
||||||
currentRemoved1 = listOf()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (exitCode == ExitCode.OK && isIncrementalDecided1) {
|
if (exitCode == ExitCode.OK && isIncrementalDecided1) {
|
||||||
@@ -416,7 +415,6 @@ open class KotlinCompile : AbstractKotlinCompile<K2JVMCompilerArguments>(), Kotl
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
anyClassesCompiled = anyClassesCompiled || allGeneratedFiles.isNotEmpty()
|
|
||||||
caches.close(flush = true)
|
caches.close(flush = true)
|
||||||
logger.kotlinDebug { "flushed incremental caches" }
|
logger.kotlinDebug { "flushed incremental caches" }
|
||||||
processCompilerExitCode(exitCode)
|
processCompilerExitCode(exitCode)
|
||||||
|
|||||||
Reference in New Issue
Block a user