Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add package dependency for a binary target with Swift Package Manager

I'm developing a closed source framework, that will be distributed as an XCFramework using SPM. This is possible thanks to the new binaryTarget from swift tools 5.3. This works fine until the framework has dependencies.

416 - Binary frameworks states ... binary frameworks cannot depend on Packages., but this was before 5.3 and binary targets were not possible at all. On the Swift forums there is a suggested workaround that basically revolves around adding a dummy target that will list the dependencies (binaryTarget initialiser doesn't have a dependencies parameter).

The workaround works until the dependency has its own dependencies. For example Lottie which doesn't have any dependencies works fine, but Auth0 which has quite a few, fails with errors Missing required modules: 'Auth0ObjectiveC', 'SimpleKeychain'. Even adding Auth0 directly to the client project using SPM doesn't fix these errors.

Here is my Package.swift which works partially.

// swift-tools-version:5.3
import PackageDescription
let package = Package(
    name: "MyFramework",
    platforms: [
        .iOS(.v13)
    ],
    products: [
        .library(name: "MyFramework", targets: ["MyFramework", "MyFramework-Dependencies"])
    ],
    dependencies: [
        .package(name: "Auth0", url: "https://github.com/auth0/Auth0.swift.git", from: "1.30.1")
    ],
    targets: [
        .binaryTarget(name: "MyFramework", path: "MyFramework.xcframework"),
        .target(name: "MyFramework-Dependencies", dependencies: ["Auth0"], path: "MyFramework-Dependencies")
    ])

It it possible to actually have a binary framework depend on a package? If not, what would be the proper way distribute a dependency for a binary framework?

like image 553
Nikola Lajic Avatar asked Dec 09 '20 16:12

Nikola Lajic


People also ask

How do I add a dependency to a Swift package?

To add a package dependency to your Xcode project, select File > Swift Packages > Add Package Dependency and enter its repository URL.

How do I distribute XCFramework?

To distribute code in binary form as a Swift package, create an XCFramework bundle, or artifact, that contains the binaries. Then, make the bundle available locally or on a server: When you host the binaries on a server, create a ZIP archive with the XCFramework in its root directory and make it available publicly.


1 Answers

We've solved this problem by creating a wrapper target that depends on both the binary framework and other dependencies. See an example here.

like image 64
Paul Beusterien Avatar answered Sep 28 '22 03:09

Paul Beusterien