Skip to content

Commit

Permalink
Fix support for original XCFramework API.
Browse files Browse the repository at this point in the history
  • Loading branch information
FilipDolnik committed May 15, 2024
1 parent f44977b commit 1990f8d
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class GradleAnalyticsManager(
val analyticsCollectorProvider = target.skieDirectories.map { skieDirectories ->
AnalyticsCollector(
skieBuildDirectory = skieDirectories.buildDirectory,
skieConfiguration = project.skieExtension.buildConfiguration(target.outputKind),
skieConfiguration = project.skieExtension.buildConfiguration(target),
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ abstract class CreateSkieConfigurationTask : DefaultTask() {
val createConfiguration = target.registerSkieTargetBasedTask<CreateSkieConfigurationTask>("createConfiguration") {
val skieExtension = project.skieExtension
configurationFile.set(target.skieBuildDirectory.skieConfiguration)
configuration.set(project.provider { skieExtension.buildConfiguration(target.outputKind) })
configuration.set(project.provider { skieExtension.buildConfiguration(target) })

dependsOn(target.createSkieBuildDirectoryTask)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,21 @@ fun SkieExtension.Companion.createExtension(project: Project): SkieExtension =
val Project.skieExtension: SkieExtension
get() = project.extensions.getByType(SkieExtension::class.java)

fun SkieExtension.buildConfiguration(outputKind: SkieTarget.OutputKind): GradleSkieConfigurationData =
fun SkieExtension.buildConfiguration(target: SkieTarget): GradleSkieConfigurationData =
GradleSkieConfigurationData(
enabledConfigurationFlags = (mergeConfigurationSetsFromConfigurations(outputKind) + additionalConfigurationFlags.get()) - suppressedConfigurationFlags.get(),
enabledConfigurationFlags = getUserConfiguredFlags() + target.requiredConfigurationFlags,
groups = features.buildGroups(),
)

private fun SkieExtension.mergeConfigurationSetsFromConfigurations(outputKind: SkieTarget.OutputKind): Set<SkieConfigurationFlag> =
private fun SkieExtension.getUserConfiguredFlags(): Set<SkieConfigurationFlag> =
(mergeConfigurationSetsFromConfigurations() + additionalConfigurationFlags.get()) - suppressedConfigurationFlags.get()

private fun SkieExtension.mergeConfigurationSetsFromConfigurations(): Set<SkieConfigurationFlag> =
analytics.buildConfigurationFlags() +
build.buildConfigurationFlags() +
debug.buildConfigurationFlags() +
features.buildConfigurationFlags() +
migration.buildConfigurationFlags() +
addSwiftLibraryEvolutionFlagIfNeeded(outputKind)

private fun addSwiftLibraryEvolutionFlagIfNeeded(outputKind: SkieTarget.OutputKind): Set<SkieConfigurationFlag> =
if (outputKind == SkieTarget.OutputKind.XCFramework) {
setOf(SkieConfigurationFlag.Build_SwiftLibraryEvolution)
} else {
emptySet()
}
migration.buildConfigurationFlags()

private fun SkieFeatureConfiguration.buildGroups(): List<GradleSkieConfigurationData.Group> =
groupConfigurations.map { it.build() }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package co.touchlab.skie.plugin

import co.touchlab.skie.configuration.SkieConfigurationFlag
import co.touchlab.skie.plugin.shim.KonanTargetShim
import co.touchlab.skie.plugin.shim.KotlinSourceSetShim
import co.touchlab.skie.plugin.util.KotlinCompilerPluginOption
Expand All @@ -23,6 +24,8 @@ sealed interface SkieTarget {

val outputKind: OutputKind

val requiredConfigurationFlags: Set<SkieConfigurationFlag>

val skieDirectories: Provider<SkieDirectories>

val freeCompilerArgs: Provider<List<String>>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package co.touchlab.skie.plugin

import co.touchlab.skie.configuration.SkieConfigurationFlag
import co.touchlab.skie.plugin.shim.ActualKonanTargetShim
import co.touchlab.skie.plugin.shim.KonanTargetShim
import co.touchlab.skie.plugin.util.KotlinCompilerPluginOption
Expand Down Expand Up @@ -35,6 +36,10 @@ class ActualSkieArtifactTarget(
override val fullArtifactName: String
get() = artifact.artifactName + artifactNameSuffix

override val requiredConfigurationFlags: Set<SkieConfigurationFlag> = setOfNotNull(
SkieConfigurationFlag.Build_SwiftLibraryEvolution.takeIf { outputKind == SkieTarget.OutputKind.XCFramework },
)

private val artifactNameSuffix = when (artifact) {
is KotlinNativeFatFramework -> "ForFat"
is KotlinNativeXCFramework -> "ForXCF"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package co.touchlab.skie.plugin

import co.touchlab.skie.configuration.SkieConfigurationFlag
import co.touchlab.skie.plugin.shim.ActualKonanTargetShim
import co.touchlab.skie.plugin.shim.ActualKotlinSourceSetShim
import co.touchlab.skie.plugin.shim.KonanTargetShim
Expand All @@ -20,7 +21,7 @@ class ActualSkieBinaryTarget(
override val project: Project,
target: KotlinNativeTarget,
private val binary: NativeBinary,
override val outputKind: SkieTarget.OutputKind,
private val isForXCFramework: Boolean,
) : SkieTarget.Binary {

override val konanTarget: KonanTargetShim = ActualKonanTargetShim(target.konanTarget)
Expand All @@ -29,6 +30,8 @@ class ActualSkieBinaryTarget(

override val name: String = "binary: ${binary.name}, target: ${target.targetName}, buildType: $buildType"

override val outputKind: SkieTarget.OutputKind = SkieTarget.OutputKind.Framework

override val task: TaskProvider<out KotlinNativeLink> = binary.linkTaskProvider

override val compileDependencyConfigurationName: String
Expand All @@ -46,6 +49,10 @@ class ActualSkieBinaryTarget(
it.binary.freeCompilerArgs
}

override val requiredConfigurationFlags: Set<SkieConfigurationFlag> = setOfNotNull(
SkieConfigurationFlag.Build_SwiftLibraryEvolution.takeIf { isForXCFramework },
)

override fun addPluginArgument(pluginId: String, option: KotlinCompilerPluginOption) {
task.configure {
compilerPluginOptions.addPluginArgument(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ import org.jetbrains.kotlin.gradle.plugin.getKotlinPluginVersion
import org.jetbrains.kotlin.gradle.plugin.mpp.Framework
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinUsages
import org.jetbrains.kotlin.gradle.plugin.mpp.apple.XCFrameworkTask
import org.jetbrains.kotlin.gradle.targets.native.tasks.artifact.kotlinArtifactsExtension
import org.jetbrains.kotlin.gradle.tasks.FatFrameworkTask
import org.jetbrains.kotlin.gradle.tasks.KotlinNativeLink
import org.jetbrains.kotlin.gradle.utils.NativeCompilerDownloader
import org.jetbrains.kotlin.konan.properties.resolvablePropertyString
import org.jetbrains.kotlin.konan.target.Distribution
Expand Down Expand Up @@ -53,24 +55,39 @@ class ActualKgpShim(
}

private fun initializeBinaryTargets() {
val frameworksUsedInXCFrameworks = getFrameworksUsedInXCFrameworks()

project.kotlinMultiplatformExtension?.appleTargets?.configureEach {
val target = this

binaries.withType<Framework>().configureEach {
val binary = this

val binaryTarget = ActualSkieBinaryTarget(
project = project,
target = target,
binary = binary,
outputKind = SkieTarget.OutputKind.Framework,
)

targets.add(binaryTarget)
registerBinaryTarget(target, this, frameworksUsedInXCFrameworks)
}
}
}

// Must be done eagerly because we need to go through all potential tasks even those that will not be executed.
// This is to ensure the Link task configuration will be the same when executed separately or as part of the XCFramework task.
private fun getFrameworksUsedInXCFrameworks(): Set<Framework> =
project.tasks.withType<XCFrameworkTask>()
.toList()
.flatMap { xcFrameworkTask ->
xcFrameworkTask.taskDependencies.getDependencies(xcFrameworkTask).filterIsInstance<KotlinNativeLink>().map { it.binary }
}
.filterIsInstance<Framework>()
.toSet()

private fun registerBinaryTarget(target: KotlinNativeTarget, binary: Framework, frameworksUsedInXCFrameworks: Set<Framework>) {
val binaryTarget = ActualSkieBinaryTarget(
project = project,
target = target,
binary = binary,
isForXCFramework = binary in frameworksUsedInXCFrameworks,
)

targets.add(binaryTarget)
}

private fun initializeArtifactTargets() {
project.kotlinArtifactsExtension.artifacts.withType<KotlinNativeArtifact>().configureEach {
val artifactTargets = ActualSkieArtifactTarget.createFromArtifact(this, project)
Expand Down

0 comments on commit 1990f8d

Please sign in to comment.