Model mapping: apply user-defined attributes in KPM
This commit is contained in:
+45
-7
@@ -32,6 +32,7 @@ import org.jetbrains.kotlin.gradle.plugin.mpp.pm20.hasKpmModel
|
|||||||
import org.jetbrains.kotlin.gradle.plugin.mpp.pm20.registerDefaultVariantFactories
|
import org.jetbrains.kotlin.gradle.plugin.mpp.pm20.registerDefaultVariantFactories
|
||||||
import org.jetbrains.kotlin.gradle.plugin.mpp.pm20.setupFragmentsMetadataForKpmModules
|
import org.jetbrains.kotlin.gradle.plugin.mpp.pm20.setupFragmentsMetadataForKpmModules
|
||||||
import org.jetbrains.kotlin.gradle.plugin.mpp.pm20.setupKpmModulesPublication
|
import org.jetbrains.kotlin.gradle.plugin.mpp.pm20.setupKpmModulesPublication
|
||||||
|
import org.jetbrains.kotlin.gradle.plugin.mpp.pm20.util.copyAttributes
|
||||||
import org.jetbrains.kotlin.gradle.plugin.sources.CleanupStaleSourceSetMetadataEntriesService
|
import org.jetbrains.kotlin.gradle.plugin.sources.CleanupStaleSourceSetMetadataEntriesService
|
||||||
import org.jetbrains.kotlin.gradle.plugin.sources.DefaultLanguageSettingsBuilder
|
import org.jetbrains.kotlin.gradle.plugin.sources.DefaultLanguageSettingsBuilder
|
||||||
import org.jetbrains.kotlin.gradle.plugin.sources.SourceSetMetadataStorageForIde
|
import org.jetbrains.kotlin.gradle.plugin.sources.SourceSetMetadataStorageForIde
|
||||||
@@ -105,8 +106,8 @@ class KotlinMultiplatformPlugin : Plugin<Project> {
|
|||||||
|
|
||||||
if (!project.hasKpmModel) {
|
if (!project.hasKpmModel) {
|
||||||
configurePublishingWithMavenPublish(project)
|
configurePublishingWithMavenPublish(project)
|
||||||
targetsContainer.withType(AbstractKotlinTarget::class.java).all { applyUserDefinedAttributes(it) }
|
|
||||||
}
|
}
|
||||||
|
targetsContainer.withType(AbstractKotlinTarget::class.java).all { applyUserDefinedAttributes(it) }
|
||||||
|
|
||||||
// propagate compiler plugin options to the source set language settings
|
// propagate compiler plugin options to the source set language settings
|
||||||
setupAdditionalCompilerArguments(project)
|
setupAdditionalCompilerArguments(project)
|
||||||
@@ -268,15 +269,52 @@ class KotlinMultiplatformPlugin : Plugin<Project> {
|
|||||||
internal fun applyUserDefinedAttributes(target: AbstractKotlinTarget) {
|
internal fun applyUserDefinedAttributes(target: AbstractKotlinTarget) {
|
||||||
val project = target.project
|
val project = target.project
|
||||||
|
|
||||||
project.whenEvaluated {
|
if (!project.hasKpmModel) {
|
||||||
fun copyAttributes(from: AttributeContainer, to: AttributeContainer) {
|
applyUserDefinedAttributesWithLegacyModel(target)
|
||||||
fun <T> copyAttribute(key: Attribute<T>, from: AttributeContainer, to: AttributeContainer) {
|
} else {
|
||||||
to.attribute(key, from.getAttribute(key)!!)
|
applyUserDefinedAttributesWithKpm(target)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
from.keySet().forEach { key -> copyAttribute(key, from, to) }
|
private fun applyUserDefinedAttributesWithKpm(
|
||||||
|
target: AbstractKotlinTarget,
|
||||||
|
) {
|
||||||
|
fun copyAttributesToVariant(variant: KotlinGradleVariant, from: AttributeContainer) {
|
||||||
|
variant.gradleVariantNames.forEach { configurationOrVariantName ->
|
||||||
|
val configuration = variant.project.configurations.findByName(configurationOrVariantName)
|
||||||
|
?: return@forEach
|
||||||
|
copyAttributes(from, configuration.attributes)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
val project = target.project
|
||||||
|
project.whenEvaluated {
|
||||||
|
multiplatformExtension.targets.all target@{ target ->
|
||||||
|
if (target is KotlinMetadataTarget)
|
||||||
|
return@target
|
||||||
|
|
||||||
|
target.compilations.all compilation@{ compilation ->
|
||||||
|
if (compilation is AbstractKotlinCompilation) {
|
||||||
|
val details = compilation.compilationDetails as? VariantMappedCompilationDetails<*>
|
||||||
|
?: return@compilation
|
||||||
|
copyAttributesToVariant(details.variant, compilation.attributes)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Also handle the legacy-mapped variants, which are not accessible through the compilations in the loop above
|
||||||
|
project.kpmModules.getByName(KotlinGradleModule.MAIN_MODULE_NAME).variants.withType(LegacyMappedVariant::class.java).all { variant ->
|
||||||
|
val compilation = variant.compilation
|
||||||
|
copyAttributesToVariant(variant, compilation.attributes)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun applyUserDefinedAttributesWithLegacyModel(
|
||||||
|
target: AbstractKotlinTarget,
|
||||||
|
) {
|
||||||
|
val project = target.project
|
||||||
|
project.whenEvaluated {
|
||||||
// To copy the attributes to the output configurations, find those output configurations and their producing compilations
|
// To copy the attributes to the output configurations, find those output configurations and their producing compilations
|
||||||
// based on the target's components:
|
// based on the target's components:
|
||||||
val outputConfigurationsWithCompilations =
|
val outputConfigurationsWithCompilations =
|
||||||
|
|||||||
Reference in New Issue
Block a user