[KGP] Introduce Incremental Compilation Feature Toggles

Makes it easier to introduce a Gradle property for configuring
IncrementalCompilerRunner.

^KT-64513 Fixed
^KT-63837 In Progress


Merge-request: KT-MR-13671
Merged-by: Evgenii Mazhukin <evgenii.mazhukin@jetbrains.com>
This commit is contained in:
Evgenii Mazhukin
2023-12-27 13:43:54 +00:00
committed by Space Team
parent 97f8f7a734
commit ee3119e9d2
19 changed files with 157 additions and 70 deletions
@@ -72,9 +72,11 @@ abstract class IncrementalCompilerRunner<
*/
private val outputDirs: Collection<File>?,
protected val withAbiSnapshot: Boolean = false,
private val preciseCompilationResultsBackup: Boolean = false,
private val keepIncrementalCompilationCachesInMemory: Boolean = false,
/**
* Various options. Boolean flags, both stable and experimental, should be added there.
* Non-trivial configuration should NOT be added there.
*/
protected val icFeatures: IncrementalCompilationFeatures,
) {
protected val cacheDirectory = File(workingDir, cacheDirName)
@@ -96,7 +98,7 @@ abstract class IncrementalCompilerRunner<
reporter = reporter,
trackChangesInLookupCache = shouldTrackChangesInLookupCache,
storeFullFqNamesInLookupCache = shouldStoreFullFqNamesInLookupCache,
keepIncrementalCompilationCachesInMemory = keepIncrementalCompilationCachesInMemory,
icFeatures = icFeatures,
)
protected abstract val shouldTrackChangesInLookupCache: Boolean
@@ -201,7 +203,7 @@ abstract class IncrementalCompilerRunner<
return ICResult.Failed(IC_FAILED_TO_GET_CHANGED_FILES, e)
}
val classpathAbiSnapshot = if (withAbiSnapshot) getClasspathAbiSnapshot(args) else null
val classpathAbiSnapshot = if (icFeatures.withAbiSnapshot) getClasspathAbiSnapshot(args) else null
// Step 2: Compute files to recompile
val compilationMode = try {
@@ -216,7 +218,7 @@ abstract class IncrementalCompilerRunner<
return ICResult.RequiresRebuild(compilationMode.reason)
}
val abiSnapshotData = if (withAbiSnapshot) {
val abiSnapshotData = if (icFeatures.withAbiSnapshot) {
if (!abiSnapshotFile.exists()) {
reporter.debug { "Jar snapshot file does not exist: ${abiSnapshotFile.path}" }
return ICResult.RequiresRebuild(NO_ABI_SNAPSHOT)
@@ -276,7 +278,7 @@ abstract class IncrementalCompilerRunner<
if (trackChangedFiles) {
caches.inputsCache.sourceSnapshotMap.compareAndUpdate(allSourceFiles)
}
val abiSnapshotData = if (withAbiSnapshot) {
val abiSnapshotData = if (icFeatures.withAbiSnapshot) {
AbiSnapshotData(snapshot = AbiSnapshotImpl(mutableMapOf()), classpathAbiSnapshot = getClasspathAbiSnapshot(args))
} else null
@@ -405,7 +407,7 @@ abstract class IncrementalCompilerRunner<
return exitCode
}
private fun createTransaction() = if (preciseCompilationResultsBackup) {
private fun createTransaction() = if (icFeatures.preciseCompilationResultsBackup) {
RecoverableCompilationTransaction(reporter, Files.createTempDirectory("kotlin-backups"))
} else {
NonRecoverableCompilationTransaction()
@@ -519,7 +521,7 @@ abstract class IncrementalCompilerRunner<
updateCaches(services, caches, generatedFiles, changesCollector)
}
if (compilationMode is CompilationMode.Rebuild) {
if (withAbiSnapshot) {
if (icFeatures.withAbiSnapshot) {
abiSnapshotData!!.snapshot.protos.putAll(changesCollector.protoDataChanges())
}
break
@@ -552,7 +554,7 @@ abstract class IncrementalCompilerRunner<
buildDirtyFqNames.addAll(dirtyClassFqNames)
//update
if (withAbiSnapshot) {
if (icFeatures.withAbiSnapshot) {
//TODO(valtman) check method/ kts class remove
changesCollector.protoDataRemoved().forEach { abiSnapshotData!!.snapshot.protos.remove(it) }
abiSnapshotData!!.snapshot.protos.putAll(changesCollector.protoDataChanges())
@@ -564,7 +566,7 @@ abstract class IncrementalCompilerRunner<
BuildInfo.write(icContext, currentBuildInfo, lastBuildInfoFile)
//write abi snapshot
if (withAbiSnapshot) {
if (icFeatures.withAbiSnapshot) {
//TODO(valtman) check method/class remove
AbiSnapshotImpl.write(icContext, abiSnapshotData!!.snapshot, abiSnapshotFile)
}
@@ -89,24 +89,20 @@ class IncrementalJsCompilerRunner(
buildHistoryFile: File?,
private val modulesApiHistory: ModulesApiHistory,
private val scopeExpansion: CompileScopeExpansionMode = CompileScopeExpansionMode.NEVER,
withAbiSnapshot: Boolean = false,
preciseCompilationResultsBackup: Boolean = false,
keepIncrementalCompilationCachesInMemory: Boolean = false,
icFeatures: IncrementalCompilationFeatures = IncrementalCompilationFeatures.DEFAULT_CONFIGURATION,
) : IncrementalCompilerRunner<K2JSCompilerArguments, IncrementalJsCachesManager>(
workingDir,
"caches-js",
reporter,
buildHistoryFile = buildHistoryFile,
outputDirs = null,
withAbiSnapshot = withAbiSnapshot,
preciseCompilationResultsBackup = preciseCompilationResultsBackup,
keepIncrementalCompilationCachesInMemory = keepIncrementalCompilationCachesInMemory,
icFeatures = icFeatures,
) {
override val shouldTrackChangesInLookupCache
get() = false
override val shouldStoreFullFqNamesInLookupCache
get() = withAbiSnapshot
get() = icFeatures.withAbiSnapshot
override fun createCacheManager(icContext: IncrementalCompilationContext, args: K2JSCompilerArguments) =
IncrementalJsCachesManager(icContext, if (!args.isIrBackendEnabled()) JsSerializerProtocol else KlibMetadataSerializerProtocol, cacheDirectory)
@@ -128,7 +124,7 @@ class IncrementalJsCompilerRunner(
if (buildHistoryFile == null) {
error("The build is configured to use the build-history based IC approach, but doesn't specify the buildHistoryFile")
}
if (!withAbiSnapshot && !buildHistoryFile.isFile) {
if (!icFeatures.withAbiSnapshot && !buildHistoryFile.isFile) {
return CompilationMode.Rebuild(BuildAttribute.NO_BUILD_HISTORY)
}
val lastBuildInfo = BuildInfo.read(lastBuildInfoFile, messageCollector) ?: return CompilationMode.Rebuild(BuildAttribute.INVALID_LAST_BUILD_INFO)
@@ -68,24 +68,20 @@ open class IncrementalJvmCompilerRunner(
private val modulesApiHistory: ModulesApiHistory,
override val kotlinSourceFilesExtensions: Set<String> = DEFAULT_KOTLIN_SOURCE_FILES_EXTENSIONS,
private val classpathChanges: ClasspathChanges,
withAbiSnapshot: Boolean = false,
preciseCompilationResultsBackup: Boolean = false,
keepIncrementalCompilationCachesInMemory: Boolean = false,
icFeatures: IncrementalCompilationFeatures = IncrementalCompilationFeatures.DEFAULT_CONFIGURATION,
) : IncrementalCompilerRunner<K2JVMCompilerArguments, IncrementalJvmCachesManager>(
workingDir,
"caches-jvm",
reporter,
buildHistoryFile = buildHistoryFile,
outputDirs = outputDirs,
withAbiSnapshot = withAbiSnapshot,
preciseCompilationResultsBackup = preciseCompilationResultsBackup,
keepIncrementalCompilationCachesInMemory = keepIncrementalCompilationCachesInMemory,
icFeatures = icFeatures,
) {
override val shouldTrackChangesInLookupCache
get() = classpathChanges is ClasspathChanges.ClasspathSnapshotEnabled.IncrementalRun
override val shouldStoreFullFqNamesInLookupCache
get() = withAbiSnapshot || classpathChanges is ClasspathChanges.ClasspathSnapshotEnabled
get() = icFeatures.withAbiSnapshot || classpathChanges is ClasspathChanges.ClasspathSnapshotEnabled
override fun createCacheManager(icContext: IncrementalCompilationContext, args: K2JVMCompilerArguments) =
IncrementalJvmCachesManager(icContext, args.destination?.let { File(it) }, cacheDirectory)
@@ -133,7 +129,7 @@ open class IncrementalJvmCompilerRunner(
classpathAbiSnapshots: Map<String, AbiSnapshot>
): CompilationMode {
return try {
calculateSourcesToCompileImpl(caches, changedFiles, args, messageCollector, classpathAbiSnapshots, withAbiSnapshot)
calculateSourcesToCompileImpl(caches, changedFiles, args, messageCollector, classpathAbiSnapshots, icFeatures.withAbiSnapshot)
} finally {
this.messageCollector.flush(messageCollector)
this.messageCollector.clear()
@@ -32,7 +32,9 @@ class IncrementalJvmCompilerTestRunner(
modulesApiHistory,
kotlinSourceFilesExtensions,
classpathChanges,
withAbiSnapshot
icFeatures = IncrementalCompilationFeatures(
withAbiSnapshot = withAbiSnapshot
),
) {
override fun createCacheManager(icContext: IncrementalCompilationContext, args: K2JVMCompilerArguments): IncrementalJvmCachesManager =
object : IncrementalJvmCachesManager(