From d0acaa4024ca84c4679fa1da95b6a7d985006e3a Mon Sep 17 00:00:00 2001 From: Igor Chevdar Date: Fri, 8 Dec 2023 16:33:30 +0200 Subject: [PATCH] [gradle] Use more fine grained directory for K/N incremental compilation Otherwise, two or more link tasks being executed in parallel will use the same directory for IC caches which might lead to races during compilation #KT-63471 Fixed --- .../native/NativeIncrementalCompilationIT.kt | 25 +++++++++++++------ .../gradle/testbase/nativeTestHelpers.kt | 14 +++++------ .../targets/native/tasks/KotlinNativeLink.kt | 4 ++- 3 files changed, 26 insertions(+), 17 deletions(-) diff --git a/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/native/NativeIncrementalCompilationIT.kt b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/native/NativeIncrementalCompilationIT.kt index 324211b9d70..6312184b4ff 100644 --- a/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/native/NativeIncrementalCompilationIT.kt +++ b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/native/NativeIncrementalCompilationIT.kt @@ -93,7 +93,7 @@ class NativeIncrementalCompilationIT : KGPBaseTest() { nativeProject("native-incremental-simple", gradleVersion) { build("linkDebugExecutableHost") { assertDirectoryExists( - getFileCache("native-incremental-simple", "src/hostMain/kotlin/main.kt", "") + getFileCache("native-incremental-simple", "src/hostMain/kotlin/main.kt") ) } } @@ -105,8 +105,8 @@ class NativeIncrementalCompilationIT : KGPBaseTest() { nativeProject("native-incremental-multifile", gradleVersion) { var mainKtCacheModified = 0L var fooKtCacheModified = 0L - val mainKtCache = getFileCache("native-incremental-multifile", "src/hostMain/kotlin/main.kt", "") - val fooKtCache = getFileCache("native-incremental-multifile", "src/hostMain/kotlin/foo.kt", "") + val mainKtCache = getFileCache("native-incremental-multifile", "src/hostMain/kotlin/main.kt") + val fooKtCache = getFileCache("native-incremental-multifile", "src/hostMain/kotlin/foo.kt") build("linkDebugExecutableHost") { assertDirectoryExists(mainKtCache) assertDirectoryExists(fooKtCache) @@ -138,8 +138,8 @@ class NativeIncrementalCompilationIT : KGPBaseTest() { nativeProject("native-incremental-multifile", gradleVersion) { var mainKtCacheModified = 0L var fooKtCacheModified = 0L - val mainKtCache = getFileCache("native-incremental-multifile", "src/hostMain/kotlin/main.kt", "") - val fooKtCache = getFileCache("native-incremental-multifile", "src/hostMain/kotlin/foo.kt", "") + val mainKtCache = getFileCache("native-incremental-multifile", "src/hostMain/kotlin/main.kt") + val fooKtCache = getFileCache("native-incremental-multifile", "src/hostMain/kotlin/foo.kt") build("linkDebugExecutableHost") { assertDirectoryExists(mainKtCache) assertDirectoryExists(fooKtCache) @@ -172,9 +172,18 @@ class NativeIncrementalCompilationIT : KGPBaseTest() { var fooKtCacheModified = 0L var barKtCacheModified = 0L var mainKtCacheModified = 0L - val fooKtCache = getFileCache("program", "MultiProject:library", "library/src/hostMain/kotlin/foo.kt", "") - val barKtCache = getFileCache("program", "MultiProject:program", "program/src/hostMain/kotlin/bar.kt", "") - val mainKtCache = getFileCache("program", "MultiProject:program", "program/src/hostMain/kotlin/main.kt", "") + val fooKtCache = getFileCache( + "MultiProject:library", "library/src/hostMain/kotlin/foo.kt", + executableProjectName = "program" + ) + val barKtCache = getFileCache( + "MultiProject:program", "program/src/hostMain/kotlin/bar.kt", + executableProjectName = "program" + ) + val mainKtCache = getFileCache( + "MultiProject:program", "program/src/hostMain/kotlin/main.kt", + executableProjectName = "program" + ) build("linkDebugExecutableHost") { assertDirectoryExists(fooKtCache) assertDirectoryExists(barKtCache) diff --git a/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/testbase/nativeTestHelpers.kt b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/testbase/nativeTestHelpers.kt index ca18d92170a..a6c2772e088 100644 --- a/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/testbase/nativeTestHelpers.kt +++ b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/testbase/nativeTestHelpers.kt @@ -99,24 +99,22 @@ fun computeCacheDirName( partialLinkageEnabled: Boolean ) = "$testTarget${if (debuggable) "-g" else ""}$cacheKind${if (partialLinkageEnabled) "-pl" else ""}" -fun TestProject.getFileCache(fileProjectName: String, fileRelativePath: String, fqName: String) = - getFileCache("", fileProjectName, fileRelativePath, fqName) - fun TestProject.getFileCache( - executableProjectName: String, fileProjectName: String, fileRelativePath: String, - fqName: String, + fqName: String = "", + executableProjectName: String = "", + executableName: String = "debugExecutable", ): Path { val cacheFlavor = computeCacheDirName(HostManager.host, NativeCacheKind.STATIC.name, true, true) - val libCacheDir = getICCacheDir(executableProjectName).resolve(cacheFlavor).resolve("$fileProjectName-per-file-cache") + val libCacheDir = getICCacheDir(executableName, executableProjectName).resolve(cacheFlavor).resolve("$fileProjectName-per-file-cache") val fileId = cacheFileId(fqName, projectPath.resolve(fileRelativePath).toFile().canonicalPath) return libCacheDir.resolve(fileId) } -private fun TestProject.getICCacheDir(projectName: String = "") = +private fun TestProject.getICCacheDir(executableName: String, projectName: String = "") = (if (projectName == "") projectPath else projectPath.resolve(projectName)) - .resolve("build/kotlin-native-ic-cache") + .resolve("build/kotlin-native-ic-cache/$executableName") private fun cacheFileId(fqName: String, filePath: String) = "${if (fqName == "") "ROOT" else fqName}.${filePath.hashCode().toString(Character.MAX_RADIX)}" diff --git a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/native/tasks/KotlinNativeLink.kt b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/native/tasks/KotlinNativeLink.kt index 264ff0e8f9c..d561ce8a671 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/native/tasks/KotlinNativeLink.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/native/tasks/KotlinNativeLink.kt @@ -378,7 +378,9 @@ constructor( add("-Xauto-cache-from=${cacheSettings.gradleUserHomeDir}") add("-Xbackend-threads=${cacheSettings.threads}") if (cacheSettings.icEnabled) { - val icCacheDir = cacheSettings.gradleBuildDir.resolve("kotlin-native-ic-cache") + val icCacheDir = cacheSettings.gradleBuildDir + .resolve("kotlin-native-ic-cache") + .resolve(binaryName) icCacheDir.mkdirs() add("-Xenable-incremental-compilation") add("-Xic-cache-dir=$icCacheDir")