Fix clearing IC caches on rebuild

Previously IC caches in Gradle were not cleared correctly on rebuild,
because only `clean` method was called, but `flush` and `close`
were not.
This commit is contained in:
Alexey Tsvetkov
2019-01-17 22:52:25 +03:00
parent 7dcdba7e1a
commit 9176f9b254
18 changed files with 166 additions and 98 deletions
@@ -20,7 +20,7 @@ import org.jetbrains.kotlin.incremental.storage.BasicMapsOwner
import java.io.File
abstract class IncrementalCachesManager<PlatformCache : AbstractIncrementalCache<*>>(
protected val cachesRootDir: File,
cachesRootDir: File,
protected val reporter: ICReporter
) {
private val caches = arrayListOf<BasicMapsOwner>()
@@ -35,11 +35,6 @@ abstract class IncrementalCachesManager<PlatformCache : AbstractIncrementalCache
val lookupCache: LookupStorage = LookupStorage(lookupCacheDir).apply { registerCache() }
abstract val platformCache: PlatformCache
fun clean() {
caches.forEach { it.clean() }
cachesRootDir.deleteRecursively()
}
fun close(flush: Boolean = false): Boolean {
var successful = true
@@ -47,8 +42,7 @@ abstract class IncrementalCachesManager<PlatformCache : AbstractIncrementalCache
if (flush) {
try {
cache.flush(false)
}
catch (e: Throwable) {
} catch (e: Throwable) {
successful = false
reporter.report { "Exception when flushing cache ${cache.javaClass}: $e" }
}
@@ -56,8 +50,7 @@ abstract class IncrementalCachesManager<PlatformCache : AbstractIncrementalCache
try {
cache.close()
}
catch (e: Throwable) {
} catch (e: Throwable) {
successful = false
reporter.report { "Exception when closing cache ${cache.javaClass}: $e" }
}
@@ -38,11 +38,13 @@ abstract class IncrementalCompilerRunner<
Args : CommonCompilerArguments,
CacheManager : IncrementalCachesManager<*>
>(
workingDir: File,
private val workingDir: File,
cacheDirName: String,
protected val reporter: ICReporter,
private val buildHistoryFile: File,
private val localStateDirs: Collection<File> = emptyList()
// there might be some additional output directories (e.g. for generated java in kapt)
// to remove them correctly on rebuild, we pass them as additional argument
private val outputFiles: Collection<File> = emptyList()
) {
protected val cacheDirectory = File(workingDir, cacheDirName)
@@ -67,19 +69,8 @@ abstract class IncrementalCompilerRunner<
fun rebuild(reason: () -> String): ExitCode {
reporter.report(reason)
caches.clean()
dirtySourcesSinceLastTimeFile.delete()
reporter.report { "Deleting output directories on rebuild:" }
for (dir in sequenceOf(destinationDir(args)) + localStateDirs.asSequence()) {
if (!dir.isDirectory) continue
dir.deleteRecursively()
dir.mkdirs()
reporter.report { "deleted $dir" }
}
caches.close(false)
clearLocalStateOnRebuild(args)
caches = createCacheManager(args)
if (providedChangedFiles == null) {
caches.inputsCache.sourceSnapshotMap.compareAndUpdate(allSourceFiles)
@@ -111,6 +102,34 @@ abstract class IncrementalCompilerRunner<
}
}
private fun clearLocalStateOnRebuild(args: Args) {
val destinationDir = destinationDir(args)
reporter.report { "Clearing output on rebuild" }
for (file in sequenceOf(destinationDir, workingDir) + outputFiles.asSequence()) {
val deleted: Boolean? = when {
file.isDirectory -> {
reporter.report { "Deleting directory $file" }
file.deleteRecursively()
}
file.isFile -> {
reporter.report { "Deleting $file" }
file.delete()
}
else -> null
}
if (deleted == false) {
reporter.report { "Could not delete $file" }
}
}
assert(!destinationDir.exists()) { "Could not delete destination dir $destinationDir" }
assert(!workingDir.exists()) { "Could not delete caches dir $workingDir" }
destinationDir.mkdirs()
workingDir.mkdirs()
}
private fun sourcesToCompile(caches: CacheManager, changedFiles: ChangedFiles, args: Args): CompilationMode =
when (changedFiles) {
is ChangedFiles.Known -> calculateSourcesToCompile(caches, changedFiles, args)
@@ -65,15 +65,15 @@ fun makeIncrementally(
withIC {
val compiler = IncrementalJvmCompilerRunner(
cachesDir,
sourceRoots.map { JvmSourceRoot(it, null) }.toSet(),
reporter,
// Use precise setting in case of non-Gradle build
usePreciseJavaTracking = true,
localStateDirs = emptyList(),
buildHistoryFile = buildHistoryFile,
modulesApiHistory = EmptyModulesApiHistory,
kotlinSourceFilesExtensions = kotlinExtensions
cachesDir,
sourceRoots.map { JvmSourceRoot(it, null) }.toSet(),
reporter,
// Use precise setting in case of non-Gradle build
usePreciseJavaTracking = true,
outputFiles = emptyList(),
buildHistoryFile = buildHistoryFile,
modulesApiHistory = EmptyModulesApiHistory,
kotlinSourceFilesExtensions = kotlinExtensions
)
compiler.compile(sourceFiles, args, messageCollector, providedChangedFiles = null)
}
@@ -102,15 +102,15 @@ class IncrementalJvmCompilerRunner(
reporter: ICReporter,
private val usePreciseJavaTracking: Boolean,
buildHistoryFile: File,
localStateDirs: Collection<File>,
outputFiles: Collection<File>,
private val modulesApiHistory: ModulesApiHistory,
override val kotlinSourceFilesExtensions: List<String> = DEFAULT_KOTLIN_SOURCE_FILES_EXTENSIONS
) : IncrementalCompilerRunner<K2JVMCompilerArguments, IncrementalJvmCachesManager>(
workingDir,
"caches-jvm",
reporter,
localStateDirs = localStateDirs,
buildHistoryFile = buildHistoryFile
outputFiles = outputFiles,
buildHistoryFile = buildHistoryFile
) {
override fun isICEnabled(): Boolean =
IncrementalCompilation.isEnabledForJvm()