[Gradle] Replace 'visibleSourceSetsFromParentsProvider' by ParentSourceSetVisibilityProvider interface

KT-49933
This commit is contained in:
Sebastian Sellmair
2023-01-18 16:14:59 +01:00
committed by Space Team
parent 66e426e9ee
commit f62fb6399b
5 changed files with 64 additions and 40 deletions
@@ -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.mpp.MetadataDependencyResolution.ChooseVisibleSourceSets.MetadataProvider.ArtifactMetadataProvider
import org.jetbrains.kotlin.gradle.plugin.sources.internal import org.jetbrains.kotlin.gradle.plugin.sources.internal
import org.jetbrains.kotlin.gradle.utils.LazyResolvedConfiguration import org.jetbrains.kotlin.gradle.utils.LazyResolvedConfiguration
import org.jetbrains.kotlin.gradle.utils.mergeWith
import java.util.* import java.util.*
internal sealed class MetadataDependencyResolution( 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( internal class GranularMetadataTransformation(
val params: Params, val params: Params,
visibleSourceSetsFromParentsProvider: () -> Iterable<Map<ComponentIdentifierKey, Set<String>>> val parentSourceSetVisibilityProvider: ParentSourceSetVisibilityProvider
) { ) {
private val logger = Logging.getLogger("GranularMetadataTransformation[${params.sourceSetName}]") private val logger = Logging.getLogger("GranularMetadataTransformation[${params.sourceSetName}]")
@@ -136,16 +121,13 @@ internal class GranularMetadataTransformation(
val moduleId: Provider<ModuleDependencyIdentifier> val moduleId: Provider<ModuleDependencyIdentifier>
) )
private val visibleSourceSetsFromParents: Map<ComponentIdentifierKey, Set<String>> by lazy {
visibleSourceSetsFromParentsProvider().reduceOrNull { acc, map -> acc mergeWith map }.orEmpty()
}
val metadataDependencyResolutions: Iterable<MetadataDependencyResolution> by lazy { doTransform() } val metadataDependencyResolutions: Iterable<MetadataDependencyResolution> by lazy { doTransform() }
val visibleSourceSetsByComponentId: Map<ComponentIdentifierKey, Set<String>> by lazy { val visibleSourceSetsByComponentId: Map<ComponentIdentifier, Set<String>> by lazy {
metadataDependencyResolutions metadataDependencyResolutions
.filterIsInstance<MetadataDependencyResolution.ChooseVisibleSourceSets>() .filterIsInstance<MetadataDependencyResolution.ChooseVisibleSourceSets>()
.groupBy { it.dependency.id.uniqueKey } .groupBy { it.dependency.id }
.mapValues { (_, visibleSourceSets) -> visibleSourceSets.flatMap { it.allVisibleSourceSetNames }.toSet() } .mapValues { (_, visibleSourceSets) -> visibleSourceSets.flatMap { it.allVisibleSourceSetNames }.toSet() }
} }
@@ -175,8 +157,7 @@ internal class GranularMetadataTransformation(
logger.debug("Transform dependency: $resolvedDependency") logger.debug("Transform dependency: $resolvedDependency")
val dependencyResult = processDependency( val dependencyResult = processDependency(
resolvedDependency, resolvedDependency, parentSourceSetVisibilityProvider.getSourceSetsVisibleInParents(componentId)
visibleSourceSetsFromParents[componentId.uniqueKey].orEmpty()
) )
logger.debug("Transformation result of dependency $resolvedDependency: $dependencyResult") logger.debug("Transformation result of dependency $resolvedDependency: $dependencyResult")
@@ -6,6 +6,9 @@
package org.jetbrains.kotlin.gradle.plugin.mpp package org.jetbrains.kotlin.gradle.plugin.mpp
import org.gradle.api.DefaultTask 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.FileCollection
import org.gradle.api.file.ProjectLayout import org.gradle.api.file.ProjectLayout
import org.gradle.api.file.RegularFileProperty import org.gradle.api.file.RegularFileProperty
@@ -128,7 +131,12 @@ open class MetadataDependencyTransformationTask
fun transformMetadata() { fun transformMetadata() {
val transformation = GranularMetadataTransformation( val transformation = GranularMetadataTransformation(
params = transformationParameters, 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) { if (outputsDir.isDirectory) {
@@ -157,9 +165,9 @@ open class MetadataDependencyTransformationTask
KotlinMetadataLibrariesIndexFile(transformedLibrariesFileIndex.get().asFile).write(files) KotlinMetadataLibrariesIndexFile(transformedLibrariesFileIndex.get().asFile).write(files)
} }
private fun writeVisibleSourceSets(visibleSourceSetsByComponentId: Map<String, Set<String>>) { private fun writeVisibleSourceSets(visibleSourceSetsByComponentId: Map<ComponentIdentifier, Set<String>>) {
val content = visibleSourceSetsByComponentId.entries.joinToString("\n") { (id, visibleSourceSets) -> val content = visibleSourceSetsByComponentId.entries.joinToString("\n") { (id, visibleSourceSets) ->
"$id => ${visibleSourceSets.joinToString(",")}" "${id.serializableUniqueKey} => ${visibleSourceSets.joinToString(",")}"
} }
visibleSourceSetsFile.get().asFile.writeText(content) visibleSourceSetsFile.get().asFile.writeText(content)
} }
@@ -181,3 +189,16 @@ open class MetadataDependencyTransformationTask
@get:Internal // Warning! allTransformedLibraries is available only after Task Execution @get:Internal // Warning! allTransformedLibraries is available only after Task Execution
val allTransformedLibraries: FileCollection get() = ownTransformedLibraries + parentTransformedLibraries val allTransformedLibraries: FileCollection get() = ownTransformedLibraries + parentTransformedLibraries
} }
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}")
}
@@ -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<String>
object Empty : ParentSourceSetVisibilityProvider {
override fun getSourceSetsVisibleInParents(identifier: ComponentIdentifier): Set<String> = emptySet()
}
}
@@ -368,26 +368,27 @@ class KotlinMetadataTargetConfigurator :
private val ResolvedArtifactResult.isMpp: Boolean get() = variant.attributes.containsMultiplatformAttributes private val ResolvedArtifactResult.isMpp: Boolean get() = variant.attributes.containsMultiplatformAttributes
private val KotlinSourceSet.dependsOnClassesDirs: FileCollection get() = project.filesProvider { private val KotlinSourceSet.dependsOnClassesDirs: FileCollection
internal.dependsOnClosure.mapNotNull { hierarchySourceSet -> get() = project.filesProvider {
val compilation = internal.dependsOnClosure.mapNotNull { hierarchySourceSet ->
project.getMetadataCompilationForSourceSet( val compilation = project.getMetadataCompilationForSourceSet(hierarchySourceSet) ?: return@mapNotNull null
hierarchySourceSet compilation.output.classesDirs
) ?: return@mapNotNull null }
compilation.output.classesDirs
} }
}
private fun setupDependencyTransformationForSourceSet( private fun setupDependencyTransformationForSourceSet(
project: Project, project: Project,
sourceSet: KotlinSourceSet sourceSet: KotlinSourceSet
) { ) {
val parentSourceSetVisibilityProvider = ParentSourceSetVisibilityProvider { componentIdentifier ->
dependsOnClosureWithInterCompilationDependencies(sourceSet).filterIsInstance<DefaultKotlinSourceSet>()
.flatMap { it.compileDependenciesTransformationOrFail.visibleSourceSetsByComponentId[componentIdentifier].orEmpty() }
.toSet()
}
val granularMetadataTransformation = GranularMetadataTransformation( val granularMetadataTransformation = GranularMetadataTransformation(
params = GranularMetadataTransformation.Params(project, sourceSet), params = GranularMetadataTransformation.Params(project, sourceSet),
visibleSourceSetsFromParentsProvider = { parentSourceSetVisibilityProvider = parentSourceSetVisibilityProvider
dependsOnClosureWithInterCompilationDependencies(sourceSet).filterIsInstance<DefaultKotlinSourceSet>()
.map { it.compileDependenciesTransformationOrFail.visibleSourceSetsByComponentId }
}
) )
@Suppress("DEPRECATION") @Suppress("DEPRECATION")
@@ -183,7 +183,12 @@ internal open class CInteropMetadataDependencyTransformationTask @Inject constru
protected fun transformDependencies() { protected fun transformDependencies() {
cleaning.cleanOutputDirectory(outputDirectory) cleaning.cleanOutputDirectory(outputDirectory)
outputDirectory.mkdirs() 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<ChooseVisibleSourceSets>() val chooseVisibleSourceSets = transformation.metadataDependencyResolutions.filterIsInstance<ChooseVisibleSourceSets>()
val transformedLibraries = chooseVisibleSourceSets.flatMap(::materializeMetadata) val transformedLibraries = chooseVisibleSourceSets.flatMap(::materializeMetadata)
KotlinMetadataLibrariesIndexFile(outputLibrariesFileIndex.get().asFile).write(transformedLibraries) KotlinMetadataLibrariesIndexFile(outputLibrariesFileIndex.get().asFile).write(transformedLibraries)