[Gradle] Project.getCommonizerTarget(sourceSet: KotlinSourceSet): Ignore orphan source sets

Orphan source set's (source sets without compilation) can't break any
compilation. The decision to ignore those source sets for determining
the platform was already done for the IDE import.
In order to stay consistent, this function also ignores orphans.
This commit is contained in:
sebastian.sellmair
2021-06-21 15:27:52 +02:00
committed by Space
parent 97ea9c2c16
commit 19b99d5d91
2 changed files with 35 additions and 12 deletions
@@ -177,4 +177,32 @@ class SourceSetCommonizerTargetTest {
assertNull(project.getCommonizerTarget(nativeMain), "Expected no commonizer target, since no real source set hierarchy is given")
}
@Test
fun `orphan source sets are ignored`() {
val linux1 = kotlin.linuxX64("linux1")
val linux2 = kotlin.linuxArm64("linux2")
val nativeMain = kotlin.sourceSets.create("nativeMain")
val linux1Main = kotlin.sourceSets.getByName("linux1Main")
val linux2Main = kotlin.sourceSets.getByName("linux2Main")
val orphan = kotlin.sourceSets.create("orphan")
linux1Main.dependsOn(nativeMain)
linux2Main.dependsOn(nativeMain)
orphan.dependsOn(nativeMain)
assertEquals(CommonizerTarget(linux1.konanTarget, linux2.konanTarget), project.getCommonizerTarget(nativeMain))
}
@Test
fun `orphan source sets only`() {
val nativeMain = kotlin.sourceSets.create("nativeMain")
val orphan1 = kotlin.sourceSets.create("orphan1")
val orphan2 = kotlin.sourceSets.create("orphan2")
orphan1.dependsOn(nativeMain)
orphan2.dependsOn(nativeMain)
assertEquals(null, project.getCommonizerTarget(nativeMain))
}
}
@@ -13,15 +13,17 @@ import org.jetbrains.kotlin.commonizer.allLeaves
import org.jetbrains.kotlin.gradle.dsl.multiplatformExtensionOrNull
import org.jetbrains.kotlin.gradle.plugin.KotlinCompilation
import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet
import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSetContainer
import org.jetbrains.kotlin.gradle.plugin.mpp.CompilationSourceSetUtil.compilationsBySourceSets
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeCompilation
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinSharedNativeCompilation
import org.jetbrains.kotlin.gradle.plugin.sources.resolveAllSourceSetsDependingOn
import org.jetbrains.kotlin.konan.target.KonanTarget
internal fun Project.getCommonizerTarget(sourceSet: KotlinSourceSet): CommonizerTarget? {
val kotlin = multiplatformExtensionOrNull ?: return null
val sourceSetsDependingOnThisSourceSet = kotlin.resolveSourceSetsDirectlyDependingOn(sourceSet)
val sourceSetsDependingOnThisSourceSet = kotlin.resolveAllSourceSetsDependingOn(sourceSet)
.filterNot(::isOrphanSourceSet)
if (sourceSetsDependingOnThisSourceSet.isEmpty()) {
return getLeafCommonizerTarget(sourceSet)
}
@@ -50,16 +52,8 @@ private fun Project.getLeafCommonizerTarget(sourceSet: KotlinSourceSet): LeafCom
else null
}
private fun KotlinSourceSetContainer.resolveSourceSetsDirectlyDependingOn(sourceSet: KotlinSourceSet): Set<KotlinSourceSet> {
val dependeeSourceSets = sourceSets
.filter { candidateSourceSet -> candidateSourceSet != sourceSet }
.filter { candidateSourceSet -> sourceSet in candidateSourceSet.dependsOn }
fun isTransitiveDependee(sourceSet: KotlinSourceSet): Boolean {
return sourceSet.dependsOn.any { it in dependeeSourceSets || isTransitiveDependee(it) }
}
return dependeeSourceSets.filterNot(::isTransitiveDependee).toSet()
private fun Project.isOrphanSourceSet(sourceSet: KotlinSourceSet): Boolean {
return compilationsBySourceSets(this)[sourceSet].orEmpty().isEmpty()
}
private val KotlinCompilation<*>.konanTargets: Set<KonanTarget>
@@ -70,3 +64,4 @@ private val KotlinCompilation<*>.konanTargets: Set<KonanTarget>
else -> emptySet()
}
}