From f62fb6399ba4bd28f2196085a27e85753e6575cf Mon Sep 17 00:00:00 2001 From: Sebastian Sellmair Date: Wed, 18 Jan 2023 16:14:59 +0100 Subject: [PATCH] [Gradle] Replace 'visibleSourceSetsFromParentsProvider' by ParentSourceSetVisibilityProvider interface KT-49933 --- .../mpp/GranularMetadataTransformation.kt | 27 +++-------------- .../MetadataDependencyTransformationTask.kt | 29 ++++++++++++++++--- .../mpp/ParentSourceSetVisibilityProvider.kt | 16 ++++++++++ .../KotlinMetadataTargetConfigurator.kt | 25 ++++++++-------- ...ropMetadataDependencyTransformationTask.kt | 7 ++++- 5 files changed, 64 insertions(+), 40 deletions(-) create mode 100644 libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/plugin/mpp/ParentSourceSetVisibilityProvider.kt diff --git a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/plugin/mpp/GranularMetadataTransformation.kt b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/plugin/mpp/GranularMetadataTransformation.kt index 5332139b10c..5d5d295acbb 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/plugin/mpp/GranularMetadataTransformation.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/plugin/mpp/GranularMetadataTransformation.kt @@ -21,7 +21,6 @@ import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet import org.jetbrains.kotlin.gradle.plugin.mpp.MetadataDependencyResolution.ChooseVisibleSourceSets.MetadataProvider.ArtifactMetadataProvider import org.jetbrains.kotlin.gradle.plugin.sources.internal import org.jetbrains.kotlin.gradle.utils.LazyResolvedConfiguration -import org.jetbrains.kotlin.gradle.utils.mergeWith import java.util.* internal sealed class MetadataDependencyResolution( @@ -91,23 +90,9 @@ internal sealed class MetadataDependencyResolution( } } -private typealias ComponentIdentifierKey = String - -/** - * This unique key can be used to lookup various info for related Resolved Dependency - * that gets serialized - */ -private val ComponentIdentifier.uniqueKey - get(): ComponentIdentifierKey = - when (this) { - is ProjectComponentIdentifier -> "project ${build.name}$projectPath" - is ModuleComponentIdentifier -> "module $group:$module:$version" - else -> error("Unexpected Component Identifier: '$this' of type $javaClass") - } - internal class GranularMetadataTransformation( val params: Params, - visibleSourceSetsFromParentsProvider: () -> Iterable>> + val parentSourceSetVisibilityProvider: ParentSourceSetVisibilityProvider ) { private val logger = Logging.getLogger("GranularMetadataTransformation[${params.sourceSetName}]") @@ -136,16 +121,13 @@ internal class GranularMetadataTransformation( val moduleId: Provider ) - private val visibleSourceSetsFromParents: Map> by lazy { - visibleSourceSetsFromParentsProvider().reduceOrNull { acc, map -> acc mergeWith map }.orEmpty() - } val metadataDependencyResolutions: Iterable by lazy { doTransform() } - val visibleSourceSetsByComponentId: Map> by lazy { + val visibleSourceSetsByComponentId: Map> by lazy { metadataDependencyResolutions .filterIsInstance() - .groupBy { it.dependency.id.uniqueKey } + .groupBy { it.dependency.id } .mapValues { (_, visibleSourceSets) -> visibleSourceSets.flatMap { it.allVisibleSourceSetNames }.toSet() } } @@ -175,8 +157,7 @@ internal class GranularMetadataTransformation( logger.debug("Transform dependency: $resolvedDependency") val dependencyResult = processDependency( - resolvedDependency, - visibleSourceSetsFromParents[componentId.uniqueKey].orEmpty() + resolvedDependency, parentSourceSetVisibilityProvider.getSourceSetsVisibleInParents(componentId) ) logger.debug("Transformation result of dependency $resolvedDependency: $dependencyResult") diff --git a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/plugin/mpp/MetadataDependencyTransformationTask.kt b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/plugin/mpp/MetadataDependencyTransformationTask.kt index fa577a59806..41d7b60af36 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/plugin/mpp/MetadataDependencyTransformationTask.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/plugin/mpp/MetadataDependencyTransformationTask.kt @@ -6,6 +6,9 @@ package org.jetbrains.kotlin.gradle.plugin.mpp import org.gradle.api.DefaultTask +import org.gradle.api.artifacts.component.ComponentIdentifier +import org.gradle.api.artifacts.component.ModuleComponentIdentifier +import org.gradle.api.artifacts.component.ProjectComponentIdentifier import org.gradle.api.file.FileCollection import org.gradle.api.file.ProjectLayout import org.gradle.api.file.RegularFileProperty @@ -128,7 +131,12 @@ open class MetadataDependencyTransformationTask fun transformMetadata() { val transformation = GranularMetadataTransformation( params = transformationParameters, - visibleSourceSetsFromParentsProvider = { parentVisibleSourceSetFiles.map(::readVisibleSourceSetsFile) } + parentSourceSetVisibilityProvider = ParentSourceSetVisibilityProvider { identifier: ComponentIdentifier -> + val serializableKey = identifier.serializableUniqueKey + parentVisibleSourceSetFiles.flatMap { visibleSourceSetsFile -> + readVisibleSourceSetsFile(visibleSourceSetsFile)[serializableKey].orEmpty() + }.toSet() + } ) if (outputsDir.isDirectory) { @@ -157,9 +165,9 @@ open class MetadataDependencyTransformationTask KotlinMetadataLibrariesIndexFile(transformedLibrariesFileIndex.get().asFile).write(files) } - private fun writeVisibleSourceSets(visibleSourceSetsByComponentId: Map>) { + private fun writeVisibleSourceSets(visibleSourceSetsByComponentId: Map>) { val content = visibleSourceSetsByComponentId.entries.joinToString("\n") { (id, visibleSourceSets) -> - "$id => ${visibleSourceSets.joinToString(",")}" + "${id.serializableUniqueKey} => ${visibleSourceSets.joinToString(",")}" } visibleSourceSetsFile.get().asFile.writeText(content) } @@ -180,4 +188,17 @@ open class MetadataDependencyTransformationTask @get:Internal // Warning! allTransformedLibraries is available only after Task Execution val allTransformedLibraries: FileCollection get() = ownTransformedLibraries + parentTransformedLibraries -} \ No newline at end of file +} + +private typealias SerializableComponentIdentifierKey = String + +/** + * This unique key can be used to lookup various info for related Resolved Dependency + * that gets serialized + */ +private val ComponentIdentifier.serializableUniqueKey + get(): SerializableComponentIdentifierKey = when (this) { + is ProjectComponentIdentifier -> "project ${build.name}$projectPath" + is ModuleComponentIdentifier -> "module $group:$module:$version" + else -> error("Unexpected Component Identifier: '$this' of type ${this.javaClass}") + } \ No newline at end of file diff --git a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/plugin/mpp/ParentSourceSetVisibilityProvider.kt b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/plugin/mpp/ParentSourceSetVisibilityProvider.kt new file mode 100644 index 00000000000..e25bb2dd05d --- /dev/null +++ b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/plugin/mpp/ParentSourceSetVisibilityProvider.kt @@ -0,0 +1,16 @@ +/* + * Copyright 2010-2023 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.gradle.plugin.mpp + +import org.gradle.api.artifacts.component.ComponentIdentifier + +internal fun interface ParentSourceSetVisibilityProvider { + fun getSourceSetsVisibleInParents(identifier: ComponentIdentifier): Set + + object Empty : ParentSourceSetVisibilityProvider { + override fun getSourceSetsVisibleInParents(identifier: ComponentIdentifier): Set = emptySet() + } +} diff --git a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/metadata/KotlinMetadataTargetConfigurator.kt b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/metadata/KotlinMetadataTargetConfigurator.kt index b69cf505ef3..5194843127f 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/metadata/KotlinMetadataTargetConfigurator.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/metadata/KotlinMetadataTargetConfigurator.kt @@ -368,26 +368,27 @@ class KotlinMetadataTargetConfigurator : private val ResolvedArtifactResult.isMpp: Boolean get() = variant.attributes.containsMultiplatformAttributes - private val KotlinSourceSet.dependsOnClassesDirs: FileCollection get() = project.filesProvider { - internal.dependsOnClosure.mapNotNull { hierarchySourceSet -> - val compilation = - project.getMetadataCompilationForSourceSet( - hierarchySourceSet - ) ?: return@mapNotNull null - compilation.output.classesDirs + private val KotlinSourceSet.dependsOnClassesDirs: FileCollection + get() = project.filesProvider { + internal.dependsOnClosure.mapNotNull { hierarchySourceSet -> + val compilation = project.getMetadataCompilationForSourceSet(hierarchySourceSet) ?: return@mapNotNull null + compilation.output.classesDirs + } } - } private fun setupDependencyTransformationForSourceSet( project: Project, sourceSet: KotlinSourceSet ) { + val parentSourceSetVisibilityProvider = ParentSourceSetVisibilityProvider { componentIdentifier -> + dependsOnClosureWithInterCompilationDependencies(sourceSet).filterIsInstance() + .flatMap { it.compileDependenciesTransformationOrFail.visibleSourceSetsByComponentId[componentIdentifier].orEmpty() } + .toSet() + } + val granularMetadataTransformation = GranularMetadataTransformation( params = GranularMetadataTransformation.Params(project, sourceSet), - visibleSourceSetsFromParentsProvider = { - dependsOnClosureWithInterCompilationDependencies(sourceSet).filterIsInstance() - .map { it.compileDependenciesTransformationOrFail.visibleSourceSetsByComponentId } - } + parentSourceSetVisibilityProvider = parentSourceSetVisibilityProvider ) @Suppress("DEPRECATION") diff --git a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/native/internal/CInteropMetadataDependencyTransformationTask.kt b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/native/internal/CInteropMetadataDependencyTransformationTask.kt index f312f418b75..c19cacd61a0 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/native/internal/CInteropMetadataDependencyTransformationTask.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/native/internal/CInteropMetadataDependencyTransformationTask.kt @@ -183,7 +183,12 @@ internal open class CInteropMetadataDependencyTransformationTask @Inject constru protected fun transformDependencies() { cleaning.cleanOutputDirectory(outputDirectory) outputDirectory.mkdirs() - val transformation = GranularMetadataTransformation(parameters) { emptyList() } + /* Warning: + Passing an empty ParentSourceSetVisibilityProvider will create ChooseVisibleSourceSet instances + with bad 'visibleSourceSetNamesExcludingDependsOn'. This is okay, since cinterop transformations do not look + into this field + */ + val transformation = GranularMetadataTransformation(parameters, ParentSourceSetVisibilityProvider.Empty) val chooseVisibleSourceSets = transformation.metadataDependencyResolutions.filterIsInstance() val transformedLibraries = chooseVisibleSourceSets.flatMap(::materializeMetadata) KotlinMetadataLibrariesIndexFile(outputLibrariesFileIndex.get().asFile).write(transformedLibraries)