diff --git a/build-common/src/org/jetbrains/kotlin/incremental/storage/IncrementalFileToPathConverter.kt b/build-common/src/org/jetbrains/kotlin/incremental/storage/IncrementalFileToPathConverter.kt new file mode 100644 index 00000000000..0e4bf9b6277 --- /dev/null +++ b/build-common/src/org/jetbrains/kotlin/incremental/storage/IncrementalFileToPathConverter.kt @@ -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/" + + } +} \ No newline at end of file diff --git a/build-common/test/org/jetbrains/kotlin/incremental/storage/IncrementalFileToPathConverterTest.kt b/build-common/test/org/jetbrains/kotlin/incremental/storage/IncrementalFileToPathConverterTest.kt new file mode 100644 index 00000000000..b049077b810 --- /dev/null +++ b/build-common/test/org/jetbrains/kotlin/incremental/storage/IncrementalFileToPathConverterTest.kt @@ -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 + } +} \ No newline at end of file diff --git a/compiler/daemon/src/org/jetbrains/kotlin/daemon/CompileServiceImpl.kt b/compiler/daemon/src/org/jetbrains/kotlin/daemon/CompileServiceImpl.kt index 53fa924fe76..11e32d1147a 100644 --- a/compiler/daemon/src/org/jetbrains/kotlin/daemon/CompileServiceImpl.kt +++ b/compiler/daemon/src/org/jetbrains/kotlin/daemon/CompileServiceImpl.kt @@ -589,6 +589,8 @@ abstract class CompileServiceImplBase( } } + val projectRoot = incrementalCompilationOptions.modulesInfo.projectRoot + val compiler = IncrementalJvmCompilerRunner( workingDir, reporter, @@ -599,7 +601,7 @@ abstract class CompileServiceImplBase( kotlinSourceFilesExtensions = allKotlinExtensions ) return try { - compiler.compile(allKotlinFiles, k2jvmArgs, compilerMessageCollector, changedFiles) + compiler.compile(allKotlinFiles, k2jvmArgs, compilerMessageCollector, changedFiles, projectRoot) } finally { reporter.flush() } diff --git a/compiler/incremental-compilation-impl/src/org/jetbrains/kotlin/incremental/IncrementalCachesManager.kt b/compiler/incremental-compilation-impl/src/org/jetbrains/kotlin/incremental/IncrementalCachesManager.kt index 68d63bc85ad..d66f7a92a7e 100644 --- a/compiler/incremental-compilation-impl/src/org/jetbrains/kotlin/incremental/IncrementalCachesManager.kt +++ b/compiler/incremental-compilation-impl/src/org/jetbrains/kotlin/incremental/IncrementalCachesManager.kt @@ -17,16 +17,17 @@ package org.jetbrains.kotlin.incremental 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 java.io.File -private val PATH_CONVERTER = FileToCanonicalPathConverter abstract class IncrementalCachesManager>( cachesRootDir: File, + rootProjectDir: File?, protected val reporter: ICReporter ) { + val pathConverter = IncrementalFileToPathConverter(rootProjectDir) private val caches = arrayListOf() protected fun T.registerCache() { caches.add(this) @@ -36,7 +37,7 @@ abstract class IncrementalCachesManager(cacheDirectory, reporter) { - +) : IncrementalCachesManager(cacheDirectory, rootProjectDir, reporter) { 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( cachesRootDir: File, + rootProjectDir: File, reporter: ICReporter, serializerProtocol: SerializerExtensionProtocol -) : IncrementalCachesManager(cachesRootDir, reporter) { - +) : IncrementalCachesManager(cachesRootDir, rootProjectDir, reporter) { 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() } } \ No newline at end of file diff --git a/compiler/incremental-compilation-impl/src/org/jetbrains/kotlin/incremental/IncrementalCompilerRunner.kt b/compiler/incremental-compilation-impl/src/org/jetbrains/kotlin/incremental/IncrementalCompilerRunner.kt index 80fc775b2ff..fef27c91c2b 100644 --- a/compiler/incremental-compilation-impl/src/org/jetbrains/kotlin/incremental/IncrementalCompilerRunner.kt +++ b/compiler/incremental-compilation-impl/src/org/jetbrains/kotlin/incremental/IncrementalCompilerRunner.kt @@ -54,7 +54,7 @@ abstract class IncrementalCompilerRunner< protected open val kotlinSourceFilesExtensions: List = DEFAULT_KOTLIN_SOURCE_FILES_EXTENSIONS 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 fun compile( @@ -63,16 +63,17 @@ abstract class IncrementalCompilerRunner< messageCollector: MessageCollector, // when [providedChangedFiles] is not null, changes are provided by external system (e.g. Gradle) // otherwise we track source files changes ourselves. - providedChangedFiles: ChangedFiles? + providedChangedFiles: ChangedFiles?, + projectDir: File? = null ): ExitCode { assert(isICEnabled()) { "Incremental compilation is not enabled" } - var caches = createCacheManager(args) + var caches = createCacheManager(args, projectDir) fun rebuild(reason: () -> String): ExitCode { reporter.report(reason) caches.close(false) clearLocalStateOnRebuild(args) - caches = createCacheManager(args) + caches = createCacheManager(args, projectDir) if (providedChangedFiles == null) { caches.inputsCache.sourceSnapshotMap.compareAndUpdate(allSourceFiles) } diff --git a/compiler/incremental-compilation-impl/src/org/jetbrains/kotlin/incremental/IncrementalJsCompilerRunner.kt b/compiler/incremental-compilation-impl/src/org/jetbrains/kotlin/incremental/IncrementalJsCompilerRunner.kt index 9709b75e6a4..51d66a2d573 100644 --- a/compiler/incremental-compilation-impl/src/org/jetbrains/kotlin/incremental/IncrementalJsCompilerRunner.kt +++ b/compiler/incremental-compilation-impl/src/org/jetbrains/kotlin/incremental/IncrementalJsCompilerRunner.kt @@ -68,7 +68,7 @@ inline fun withJsIC(fn: () -> R): R { } class IncrementalJsCompilerRunner( - workingDir: File, + private val workingDir: File, reporter: ICReporter, buildHistoryFile: File, private val modulesApiHistory: ModulesApiHistory, @@ -82,9 +82,9 @@ class IncrementalJsCompilerRunner( override fun isICEnabled(): Boolean = IncrementalCompilation.isEnabledForJs() - override fun createCacheManager(args: K2JSCompilerArguments): IncrementalJsCachesManager { + override fun createCacheManager(args: K2JSCompilerArguments, projectDir: File?): IncrementalJsCachesManager { 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 = diff --git a/compiler/incremental-compilation-impl/src/org/jetbrains/kotlin/incremental/IncrementalJvmCompilerRunner.kt b/compiler/incremental-compilation-impl/src/org/jetbrains/kotlin/incremental/IncrementalJvmCompilerRunner.kt index 2694e9337d2..5ba09291e97 100644 --- a/compiler/incremental-compilation-impl/src/org/jetbrains/kotlin/incremental/IncrementalJvmCompilerRunner.kt +++ b/compiler/incremental-compilation-impl/src/org/jetbrains/kotlin/incremental/IncrementalJvmCompilerRunner.kt @@ -120,8 +120,9 @@ class IncrementalJvmCompilerRunner( override fun isICEnabled(): Boolean = IncrementalCompilation.isEnabledForJvm() - override fun createCacheManager(args: K2JVMCompilerArguments): IncrementalJvmCachesManager = - IncrementalJvmCachesManager(cacheDirectory, File(args.destination), reporter) + //TODO + override fun createCacheManager(args: K2JVMCompilerArguments, projectDir: File?): IncrementalJvmCachesManager = + IncrementalJvmCachesManager(cacheDirectory, projectDir, File(args.destination), reporter) override fun destinationDir(args: K2JVMCompilerArguments): File = args.destinationAsFile diff --git a/jps-plugin/src/org/jetbrains/kotlin/jps/incremental/local.kt b/jps-plugin/src/org/jetbrains/kotlin/jps/incremental/local.kt index 2d5b915f069..6f51a4e29b3 100644 --- a/jps-plugin/src/org/jetbrains/kotlin/jps/incremental/local.kt +++ b/jps-plugin/src/org/jetbrains/kotlin/jps/incremental/local.kt @@ -7,7 +7,7 @@ package org.jetbrains.kotlin.jps.incremental import java.io.File -private val NORMAL_VERSION = 13 +private val NORMAL_VERSION = 14 private val NORMAL_VERSION_FILE_NAME = "format-version.txt" fun localCacheVersionManager(dataRoot: File, isCachesEnabled: Boolean) = diff --git a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/tasks/kotlinCompileTaskData.kt b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/tasks/kotlinCompileTaskData.kt index 1f46ac220dd..7e2a05fb0b6 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/tasks/kotlinCompileTaskData.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/tasks/kotlinCompileTaskData.kt @@ -27,6 +27,7 @@ internal open class KotlinCompileTaskData( File(File(compilation.target.project.buildDir, KOTLIN_BUILD_DIR_NAME), taskName) } + //TODO val buildHistoryFile: File by project.provider { File(taskBuildDirectory, "build-history.bin") }