KT-45777: Address review comments - use InputChanges instead of ChangedFiles

This commit is contained in:
Hung Nguyen
2021-09-06 12:55:25 +01:00
committed by nataliya.valtman
parent 9b71bd5bb4
commit 97e9e4efe7
3 changed files with 21 additions and 40 deletions
@@ -14,6 +14,7 @@ import org.gradle.api.file.FileTree
import org.gradle.api.logging.Logger import org.gradle.api.logging.Logger
import org.gradle.api.model.ObjectFactory import org.gradle.api.model.ObjectFactory
import org.gradle.api.tasks.* import org.gradle.api.tasks.*
import org.gradle.work.InputChanges
import org.jetbrains.kotlin.cli.common.arguments.K2JSCompilerArguments import org.jetbrains.kotlin.cli.common.arguments.K2JSCompilerArguments
import org.jetbrains.kotlin.compilerRunner.GradleCompilerEnvironment import org.jetbrains.kotlin.compilerRunner.GradleCompilerEnvironment
import org.jetbrains.kotlin.compilerRunner.GradleCompilerRunner import org.jetbrains.kotlin.compilerRunner.GradleCompilerRunner
@@ -35,7 +36,6 @@ import org.jetbrains.kotlin.gradle.tasks.SourceRoots
import org.jetbrains.kotlin.gradle.utils.getAllDependencies import org.jetbrains.kotlin.gradle.utils.getAllDependencies
import org.jetbrains.kotlin.gradle.utils.getCacheDirectory import org.jetbrains.kotlin.gradle.utils.getCacheDirectory
import org.jetbrains.kotlin.gradle.utils.getDependenciesCacheDirectories import org.jetbrains.kotlin.gradle.utils.getDependenciesCacheDirectories
import org.jetbrains.kotlin.incremental.ChangedFiles
import org.jetbrains.kotlin.statistics.metrics.BooleanMetrics import org.jetbrains.kotlin.statistics.metrics.BooleanMetrics
import java.io.File import java.io.File
import javax.inject.Inject import javax.inject.Inject
@@ -94,7 +94,7 @@ abstract class KotlinJsIrLink @Inject constructor(
return !entryModule.get().asFile.exists() return !entryModule.get().asFile.exists()
} }
override fun callCompilerAsync(args: K2JSCompilerArguments, sourceRoots: SourceRoots, changedFiles: ChangedFiles) { override fun callCompilerAsync(args: K2JSCompilerArguments, sourceRoots: SourceRoots, inputChanges: InputChanges) {
KotlinBuildStatsService.applyIfInitialised { KotlinBuildStatsService.applyIfInitialised {
it.report(BooleanMetrics.JS_IR_INCREMENTAL, incrementalJsIr) it.report(BooleanMetrics.JS_IR_INCREMENTAL, incrementalJsIr)
} }
@@ -125,7 +125,7 @@ abstract class KotlinJsIrLink @Inject constructor(
it.normalize().absolutePath it.normalize().absolutePath
} }
} }
super.callCompilerAsync(args, sourceRoots, changedFiles) super.callCompilerAsync(args, sourceRoots, inputChanges)
} }
private fun visitCompilation( private fun visitCompilation(
@@ -20,6 +20,7 @@ import org.gradle.api.Project
import org.gradle.api.file.ConfigurableFileCollection import org.gradle.api.file.ConfigurableFileCollection
import org.gradle.api.provider.Provider import org.gradle.api.provider.Provider
import org.gradle.api.tasks.* import org.gradle.api.tasks.*
import org.gradle.work.InputChanges
import org.jetbrains.kotlin.cli.common.arguments.K2MetadataCompilerArguments import org.jetbrains.kotlin.cli.common.arguments.K2MetadataCompilerArguments
import org.jetbrains.kotlin.compilerRunner.GradleCompilerEnvironment import org.jetbrains.kotlin.compilerRunner.GradleCompilerEnvironment
import org.jetbrains.kotlin.compilerRunner.OutputItemsCollectorImpl import org.jetbrains.kotlin.compilerRunner.OutputItemsCollectorImpl
@@ -35,7 +36,6 @@ import org.jetbrains.kotlin.gradle.plugin.mpp.pm20.KotlinCompilationData
import org.jetbrains.kotlin.gradle.plugin.mpp.pm20.KotlinMetadataCompilationData import org.jetbrains.kotlin.gradle.plugin.mpp.pm20.KotlinMetadataCompilationData
import org.jetbrains.kotlin.gradle.plugin.mpp.pm20.refinesClosure import org.jetbrains.kotlin.gradle.plugin.mpp.pm20.refinesClosure
import org.jetbrains.kotlin.gradle.plugin.sources.resolveAllDependsOnSourceSets import org.jetbrains.kotlin.gradle.plugin.sources.resolveAllDependsOnSourceSets
import org.jetbrains.kotlin.incremental.ChangedFiles
import java.io.File import java.io.File
import javax.inject.Inject import javax.inject.Inject
@@ -116,7 +116,7 @@ abstract class KotlinCompileCommon @Inject constructor(
@get:Internal @get:Internal
internal val expectActualLinker = objects.property(Boolean::class.java) internal val expectActualLinker = objects.property(Boolean::class.java)
override fun callCompilerAsync(args: K2MetadataCompilerArguments, sourceRoots: SourceRoots, changedFiles: ChangedFiles) { override fun callCompilerAsync(args: K2MetadataCompilerArguments, sourceRoots: SourceRoots, inputChanges: InputChanges) {
val messageCollector = GradlePrintingMessageCollector(logger, args.allWarningsAsErrors) val messageCollector = GradlePrintingMessageCollector(logger, args.allWarningsAsErrors)
val outputItemCollector = OutputItemsCollectorImpl() val outputItemCollector = OutputItemsCollectorImpl()
val compilerRunner = compilerRunner.get() val compilerRunner = compilerRunner.get()
@@ -396,11 +396,7 @@ abstract class AbstractKotlinCompile<T : CommonCompilerArguments> : AbstractKotl
sourceRoots.log(this.name, logger) sourceRoots.log(this.name, logger)
val args = prepareCompilerArguments() val args = prepareCompilerArguments()
taskBuildDirectory.get().asFile.mkdirs() taskBuildDirectory.get().asFile.mkdirs()
callCompilerAsync( callCompilerAsync(args, sourceRoots, inputChanges)
args,
sourceRoots,
getChangedFiles(inputChanges, incrementalProps)
)
} }
protected fun getChangedFiles( protected fun getChangedFiles(
@@ -432,7 +428,7 @@ abstract class AbstractKotlinCompile<T : CommonCompilerArguments> : AbstractKotl
* Compiler might be executed asynchronously. Do not do anything requiring end of compilation after this function is called. * Compiler might be executed asynchronously. Do not do anything requiring end of compilation after this function is called.
* @see [GradleKotlinCompilerWork] * @see [GradleKotlinCompilerWork]
*/ */
internal abstract fun callCompilerAsync(args: T, sourceRoots: SourceRoots, changedFiles: ChangedFiles) internal abstract fun callCompilerAsync(args: T, sourceRoots: SourceRoots, inputChanges: InputChanges)
@get:Input @get:Input
internal val multiPlatformEnabled: Property<Boolean> = objects.property(Boolean::class.java) internal val multiPlatformEnabled: Property<Boolean> = objects.property(Boolean::class.java)
@@ -679,7 +675,7 @@ abstract class KotlinCompile @Inject constructor(
override fun getSourceRoots(): SourceRoots.ForJvm = jvmSourceRoots override fun getSourceRoots(): SourceRoots.ForJvm = jvmSourceRoots
override fun callCompilerAsync(args: K2JVMCompilerArguments, sourceRoots: SourceRoots, changedFiles: ChangedFiles) { override fun callCompilerAsync(args: K2JVMCompilerArguments, sourceRoots: SourceRoots, inputChanges: InputChanges) {
sourceRoots as SourceRoots.ForJvm sourceRoots as SourceRoots.ForJvm
validateKotlinAndJavaHasSameTargetCompatibility(args) validateKotlinAndJavaHasSameTargetCompatibility(args)
@@ -691,15 +687,12 @@ abstract class KotlinCompile @Inject constructor(
val icEnv = if (isIncrementalCompilationEnabled()) { val icEnv = if (isIncrementalCompilationEnabled()) {
val classpathChanges = when { val classpathChanges = when {
!classpathSnapshotProperties.useClasspathSnapshot.get() -> ClasspathChanges.NotAvailable.ClasspathSnapshotIsDisabled !classpathSnapshotProperties.useClasspathSnapshot.get() -> ClasspathChanges.NotAvailable.ClasspathSnapshotIsDisabled
else -> when (changedFiles) { inputChanges.isIncremental -> getClasspathChanges(inputChanges)
is ChangedFiles.Known -> getClasspathChanges(changedFiles) else -> ClasspathChanges.NotAvailable.ForNonIncrementalRun
is ChangedFiles.Unknown -> ClasspathChanges.NotAvailable.ForNonIncrementalRun
is ChangedFiles.Dependencies -> error("Unexpected type: ${changedFiles.javaClass.name}")
}
} }
logger.info(USING_JVM_INCREMENTAL_COMPILATION_MESSAGE) logger.info(USING_JVM_INCREMENTAL_COMPILATION_MESSAGE)
IncrementalCompilationEnvironment( IncrementalCompilationEnvironment(
changedFiles = changedFiles, changedFiles = getChangedFiles(inputChanges, incrementalProps),
classpathChanges = classpathChanges, classpathChanges = classpathChanges,
workingDir = taskBuildDirectory.get().asFile, workingDir = taskBuildDirectory.get().asFile,
usePreciseJavaTracking = usePreciseJavaTracking, usePreciseJavaTracking = usePreciseJavaTracking,
@@ -797,29 +790,17 @@ abstract class KotlinCompile @Inject constructor(
return super.source(*sources) return super.source(*sources)
} }
private fun getClasspathChanges(knownChangedFiles: ChangedFiles.Known): ClasspathChanges { private fun getClasspathChanges(@Suppress("UNUSED_PARAMETER") inputChanges: InputChanges): ClasspathChanges {
// Find current snapshot files that have been changed (added or modified) val currentSnapshotFiles = classpathSnapshotProperties.classpathSnapshot.files.toList()
val currentSnapshotFiles = classpathSnapshotProperties.classpathSnapshot.files
val addedOrModifiedFiles = knownChangedFiles.modified.toSet()
val (changedCurrentSnapshotFiles, unchangedCurrentSnapshotFiles) = currentSnapshotFiles.partition { it in addedOrModifiedFiles }
// Find previous snapshot files that have been changed (modified or removed)
val previousSnapshotFiles = getClasspathSnapshotFilesInDir(classpathSnapshotProperties.classpathSnapshotDir.get().asFile) val previousSnapshotFiles = getClasspathSnapshotFilesInDir(classpathSnapshotProperties.classpathSnapshotDir.get().asFile)
var unchangedSnapshotIndex = 0
var unchangedSnapshot: ByteArray? = unchangedCurrentSnapshotFiles.getOrNull(unchangedSnapshotIndex)?.readBytes()
val changedPreviousSnapshotFiles = previousSnapshotFiles.filter {
if (unchangedSnapshot != null && it.readBytes().contentEquals(unchangedSnapshot)) {
unchangedSnapshotIndex++
unchangedSnapshot = unchangedCurrentSnapshotFiles.getOrNull(unchangedSnapshotIndex)?.readBytes()
false
} else true
}
// Compute changes for the changed snapshot files only, ignoring unchanged ones // TODO: Compute changes for the changed snapshot files only, ignoring unchanged ones.
val changedCurrentSnapshot = ClasspathSnapshotSerializer.load(changedCurrentSnapshotFiles) // We'll need to be careful when the classpath contains duplicate classes, as we'll need to look at the entire classpath in order to
val changedPreviousSnapshot = ClasspathSnapshotSerializer.load(changedPreviousSnapshotFiles) // detect them.
val currentSnapshot = ClasspathSnapshotSerializer.load(currentSnapshotFiles)
val previousSnapshot = ClasspathSnapshotSerializer.load(previousSnapshotFiles)
return ClasspathChangesComputer.compute(changedCurrentSnapshot, changedPreviousSnapshot) return ClasspathChangesComputer.compute(currentSnapshot, previousSnapshot)
} }
/** /**
@@ -1095,7 +1076,7 @@ abstract class Kotlin2JsCompile @Inject constructor(
override val incrementalProps: List<FileCollection> override val incrementalProps: List<FileCollection>
get() = super.incrementalProps + listOf(friendDependencies) get() = super.incrementalProps + listOf(friendDependencies)
override fun callCompilerAsync(args: K2JSCompilerArguments, sourceRoots: SourceRoots, changedFiles: ChangedFiles) { override fun callCompilerAsync(args: K2JSCompilerArguments, sourceRoots: SourceRoots, inputChanges: InputChanges) {
sourceRoots as SourceRoots.KotlinOnly sourceRoots as SourceRoots.KotlinOnly
logger.debug("Calling compiler") logger.debug("Calling compiler")
@@ -1130,7 +1111,7 @@ abstract class Kotlin2JsCompile @Inject constructor(
val icEnv = if (isIncrementalCompilationEnabled()) { val icEnv = if (isIncrementalCompilationEnabled()) {
logger.info(USING_JS_INCREMENTAL_COMPILATION_MESSAGE) logger.info(USING_JS_INCREMENTAL_COMPILATION_MESSAGE)
IncrementalCompilationEnvironment( IncrementalCompilationEnvironment(
changedFiles, getChangedFiles(inputChanges, incrementalProps),
ClasspathChanges.NotAvailable.ForJSCompiler, ClasspathChanges.NotAvailable.ForJSCompiler,
taskBuildDirectory.get().asFile, taskBuildDirectory.get().asFile,
multiModuleICSettings = multiModuleICSettings multiModuleICSettings = multiModuleICSettings