Skip to content

Commit

Permalink
[Gradle] Use packed klib variant as the default when no packaging att…
Browse files Browse the repository at this point in the history
…ribute is present

^KT-72092 Fixed
  • Loading branch information
ALikhachev authored and qodana-bot committed Oct 10, 2024
1 parent 14ef60f commit 77984b5
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,11 @@ private class KlibPackagingCompatibilityRule : AttributeCompatibilityRule<KlibPa
private class KlibPackagingDisambiguationRule : AttributeDisambiguationRule<KlibPackaging> {
override fun execute(details: MultipleCandidatesDetails<KlibPackaging>) = 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)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -26,6 +29,9 @@ internal val ConfigureFrameworkExportSideEffect = KotlinTargetSideEffect<KotlinN
usesPlatformOf(target)
attributes.setAttribute(Usage.USAGE_ATTRIBUTE, KotlinUsages.consumerApiUsage(target))
attributes.setAttribute(Category.CATEGORY_ATTRIBUTE, project.categoryByName(Category.LIBRARY))
if (project.kotlinPropertiesProvider.useNonPackedKlibs) {
KlibPackaging.setAttributeTo(project, attributes, false)
}
description = "Dependencies to be exported in framework ${framework.name} for target ${target.targetName}"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ import org.gradle.api.artifacts.Configuration
import org.gradle.api.plugins.BasePlugin
import org.gradle.api.tasks.Exec
import org.gradle.language.base.plugins.LifecycleBasePlugin
import org.jetbrains.kotlin.gradle.internal.attributes.setAttributeTo
import org.jetbrains.kotlin.gradle.plugin.*
import org.jetbrains.kotlin.gradle.plugin.PropertiesProvider
import org.jetbrains.kotlin.gradle.plugin.PropertiesProvider.Companion.kotlinPropertiesProvider
import org.jetbrains.kotlin.gradle.plugin.attributes.KlibPackaging
import org.jetbrains.kotlin.gradle.plugin.mpp.*
import org.jetbrains.kotlin.gradle.plugin.statistics.NativeLinkTaskMetrics
import org.jetbrains.kotlin.gradle.targets.KotlinTargetSideEffect
Expand Down Expand Up @@ -91,6 +93,9 @@ private fun KotlinNativeCompilation.resolvableApiConfiguration(): Configuration
extendsFrom(apiConfiguration)
val compileConfiguration = compilation.internal.configurations.compileDependencyConfiguration
compileConfiguration.copyAttributesTo(project.providers, this)
if (project.kotlinPropertiesProvider.useNonPackedKlibs) {
KlibPackaging.setAttributeTo(project, attributes, false)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ class KotlinTargetVariantResourcesResolutionTests {
expectedResult = { _, middle, producer ->
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"),
)
}
)
Expand All @@ -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"),
)
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -23,13 +24,17 @@ 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
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.*

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 77984b5

Please sign in to comment.