Skip to content

Commit f3b53bb

Browse files
authored
Check source sets existence for Klib dump tasks only after compilation (#210)
* Added tests * Change dependencies between tasks so that tasks using information about source sets depended on klib compilation
1 parent e478dd6 commit f3b53bb

File tree

5 files changed

+75
-5
lines changed

5 files changed

+75
-5
lines changed

src/functionalTest/kotlin/kotlinx/validation/api/TestDsl.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,8 @@ internal fun FileContainer.emptyApiFile(projectName: String) {
148148
apiFile(projectName) {}
149149
}
150150

151-
internal fun BaseKotlinScope.runner(fn: Runner.() -> Unit) {
152-
val runner = Runner()
151+
internal fun BaseKotlinScope.runner(withConfigurationCache: Boolean = true, fn: Runner.() -> Unit) {
152+
val runner = Runner(withConfigurationCache)
153153
fn(runner)
154154

155155
this.runner = runner
@@ -188,9 +188,9 @@ internal class AppendableScope(val filePath: String) {
188188
val files: MutableList<String> = mutableListOf()
189189
}
190190

191-
internal class Runner {
191+
internal class Runner(withConfigurationCache: Boolean = true) {
192192
val arguments: MutableList<String> = mutableListOf<String>().apply {
193-
if (!koverEnabled) {
193+
if (!koverEnabled && withConfigurationCache) {
194194
// Configuration cache is incompatible with javaagents being enabled for Gradle
195195
// See https://github.com/gradle/gradle/issues/25979
196196
add("--configuration-cache")

src/functionalTest/kotlin/kotlinx/validation/test/KlibVerificationTests.kt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -669,4 +669,33 @@ internal class KlibVerificationTests : BaseKotlinGradleTest() {
669669
}
670670
runner.build()
671671
}
672+
673+
@Test
674+
fun `apiDump for a project with generated sources only`() {
675+
val runner = test {
676+
baseProjectSetting()
677+
additionalBuildConfig("/examples/gradle/configuration/generatedSources/generatedSources.gradle.kts")
678+
// TODO: enable configuration cache back when we start skipping tasks correctly
679+
runner(withConfigurationCache = false) {
680+
arguments.add(":apiDump")
681+
}
682+
}
683+
checkKlibDump(runner.build(), "/examples/classes/GeneratedSources.klib.dump")
684+
}
685+
686+
@Test
687+
fun `apiCheck for a project with generated sources only`() {
688+
val runner = test {
689+
baseProjectSetting()
690+
additionalBuildConfig("/examples/gradle/configuration/generatedSources/generatedSources.gradle.kts")
691+
abiFile(projectName = "testproject") {
692+
resolve("/examples/classes/GeneratedSources.klib.dump")
693+
}
694+
// TODO: enable configuration cache back when we start skipping tasks correctly
695+
runner(withConfigurationCache = false) {
696+
arguments.add(":apiCheck")
697+
}
698+
}
699+
assertApiCheckPassed(runner.build())
700+
}
672701
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Klib ABI Dump
2+
// Targets: [androidNativeArm32, androidNativeArm64, androidNativeX64, androidNativeX86, linuxArm64, linuxX64, mingwX64]
3+
// Rendering settings:
4+
// - Signature version: 2
5+
// - Show manifest properties: true
6+
// - Show declarations: true
7+
8+
// Library unique name: <testproject>
9+
final class /Generated { // /Generated|null[0]
10+
constructor <init>() // /Generated.<init>|<init>(){}[0]
11+
final fun helloCreator(): kotlin/Int // /Generated.helloCreator|helloCreator(){}[0]
12+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
abstract class GenerateSourcesTask : org.gradle.api.DefaultTask() {
2+
@get:org.gradle.api.tasks.OutputDirectory
3+
abstract val outputDirectory: org.gradle.api.file.DirectoryProperty
4+
5+
@org.gradle.api.tasks.TaskAction
6+
fun generate() {
7+
outputDirectory.asFile.get().mkdirs()
8+
outputDirectory.file("Generated.kt").get().asFile.writeText("""
9+
public class Generated { public fun helloCreator(): Int = 42 }
10+
""".trimIndent())
11+
}
12+
}
13+
14+
val srcgen = project.tasks.register("generateSources", GenerateSourcesTask::class.java)
15+
srcgen.configure {
16+
outputDirectory.set(project.layout.buildDirectory.get().dir("generated").dir("kotlin"))
17+
}
18+
19+
val kotlin = project.extensions.getByType(org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension::class.java)
20+
kotlin.sourceSets.getByName("commonMain") {
21+
kotlin.srcDir(srcgen)
22+
}

src/main/kotlin/BinaryCompatibilityValidatorPlugin.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,9 +398,16 @@ private class KlibValidationPipelineBuilder(
398398
commonApiCheck.configure { it.dependsOn(klibCheck) }
399399

400400
klibDump.configure { it.dependsOn(klibMergeInferred) }
401+
// Extraction task depends on supportedTargets() provider which returns a set of targets supported
402+
// by the host compiler and having some sources. A set of sources for a target may change until the actual
403+
// klib compilation will take place, so we may observe incorrect value if check source sets earlier.
404+
// Merge task already depends on compilations, so instead of adding each compilation task to the extraction's
405+
// dependency set, we can depend on the merge task itself.
406+
klibExtractAbiForSupportedTargets.configure {
407+
it.dependsOn(klibMerge)
408+
}
401409
klibCheck.configure {
402410
it.dependsOn(klibExtractAbiForSupportedTargets)
403-
it.dependsOn(klibMerge)
404411
}
405412

406413
project.configureTargets(klibApiDir, klibMerge, klibMergeInferred)

0 commit comments

Comments
 (0)