Packaging as XCFramework
Learn how XCCacheAgent packages dependencies as XCFrameworks
The steps to create an xcframework out of a collection of Swift sources are:
- Creating a framework slice (ex. for iOS iphone simulator -
arm64-apple-ios-simulator). The result of this step is a framework bundle, ex.SwiftyBeaver.framework. - Creating an xcframework out of framework slices using
xcodebuild -create-xcframework.
There are some tricky actions in step (1) so that the framework bundle meets requirements in step (2). For example, in case of Swift frameworks, xcodebuild -create-xcframework requires a swiftinterface in the swiftmodule.
Creating a Framework Slice
By default, building a Swift package target (with swift build) does not produce a .framework bundle. We have to package it outselve from object files, headers, swiftmodules, etc.
A.framework
|-- A (binary)
|-- Info.plist
|
|-- Headers /
|-- Modules /
|-- module.modulemap
|-- A.swiftmodule /
|-- arm64-apple-ios-simulator.swiftinterface
|-- arm64-apple-ios-simulator.swiftdoc
...Steps to create a framework:
- Run
swift build --target A ...to build the target. Build artifacts are stored under.build/debug. - Create the framework binary using
libtoolfrom.ofiles in.build/debug/A.build:libtool -static -o A.framework/A .build/debug/A.build/**/*.o - Copying swiftmodules & swiftinterfaces in
.build/debug/A.buildand.build/debug/ModulestoA.framework/Modules. Also, creating the modulemapmodule.modulemapunderA.framework/Modulesso that this framework is visible to ObjC code. - Copying headers (if any) to
A.framework/Headers. - Copying the resource bundle (if any) (ex. in
.build/debug/A_A.bundle) to the framework bundle.
Creating an xcframework from Framework Slices
xcodebuild -create-xcframework \
-framework arm64-apple-ios-simulator/SwiftyBeaver.framework \
-framework arm64-apple-ios/SwiftyBeaver.framework \
-output SwiftyBeaver.xcframework