[Gradle] Merge scoped dependency transformations to one: compile scope

There is no need on fine-grained metadata dependencies transformation
per scope. Because it is only needed for compilation.

^KT-55230 Fixed
This commit is contained in:
Anton Lakotka
2022-12-05 10:48:07 +01:00
committed by Space Team
parent 98678bce0a
commit d28659fdbe
5 changed files with 44 additions and 38 deletions
@@ -8,14 +8,8 @@ package org.jetbrains.kotlin.gradle.plugin.ide.dependencyResolvers
import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet
import org.jetbrains.kotlin.gradle.plugin.mpp.MetadataDependencyResolution
import org.jetbrains.kotlin.gradle.plugin.sources.DefaultKotlinSourceSet
import org.jetbrains.kotlin.gradle.plugin.sources.KotlinDependencyScope
import org.jetbrains.kotlin.gradle.plugin.sources.KotlinDependencyScope.*
internal inline fun <reified T : MetadataDependencyResolution> KotlinSourceSet.resolveMetadata(
scopes: Set<KotlinDependencyScope> = setOf(API_SCOPE, IMPLEMENTATION_SCOPE, COMPILE_ONLY_SCOPE)
): List<T> {
internal inline fun <reified T : MetadataDependencyResolution> KotlinSourceSet.resolveMetadata(): List<T> {
if (this !is DefaultKotlinSourceSet) return emptyList()
return scopes
.flatMap { scope -> dependencyTransformations[scope]?.metadataDependencyResolutions?.toList().orEmpty() }
.filterIsInstance<T>()
return compileDependenciesTransformation.metadataDependencyResolutions.filterIsInstance<T>()
}
@@ -118,7 +118,20 @@ abstract class DefaultKotlinSourceSet @Inject constructor(
explicitlyAddedCustomSourceFilesExtensions.addAll(extensions)
}
internal val dependencyTransformations: MutableMap<KotlinDependencyScope, GranularMetadataTransformation> = mutableMapOf()
private var _compileDependenciesTransformation: GranularMetadataTransformation? = null
internal fun setCompileDependenciesTransformation(value: GranularMetadataTransformation) {
_compileDependenciesTransformation = value
}
/**
* Returns [GranularMetadataTransformation] for all requested compile dependencies
* scopes: API, IMPLEMENTATION, COMPILE_ONLY; See [KotlinDependencyScope.compileScopes]
*/
internal val compileDependenciesTransformation: GranularMetadataTransformation
get() = _compileDependenciesTransformation
?: error("Accessing Compile Dependencies Transformations is not yet initialised; " +
"Check when setCompileDependenciesTransformation is called")
private val _requiresVisibilityOf = mutableSetOf<KotlinSourceSet>()
@@ -142,22 +155,17 @@ abstract class DefaultKotlinSourceSet @Inject constructor(
val useFilesForSourceSets: Map<String, Iterable<File>>
)
@Suppress("unused") // Used in IDE import
@Suppress("unused", "UNUSED_PARAMETER") // Used in IDE import, [configurationName] is kept for backward compatibility
fun getDependenciesTransformation(configurationName: String): Iterable<MetadataDependencyTransformation> {
val scope = KotlinDependencyScope.compileScopes.find {
project.sourceSetMetadataConfigurationByScope(this, it)?.name == configurationName
} ?: return emptyList()
return getDependenciesTransformation(scope)
return getDependenciesTransformation()
}
fun getAdditionalVisibleSourceSets(): List<KotlinSourceSet> = getVisibleSourceSetsFromAssociateCompilations(this)
internal fun getDependenciesTransformation(scope: KotlinDependencyScope): Iterable<MetadataDependencyTransformation> {
private fun getDependenciesTransformation(): Iterable<MetadataDependencyTransformation> {
val metadataDependencyResolutionByModule =
dependencyTransformations[scope]?.metadataDependencyResolutions
?.associateBy { ModuleIds.fromComponent(project, it.dependency) }
?: emptyMap()
compileDependenciesTransformation.metadataDependencyResolutions
.associateBy { ModuleIds.fromComponent(project, it.dependency) }
return metadataDependencyResolutionByModule.mapNotNull { (groupAndName, resolution) ->
val (group, name) = groupAndName
@@ -359,20 +359,22 @@ class KotlinMetadataTargetConfigurator :
sourceSet: KotlinSourceSet,
isSourceSetPublished: Boolean
) {
KotlinDependencyScope.compileScopes.forEach { scope ->
val granularMetadataTransformation = GranularMetadataTransformation(
project,
sourceSet,
listOf(scope),
lazy {
dependsOnClosureWithInterCompilationDependencies(sourceSet).filterIsInstance<DefaultKotlinSourceSet>()
.map { checkNotNull(it.dependencyTransformations[scope]) }
}
)
val dependencyScopesToTransform = KotlinDependencyScope.compileScopes
if (sourceSet is DefaultKotlinSourceSet)
sourceSet.dependencyTransformations[scope] = granularMetadataTransformation
val granularMetadataTransformation = GranularMetadataTransformation(
project = project,
kotlinSourceSet = sourceSet,
sourceSetRequestedScopes = dependencyScopesToTransform,
parentTransformations = lazy {
dependsOnClosureWithInterCompilationDependencies(sourceSet).filterIsInstance<DefaultKotlinSourceSet>()
.map { it.compileDependenciesTransformation }
}
)
if (sourceSet is DefaultKotlinSourceSet)
sourceSet.setCompileDependenciesTransformation(granularMetadataTransformation)
dependencyScopesToTransform.forEach { scope ->
val sourceSetDependencyConfigurationByScope = project.configurations.sourceSetDependencyConfigurationByScope(sourceSet, scope)
if (isSourceSetPublished) {
@@ -50,7 +50,8 @@ private fun Project.createCInteropMetadataDependencyClasspathFromProjectDependen
forIde: Boolean
): FileCollection {
return filesProvider {
sourceSet.dependencyTransformations.values.flatMap { it.metadataDependencyResolutions }
sourceSet.compileDependenciesTransformation
.metadataDependencyResolutions
.filterIsInstance<ChooseVisibleSourceSets>()
.flatMap { chooseVisibleSourceSets ->
/* We only want to access resolutions that provide metadata from dependency projects */
@@ -24,7 +24,6 @@ import org.jetbrains.kotlin.gradle.plugin.sources.DefaultKotlinSourceSet
import org.jetbrains.kotlin.gradle.tasks.dependsOn
import org.jetbrains.kotlin.gradle.tasks.locateOrRegisterTask
import org.jetbrains.kotlin.gradle.tasks.withType
import org.jetbrains.kotlin.gradle.utils.filesProvider
import org.jetbrains.kotlin.gradle.utils.lowerCamelCaseName
import org.jetbrains.kotlin.gradle.utils.notCompatibleWithConfigurationCacheCompat
import org.jetbrains.kotlin.gradle.utils.outputFilesProvider
@@ -180,14 +179,16 @@ internal open class CInteropMetadataDependencyTransformationTask @Inject constru
@Suppress("unused")
@get:Classpath
protected val inputArtifactFiles: FileCollection = project.filesProvider {
sourceSet.dependencyTransformations.values.map { it.configurationToResolve.withoutProjectDependencies() }
}
protected val inputArtifactFiles: FileCollection get() = sourceSet
.compileDependenciesTransformation
.configurationToResolve
.withoutProjectDependencies()
@get:Internal
protected val chooseVisibleSourceSets
get() = sourceSet.dependencyTransformations.values
.flatMap { it.metadataDependencyResolutions }
get() = sourceSet
.compileDependenciesTransformation
.metadataDependencyResolutions
.filterIsInstance<ChooseVisibleSourceSets>()
@Suppress("unused")