Build Swift packages as universal binaries
So, after Apple announced during WWDC 2020 that they will be transitioning Macs from Intel processors to Apple silicon, now is the time for everyone to get their software ready.
The transition is likely to be easier this time around for most people, especially those who already have arm64 support on iOS, but there's still work to be done to ensure tools and pre-compiled distributions support Macs using Apple Silicon publicly There are two ready-made architectures. If you haven't seen it, a lot of it is covered in the Porting Mac Apps to Apple Silicon WWDC session video.
If you use the Xcode compilation command line tool, things are pretty simple as long as you set your ARCHS
build settings to $(ARCHS_STANDARD)
default). In Xcode 12, this value is described as standard architecture (64-bit Intel and ARM), but if you use the Swift package manager to build and distribute binaries or libraries, there is no such option.
Instead, starting with the Swift Package Manager in Swift 5.3 (Xcode 12), swift-build
executables now introduce --arch
options ( apple/swift-package-manager#2787 ).
Build a universal binary
First, make sure you are using the correct version of Xcode/Swift:
$ xcrun swift build --version
Swift Package Manager - Swift 5.3.0
Note: If this is not Swift 5.3 or higher, please use xcode-select -s
Switch to Xcode 12 Beta.
Now, when compiling the package, specify both architectures to compile the universal binary:
$ xcrun swift build -c release --arch arm64 --arch x86_64
$ xcrun swift build -c debug --arch arm64 --arch x86_64
To verify that the binary you built contains both architectures, you can use lipo -info
the command to inspect the binary and confirm:
$ lipo -info .build/apple/Products/Release/swiftlint
Architectures in the fat file: .build/apple/Products/Release/swiftlint are: x86_64 arm64
$ lipo -info .build/apple/Products/Debug/swiftlint
That's it, building your Swift package into a universal binary is as easy as that!