touchlab / KMMBridge

KMMBridge is a tool that helps publish Kotlin Multiplatform (KMP) Xcode binaries for use from Swift Package Manager (SPM) and CocoaPods.
https://touchlab.co/kmmbridge/
Apache License 2.0
358 stars 21 forks source link

spm with latest version(1.0.1) generated Package.swift looks not correct #258

Open hanrw opened 11 hours ago

hanrw commented 11 hours ago

by given:

 spm(swiftToolVersion = "5.9") {
        iOS { v("14") }
        macOS { v("13") }
    }

expect:

// swift-tools-version:5.9
import PackageDescription

let packageName = "shared"

let package = Package(
    name: packageName,
    platforms: [
         .iOS(.v14),
         .macOS(.v13)
    ],
    products: [
        .library(
            name: packageName,
            targets: [packageName]
        ),
    ],
    targets: [
        .binaryTarget(
            name: packageName,
            path: "./shared/build/XCFrameworks/debug/\(packageName).xcframework"
        )
        ,
    ]
)

but generated

// swift-tools-version:5.9
import PackageDescription

let packageName = "shared"

let package = Package(
    name: packageName,
    platforms: [
         .macOS(.v13)
    ],
    products: [
        .library(
            name: packageName,
            targets: [packageName]
        ),
    ],
    targets: [
        .binaryTarget(
            name: packageName,
            path: "./shared/build/XCFrameworks/debug/\(packageName).xcframework"
        )
        ,
    ]
)
kpgalligan commented 4 hours ago

The logic that builds the Package.swift file checks the apple platforms you've defined for spm against the Kotlin target architectures you've defined. If you have an spm platform definition, but your build isn't actually building for that platform, it isn't written to your Package.swift file.

For example, if your Kotlin gradle config looks like this:

kotlin {
    listOf(
        iosX64(),
        iosArm64(),
        iosSimulatorArm64(),
        macosArm64()
    ).forEach {
        it.binaries.framework {
            isStatic = true
        }
    }
}

kmmbridge {
    gitHubReleaseArtifacts()
    spm(swiftToolVersion = "5.8") {
        iOS { v("14") }
        macOS { v("13") }
    }
}

The relevant part of the Package.swift file will look like this:

let package = Package(
    name: packageName,
    platforms: [
        .iOS(.v14),
        .macOS(.v13)
    ],
    products: [
// Etc

If you remove macosArm64() it'll result in this:

let package = Package(
    name: packageName,
    platforms: [
        .iOS(.v14)
    ],
    products: [
// Etc

It might be confusing, but not an unreasonable solution to the question of what to do in that case. The choices would be:

I don't have a strong feeling either way, TBH. What I don't know is if there are potential downsides for declaring a platform that your framework doesn't actually implement. I don't know if it'll cause build issues, but it is kind of odd to declare it if it isn't supported.