I'm having a problem defining the Package.swift file for my binary closed-source dynamic framework which depends on a number of non-binary open-source dynamic frameworks.
My Package.swift file looks like this:
// swift-tools-version: 5.8
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
import Foundation
let package = Package(
name: "MyLibrary",
platforms: [
.iOS(.v12)
],
products: [
.library(
name: "MyLibrary",
targets: ["MyLibraryWrapper"]
),
],
dependencies: [
.package(url: "<dependency-url>", branch: "master"),
],
targets: [
.target(
name: "MyLibraryWrapper",
dependencies: [
.target(name: "MyLibrary"),
.product(name: "<dependency-product-name>", package: "<dependency-package-name>")
],
path: "MyLibraryWrapper"
),
.binaryTarget(
name: "MyLibrary",
url: "<url-to-xcframework-zip>",
checksum: "<checksum>"
)
]
)
The package builds successfully. However, when I add it to any app, I get the following runtime error:
Library not loaded: @rpath/<dependency-product-name>.framework
The only workaround I know is to add type: .dynamic to the library(name:type:targets:) declaration in the Package.swift file of each of my framework's dependencies. This requires me to fork each of my framework's dependencies and maintain the forks. That's not something I'm keen on.
Is there is a better, simpler solution to this problem?
Notes
- I need to declare the
MyLibraryWrappertarget in myPackage.swiftfile because the binaryTarget(name:url:checksum:) method does not offer adependenciesparameter. - The
MyLibraryWrapperdirectory which theMyLibraryWrappertarget declares as itspathcontains a single empty source file and nothing else. - I tried declaring the
typeofMyLibraryas .dynamic via the library(name:type:targets:) method. I was hoping that Swift Package Manager would do the right thing of dynamically linkingMyLibraryand its dependencies. Sadly, declaring thetypeofMyLibraryas .dynamic results in a"Multiple commands produce MyLibrary.framework"build error. - The non-binary dynamic frameworks which
MyLibrarydepends on do not specify an explicittypevalue in theirPackage.swiftfiles. This is based on Apple's recommendation in the library(name:type:targets:) method documentation: "It’s recommended that you don’t explicitly declare the type of library, so Swift Package Manager can choose between static or dynamic linking based on the preference of the package’s consumer."