Skip to content

Commit f62fb63

Browse files
sellmairSpace Team
authored and
Space Team
committed
[Gradle] Replace 'visibleSourceSetsFromParentsProvider' by ParentSourceSetVisibilityProvider interface
KT-49933
1 parent 66e426e commit f62fb63

File tree

5 files changed

+64
-40
lines changed

5 files changed

+64
-40
lines changed

libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/plugin/mpp/GranularMetadataTransformation.kt

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet
2121
import org.jetbrains.kotlin.gradle.plugin.mpp.MetadataDependencyResolution.ChooseVisibleSourceSets.MetadataProvider.ArtifactMetadataProvider
2222
import org.jetbrains.kotlin.gradle.plugin.sources.internal
2323
import org.jetbrains.kotlin.gradle.utils.LazyResolvedConfiguration
24-
import org.jetbrains.kotlin.gradle.utils.mergeWith
2524
import java.util.*
2625

2726
internal sealed class MetadataDependencyResolution(
@@ -91,23 +90,9 @@ internal sealed class MetadataDependencyResolution(
9190
}
9291
}
9392

94-
private typealias ComponentIdentifierKey = String
95-
96-
/**
97-
* This unique key can be used to lookup various info for related Resolved Dependency
98-
* that gets serialized
99-
*/
100-
private val ComponentIdentifier.uniqueKey
101-
get(): ComponentIdentifierKey =
102-
when (this) {
103-
is ProjectComponentIdentifier -> "project ${build.name}$projectPath"
104-
is ModuleComponentIdentifier -> "module $group:$module:$version"
105-
else -> error("Unexpected Component Identifier: '$this' of type $javaClass")
106-
}
107-
10893
internal class GranularMetadataTransformation(
10994
val params: Params,
110-
visibleSourceSetsFromParentsProvider: () -> Iterable<Map<ComponentIdentifierKey, Set<String>>>
95+
val parentSourceSetVisibilityProvider: ParentSourceSetVisibilityProvider
11196
) {
11297
private val logger = Logging.getLogger("GranularMetadataTransformation[${params.sourceSetName}]")
11398

@@ -136,16 +121,13 @@ internal class GranularMetadataTransformation(
136121
val moduleId: Provider<ModuleDependencyIdentifier>
137122
)
138123

139-
private val visibleSourceSetsFromParents: Map<ComponentIdentifierKey, Set<String>> by lazy {
140-
visibleSourceSetsFromParentsProvider().reduceOrNull { acc, map -> acc mergeWith map }.orEmpty()
141-
}
142124

143125
val metadataDependencyResolutions: Iterable<MetadataDependencyResolution> by lazy { doTransform() }
144126

145-
val visibleSourceSetsByComponentId: Map<ComponentIdentifierKey, Set<String>> by lazy {
127+
val visibleSourceSetsByComponentId: Map<ComponentIdentifier, Set<String>> by lazy {
146128
metadataDependencyResolutions
147129
.filterIsInstance<MetadataDependencyResolution.ChooseVisibleSourceSets>()
148-
.groupBy { it.dependency.id.uniqueKey }
130+
.groupBy { it.dependency.id }
149131
.mapValues { (_, visibleSourceSets) -> visibleSourceSets.flatMap { it.allVisibleSourceSetNames }.toSet() }
150132
}
151133

@@ -175,8 +157,7 @@ internal class GranularMetadataTransformation(
175157

176158
logger.debug("Transform dependency: $resolvedDependency")
177159
val dependencyResult = processDependency(
178-
resolvedDependency,
179-
visibleSourceSetsFromParents[componentId.uniqueKey].orEmpty()
160+
resolvedDependency, parentSourceSetVisibilityProvider.getSourceSetsVisibleInParents(componentId)
180161
)
181162
logger.debug("Transformation result of dependency $resolvedDependency: $dependencyResult")
182163

libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/plugin/mpp/MetadataDependencyTransformationTask.kt

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
package org.jetbrains.kotlin.gradle.plugin.mpp
77

88
import org.gradle.api.DefaultTask
9+
import org.gradle.api.artifacts.component.ComponentIdentifier
10+
import org.gradle.api.artifacts.component.ModuleComponentIdentifier
11+
import org.gradle.api.artifacts.component.ProjectComponentIdentifier
912
import org.gradle.api.file.FileCollection
1013
import org.gradle.api.file.ProjectLayout
1114
import org.gradle.api.file.RegularFileProperty
@@ -128,7 +131,12 @@ open class MetadataDependencyTransformationTask
128131
fun transformMetadata() {
129132
val transformation = GranularMetadataTransformation(
130133
params = transformationParameters,
131-
visibleSourceSetsFromParentsProvider = { parentVisibleSourceSetFiles.map(::readVisibleSourceSetsFile) }
134+
parentSourceSetVisibilityProvider = ParentSourceSetVisibilityProvider { identifier: ComponentIdentifier ->
135+
val serializableKey = identifier.serializableUniqueKey
136+
parentVisibleSourceSetFiles.flatMap { visibleSourceSetsFile ->
137+
readVisibleSourceSetsFile(visibleSourceSetsFile)[serializableKey].orEmpty()
138+
}.toSet()
139+
}
132140
)
133141

134142
if (outputsDir.isDirectory) {
@@ -157,9 +165,9 @@ open class MetadataDependencyTransformationTask
157165
KotlinMetadataLibrariesIndexFile(transformedLibrariesFileIndex.get().asFile).write(files)
158166
}
159167

160-
private fun writeVisibleSourceSets(visibleSourceSetsByComponentId: Map<String, Set<String>>) {
168+
private fun writeVisibleSourceSets(visibleSourceSetsByComponentId: Map<ComponentIdentifier, Set<String>>) {
161169
val content = visibleSourceSetsByComponentId.entries.joinToString("\n") { (id, visibleSourceSets) ->
162-
"$id => ${visibleSourceSets.joinToString(",")}"
170+
"${id.serializableUniqueKey} => ${visibleSourceSets.joinToString(",")}"
163171
}
164172
visibleSourceSetsFile.get().asFile.writeText(content)
165173
}
@@ -180,4 +188,17 @@ open class MetadataDependencyTransformationTask
180188

181189
@get:Internal // Warning! allTransformedLibraries is available only after Task Execution
182190
val allTransformedLibraries: FileCollection get() = ownTransformedLibraries + parentTransformedLibraries
183-
}
191+
}
192+
193+
private typealias SerializableComponentIdentifierKey = String
194+
195+
/**
196+
* This unique key can be used to lookup various info for related Resolved Dependency
197+
* that gets serialized
198+
*/
199+
private val ComponentIdentifier.serializableUniqueKey
200+
get(): SerializableComponentIdentifierKey = when (this) {
201+
is ProjectComponentIdentifier -> "project ${build.name}$projectPath"
202+
is ModuleComponentIdentifier -> "module $group:$module:$version"
203+
else -> error("Unexpected Component Identifier: '$this' of type ${this.javaClass}")
204+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
3+
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
4+
*/
5+
6+
package org.jetbrains.kotlin.gradle.plugin.mpp
7+
8+
import org.gradle.api.artifacts.component.ComponentIdentifier
9+
10+
internal fun interface ParentSourceSetVisibilityProvider {
11+
fun getSourceSetsVisibleInParents(identifier: ComponentIdentifier): Set<String>
12+
13+
object Empty : ParentSourceSetVisibilityProvider {
14+
override fun getSourceSetsVisibleInParents(identifier: ComponentIdentifier): Set<String> = emptySet()
15+
}
16+
}

libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/metadata/KotlinMetadataTargetConfigurator.kt

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -368,26 +368,27 @@ class KotlinMetadataTargetConfigurator :
368368

369369
private val ResolvedArtifactResult.isMpp: Boolean get() = variant.attributes.containsMultiplatformAttributes
370370

371-
private val KotlinSourceSet.dependsOnClassesDirs: FileCollection get() = project.filesProvider {
372-
internal.dependsOnClosure.mapNotNull { hierarchySourceSet ->
373-
val compilation =
374-
project.getMetadataCompilationForSourceSet(
375-
hierarchySourceSet
376-
) ?: return@mapNotNull null
377-
compilation.output.classesDirs
371+
private val KotlinSourceSet.dependsOnClassesDirs: FileCollection
372+
get() = project.filesProvider {
373+
internal.dependsOnClosure.mapNotNull { hierarchySourceSet ->
374+
val compilation = project.getMetadataCompilationForSourceSet(hierarchySourceSet) ?: return@mapNotNull null
375+
compilation.output.classesDirs
376+
}
378377
}
379-
}
380378

381379
private fun setupDependencyTransformationForSourceSet(
382380
project: Project,
383381
sourceSet: KotlinSourceSet
384382
) {
383+
val parentSourceSetVisibilityProvider = ParentSourceSetVisibilityProvider { componentIdentifier ->
384+
dependsOnClosureWithInterCompilationDependencies(sourceSet).filterIsInstance<DefaultKotlinSourceSet>()
385+
.flatMap { it.compileDependenciesTransformationOrFail.visibleSourceSetsByComponentId[componentIdentifier].orEmpty() }
386+
.toSet()
387+
}
388+
385389
val granularMetadataTransformation = GranularMetadataTransformation(
386390
params = GranularMetadataTransformation.Params(project, sourceSet),
387-
visibleSourceSetsFromParentsProvider = {
388-
dependsOnClosureWithInterCompilationDependencies(sourceSet).filterIsInstance<DefaultKotlinSourceSet>()
389-
.map { it.compileDependenciesTransformationOrFail.visibleSourceSetsByComponentId }
390-
}
391+
parentSourceSetVisibilityProvider = parentSourceSetVisibilityProvider
391392
)
392393

393394
@Suppress("DEPRECATION")

libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/native/internal/CInteropMetadataDependencyTransformationTask.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,12 @@ internal open class CInteropMetadataDependencyTransformationTask @Inject constru
183183
protected fun transformDependencies() {
184184
cleaning.cleanOutputDirectory(outputDirectory)
185185
outputDirectory.mkdirs()
186-
val transformation = GranularMetadataTransformation(parameters) { emptyList() }
186+
/* Warning:
187+
Passing an empty ParentSourceSetVisibilityProvider will create ChooseVisibleSourceSet instances
188+
with bad 'visibleSourceSetNamesExcludingDependsOn'. This is okay, since cinterop transformations do not look
189+
into this field
190+
*/
191+
val transformation = GranularMetadataTransformation(parameters, ParentSourceSetVisibilityProvider.Empty)
187192
val chooseVisibleSourceSets = transformation.metadataDependencyResolutions.filterIsInstance<ChooseVisibleSourceSets>()
188193
val transformedLibraries = chooseVisibleSourceSets.flatMap(::materializeMetadata)
189194
KotlinMetadataLibrariesIndexFile(outputLibrariesFileIndex.get().asFile).write(transformedLibraries)

0 commit comments

Comments
 (0)