Add defaultSourceSet to KotlinCompilation
Previously, the connection between a `KotlinCompilation` and its default `KotlinSourceSet` (the one automatically created and added into the compilation) was not expressed in the MPP model in any reasonable way that can be used in the build scripts. However, this connection seems to have settled in the build logic, and it may be useful to be able to get the default source set from a compilation, for example, to be able to configure dependencies across several targets in a uniform way. The metadata compilations don't have their dedicated source sets, but we can think of commonMain as a default source set for the project's metadata, is it is currently in fact only contains the declarations from commonMain. Issue #KT-27685 Fixed
This commit is contained in:
+4
@@ -29,6 +29,10 @@ interface KotlinCompilation<out T : KotlinCommonOptions> : Named, HasAttributes,
|
||||
|
||||
val kotlinSourceSets: Set<KotlinSourceSet>
|
||||
|
||||
val defaultSourceSet: KotlinSourceSet
|
||||
|
||||
fun defaultSourceSet(configure: KotlinSourceSet.() -> Unit)
|
||||
|
||||
val compileDependencyConfigurationName: String
|
||||
|
||||
var compileDependencyFiles: FileCollection
|
||||
|
||||
+36
@@ -1114,6 +1114,42 @@ class NewMultiplatformIT : BaseGradleIT() {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testDefaultSourceSetsDsl() = with(Project("sample-lib", gradleVersion, "new-mpp-lib-and-app")) {
|
||||
setupWorkingDir()
|
||||
|
||||
val testOutputPrefix = "# default source set "
|
||||
val testOutputRegex = Regex("${Regex.escape(testOutputPrefix)} (.*?) (.*?) (.*)")
|
||||
|
||||
gradleBuildScript().appendText(
|
||||
"\n" + """
|
||||
kotlin.targets.each { target ->
|
||||
target.compilations.each { compilation ->
|
||||
println "$testOutputPrefix ${'$'}{target.name} ${'$'}{compilation.name} ${'$'}{compilation.defaultSourceSet.name}"
|
||||
}
|
||||
}
|
||||
""".trimIndent()
|
||||
)
|
||||
|
||||
build {
|
||||
assertSuccessful()
|
||||
|
||||
val actualDefaultSourceSets = testOutputRegex.findAll(output).mapTo(mutableSetOf()) {
|
||||
it.groupValues.let { (_, target, compilation, sourceSet) -> Triple(target, compilation, sourceSet) }
|
||||
}
|
||||
|
||||
val expectedDefaultSourceSets = listOf(
|
||||
"jvm6", "nodeJs", "wasm32", "mingw64", "linux64", "macos64"
|
||||
).flatMapTo(mutableSetOf()) { target ->
|
||||
listOf("main", "test").map { compilation ->
|
||||
Triple(target, compilation, "$target${compilation.capitalize()}")
|
||||
}
|
||||
} + Triple("metadata", "main", "commonMain")
|
||||
|
||||
assertEquals(expectedDefaultSourceSets, actualDefaultSourceSets)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testUnusedSourceSetsReport() = with(Project("sample-lib", gradleVersion, "new-mpp-lib-and-app")) {
|
||||
setupWorkingDir()
|
||||
|
||||
+16
@@ -80,6 +80,12 @@ abstract class AbstractKotlinCompilation<T : KotlinCommonOptions>(
|
||||
|
||||
override val kotlinSourceSets: MutableSet<KotlinSourceSet> = mutableSetOf()
|
||||
|
||||
override val defaultSourceSet: KotlinSourceSet
|
||||
get() = target.project.kotlinExtension.sourceSets.getByName(defaultSourceSetName)
|
||||
|
||||
override fun defaultSourceSet(configure: KotlinSourceSet.() -> Unit) =
|
||||
configure(defaultSourceSet)
|
||||
|
||||
override val output: KotlinCompilationOutput by lazy {
|
||||
DefaultKotlinCompilationOutput(
|
||||
target.project,
|
||||
@@ -285,6 +291,16 @@ class KotlinCommonCompilation(
|
||||
) : AbstractKotlinCompilation<KotlinMultiplatformCommonOptions>(target, name) {
|
||||
override val compileKotlinTask: KotlinCompileCommon
|
||||
get() = super.compileKotlinTask as KotlinCompileCommon
|
||||
|
||||
// TODO once we properly compile metadata for each source set, the default source sets will likely become just the source sets
|
||||
// which are transformed to metadata
|
||||
private val commonSourceSetName = when (compilationName) {
|
||||
KotlinCompilation.MAIN_COMPILATION_NAME -> KotlinSourceSet.COMMON_MAIN_SOURCE_SET_NAME
|
||||
else -> error("Custom metadata compilations are not supported yet")
|
||||
}
|
||||
|
||||
override val defaultSourceSet: KotlinSourceSet
|
||||
get() = target.project.kotlinExtension.sourceSets.getByName(commonSourceSetName)
|
||||
}
|
||||
|
||||
class KotlinNativeCompilation(
|
||||
|
||||
Reference in New Issue
Block a user