Don't use global state for keeping incremental compilation state

Previously IC state was stored in System properties. As result parallel
compilation might cause incorrect state of IC, what led to corruption
of kotlin_module files. Now IC state is stored via CompilerArguments
and CompilerConfiguration
#KT-46038 Fixed
This commit is contained in:
Andrey Uskov
2021-09-20 21:17:18 +03:00
committed by teamcity
parent 99300bd885
commit 2adc851f1b
21 changed files with 98 additions and 32 deletions
@@ -61,7 +61,6 @@ abstract class IncrementalCompilerRunner<
//TODO(valtman) temporal measure to ensure quick disable, should be deleted after successful release
protected val withSnapshot: Boolean = CompilerSystemProperties.COMPILE_INCREMENTAL_WITH_CLASSPATH_SNAPSHOTS.value.toBooleanLenient() ?: false
protected abstract fun isICEnabled(): Boolean
protected abstract fun createCacheManager(args: Args, projectDir: File?): CacheManager
protected abstract fun destinationDir(args: Args): File
@@ -84,7 +83,6 @@ abstract class IncrementalCompilerRunner<
providedChangedFiles: ChangedFiles?,
projectDir: File? = null
): ExitCode {
assert(isICEnabled()) { "Incremental compilation is not enabled" }
var caches = createCacheManager(args, projectDir)
if (withSnapshot) {
@@ -22,6 +22,7 @@ import org.jetbrains.kotlin.build.report.ICReporter
import org.jetbrains.kotlin.build.report.metrics.BuildAttribute
import org.jetbrains.kotlin.build.report.metrics.DoNothingBuildMetricsReporter
import org.jetbrains.kotlin.cli.common.ExitCode
import org.jetbrains.kotlin.cli.common.arguments.CommonCompilerArguments
import org.jetbrains.kotlin.cli.common.arguments.K2JSCompilerArguments
import org.jetbrains.kotlin.cli.common.arguments.isIrBackendEnabled
import org.jetbrains.kotlin.cli.common.messages.MessageCollector
@@ -52,7 +53,7 @@ fun makeJsIncrementally(
.filter { it.isFile && it.extension.equals("kt", ignoreCase = true) }.toList()
val buildReporter = BuildReporter(icReporter = reporter, buildMetricsReporter = DoNothingBuildMetricsReporter)
withJsIC {
withJsIC(args) {
val compiler = IncrementalJsCompilerRunner(
cachesDir, buildReporter,
buildHistoryFile = buildHistoryFile,
@@ -63,11 +64,15 @@ fun makeJsIncrementally(
}
}
inline fun <R> withJsIC(fn: () -> R): R {
@Suppress("DEPRECATION")
inline fun <R> withJsIC(args: CommonCompilerArguments, enabled: Boolean = true, fn: () -> R): R {
val isJsEnabledBackup = IncrementalCompilation.isEnabledForJs()
IncrementalCompilation.setIsEnabledForJs(true)
try {
if (args.incrementalCompilation == null) {
args.incrementalCompilation = enabled
}
return fn()
} finally {
IncrementalCompilation.setIsEnabledForJs(isJsEnabledBackup)
@@ -86,8 +91,6 @@ class IncrementalJsCompilerRunner(
reporter,
buildHistoryFile = buildHistoryFile
) {
override fun isICEnabled(): Boolean =
IncrementalCompilation.isEnabledForJs()
override fun createCacheManager(args: K2JSCompilerArguments, projectDir: File?): IncrementalJsCachesManager {
val serializerProtocol = if (!args.isIrBackendEnabled()) JsSerializerProtocol else KlibMetadataSerializerProtocol
@@ -35,6 +35,7 @@ import org.jetbrains.kotlin.build.report.metrics.DoNothingBuildMetricsReporter
import org.jetbrains.kotlin.build.report.metrics.measure
import org.jetbrains.kotlin.cli.common.CLIConfigurationKeys
import org.jetbrains.kotlin.cli.common.ExitCode
import org.jetbrains.kotlin.cli.common.arguments.CommonCompilerArguments
import org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments
import org.jetbrains.kotlin.cli.common.messages.FilteringMessageCollector
import org.jetbrains.kotlin.cli.common.messages.MessageCollector
@@ -83,7 +84,7 @@ fun makeIncrementally(
args.javaSourceRoots = sourceRoots.map { it.absolutePath }.toTypedArray()
val buildReporter = BuildReporter(icReporter = reporter, buildMetricsReporter = DoNothingBuildMetricsReporter)
withIC {
withIC(args) {
val compiler = IncrementalJvmCompilerRunner(
cachesDir,
buildReporter,
@@ -109,11 +110,15 @@ object EmptyICReporter : ICReporterBase() {
override fun reportMarkDirty(affectedFiles: Iterable<File>, reason: String) {}
}
inline fun <R> withIC(enabled: Boolean = true, fn: () -> R): R {
@Suppress("DEPRECATION")
inline fun <R> withIC(args: CommonCompilerArguments, enabled: Boolean = true, fn: () -> R): R {
val isEnabledBackup = IncrementalCompilation.isEnabledForJvm()
IncrementalCompilation.setIsEnabledForJvm(enabled)
try {
if (args.incrementalCompilation == null) {
args.incrementalCompilation = enabled
}
return fn()
} finally {
IncrementalCompilation.setIsEnabledForJvm(isEnabledBackup)
@@ -136,9 +141,6 @@ class IncrementalJvmCompilerRunner(
additionalOutputFiles = outputFiles,
buildHistoryFile = buildHistoryFile
) {
override fun isICEnabled(): Boolean =
IncrementalCompilation.isEnabledForJvm()
override fun createCacheManager(args: K2JVMCompilerArguments, projectDir: File?): IncrementalJvmCachesManager =
IncrementalJvmCachesManager(
cacheDirectory,