From 085dbb537a8ef3baba7b618bcdb7b7025dcf3885 Mon Sep 17 00:00:00 2001 From: Sergey Rostov Date: Mon, 15 Oct 2018 12:41:13 +0300 Subject: [PATCH] IC: move CacheVersionManager from build_common to jps plugin Original commit: d91f6f8c433fcc49b6851b734f00bedc1547942b --- .../jps/build/AbstractIncrementalJpsTest.kt | 2 +- .../kotlin/jps/build/KotlinJpsBuildTest.kt | 2 +- ...positeLookupsCacheAttributesManagerTest.kt | 4 +- .../jetbrains/kotlin/jps/build/KotlinChunk.kt | 2 +- .../kotlin/jps/build/KotlinCompileContext.kt | 6 +- .../kotlin/jps/build/TestingBuildLogger.kt | 2 +- .../jps/incremental/CacheAttributesDiff.kt | 33 ++++++++++ .../jps/incremental/CacheAttributesManager.kt | 62 +++++++++++++++++++ .../kotlin/jps/incremental/CacheStatus.kt | 31 ++++++++++ .../jps/incremental/CacheVersionManager.kt | 60 ++++++++++++++++++ .../CompositeLookupsCacheAttributes.kt | 6 +- .../jetbrains/kotlin/jps/incremental/local.kt | 17 +++++ .../kotlin/jps/incremental/lookups.kt | 17 +++++ .../jps/targets/KotlinModuleBuildTarget.kt | 6 +- 14 files changed, 235 insertions(+), 15 deletions(-) create mode 100644 jps/jps-plugin/src/org/jetbrains/kotlin/jps/incremental/CacheAttributesDiff.kt create mode 100644 jps/jps-plugin/src/org/jetbrains/kotlin/jps/incremental/CacheAttributesManager.kt create mode 100644 jps/jps-plugin/src/org/jetbrains/kotlin/jps/incremental/CacheStatus.kt create mode 100644 jps/jps-plugin/src/org/jetbrains/kotlin/jps/incremental/CacheVersionManager.kt create mode 100644 jps/jps-plugin/src/org/jetbrains/kotlin/jps/incremental/local.kt create mode 100644 jps/jps-plugin/src/org/jetbrains/kotlin/jps/incremental/lookups.kt diff --git a/jps/jps-plugin/jps-tests/test/org/jetbrains/kotlin/jps/build/AbstractIncrementalJpsTest.kt b/jps/jps-plugin/jps-tests/test/org/jetbrains/kotlin/jps/build/AbstractIncrementalJpsTest.kt index b5a4fe9598d..293f350c618 100644 --- a/jps/jps-plugin/jps-tests/test/org/jetbrains/kotlin/jps/build/AbstractIncrementalJpsTest.kt +++ b/jps/jps-plugin/jps-tests/test/org/jetbrains/kotlin/jps/build/AbstractIncrementalJpsTest.kt @@ -46,7 +46,7 @@ import org.jetbrains.jps.util.JpsPathUtil import org.jetbrains.kotlin.cli.common.arguments.K2MetadataCompilerArguments import org.jetbrains.kotlin.config.IncrementalCompilation import org.jetbrains.kotlin.incremental.LookupSymbol -import org.jetbrains.kotlin.incremental.storage.version.CacheAttributesDiff +import org.jetbrains.kotlin.jps.incremental.CacheAttributesDiff import org.jetbrains.kotlin.incremental.storage.version.CacheVersionManager import org.jetbrains.kotlin.incremental.testingUtils.* import org.jetbrains.kotlin.jps.build.dependeciestxt.ModulesTxt diff --git a/jps/jps-plugin/jps-tests/test/org/jetbrains/kotlin/jps/build/KotlinJpsBuildTest.kt b/jps/jps-plugin/jps-tests/test/org/jetbrains/kotlin/jps/build/KotlinJpsBuildTest.kt index 8bf4420fc8e..f2ac3589757 100644 --- a/jps/jps-plugin/jps-tests/test/org/jetbrains/kotlin/jps/build/KotlinJpsBuildTest.kt +++ b/jps/jps-plugin/jps-tests/test/org/jetbrains/kotlin/jps/build/KotlinJpsBuildTest.kt @@ -59,7 +59,7 @@ import org.jetbrains.kotlin.codegen.JvmCodegenUtil import org.jetbrains.kotlin.config.IncrementalCompilation import org.jetbrains.kotlin.config.KotlinCompilerVersion.TEST_IS_PRE_RELEASE_SYSTEM_PROPERTY import org.jetbrains.kotlin.incremental.components.LookupTracker -import org.jetbrains.kotlin.incremental.storage.version.CacheAttributesDiff +import org.jetbrains.kotlin.jps.incremental.CacheAttributesDiff import org.jetbrains.kotlin.incremental.withIC import org.jetbrains.kotlin.jps.build.KotlinJpsBuildTest.LibraryDependency.* import org.jetbrains.kotlin.jps.model.kotlinCommonCompilerArguments diff --git a/jps/jps-plugin/jps-tests/test/org/jetbrains/kotlin/jps/incremental/CompositeLookupsCacheAttributesManagerTest.kt b/jps/jps-plugin/jps-tests/test/org/jetbrains/kotlin/jps/incremental/CompositeLookupsCacheAttributesManagerTest.kt index 926105dca9c..4cbabd71dea 100644 --- a/jps/jps-plugin/jps-tests/test/org/jetbrains/kotlin/jps/incremental/CompositeLookupsCacheAttributesManagerTest.kt +++ b/jps/jps-plugin/jps-tests/test/org/jetbrains/kotlin/jps/incremental/CompositeLookupsCacheAttributesManagerTest.kt @@ -5,8 +5,8 @@ package org.jetbrains.kotlin.jps.incremental -import org.jetbrains.kotlin.incremental.storage.version.CacheStatus -import org.jetbrains.kotlin.incremental.storage.version.loadDiff +import org.jetbrains.kotlin.jps.incremental.CacheStatus +import org.jetbrains.kotlin.jps.incremental.loadDiff import org.junit.Test import java.io.File import kotlin.test.assertEquals diff --git a/jps/jps-plugin/src/org/jetbrains/kotlin/jps/build/KotlinChunk.kt b/jps/jps-plugin/src/org/jetbrains/kotlin/jps/build/KotlinChunk.kt index 626b78c4458..b28a19750d2 100644 --- a/jps/jps-plugin/src/org/jetbrains/kotlin/jps/build/KotlinChunk.kt +++ b/jps/jps-plugin/src/org/jetbrains/kotlin/jps/build/KotlinChunk.kt @@ -9,7 +9,7 @@ import org.jetbrains.jps.incremental.ModuleBuildTarget import org.jetbrains.kotlin.config.ApiVersion import org.jetbrains.kotlin.config.LanguageVersion import org.jetbrains.kotlin.config.VersionView -import org.jetbrains.kotlin.incremental.storage.version.CacheStatus +import org.jetbrains.kotlin.jps.incremental.CacheStatus import org.jetbrains.kotlin.jps.incremental.JpsIncrementalCache import org.jetbrains.kotlin.jps.incremental.getKotlinCache import org.jetbrains.kotlin.jps.model.kotlinCompilerArguments diff --git a/jps/jps-plugin/src/org/jetbrains/kotlin/jps/build/KotlinCompileContext.kt b/jps/jps-plugin/src/org/jetbrains/kotlin/jps/build/KotlinCompileContext.kt index 1eaa8a049d6..d3030087750 100644 --- a/jps/jps-plugin/src/org/jetbrains/kotlin/jps/build/KotlinCompileContext.kt +++ b/jps/jps-plugin/src/org/jetbrains/kotlin/jps/build/KotlinCompileContext.kt @@ -11,9 +11,9 @@ import org.jetbrains.jps.incremental.FSOperations import org.jetbrains.jps.incremental.GlobalContextKey import org.jetbrains.jps.incremental.fs.CompilationRound import org.jetbrains.kotlin.incremental.LookupSymbol -import org.jetbrains.kotlin.incremental.storage.version.CacheAttributesDiff -import org.jetbrains.kotlin.incremental.storage.version.CacheStatus -import org.jetbrains.kotlin.incremental.storage.version.loadDiff +import org.jetbrains.kotlin.jps.incremental.CacheAttributesDiff +import org.jetbrains.kotlin.jps.incremental.CacheStatus +import org.jetbrains.kotlin.jps.incremental.loadDiff import org.jetbrains.kotlin.jps.incremental.* import org.jetbrains.kotlin.jps.targets.KotlinTargetsIndex import org.jetbrains.kotlin.jps.targets.KotlinTargetsIndexBuilder diff --git a/jps/jps-plugin/src/org/jetbrains/kotlin/jps/build/TestingBuildLogger.kt b/jps/jps-plugin/src/org/jetbrains/kotlin/jps/build/TestingBuildLogger.kt index 3d9852e9237..f7911aed25c 100644 --- a/jps/jps-plugin/src/org/jetbrains/kotlin/jps/build/TestingBuildLogger.kt +++ b/jps/jps-plugin/src/org/jetbrains/kotlin/jps/build/TestingBuildLogger.kt @@ -18,7 +18,7 @@ package org.jetbrains.kotlin.jps.build import org.jetbrains.jps.incremental.CompileContext import org.jetbrains.jps.incremental.ModuleLevelBuilder -import org.jetbrains.kotlin.incremental.storage.version.CacheAttributesDiff +import org.jetbrains.kotlin.jps.incremental.CacheAttributesDiff import org.jetbrains.kotlin.jps.targets.KotlinModuleBuildTarget import java.io.File diff --git a/jps/jps-plugin/src/org/jetbrains/kotlin/jps/incremental/CacheAttributesDiff.kt b/jps/jps-plugin/src/org/jetbrains/kotlin/jps/incremental/CacheAttributesDiff.kt new file mode 100644 index 00000000000..67773a3f925 --- /dev/null +++ b/jps/jps-plugin/src/org/jetbrains/kotlin/jps/incremental/CacheAttributesDiff.kt @@ -0,0 +1,33 @@ +/* + * Copyright 2010-2018 JetBrains s.r.o. 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.jps.incremental + +/** + * Diff between actual and expected cache attributes. + * [status] are calculated based on this diff (see [CacheStatus]). + * Based on that [status] system may perform required actions (i.e. rebuild something, clearing caches, etc...). + * + * [CacheAttributesDiff] can be used to cache current attribute values and as facade for version operations. + */ +data class CacheAttributesDiff( + val manager: CacheAttributesManager, + val actual: Attrs?, + val expected: Attrs? +) { + val status: CacheStatus + get() = + if (expected != null) { + if (actual != null && manager.isCompatible(actual, expected)) CacheStatus.VALID + else CacheStatus.INVALID + } else { + if (actual != null) CacheStatus.SHOULD_BE_CLEARED + else CacheStatus.CLEARED + } + + override fun toString(): String { + return "$status: actual=$actual -> expected=$expected" + } +} \ No newline at end of file diff --git a/jps/jps-plugin/src/org/jetbrains/kotlin/jps/incremental/CacheAttributesManager.kt b/jps/jps-plugin/src/org/jetbrains/kotlin/jps/incremental/CacheAttributesManager.kt new file mode 100644 index 00000000000..3aa1d3c8db9 --- /dev/null +++ b/jps/jps-plugin/src/org/jetbrains/kotlin/jps/incremental/CacheAttributesManager.kt @@ -0,0 +1,62 @@ +/* + * Copyright 2010-2018 JetBrains s.r.o. 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.jps.incremental + +/** + * Manages cache attributes values. + * + * Attribute values can be loaded by calling [loadActual]. + * Based on loaded actual and fixed [expected] values [CacheAttributesDiff] can be constructed which can calculate [CacheStatus]. + * Build system may perform required actions based on that (i.e. rebuild something, clearing caches, etc...). + * + * [CacheAttributesDiff] can be used to cache current attribute values and then can be used as facade for cache version operations. + */ +interface CacheAttributesManager { + /** + * Cache attribute values expected by the current version of build system and compiler. + * `null` means that cache is not required (incremental compilation is disabled). + */ + val expected: Attrs? + + /** + * Load actual cache attribute values. + * `null` means that cache is not yet created. + * + * This is internal operation that should be implemented by particular implementation of CacheAttributesManager. + * Consider using `loadDiff().actual` for getting actual values. + */ + fun loadActual(): Attrs? + + /** + * Write [values] as cache attributes for next build execution. + */ + fun writeVersion(values: Attrs? = expected) + + /** + * Check if cache with [actual] attributes values can be used when [expected] attributes are required. + */ + fun isCompatible(actual: Attrs, expected: Attrs): Boolean = actual == expected +} + +fun CacheAttributesManager.loadDiff( + actual: Attrs? = this.loadActual(), + expected: Attrs? = this.expected +) = CacheAttributesDiff(this, actual, expected) + +fun CacheAttributesManager.loadAndCheckStatus() = + loadDiff().status + +/** + * This method is kept only for compatibility. + * Delete actual cache attributes values if it existed. + */ +@Deprecated( + message = "Consider using `this.loadDiff().saveExpectedIfNeeded()` and cache `loadDiff()` result.", + replaceWith = ReplaceWith("writeVersion(null)") +) +fun CacheAttributesManager<*>.clean() { + writeVersion(null) +} \ No newline at end of file diff --git a/jps/jps-plugin/src/org/jetbrains/kotlin/jps/incremental/CacheStatus.kt b/jps/jps-plugin/src/org/jetbrains/kotlin/jps/incremental/CacheStatus.kt new file mode 100644 index 00000000000..5cff0300018 --- /dev/null +++ b/jps/jps-plugin/src/org/jetbrains/kotlin/jps/incremental/CacheStatus.kt @@ -0,0 +1,31 @@ +/* + * Copyright 2010-2018 JetBrains s.r.o. 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.jps.incremental + +/** + * Status that is used by system to perform required actions (i.e. rebuild something, clearing caches, etc...). + */ +enum class CacheStatus { + /** + * Cache is valid and ready to use. + */ + VALID, + + /** + * Cache is not exists or have outdated versions and/or other attributes. + */ + INVALID, + + /** + * Cache is exists, but not required anymore. + */ + SHOULD_BE_CLEARED, + + /** + * Cache is not exists and not required. + */ + CLEARED +} \ No newline at end of file diff --git a/jps/jps-plugin/src/org/jetbrains/kotlin/jps/incremental/CacheVersionManager.kt b/jps/jps-plugin/src/org/jetbrains/kotlin/jps/incremental/CacheVersionManager.kt new file mode 100644 index 00000000000..5f0192aabb2 --- /dev/null +++ b/jps/jps-plugin/src/org/jetbrains/kotlin/jps/incremental/CacheVersionManager.kt @@ -0,0 +1,60 @@ +/* + * Copyright 2010-2018 JetBrains s.r.o. 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.jps.incremental + +import org.jetbrains.annotations.TestOnly +import org.jetbrains.kotlin.metadata.jvm.deserialization.JvmBytecodeBinaryVersion +import org.jetbrains.kotlin.metadata.jvm.deserialization.JvmMetadataVersion +import java.io.File +import java.io.IOException + +/** + * Manages files with actual version [loadActual] and provides expected version [expected]. + * Based on that actual and expected versions [CacheStatus] can be calculated. + * This can be done by constructing [CacheAttributesDiff] and calling [CacheAttributesDiff.status]. + * Based on that status system may perform required actions (i.e. rebuild something, clearing caches, etc...). + */ +class CacheVersionManager( + private val versionFile: File, + expectedOwnVersion: Int? +) : CacheAttributesManager { + override val expected: CacheVersion? = + if (expectedOwnVersion == null) null + else { + val metadata = JvmMetadataVersion.INSTANCE + val bytecode = JvmBytecodeBinaryVersion.INSTANCE + + CacheVersion( + expectedOwnVersion * 1000000 + + bytecode.major * 10000 + bytecode.minor * 100 + + metadata.major * 1000 + metadata.minor + ) + } + + override fun loadActual(): CacheVersion? = + if (!versionFile.exists()) null + else try { + CacheVersion(versionFile.readText().toInt()) + } catch (e: NumberFormatException) { + null + } catch (e: IOException) { + null + } + + override fun writeVersion(values: CacheVersion?) { + if (values == null) versionFile.delete() + else { + versionFile.parentFile.mkdirs() + versionFile.writeText(values.version.toString()) + } + } + + @get:TestOnly + val versionFileForTesting: File + get() = versionFile +} + +data class CacheVersion(val version: Int) \ No newline at end of file diff --git a/jps/jps-plugin/src/org/jetbrains/kotlin/jps/incremental/CompositeLookupsCacheAttributes.kt b/jps/jps-plugin/src/org/jetbrains/kotlin/jps/incremental/CompositeLookupsCacheAttributes.kt index 5e9c6a9f16f..864d9c22f96 100644 --- a/jps/jps-plugin/src/org/jetbrains/kotlin/jps/incremental/CompositeLookupsCacheAttributes.kt +++ b/jps/jps-plugin/src/org/jetbrains/kotlin/jps/incremental/CompositeLookupsCacheAttributes.kt @@ -6,9 +6,9 @@ package org.jetbrains.kotlin.jps.incremental import org.jetbrains.annotations.TestOnly -import org.jetbrains.kotlin.incremental.storage.version.CacheAttributesManager -import org.jetbrains.kotlin.incremental.storage.version.CacheVersion -import org.jetbrains.kotlin.incremental.storage.version.lookupsCacheVersionManager +import org.jetbrains.kotlin.jps.incremental.CacheAttributesManager +import org.jetbrains.kotlin.jps.incremental.CacheVersion +import org.jetbrains.kotlin.jps.incremental.lookupsCacheVersionManager import java.io.File import java.io.IOException diff --git a/jps/jps-plugin/src/org/jetbrains/kotlin/jps/incremental/local.kt b/jps/jps-plugin/src/org/jetbrains/kotlin/jps/incremental/local.kt new file mode 100644 index 00000000000..405793d5893 --- /dev/null +++ b/jps/jps-plugin/src/org/jetbrains/kotlin/jps/incremental/local.kt @@ -0,0 +1,17 @@ +/* + * Copyright 2010-2018 JetBrains s.r.o. 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.jps.incremental + +import java.io.File + +private val NORMAL_VERSION = 9 +private val NORMAL_VERSION_FILE_NAME = "format-version.txt" + +fun localCacheVersionManager(dataRoot: File, isCachesEnabled: Boolean) = + CacheVersionManager( + File(dataRoot, NORMAL_VERSION_FILE_NAME), + if (isCachesEnabled) NORMAL_VERSION else null + ) \ No newline at end of file diff --git a/jps/jps-plugin/src/org/jetbrains/kotlin/jps/incremental/lookups.kt b/jps/jps-plugin/src/org/jetbrains/kotlin/jps/incremental/lookups.kt new file mode 100644 index 00000000000..7c06d54a3e4 --- /dev/null +++ b/jps/jps-plugin/src/org/jetbrains/kotlin/jps/incremental/lookups.kt @@ -0,0 +1,17 @@ +/* + * Copyright 2010-2018 JetBrains s.r.o. 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.jps.incremental + +import java.io.File + +private val DATA_CONTAINER_VERSION_FILE_NAME = "data-container-format-version.txt" +private val DATA_CONTAINER_VERSION = 3 + +fun lookupsCacheVersionManager(dataRoot: File, isEnabled: Boolean) = + CacheVersionManager( + File(dataRoot, DATA_CONTAINER_VERSION_FILE_NAME), + if (isEnabled) DATA_CONTAINER_VERSION else null + ) \ No newline at end of file diff --git a/jps/jps-plugin/src/org/jetbrains/kotlin/jps/targets/KotlinModuleBuildTarget.kt b/jps/jps-plugin/src/org/jetbrains/kotlin/jps/targets/KotlinModuleBuildTarget.kt index 69e22c38c16..3e66c6a8662 100644 --- a/jps/jps-plugin/src/org/jetbrains/kotlin/jps/targets/KotlinModuleBuildTarget.kt +++ b/jps/jps-plugin/src/org/jetbrains/kotlin/jps/targets/KotlinModuleBuildTarget.kt @@ -27,9 +27,9 @@ import org.jetbrains.kotlin.incremental.ChangesCollector import org.jetbrains.kotlin.incremental.ExpectActualTrackerImpl import org.jetbrains.kotlin.incremental.components.ExpectActualTracker import org.jetbrains.kotlin.incremental.components.LookupTracker -import org.jetbrains.kotlin.incremental.storage.version.CacheAttributesDiff -import org.jetbrains.kotlin.incremental.storage.version.loadDiff -import org.jetbrains.kotlin.incremental.storage.version.localCacheVersionManager +import org.jetbrains.kotlin.jps.incremental.CacheAttributesDiff +import org.jetbrains.kotlin.jps.incremental.loadDiff +import org.jetbrains.kotlin.jps.incremental.localCacheVersionManager import org.jetbrains.kotlin.jps.build.* import org.jetbrains.kotlin.jps.incremental.JpsIncrementalCache import org.jetbrains.kotlin.jps.model.productionOutputFilePath