KT-48709: Fix CInterop configurations ambiguity with user variants

* Introduce a new org.gradle.usage value: kotlin-cinterop

* Add compatibility+disambiguation rules to ensure that kotlin-cinterop
  consumers can also fall back to ordinary (kotlin-api) published
  variants, but not vice versa

  * This ensures that ordinary kotlin-api consumers don't encounter
    equally-compatible candidates where one is the normal API elements
    and the other is C interop API elements

* Set org.gradle.usage = kotlin-cinterop in the C interop API elements
  configurations and dependency-consuming configurations

Issue #KT-48709
This commit is contained in:
Sergey Igushkin
2021-10-20 23:32:52 +04:00
committed by Space
parent a474e8a00b
commit 8868738ac8
3 changed files with 80 additions and 4 deletions
@@ -26,7 +26,9 @@ import org.jetbrains.kotlin.konan.target.KonanTarget
import org.jetbrains.kotlin.konan.target.presetName
import org.junit.Assume
import org.junit.Ignore
import org.junit.Rule
import org.junit.Test
import org.junit.rules.ErrorCollector
import java.io.File
import java.util.*
import kotlin.test.assertEquals
@@ -1034,6 +1036,51 @@ class GeneralNativeIT : BaseGradleIT() {
}
}
@Test
fun testCinteropConfigurationsVariantAwareResolution() = with(transformNativeTestProjectWithPluginDsl("native-cinterop")) {
build(":publishedLibrary:publish") {
assertSuccessful()
}
fun CompiledProject.assertVariantInDependencyInsight(variantName: String) {
try {
assertContains("variant \"$variantName\" [")
} catch (originalError: AssertionError) {
val matchedVariants = Regex("variant \"(.*?)\" \\[").findAll(output).toList()
throw AssertionError(
"Expected variant $variantName. " +
if (matchedVariants.isNotEmpty())
"Matched instead: " + matchedVariants.joinToString { it.groupValues[1] }
else "No match.",
originalError
)
}
}
build(":dependencyInsight", "--configuration", "hostTestTestNumberCInterop", "--dependency", "org.example:publishedLibrary") {
assertSuccessful()
assertVariantInDependencyInsight("hostApiElements-published")
}
gradleBuildScript("projectLibrary").appendText(
"\n" + """
configurations.create("ktlint") {
def bundlingAttribute = Attribute.of("org.gradle.dependency.bundling", String)
attributes.attribute(bundlingAttribute, "external")
}
""".trimIndent()
)
build(":dependencyInsight", "--configuration", "hostTestTestNumberCInterop", "--dependency", ":projectLibrary") {
assertSuccessful()
assertVariantInDependencyInsight("hostCInteropApiElements")
}
build(":dependencyInsight", "--configuration", "hostCompileKlibraries", "--dependency", ":projectLibrary") {
assertSuccessful()
assertVariantInDependencyInsight("hostApiElements")
}
}
companion object {
fun List<String>.containsSequentially(vararg elements: String): Boolean {
check(elements.isNotEmpty())