[Gradle] Deprecate scoped metadata dependencies

Scoped metadata dependencies are not needed since
metadata dependencies are used only for metadata compilation. Metadata
is not consumed at runtime. Therefore, scoped metadata dependencies
are not required.

^KT-55312
This commit is contained in:
Anton Lakotka
2022-12-08 16:16:23 +01:00
committed by Space Team
parent 417fef0d58
commit 35b2672436
11 changed files with 40 additions and 69 deletions
@@ -25,19 +25,18 @@ interface KotlinSourceSet : Named, HasKotlinDependencies {
fun dependsOn(other: KotlinSourceSet) fun dependsOn(other: KotlinSourceSet)
val dependsOn: Set<KotlinSourceSet> val dependsOn: Set<KotlinSourceSet>
val metadataLibrariesConfigurationName: String
@Deprecated(message = "KT-55312")
val apiMetadataConfigurationName: String val apiMetadataConfigurationName: String
@Deprecated(message = "KT-55312")
val implementationMetadataConfigurationName: String val implementationMetadataConfigurationName: String
@Deprecated(message = "KT-55312")
val compileOnlyMetadataConfigurationName: String val compileOnlyMetadataConfigurationName: String
@Deprecated(message = "KT-55230: RuntimeOnly scope is not supported for metadata dependency transformation") @Deprecated(message = "KT-55230: RuntimeOnly scope is not supported for metadata dependency transformation")
val runtimeOnlyMetadataConfigurationName: String val runtimeOnlyMetadataConfigurationName: String
override val relatedConfigurationNames: List<String> override val relatedConfigurationNames: List<String>
get() = super.relatedConfigurationNames + get() = super.relatedConfigurationNames + metadataLibrariesConfigurationName
listOf(
apiMetadataConfigurationName,
implementationMetadataConfigurationName,
compileOnlyMetadataConfigurationName,
)
companion object { companion object {
const val COMMON_MAIN_SOURCE_SET_NAME = "commonMain" const val COMMON_MAIN_SOURCE_SET_NAME = "commonMain"
@@ -86,8 +86,7 @@ private fun KotlinTarget.excludeStdlibAndKotlinTestCommonFromPlatformCompilation
compilations.all { compilations.all {
listOfNotNull( listOfNotNull(
it.compileDependencyConfigurationName, it.compileDependencyConfigurationName,
it.defaultSourceSet.apiMetadataConfigurationName, it.defaultSourceSet.metadataLibrariesConfigurationName,
it.defaultSourceSet.implementationMetadataConfigurationName,
(it as? KotlinCompilationToRunnableFiles<*>)?.runtimeDependencyConfigurationName, (it as? KotlinCompilationToRunnableFiles<*>)?.runtimeDependencyConfigurationName,
// Additional configurations for (old) jvmWithJava-preset. Remove it when we drop it completely // Additional configurations for (old) jvmWithJava-preset. Remove it when we drop it completely
@@ -97,17 +97,9 @@ internal sealed class MetadataDependencyResolution(
internal class GranularMetadataTransformation( internal class GranularMetadataTransformation(
val project: Project, val project: Project,
val kotlinSourceSet: KotlinSourceSet, val kotlinSourceSet: KotlinSourceSet,
/** A list of scopes that the dependencies from [kotlinSourceSet] are treated as requested dependencies. */
private val sourceSetRequestedScopes: List<KotlinDependencyScope>,
/** A configuration that holds the dependencies of the appropriate scope for all Kotlin source sets in the project */ /** A configuration that holds the dependencies of the appropriate scope for all Kotlin source sets in the project */
private val parentTransformations: Lazy<Iterable<GranularMetadataTransformation>> private val parentTransformations: Lazy<Iterable<GranularMetadataTransformation>>
) { ) {
init {
require(KotlinDependencyScope.RUNTIME_ONLY_SCOPE !in sourceSetRequestedScopes) {
"KT-55230: RuntimeOnly scope is not supported for metadata dependency transformation"
}
}
val metadataDependencyResolutions: Iterable<MetadataDependencyResolution> by lazy { doTransform() } val metadataDependencyResolutions: Iterable<MetadataDependencyResolution> by lazy { doTransform() }
// Keep parents of each dependency, too. We need a dependency's parent when it's an MPP's metadata module dependency: // Keep parents of each dependency, too. We need a dependency's parent when it's an MPP's metadata module dependency:
@@ -232,7 +224,6 @@ internal class GranularMetadataTransformation(
val sourceSetVisibility = val sourceSetVisibility =
SourceSetVisibilityProvider(project).getVisibleSourceSets( SourceSetVisibilityProvider(project).getVisibleSourceSets(
kotlinSourceSet, kotlinSourceSet,
sourceSetRequestedScopes,
if (projectStructureMetadata.isPublishedAsRoot) module else parent, module, if (projectStructureMetadata.isPublishedAsRoot) module else parent, module,
projectStructureMetadata, projectStructureMetadata,
resolvedToProject resolvedToProject
@@ -89,7 +89,6 @@ open class MetadataDependencyTransformationTask
GranularMetadataTransformation( GranularMetadataTransformation(
project, project,
kotlinSourceSet, kotlinSourceSet,
KotlinDependencyScope.compileScopes,
lazy { lazy {
dependsOnClosureWithInterCompilationDependencies(kotlinSourceSet).map { dependsOnClosureWithInterCompilationDependencies(kotlinSourceSet).map {
project.tasks.withType(MetadataDependencyTransformationTask::class.java) project.tasks.withType(MetadataDependencyTransformationTask::class.java)
@@ -49,7 +49,6 @@ internal class SourceSetVisibilityProvider(
*/ */
fun getVisibleSourceSets( fun getVisibleSourceSets(
visibleFrom: KotlinSourceSet, visibleFrom: KotlinSourceSet,
dependencyScopes: Iterable<KotlinDependencyScope>,
resolvedRootMppDependency: ResolvedComponentResult?, resolvedRootMppDependency: ResolvedComponentResult?,
resolvedMetadataDependency: ResolvedComponentResult, resolvedMetadataDependency: ResolvedComponentResult,
dependencyProjectStructureMetadata: KotlinProjectStructureMetadata, dependencyProjectStructureMetadata: KotlinProjectStructureMetadata,
@@ -65,11 +64,7 @@ internal class SourceSetVisibilityProvider(
val visiblePlatformVariantNames: Set<String?> = val visiblePlatformVariantNames: Set<String?> =
compilations compilations
.filter { it.target.platformType != KotlinPlatformType.common } .filter { it.target.platformType != KotlinPlatformType.common }
.flatMapTo(mutableSetOf()) { compilation -> .map { compilation -> project.configurations.getByName(compilation.compileDependencyConfigurationName) }
// To find out which variant the MPP dependency got resolved for each compilation, take the resolvable configurations
// that we have in the compilations:
dependencyScopes.mapNotNull { scope -> project.resolvableConfigurationFromCompilationByScope(compilation, scope) }
}
.mapTo(mutableSetOf()) { configuration -> .mapTo(mutableSetOf()) { configuration ->
val resolvedVariant = resolvedVariantsProvider.getResolvedVariantName(mppModuleIdentifier, configuration) val resolvedVariant = resolvedVariantsProvider.getResolvedVariantName(mppModuleIdentifier, configuration)
?.let { kotlinVariantNameFromPublishedVariantName(it) } ?.let { kotlinVariantNameFromPublishedVariantName(it) }
@@ -128,18 +123,3 @@ internal class SourceSetVisibilityProvider(
internal fun kotlinVariantNameFromPublishedVariantName(resolvedToVariantName: String): String = internal fun kotlinVariantNameFromPublishedVariantName(resolvedToVariantName: String): String =
originalVariantNameFromPublished(resolvedToVariantName) ?: resolvedToVariantName originalVariantNameFromPublished(resolvedToVariantName) ?: resolvedToVariantName
private fun Project.resolvableConfigurationFromCompilationByScope(
compilation: KotlinCompilation<*>,
scope: KotlinDependencyScope
): Configuration? {
val configurationName = when (scope) {
KotlinDependencyScope.API_SCOPE, KotlinDependencyScope.IMPLEMENTATION_SCOPE, KotlinDependencyScope.COMPILE_ONLY_SCOPE -> compilation.compileDependencyConfigurationName
KotlinDependencyScope.RUNTIME_ONLY_SCOPE ->
(compilation as? KotlinCompilationToRunnableFiles<*>)?.runtimeDependencyConfigurationName
?: return null
}
return project.configurations.getByName(configurationName)
}
@@ -45,12 +45,18 @@ abstract class DefaultKotlinSourceSet @Inject constructor(
override val runtimeOnlyConfigurationName: String override val runtimeOnlyConfigurationName: String
get() = disambiguateName(RUNTIME_ONLY) get() = disambiguateName(RUNTIME_ONLY)
override val metadataLibrariesConfigurationName: String
get() = disambiguateName(lowerCamelCaseName("compile", METADATA_CONFIGURATION_NAME_SUFFIX))
@Deprecated("KT-55312")
override val apiMetadataConfigurationName: String override val apiMetadataConfigurationName: String
get() = lowerCamelCaseName(apiConfigurationName, METADATA_CONFIGURATION_NAME_SUFFIX) get() = lowerCamelCaseName(apiConfigurationName, METADATA_CONFIGURATION_NAME_SUFFIX)
@Deprecated("KT-55312")
override val implementationMetadataConfigurationName: String override val implementationMetadataConfigurationName: String
get() = lowerCamelCaseName(implementationConfigurationName, METADATA_CONFIGURATION_NAME_SUFFIX) get() = lowerCamelCaseName(implementationConfigurationName, METADATA_CONFIGURATION_NAME_SUFFIX)
@Deprecated("KT-55312")
override val compileOnlyMetadataConfigurationName: String override val compileOnlyMetadataConfigurationName: String
get() = lowerCamelCaseName(compileOnlyConfigurationName, METADATA_CONFIGURATION_NAME_SUFFIX) get() = lowerCamelCaseName(compileOnlyConfigurationName, METADATA_CONFIGURATION_NAME_SUFFIX)
@@ -51,14 +51,4 @@ internal fun Project.compilationDependencyConfigurationByScope(
COMPILE_ONLY_SCOPE -> compilation.compileOnlyConfigurationName COMPILE_ONLY_SCOPE -> compilation.compileOnlyConfigurationName
RUNTIME_ONLY_SCOPE -> compilation.runtimeOnlyConfigurationName RUNTIME_ONLY_SCOPE -> compilation.runtimeOnlyConfigurationName
} }
) )
internal fun Project.sourceSetMetadataConfigurationByScope(sourceSet: KotlinSourceSet, scope: KotlinDependencyScope): Configuration? {
val configurationName = when (scope) {
API_SCOPE -> sourceSet.apiMetadataConfigurationName
IMPLEMENTATION_SCOPE -> sourceSet.implementationMetadataConfigurationName
COMPILE_ONLY_SCOPE -> sourceSet.compileOnlyMetadataConfigurationName
RUNTIME_ONLY_SCOPE -> return null // KT-55230: RuntimeOnly scope is not supported for metadata dependency transformation
}
return project.configurations.getByName(configurationName)
}
@@ -76,23 +76,24 @@ internal class DefaultKotlinSourceSetFactory(
val dependencyConfigurationWithMetadata = with(sourceSet) { val dependencyConfigurationWithMetadata = with(sourceSet) {
listOf( listOf(
apiConfigurationName to apiMetadataConfigurationName, listOf(apiConfigurationName, implementationConfigurationName, compileOnlyConfigurationName) to metadataLibrariesConfigurationName,
implementationConfigurationName to implementationMetadataConfigurationName,
compileOnlyConfigurationName to compileOnlyMetadataConfigurationName,
null to intransitiveMetadataConfigurationName null to intransitiveMetadataConfigurationName
) )
} }
dependencyConfigurationWithMetadata.forEach { (configurationName, metadataName) -> dependencyConfigurationWithMetadata.forEach { (configurationNames, metadataName) ->
project.configurations.maybeCreate(metadataName).apply { project.configurations.maybeCreate(metadataName).apply {
attributes.attribute(KotlinPlatformType.attribute, KotlinPlatformType.common) attributes.attribute(KotlinPlatformType.attribute, KotlinPlatformType.common)
attributes.attribute(Usage.USAGE_ATTRIBUTE, project.usageByName(KotlinUsages.KOTLIN_API)) attributes.attribute(Usage.USAGE_ATTRIBUTE, project.usageByName(KotlinUsages.KOTLIN_API))
attributes.attribute(Category.CATEGORY_ATTRIBUTE, project.categoryByName(Category.LIBRARY)) attributes.attribute(Category.CATEGORY_ATTRIBUTE, project.categoryByName(Category.LIBRARY))
isVisible = false isVisible = false
isCanBeConsumed = false isCanBeConsumed = false
isCanBeResolved = true
if (configurationName != null) { if (configurationNames != null) {
extendsFrom(project.configurations.maybeCreate(configurationName)) for (configurationName in configurationNames) {
extendsFrom(project.configurations.maybeCreate(configurationName))
}
} }
if (project.isKotlinGranularMetadataEnabled) { if (project.isKotlinGranularMetadataEnabled) {
@@ -345,7 +345,7 @@ class KotlinMetadataTargetConfigurator :
} }
compilation.compileDependencyFiles += createMetadataDependencyTransformationClasspath( compilation.compileDependencyFiles += createMetadataDependencyTransformationClasspath(
project.configurations.getByName(ALL_COMPILE_METADATA_CONFIGURATION_NAME), project.configurations.getByName(compilation.defaultSourceSet.metadataLibrariesConfigurationName),
compilation compilation
) )
@@ -364,7 +364,6 @@ class KotlinMetadataTargetConfigurator :
val granularMetadataTransformation = GranularMetadataTransformation( val granularMetadataTransformation = GranularMetadataTransformation(
project = project, project = project,
kotlinSourceSet = sourceSet, kotlinSourceSet = sourceSet,
sourceSetRequestedScopes = dependencyScopesToTransform,
parentTransformations = lazy { parentTransformations = lazy {
dependsOnClosureWithInterCompilationDependencies(sourceSet).filterIsInstance<DefaultKotlinSourceSet>() dependsOnClosureWithInterCompilationDependencies(sourceSet).filterIsInstance<DefaultKotlinSourceSet>()
.map { it.compileDependenciesTransformationOrFail } .map { it.compileDependenciesTransformationOrFail }
@@ -378,21 +377,28 @@ class KotlinMetadataTargetConfigurator :
val sourceSetDependencyConfigurationByScope = project.configurations.sourceSetDependencyConfigurationByScope(sourceSet, scope) val sourceSetDependencyConfigurationByScope = project.configurations.sourceSetDependencyConfigurationByScope(sourceSet, scope)
if (isSourceSetPublished) { if (isSourceSetPublished) {
project.addExtendsFromRelation(
sourceSet.metadataLibrariesConfigurationName,
sourceSetDependencyConfigurationByScope.name
)
// we need to include dependencies from sourceSetDependencyConfigurationByScope to
// ALL_COMPILE_METADATA_CONFIGURATION_NAME so then we can resolve consistently with it
// when resolving [sourceSet.metadataLibrariesConfigurationName]
project.addExtendsFromRelation( project.addExtendsFromRelation(
ALL_COMPILE_METADATA_CONFIGURATION_NAME, ALL_COMPILE_METADATA_CONFIGURATION_NAME,
sourceSetDependencyConfigurationByScope.name sourceSetDependencyConfigurationByScope.name
) )
} }
val sourceSetMetadataConfigurationByScope = project.sourceSetMetadataConfigurationByScope(sourceSet, scope)
if (sourceSetMetadataConfigurationByScope != null) {
granularMetadataTransformation.applyToConfiguration(sourceSetMetadataConfigurationByScope)
project.addExtendsFromRelation(
sourceSetMetadataConfigurationByScope.name,
ALL_COMPILE_METADATA_CONFIGURATION_NAME
)
}
} }
val sourceSetMetadataConfiguration = project.configurations.getByName(sourceSet.metadataLibrariesConfigurationName)
// not necessary: because granularMetadataTransformation already created out of this configuration
// so there is no real logic to do dependency handling there.
// !!!! granularMetadataTransformation.applyToConfiguration(sourceSetMetadataConfiguration)
sourceSetMetadataConfiguration.shouldResolveConsistentlyWith(
project.configurations.getByName(ALL_COMPILE_METADATA_CONFIGURATION_NAME)
)
} }
/** Ensure that the [configuration] excludes the dependencies that are classified by this [GranularMetadataTransformation] as /** Ensure that the [configuration] excludes the dependencies that are classified by this [GranularMetadataTransformation] as
@@ -18,6 +18,6 @@ import org.jetbrains.kotlin.gradle.plugin.sources.DefaultKotlinSourceSet
internal fun Project.addIntransitiveMetadataDependencyIfPossible(sourceSet: DefaultKotlinSourceSet, dependency: FileCollection) { internal fun Project.addIntransitiveMetadataDependencyIfPossible(sourceSet: DefaultKotlinSourceSet, dependency: FileCollection) {
val dependencyConfigurationName = val dependencyConfigurationName =
if (project.isIntransitiveMetadataConfigurationEnabled) sourceSet.intransitiveMetadataConfigurationName if (project.isIntransitiveMetadataConfigurationEnabled) sourceSet.intransitiveMetadataConfigurationName
else sourceSet.implementationMetadataConfigurationName else sourceSet.metadataLibrariesConfigurationName
project.dependencies.add(dependencyConfigurationName, dependency) project.dependencies.add(dependencyConfigurationName, dependency)
} }
@@ -97,7 +97,7 @@ private fun Project.addDependencies(
if (isIdeDependency && sourceSet is DefaultKotlinSourceSet) { if (isIdeDependency && sourceSet is DefaultKotlinSourceSet) {
val metadataConfigurationName = val metadataConfigurationName =
if (project.isIntransitiveMetadataConfigurationEnabled) sourceSet.intransitiveMetadataConfigurationName if (project.isIntransitiveMetadataConfigurationEnabled) sourceSet.intransitiveMetadataConfigurationName
else sourceSet.implementationMetadataConfigurationName else sourceSet.metadataLibrariesConfigurationName
dependencies.add(metadataConfigurationName, libraries) dependencies.add(metadataConfigurationName, libraries)
} }
} }