KT-34862 use relative path for incremental build cache

This commit is contained in:
nataliya.valtman
2020-10-02 13:43:20 +03:00
parent 09043fb98d
commit e1a380ec95
9 changed files with 120 additions and 20 deletions
@@ -0,0 +1,33 @@
/*
* Copyright 2010-2020 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.incremental.storage
import java.io.File
open class IncrementalFileToPathConverter(val rootProjectDir: File?) : FileToPathConverter {
//project root dir
private val projectDirPath = rootProjectDir?.normalize()?.absolutePath
override fun toPath(file: File): String {
val path = file.normalize().absolutePath
return when {
projectDirPath == null || !path.startsWith(projectDirPath) -> path
else -> PROJECT_DIR_PLACEHOLDER + path.substring(projectDirPath.length)
}
}
override fun toFile(path: String): File =
when {
rootProjectDir != null && path.startsWith(PROJECT_DIR_PLACEHOLDER) -> rootProjectDir.resolve(path.substring(PATH_PREFIX.length))
else -> File(path)
}
private companion object {
private const val PROJECT_DIR_PLACEHOLDER = "${'$'}PROJECT_DIR$"
private const val PATH_PREFIX = "$PROJECT_DIR_PLACEHOLDER/"
}
}
@@ -0,0 +1,61 @@
/*
* Copyright 2010-2020 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.incremental.storage
import org.jetbrains.kotlin.TestWithWorkingDir
import org.junit.Test
import java.io.File
internal class IncrementalFileToPathConverterTest : TestWithWorkingDir() {
@Test
fun testPathTransform() {
val relativeFilePath = "testFile.txt"
val transformedPath = testPathTransformation(workingDir.resolve("testDir"), relativeFilePath)
assertEquals("${'$'}PROJECT_DIR${'$'}/$relativeFilePath", transformedPath)
}
@Test
fun testComplicatedProjectRootPath() {
val relativeFilePath = "testFile.txt"
val transformedPath = testPathTransformation(workingDir.resolve("first/../testDir"), relativeFilePath)
assertEquals("${'$'}PROJECT_DIR${'$'}/$relativeFilePath", transformedPath)
}
@Test
fun testInccorectProjectRootPath() {
val relativeFilePath = "testFile.txt"
val transformedPath = testPathTransformation(workingDir.resolve("testDir/"), relativeFilePath)
assertEquals("${'$'}PROJECT_DIR${'$'}/$relativeFilePath", transformedPath)
}
@Test
fun testFileOutOfProject() {
val relativeFilePath = "../testFile.txt"
val transformedPath = testPathTransformation(workingDir.resolve("testDir"), relativeFilePath)
assertEquals("${workingDir.absolutePath}/testFile.txt", transformedPath)
}
@Test
fun testFileWithExtraSlash() {
val relativeFilePath = "testFile.txt/"
val transformedPath = testPathTransformation(workingDir.resolve("testDir"), relativeFilePath)
assertEquals("${'$'}PROJECT_DIR${'$'}/testFile.txt", transformedPath)
}
private fun testPathTransformation(projectRoot: File, relativeFilePath: String): String {
val pathConverter = IncrementalFileToPathConverter(projectRoot)
val testFile = projectRoot.resolve(relativeFilePath)
val transformedPath = pathConverter.toPath(testFile)
assertEquals(testFile.normalize().absolutePath, pathConverter.toFile(transformedPath).normalize().absolutePath)
return transformedPath
}
}
@@ -589,6 +589,8 @@ abstract class CompileServiceImplBase(
} }
} }
val projectRoot = incrementalCompilationOptions.modulesInfo.projectRoot
val compiler = IncrementalJvmCompilerRunner( val compiler = IncrementalJvmCompilerRunner(
workingDir, workingDir,
reporter, reporter,
@@ -599,7 +601,7 @@ abstract class CompileServiceImplBase(
kotlinSourceFilesExtensions = allKotlinExtensions kotlinSourceFilesExtensions = allKotlinExtensions
) )
return try { return try {
compiler.compile(allKotlinFiles, k2jvmArgs, compilerMessageCollector, changedFiles) compiler.compile(allKotlinFiles, k2jvmArgs, compilerMessageCollector, changedFiles, projectRoot)
} finally { } finally {
reporter.flush() reporter.flush()
} }
@@ -17,16 +17,17 @@
package org.jetbrains.kotlin.incremental package org.jetbrains.kotlin.incremental
import org.jetbrains.kotlin.incremental.storage.BasicMapsOwner import org.jetbrains.kotlin.incremental.storage.BasicMapsOwner
import org.jetbrains.kotlin.incremental.storage.FileToCanonicalPathConverter import org.jetbrains.kotlin.incremental.storage.IncrementalFileToPathConverter
import org.jetbrains.kotlin.serialization.SerializerExtensionProtocol import org.jetbrains.kotlin.serialization.SerializerExtensionProtocol
import java.io.File import java.io.File
private val PATH_CONVERTER = FileToCanonicalPathConverter
abstract class IncrementalCachesManager<PlatformCache : AbstractIncrementalCache<*>>( abstract class IncrementalCachesManager<PlatformCache : AbstractIncrementalCache<*>>(
cachesRootDir: File, cachesRootDir: File,
rootProjectDir: File?,
protected val reporter: ICReporter protected val reporter: ICReporter
) { ) {
val pathConverter = IncrementalFileToPathConverter(rootProjectDir)
private val caches = arrayListOf<BasicMapsOwner>() private val caches = arrayListOf<BasicMapsOwner>()
protected fun <T : BasicMapsOwner> T.registerCache() { protected fun <T : BasicMapsOwner> T.registerCache() {
caches.add(this) caches.add(this)
@@ -36,7 +37,7 @@ abstract class IncrementalCachesManager<PlatformCache : AbstractIncrementalCache
private val lookupCacheDir = File(cachesRootDir, "lookups").apply { mkdirs() } private val lookupCacheDir = File(cachesRootDir, "lookups").apply { mkdirs() }
val inputsCache: InputsCache = InputsCache(inputSnapshotsCacheDir, reporter).apply { registerCache() } val inputsCache: InputsCache = InputsCache(inputSnapshotsCacheDir, reporter).apply { registerCache() }
val lookupCache: LookupStorage = LookupStorage(lookupCacheDir, PATH_CONVERTER).apply { registerCache() } val lookupCache: LookupStorage = LookupStorage(lookupCacheDir, pathConverter).apply { registerCache() }
abstract val platformCache: PlatformCache abstract val platformCache: PlatformCache
fun close(flush: Boolean = false): Boolean { fun close(flush: Boolean = false): Boolean {
@@ -66,20 +67,20 @@ abstract class IncrementalCachesManager<PlatformCache : AbstractIncrementalCache
class IncrementalJvmCachesManager( class IncrementalJvmCachesManager(
cacheDirectory: File, cacheDirectory: File,
rootProjectDir: File?,
outputDir: File, outputDir: File,
reporter: ICReporter reporter: ICReporter
) : IncrementalCachesManager<IncrementalJvmCache>(cacheDirectory, reporter) { ) : IncrementalCachesManager<IncrementalJvmCache>(cacheDirectory, rootProjectDir, reporter) {
private val jvmCacheDir = File(cacheDirectory, "jvm").apply { mkdirs() } private val jvmCacheDir = File(cacheDirectory, "jvm").apply { mkdirs() }
override val platformCache = IncrementalJvmCache(jvmCacheDir, outputDir, PATH_CONVERTER).apply { registerCache() } override val platformCache = IncrementalJvmCache(jvmCacheDir, outputDir, pathConverter).apply { registerCache() }
} }
class IncrementalJsCachesManager( class IncrementalJsCachesManager(
cachesRootDir: File, cachesRootDir: File,
rootProjectDir: File,
reporter: ICReporter, reporter: ICReporter,
serializerProtocol: SerializerExtensionProtocol serializerProtocol: SerializerExtensionProtocol
) : IncrementalCachesManager<IncrementalJsCache>(cachesRootDir, reporter) { ) : IncrementalCachesManager<IncrementalJsCache>(cachesRootDir, rootProjectDir, reporter) {
private val jsCacheFile = File(cachesRootDir, "js").apply { mkdirs() } private val jsCacheFile = File(cachesRootDir, "js").apply { mkdirs() }
override val platformCache = IncrementalJsCache(jsCacheFile, PATH_CONVERTER, serializerProtocol).apply { registerCache() } override val platformCache = IncrementalJsCache(jsCacheFile, pathConverter, serializerProtocol).apply { registerCache() }
} }
@@ -54,7 +54,7 @@ abstract class IncrementalCompilerRunner<
protected open val kotlinSourceFilesExtensions: List<String> = DEFAULT_KOTLIN_SOURCE_FILES_EXTENSIONS protected open val kotlinSourceFilesExtensions: List<String> = DEFAULT_KOTLIN_SOURCE_FILES_EXTENSIONS
protected abstract fun isICEnabled(): Boolean protected abstract fun isICEnabled(): Boolean
protected abstract fun createCacheManager(args: Args): CacheManager protected abstract fun createCacheManager(args: Args, projectDir: File?): CacheManager
protected abstract fun destinationDir(args: Args): File protected abstract fun destinationDir(args: Args): File
fun compile( fun compile(
@@ -63,16 +63,17 @@ abstract class IncrementalCompilerRunner<
messageCollector: MessageCollector, messageCollector: MessageCollector,
// when [providedChangedFiles] is not null, changes are provided by external system (e.g. Gradle) // when [providedChangedFiles] is not null, changes are provided by external system (e.g. Gradle)
// otherwise we track source files changes ourselves. // otherwise we track source files changes ourselves.
providedChangedFiles: ChangedFiles? providedChangedFiles: ChangedFiles?,
projectDir: File? = null
): ExitCode { ): ExitCode {
assert(isICEnabled()) { "Incremental compilation is not enabled" } assert(isICEnabled()) { "Incremental compilation is not enabled" }
var caches = createCacheManager(args) var caches = createCacheManager(args, projectDir)
fun rebuild(reason: () -> String): ExitCode { fun rebuild(reason: () -> String): ExitCode {
reporter.report(reason) reporter.report(reason)
caches.close(false) caches.close(false)
clearLocalStateOnRebuild(args) clearLocalStateOnRebuild(args)
caches = createCacheManager(args) caches = createCacheManager(args, projectDir)
if (providedChangedFiles == null) { if (providedChangedFiles == null) {
caches.inputsCache.sourceSnapshotMap.compareAndUpdate(allSourceFiles) caches.inputsCache.sourceSnapshotMap.compareAndUpdate(allSourceFiles)
} }
@@ -68,7 +68,7 @@ inline fun <R> withJsIC(fn: () -> R): R {
} }
class IncrementalJsCompilerRunner( class IncrementalJsCompilerRunner(
workingDir: File, private val workingDir: File,
reporter: ICReporter, reporter: ICReporter,
buildHistoryFile: File, buildHistoryFile: File,
private val modulesApiHistory: ModulesApiHistory, private val modulesApiHistory: ModulesApiHistory,
@@ -82,9 +82,9 @@ class IncrementalJsCompilerRunner(
override fun isICEnabled(): Boolean = override fun isICEnabled(): Boolean =
IncrementalCompilation.isEnabledForJs() IncrementalCompilation.isEnabledForJs()
override fun createCacheManager(args: K2JSCompilerArguments): IncrementalJsCachesManager { override fun createCacheManager(args: K2JSCompilerArguments, projectDir: File?): IncrementalJsCachesManager {
val serializerProtocol = if (!args.isIrBackendEnabled()) JsSerializerProtocol else KlibMetadataSerializerProtocol val serializerProtocol = if (!args.isIrBackendEnabled()) JsSerializerProtocol else KlibMetadataSerializerProtocol
return IncrementalJsCachesManager(cacheDirectory, reporter, serializerProtocol) return IncrementalJsCachesManager(cacheDirectory, workingDir, reporter, serializerProtocol)
} }
override fun destinationDir(args: K2JSCompilerArguments): File = override fun destinationDir(args: K2JSCompilerArguments): File =
@@ -120,8 +120,9 @@ class IncrementalJvmCompilerRunner(
override fun isICEnabled(): Boolean = override fun isICEnabled(): Boolean =
IncrementalCompilation.isEnabledForJvm() IncrementalCompilation.isEnabledForJvm()
override fun createCacheManager(args: K2JVMCompilerArguments): IncrementalJvmCachesManager = //TODO
IncrementalJvmCachesManager(cacheDirectory, File(args.destination), reporter) override fun createCacheManager(args: K2JVMCompilerArguments, projectDir: File?): IncrementalJvmCachesManager =
IncrementalJvmCachesManager(cacheDirectory, projectDir, File(args.destination), reporter)
override fun destinationDir(args: K2JVMCompilerArguments): File = override fun destinationDir(args: K2JVMCompilerArguments): File =
args.destinationAsFile args.destinationAsFile
@@ -7,7 +7,7 @@ package org.jetbrains.kotlin.jps.incremental
import java.io.File import java.io.File
private val NORMAL_VERSION = 13 private val NORMAL_VERSION = 14
private val NORMAL_VERSION_FILE_NAME = "format-version.txt" private val NORMAL_VERSION_FILE_NAME = "format-version.txt"
fun localCacheVersionManager(dataRoot: File, isCachesEnabled: Boolean) = fun localCacheVersionManager(dataRoot: File, isCachesEnabled: Boolean) =
@@ -27,6 +27,7 @@ internal open class KotlinCompileTaskData(
File(File(compilation.target.project.buildDir, KOTLIN_BUILD_DIR_NAME), taskName) File(File(compilation.target.project.buildDir, KOTLIN_BUILD_DIR_NAME), taskName)
} }
//TODO
val buildHistoryFile: File by project.provider { val buildHistoryFile: File by project.provider {
File(taskBuildDirectory, "build-history.bin") File(taskBuildDirectory, "build-history.bin")
} }