diff --git a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/internal/attributes/KlibPackaging.kt b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/internal/attributes/KlibPackaging.kt index d9763d34fc73d..fb36f964eefc0 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/internal/attributes/KlibPackaging.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/internal/attributes/KlibPackaging.kt @@ -41,9 +41,11 @@ private class KlibPackagingCompatibilityRule : AttributeCompatibilityRule { override fun execute(details: MultipleCandidatesDetails) = with(details) { val candidateNames = getCandidateNames() + val consumerValue = consumerValue when { - KlibPackaging.NON_PACKED in candidateNames -> chooseCandidateByName(KlibPackaging.NON_PACKED) + consumerValue != null && consumerValue in candidateValues -> closestMatch(consumerValue) KlibPackaging.PACKED in candidateNames -> chooseCandidateByName(KlibPackaging.PACKED) + KlibPackaging.NON_PACKED in candidateNames -> chooseCandidateByName(KlibPackaging.NON_PACKED) } } } \ No newline at end of file diff --git a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/native/ConfigureFrameworkExportSideEffect.kt b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/native/ConfigureFrameworkExportSideEffect.kt index df6342bbd6957..64be982b2aa1d 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/native/ConfigureFrameworkExportSideEffect.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/native/ConfigureFrameworkExportSideEffect.kt @@ -7,6 +7,9 @@ package org.jetbrains.kotlin.gradle.targets.native import org.gradle.api.attributes.Category import org.gradle.api.attributes.Usage +import org.jetbrains.kotlin.gradle.internal.attributes.setAttributeTo +import org.jetbrains.kotlin.gradle.plugin.PropertiesProvider.Companion.kotlinPropertiesProvider +import org.jetbrains.kotlin.gradle.plugin.attributes.KlibPackaging import org.jetbrains.kotlin.gradle.plugin.categoryByName import org.jetbrains.kotlin.gradle.plugin.mpp.AbstractNativeLibrary import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget @@ -26,6 +29,9 @@ internal val ConfigureFrameworkExportSideEffect = KotlinTargetSideEffect setOf( producer.buildFile("kotlin-multiplatform-resources/zip-for-publication/linuxX64/producer.kotlin_resources.zip"), - middle.buildFile("classes/kotlin/linuxX64/main/klib/middle"), + middle.buildFile("libs/middle-linuxX64Main.klib"), ) } ) @@ -183,7 +183,7 @@ class KotlinTargetVariantResourcesResolutionTests { expectedResult = { _, middle, producer -> setOf( producer.buildFile("kotlin-multiplatform-resources/zip-for-publication/wasmJs/producer.kotlin_resources.zip"), - middle.buildFile("classes/kotlin/wasmJs/main"), + middle.buildFile("libs/middle-wasm-js.klib"), ) } ) diff --git a/libraries/tools/kotlin-gradle-plugin/src/functionalTest/kotlin/org/jetbrains/kotlin/gradle/unitTests/MultiplatformSecondaryOutgoingVariantsTest.kt b/libraries/tools/kotlin-gradle-plugin/src/functionalTest/kotlin/org/jetbrains/kotlin/gradle/unitTests/MultiplatformSecondaryOutgoingVariantsTest.kt index 804b8ea4de178..0b93947ac4cad 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/functionalTest/kotlin/org/jetbrains/kotlin/gradle/unitTests/MultiplatformSecondaryOutgoingVariantsTest.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/functionalTest/kotlin/org/jetbrains/kotlin/gradle/unitTests/MultiplatformSecondaryOutgoingVariantsTest.kt @@ -13,6 +13,7 @@ import org.gradle.api.component.ComponentWithVariants import org.gradle.api.file.FileCollection import org.gradle.api.internal.component.SoftwareComponentInternal import org.gradle.api.internal.project.ProjectInternal +import org.gradle.kotlin.dsl.project import org.gradle.testfixtures.ProjectBuilder import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl import org.jetbrains.kotlin.gradle.dsl.multiplatformExtension @@ -23,6 +24,9 @@ import org.jetbrains.kotlin.gradle.plugin.attributes.KlibPackaging import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinUsages import org.jetbrains.kotlin.gradle.targets.NON_PACKED_KLIB_VARIANT_NAME +import org.jetbrains.kotlin.gradle.targets.js.KotlinWasmTargetAttribute +import org.jetbrains.kotlin.gradle.targets.js.ir.KotlinJsIrTarget +import org.jetbrains.kotlin.gradle.targets.js.toAttribute import org.jetbrains.kotlin.gradle.tasks.configuration.BaseKotlinCompileConfig.Companion.CLASSES_SECONDARY_VARIANT_NAME import org.jetbrains.kotlin.gradle.util.buildProjectWithMPP import org.jetbrains.kotlin.gradle.util.enableDefaultJsDomApiDependency @@ -30,6 +34,7 @@ import org.jetbrains.kotlin.gradle.util.enableDefaultStdlibDependency import org.jetbrains.kotlin.gradle.util.enableNonPackedKlibsUsage import org.jetbrains.kotlin.gradle.util.enableSecondaryJvmClassesVariant import org.jetbrains.kotlin.gradle.util.osVariantSeparatorsPathString +import org.jetbrains.kotlin.gradle.utils.setAttribute import org.jetbrains.kotlin.test.util.JUnit4Assertions.assertTrue import kotlin.test.* @@ -199,6 +204,48 @@ class MultiplatformSecondaryOutgoingVariantsTest { checkNonPackedKlibVariantResolved(hasDependencies = { it.platformType in hasRuntimeClasspath }) { it.runtimeDependencyFiles } } + @Test + fun checkDefaultResolvedVariant() { + val project = buildKmpProjectWithKlibTargets(projectBuilder = { withName("app") }, preApplyCode = { + enableDefaultStdlibDependency(false) + enableDefaultJsDomApiDependency(false) + enableNonPackedKlibsUsage(true) + }) + val configurations = project.configurations + for (target in project.multiplatformExtension.targets) { + if (target.platformType !in PLATFORM_TYPES_SUPPORTING_NON_PACKED_KLIB) continue + val myDependencyScope = configurations.dependencyScope("${target.name}DependencyScope").get() + val myResolvable = configurations.resolvable("${target.name}Resolvable") { + it.extendsFrom(myDependencyScope) + it.attributes { + it.setAttribute(KotlinPlatformType.attribute, target.platformType) + if (target is KotlinNativeTarget) { + it.setAttribute(KotlinNativeTarget.konanTargetAttribute, target.konanTarget.name) + } + if (target.platformType == KotlinPlatformType.wasm && target is KotlinJsIrTarget) { + val wasmType = target.wasmTargetType?.toAttribute() + ?: error("Wasm type attribute expected to be set for $target") + it.setAttribute(KotlinWasmTargetAttribute.wasmTargetAttribute, wasmType) + } + it.setAttribute(Usage.USAGE_ATTRIBUTE, project.objects.named(Usage::class.java, KotlinUsages.KOTLIN_API)) + } + }.get() + + project.dependencies.add(myDependencyScope.name, project.dependencies.project(project.path)) + + val resolvedFiles = try { + myResolvable.resolve() + } catch (e: Exception) { + throw IllegalStateException("Failed to resolve dependencies for $target", e) + } + for (file in resolvedFiles) { + assert(file.invariantSeparatorsPath.matches(".*/libs/${project.name}-[a-zA-Z0-9-]+.klib".toRegex())) { + "Expected ${file.path} (resolved from $target) to be a klib in the packed form" + } + } + } + } + /** * Covers the case: * * app.main -> lib.main