[Gradle] Ensure default stdlib is added to dependsOn source sets

^KT-60901 Verification Pending
This commit is contained in:
Sebastian Sellmair
2023-08-09 14:35:34 +02:00
committed by Space Team
parent 10f3704b35
commit 00362390eb
@@ -16,6 +16,7 @@ import org.jetbrains.kotlin.gradle.plugin.KotlinPlatformType
import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet
import org.jetbrains.kotlin.gradle.plugin.KotlinTarget
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinAndroidTarget
import org.jetbrains.kotlin.gradle.plugin.mpp.internal
import org.jetbrains.kotlin.gradle.plugin.mpp.isTest
import org.jetbrains.kotlin.gradle.plugin.sources.KotlinDependencyScope
import org.jetbrains.kotlin.gradle.plugin.sources.android.AndroidBaseSourceSetName
@@ -40,7 +41,7 @@ internal const val KOTLIN_ANDROID_JVM_STDLIB_MODULE_NAME = KOTLIN_STDLIB_MODULE_
internal fun Project.configureStdlibDefaultDependency(
topLevelExtension: KotlinTopLevelExtension,
coreLibrariesVersion: Provider<String>
coreLibrariesVersion: Provider<String>,
) {
when (topLevelExtension) {
is KotlinJsProjectExtension -> topLevelExtension.registerTargetObserver { target ->
@@ -98,7 +99,7 @@ internal fun ConfigurationContainer.configureStdlibVersionAlignment() = all { co
}
private fun Configuration.alignStdlibJvmVariantVersions(
kotlinStdlibDependency: ExternalDependency
kotlinStdlibDependency: ExternalDependency,
) {
resolutionStrategy.dependencySubstitution {
if (kotlinStdlibDependency.name != KOTLIN_STDLIB_JDK7_MODULE_NAME) {
@@ -120,11 +121,9 @@ private fun KotlinTarget.addStdlibDependency(
isMppProject: Boolean,
) {
compilations.configureEach { compilation ->
compilation.allKotlinSourceSets.forEach { kotlinSourceSet ->
compilation.internal.kotlinSourceSets.forAll { kotlinSourceSet ->
val scope = if (compilation.isTest() ||
(this is KotlinAndroidTarget &&
kotlinSourceSet.isRelatedToAndroidTestSourceSet()
)
(this is KotlinAndroidTarget && kotlinSourceSet.isRelatedToAndroidTestSourceSet())
) {
KotlinDependencyScope.IMPLEMENTATION_SCOPE
} else {
@@ -135,7 +134,13 @@ private fun KotlinTarget.addStdlibDependency(
scopeConfiguration.withDependencies { dependencySet ->
// Check if stdlib is directly added to SourceSet
if (isStdlibAddedByUser(configurations, stdlibModules, kotlinSourceSet)) return@withDependencies
/*
In case of 'kotlin-stdlib-common being added: Ensure that stdlib is added also
This will help with substituting kotlin-stdlib-common with kotlin-stdlib:
Currently transformed metadata dependencies will not recognise the substitution otherwise.
*/
if (isStdlibAddedByUser(configurations, stdlibModules - KOTLIN_STDLIB_COMMON_MODULE_NAME, kotlinSourceSet))
return@withDependencies
val requestedStdlibVersion = coreLibrariesVersion.get()
val stdlibVersion = SemVer.fromGradleRichVersion(requestedStdlibVersion)
@@ -144,24 +149,13 @@ private fun KotlinTarget.addStdlibDependency(
// except standalone compilations which as not using 'common'
if (isMppProject &&
stdlibVersion >= kotlin1920Version &&
(compilation.platformType != KotlinPlatformType.common && compilation.platformType != KotlinPlatformType.wasm) &&
kotlinSourceSet.hasDependencyOnCommon()
compilation.platformType != KotlinPlatformType.wasm &&
kotlinSourceSet.dependsOn.isNotEmpty()
) return@withDependencies
val stdlibModule = compilation
.platformType
.stdlibPlatformType(this, kotlinSourceSet, stdlibVersion >= kotlin1920Version)
val stdlibModule = compilation.platformType.stdlibPlatformType(this, kotlinSourceSet, stdlibVersion >= kotlin1920Version)
?: return@withDependencies
// Check if stdlib module is added to SourceSets hierarchy
if (
isStdlibAddedByUser(
configurations,
setOf(stdlibModule),
*kotlinSourceSet.internal.dependsOnClosure.toTypedArray()
)
) return@withDependencies
dependencySet.addLater(
coreLibrariesVersion.map {
dependencies.kotlinDependency(stdlibModule, it)
@@ -172,15 +166,10 @@ private fun KotlinTarget.addStdlibDependency(
}
}
internal fun KotlinSourceSet.hasDependencyOnCommon(): Boolean = dependsOn
.any { sourceSet ->
sourceSet.internal.compilations.any { it.platformType == KotlinPlatformType.common }
}
internal fun isStdlibAddedByUser(
configurations: ConfigurationContainer,
stdlibModules: Set<String>,
vararg sourceSets: KotlinSourceSet
vararg sourceSets: KotlinSourceSet,
): Boolean {
return sourceSets
.asSequence()
@@ -198,7 +187,7 @@ internal fun isStdlibAddedByUser(
internal fun KotlinPlatformType.stdlibPlatformType(
kotlinTarget: KotlinTarget,
kotlinSourceSet: KotlinSourceSet,
isVersionWithGradleMetadata: Boolean
isVersionWithGradleMetadata: Boolean,
): String? = when (this) {
KotlinPlatformType.jvm -> if (isVersionWithGradleMetadata) KOTLIN_STDLIB_MODULE_NAME else KOTLIN_STDLIB_JDK8_MODULE_NAME
KotlinPlatformType.androidJvm -> {