n179911
n179911

Reputation: 20331

How can I add a local library as a dependency in Swift Package Manager

How can I add a local library (a *.a file) as a dependency in Swift Package Manager?

I try adding in my Package.swift:

dependencies: [
    // Dependencies declare other packages that this package depends on.,
    .package(url: "file://../otherdirectory/x86_64-apple-macosx/debug/libTest.a")
],

But I get this error when I run 'swift build'

Package.swift:17:10: error: type of expression is ambiguous without more context

Upvotes: 4

Views: 4967

Answers (1)

iUrii
iUrii

Reputation: 13818

First: package dependency can link to other packages only!

It's possible from Swift 5.3 with binaryTarget but you should build your static library with several needed architectures(arm64, x86_64) and then create XCFramework from them with next command:

xcodebuild -create-xcframework \
    -library <path> [-headers <path>] \
    [-library <path> [-headers <path>]...] \
    -output <path>

e.g.:

xcodebuild -create-xcframework \
    -library build/simulators/libMyStaticLib.a \
    -library build/devices/libMyStaticLib.a \
    -output build/MyStaticLib.xcframework

Then you can create new binary target dependency in your package:

let package = Package(
   name: "MyPackage",
   ...
   targets: [
      .target(
         name: "MyPackage",
         dependencies: ["MyStaticLib"]
      ),
      .binaryTarget(name: "MyStaticLib", path: "path/MyStaticLib.xcframework"),
      ...
   ]

Note: The path to xcframework starts from the root of the project (same as Package.swift).

Upvotes: 7

Related Questions