[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
@@ -29,6 +29,7 @@ import org.jetbrains.kotlin.daemon.client.BasicCompilerServicesWithResultsFacade
import org.jetbrains.kotlin.daemon.common.CompilerId
import org.jetbrains.kotlin.daemon.common.configureDaemonJVMOptions
import org.jetbrains.kotlin.daemon.common.filterExtractProps
import org.jetbrains.kotlin.incremental.IncrementalCompilationFeatures
import org.jetbrains.kotlin.incremental.IncrementalJvmCompilerRunner
import org.jetbrains.kotlin.incremental.classpathDiff.ClasspathEntrySnapshotter
import org.jetbrains.kotlin.incremental.extractKotlinSourcesFromFreeCompilerArguments
@@ -153,9 +154,7 @@ internal object CompilationServiceImpl : CompilationService {
outputDirs = options.outputDirs,
kotlinSourceFilesExtensions = kotlinFilenameExtensions,
classpathChanges = classpathChanges,
withAbiSnapshot = false,
preciseCompilationResultsBackup = options.preciseCompilationResultsBackupEnabled,
keepIncrementalCompilationCachesInMemory = options.incrementalCompilationCachesKeptInMemory
icFeatures = options.extractIncrementalCompilationFeatures(),
)
val rootProjectDir = options.rootProjectDir
val buildDir = options.buildDir
@@ -0,0 +1,25 @@
/*
* Copyright 2010-2023 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.buildtools.internal
import org.jetbrains.kotlin.buildtools.api.jvm.IncrementalJvmCompilationConfiguration
import org.jetbrains.kotlin.incremental.IncrementalCompilationFeatures
private typealias ICConfiguration = IncrementalJvmCompilationConfiguration<*>
/**
* IncrementalJvmCompilationConfiguration provides single-property API for forward-compatibility.
*
* configurationAdapters are there to regroup the properties and work with higher-level interfaces.
*/
internal fun ICConfiguration.extractIncrementalCompilationFeatures(): IncrementalCompilationFeatures {
return IncrementalCompilationFeatures(
withAbiSnapshot = false,
preciseCompilationResultsBackup = preciseCompilationResultsBackupEnabled,
keepIncrementalCompilationCachesInMemory = incrementalCompilationCachesKeptInMemory,
)
}
@@ -53,9 +53,7 @@ internal val JvmCompilationConfigurationImpl.asDaemonCompilationOptions: Compila
rootProjectDir = options.rootProjectDir,
buildDir = options.buildDir,
kotlinScriptExtensions = ktsExtensionsAsArray,
withAbiSnapshot = false,
preciseCompilationResultsBackup = options.preciseCompilationResultsBackupEnabled,
keepIncrementalCompilationCachesInMemory = options.incrementalCompilationCachesKeptInMemory,
icFeatures = options.extractIncrementalCompilationFeatures(),
)
}
else -> CompilationOptions(
@@ -17,6 +17,7 @@
package org.jetbrains.kotlin.daemon.common
import org.jetbrains.kotlin.incremental.ClasspathChanges
import org.jetbrains.kotlin.incremental.IncrementalCompilationFeatures
import org.jetbrains.kotlin.incremental.IncrementalModuleInfo
import java.io.File
import java.io.Serializable
@@ -76,9 +77,7 @@ class IncrementalCompilationOptions(
val buildDir: File?,
kotlinScriptExtensions: Array<String>? = null,
val withAbiSnapshot: Boolean = false,
val preciseCompilationResultsBackup: Boolean = false,
val keepIncrementalCompilationCachesInMemory: Boolean = false,
val icFeatures: IncrementalCompilationFeatures = IncrementalCompilationFeatures.DEFAULT_CONFIGURATION,
) : CompilationOptions(
compilerMode,
targetPlatform,
@@ -88,7 +87,7 @@ class IncrementalCompilationOptions(
kotlinScriptExtensions
) {
companion object {
const val serialVersionUID: Long = 3
const val serialVersionUID: Long = 4
}
override fun toString(): String {
@@ -101,6 +100,7 @@ class IncrementalCompilationOptions(
"workingDir=$workingDir, " +
"multiModuleICSettings=$multiModuleICSettings, " +
"usePreciseJavaTracking=$usePreciseJavaTracking, " +
"icFeatures=$icFeatures, " +
"outputFiles=$outputFiles" +
")"
}
@@ -614,9 +614,7 @@ abstract class CompileServiceImplBase(
buildHistoryFile = incrementalCompilationOptions.multiModuleICSettings?.buildHistoryFile,
scopeExpansion = if (args.isIrBackendEnabled()) CompileScopeExpansionMode.ALWAYS else CompileScopeExpansionMode.NEVER,
modulesApiHistory = modulesApiHistory,
withAbiSnapshot = incrementalCompilationOptions.withAbiSnapshot,
preciseCompilationResultsBackup = incrementalCompilationOptions.preciseCompilationResultsBackup,
keepIncrementalCompilationCachesInMemory = incrementalCompilationOptions.keepIncrementalCompilationCachesInMemory,
icFeatures = incrementalCompilationOptions.icFeatures,
)
return try {
compiler.compile(allKotlinFiles, args, compilerMessageCollector, changedFiles)
@@ -683,9 +681,7 @@ abstract class CompileServiceImplBase(
modulesApiHistory = modulesApiHistory,
kotlinSourceFilesExtensions = allKotlinExtensions,
classpathChanges = incrementalCompilationOptions.classpathChanges,
withAbiSnapshot = incrementalCompilationOptions.withAbiSnapshot,
preciseCompilationResultsBackup = incrementalCompilationOptions.preciseCompilationResultsBackup,
keepIncrementalCompilationCachesInMemory = incrementalCompilationOptions.keepIncrementalCompilationCachesInMemory,
icFeatures = incrementalCompilationOptions.icFeatures,
)
return try {
compiler.compile(
@@ -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(