From eaa5d43a5d27e2e80a1fc27be3b4478802269f76 Mon Sep 17 00:00:00 2001 From: Anton Lakotka Date: Tue, 21 Jun 2022 09:08:51 +0200 Subject: [PATCH] [Gradle] Support CInteropCommonizerTask with Gradle Configuration Cache --- .../native/internal/CInteropCommonizerTask.kt | 39 +++++++++++++------ 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/native/internal/CInteropCommonizerTask.kt b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/native/internal/CInteropCommonizerTask.kt index 535ef54d650..36a3e9087c2 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/native/internal/CInteropCommonizerTask.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/native/internal/CInteropCommonizerTask.kt @@ -54,6 +54,22 @@ internal open class CInteropCommonizerTask @get:Nested internal val runnerSettings = KotlinNativeCommonizerToolRunner.Settings(project) + private val konanHome = project.file(project.konanHome) + private val commonizerLogLevel = project.commonizerLogLevel + private val additionalCommonizerSettings = project.additionalCommonizerSettings + + /** + * For Gradle Configuration Cache support the Group-to-Dependencies relation should be pre-cached. + * It is used during execution phase. + */ + private val nativeDistributionDependenciesMap: Map> by lazy { + getAllInteropsGroups().associateWith { group -> + (group.targets + group.targets.allLeaves()).flatMapTo(mutableSetOf()) { target -> + project.getNativeDistributionDependencies(target).map { dependency -> TargetedCommonizerDependency(target, dependency) } + } + } + } + @get:Nested internal var cinterops = setOf() private set @@ -99,25 +115,26 @@ internal open class CInteropCommonizerTask ) GradleCliCommonizer(commonizerRunner).commonizeLibraries( - konanHome = project.file(project.konanHome), + konanHome = konanHome, outputTargets = group.targets, inputLibraries = cinteropsForTarget.map { it.libraryFile.get() }.filter { it.exists() }.toSet(), dependencyLibraries = getNativeDistributionDependencies(group), outputDirectory = outputDirectory(group), - logLevel = project.commonizerLogLevel, - additionalSettings = project.additionalCommonizerSettings, + logLevel = commonizerLogLevel, + additionalSettings = additionalCommonizerSettings, ) } private fun getNativeDistributionDependencies(group: CInteropCommonizerGroup): Set { - return (group.targets + group.targets.allLeaves()).flatMapTo(mutableSetOf()) { target -> - project.getNativeDistributionDependencies(target).map { dependency -> TargetedCommonizerDependency(target, dependency) } - } + val dependencies = nativeDistributionDependenciesMap[group] + requireNotNull(dependencies) { "Unexpected $group" } + + return dependencies } @Nested internal fun getAllInteropsGroups(): Set { - val dependents = getAllDependents() + val dependents = allDependents val allScopeSets = dependents.map { it.scopes }.toSet() val rootScopeSets = allScopeSets.filter { scopeSet -> allScopeSets.none { otherScopeSet -> otherScopeSet != scopeSet && otherScopeSet.containsAll(scopeSet) } @@ -147,9 +164,9 @@ internal open class CInteropCommonizerTask return suitableGroups.firstOrNull() } - @Internal - internal fun getAllDependents(): Set { - val multiplatformExtension = project.multiplatformExtensionOrNull ?: return emptySet() + //@Internal + private val allDependents: Set by lazy { + val multiplatformExtension = project.multiplatformExtensionOrNull ?: return@lazy emptySet() val fromSharedNativeCompilations = multiplatformExtension .targets.flatMap { target -> target.compilations } @@ -165,7 +182,7 @@ internal open class CInteropCommonizerTask .mapNotNull { sourceSet -> CInteropCommonizerDependent.fromAssociateCompilations(project, sourceSet) } .toSet() - return (fromSharedNativeCompilations + fromSourceSets + fromSourceSetsAssociateCompilations) + return@lazy (fromSharedNativeCompilations + fromSourceSets + fromSourceSetsAssociateCompilations) } }