[Gradle] Fixed race condition during k/n bundle downloading

Extended lock bounds.
Moved removing an old bundle back to configuration phase.

^KT-65985 Fixed
This commit is contained in:
Dmitrii Krasnov
2024-02-21 15:49:35 +01:00
committed by Space Team
parent b7de3709e4
commit 808d4353e5
@@ -38,6 +38,8 @@ internal abstract class KotlinNativeBundleBuildService : BuildService<BuildServi
@get:Inject
abstract val fso: FileSystemOperations
private var canBeReinstalled: Boolean = true // we can reinstall a k/n bundle once during the build
companion object {
fun registerIfAbsent(project: Project): Provider<KotlinNativeBundleBuildService> =
project.gradle.sharedServices.registerIfAbsent(
@@ -62,37 +64,57 @@ internal abstract class KotlinNativeBundleBuildService : BuildService<BuildServi
konanTargets: Set<KonanTarget>,
) {
val lock = NativeDistributionCommonizerLock(bundleDir) { message -> project.logger.info("Kotlin Native Bundle: $message") }
val lock =
NativeDistributionCommonizerLock(bundleDir) { message -> project.logger.info("Kotlin Native Bundle: $message") }
if (reinstallFlag) {
lock.withLock {
bundleDir.deleteRecursively()
}
}
lock.withLock {
if (!bundleDir.resolve(KONAN_DIRECTORY_NAME_TO_CHECK_EXISTENCE).exists()) {
val gradleCachesKotlinNativeDir =
kotlinNativeCompilerConfiguration
.singleOrNull()
?.resolve(kotlinNativeVersion)
?: error(
"Kotlin Native dependency has not been properly resolved. " +
"Please, make sure that you've declared the repository, which contains $kotlinNativeVersion."
)
removeBundleIfNeeded(reinstallFlag, bundleDir)
project.logger.info("Moving Kotlin/Native bundle from tmp directory $gradleCachesKotlinNativeDir to ${bundleDir.absolutePath}")
lock.withLock {
if (!bundleDir.resolve(KONAN_DIRECTORY_NAME_TO_CHECK_EXISTENCE).exists()) {
val gradleCachesKotlinNativeDir =
resolveKotlinNativeConfiguration(kotlinNativeVersion, kotlinNativeCompilerConfiguration)
project.logger.info("Moving Kotlin/Native bundle from tmp directory $gradleCachesKotlinNativeDir to ${bundleDir.absolutePath}")
fso.copy {
it.from(gradleCachesKotlinNativeDir)
it.into(bundleDir)
}
project.logger.info("Moved Kotlin/Native bundle from $gradleCachesKotlinNativeDir to ${bundleDir.absolutePath}")
}
project.logger.info("Moved Kotlin/Native bundle from $gradleCachesKotlinNativeDir to ${bundleDir.absolutePath}")
}
project.setupKotlinNativeDependencies(konanTargets)
}
private fun removeBundleIfNeeded(
reinstallFlag: Boolean,
bundleDir: File,
) {
if (reinstallFlag && canBeReinstalled) {
bundleDir.deleteRecursively()
canBeReinstalled = false // we don't need to reinstall k/n if it was reinstalled once during the same build
}
}
private fun resolveKotlinNativeConfiguration(
kotlinNativeVersion: String,
kotlinNativeCompilerConfiguration: ConfigurableFileCollection,
): File {
val resolutionErrorMessage = "Kotlin Native dependency has not been properly resolved. " +
"Please, make sure that you've declared the repository, which contains $kotlinNativeVersion."
val gradleCachesKotlinNativeDir = kotlinNativeCompilerConfiguration
.singleOrNull()
?.resolve(kotlinNativeVersion)
?: error(resolutionErrorMessage)
if (!gradleCachesKotlinNativeDir.exists()) {
throw IllegalArgumentException(resolutionErrorMessage)
}
return gradleCachesKotlinNativeDir
}
private fun Project.setupKotlinNativeDependencies(konanTargets: Set<KonanTarget>) {
val distributionType = NativeDistributionTypeProvider(this).getDistributionType()
if (distributionType.mustGeneratePlatformLibs) {