[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.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<Map<ComponentIdentifierKey, Set<String>>>
val parentSourceSetVisibilityProvider: ParentSourceSetVisibilityProvider
) {
private val logger = Logging.getLogger("GranularMetadataTransformation[${params.sourceSetName}]")
@@ -136,16 +121,13 @@ internal class GranularMetadataTransformation(
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 visibleSourceSetsByComponentId: Map<ComponentIdentifierKey, Set<String>> by lazy {
val visibleSourceSetsByComponentId: Map<ComponentIdentifier, Set<String>> by lazy {
metadataDependencyResolutions
.filterIsInstance<MetadataDependencyResolution.ChooseVisibleSourceSets>()
.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")
@@ -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<String, Set<String>>) {
private fun writeVisibleSourceSets(visibleSourceSetsByComponentId: Map<ComponentIdentifier, Set<String>>) {
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
}
}
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 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<DefaultKotlinSourceSet>()
.flatMap { it.compileDependenciesTransformationOrFail.visibleSourceSetsByComponentId[componentIdentifier].orEmpty() }
.toSet()
}
val granularMetadataTransformation = GranularMetadataTransformation(
params = GranularMetadataTransformation.Params(project, sourceSet),
visibleSourceSetsFromParentsProvider = {
dependsOnClosureWithInterCompilationDependencies(sourceSet).filterIsInstance<DefaultKotlinSourceSet>()
.map { it.compileDependenciesTransformationOrFail.visibleSourceSetsByComponentId }
}
parentSourceSetVisibilityProvider = parentSourceSetVisibilityProvider
)
@Suppress("DEPRECATION")
@@ -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<ChooseVisibleSourceSets>()
val transformedLibraries = chooseVisibleSourceSets.flatMap(::materializeMetadata)
KotlinMetadataLibrariesIndexFile(outputLibrariesFileIndex.get().asFile).write(transformedLibraries)