# Cross compiling for Apple Silicon with Swift Package Manager

DevFeed: [Cross compiling for Apple Silicon with Swift Package Manager](<https://devfeed.tech/articles/cross-compiling-for-apple-silicon-with-swift-package-manager-25400.md>)

Original publisher: [Read original article](<https://smileykeith.com/2020/12/24/swiftpm-cross-compile/>)

Author: Keith Smiley

Published: 2020-12-24T22:32:00Z

Content type: tutorial

Language: en

Sources: [Keith Smiley](<https://devfeed.tech/sources/keith-smiley.md>)

Topics: [swift-package-manager](<https://devfeed.tech/topics/swift-package-manager.md>), [Cross-Compilation](<https://devfeed.tech/topics/cross-compilation.md>), [Package Management](<https://devfeed.tech/topics/package-management.md>), [Swift](<https://devfeed.tech/topics/swift.md>)

Tags: [apple](<https://devfeed.tech/tags/apple.md>), [apple-silicon](<https://devfeed.tech/tags/apple-silicon.md>), [architecture](<https://devfeed.tech/tags/architecture.md>), [architectures](<https://devfeed.tech/tags/architectures.md>), [arm64](<https://devfeed.tech/tags/arm64.md>), [binaries](<https://devfeed.tech/tags/binaries.md>), [build](<https://devfeed.tech/tags/build.md>), [command-line](<https://devfeed.tech/tags/command-line.md>), [cross](<https://devfeed.tech/tags/cross.md>), [distribution](<https://devfeed.tech/tags/distribution.md>), [flag](<https://devfeed.tech/tags/flag.md>), [hidden](<https://devfeed.tech/tags/hidden.md>), [linux](<https://devfeed.tech/tags/linux.md>), [macos](<https://devfeed.tech/tags/macos.md>), [performance](<https://devfeed.tech/tags/performance.md>), [swift](<https://devfeed.tech/tags/swift.md>), [swift-package-manager](<https://devfeed.tech/tags/swift-package-manager.md>), [x86-64](<https://devfeed.tech/tags/x86-64.md>), [xcode](<https://devfeed.tech/tags/xcode.md>)

## AI overview

To distribute a Swift Package Manager command-line tool for both Intel Macs and Apple Silicon, build a universal binary containing `x86_64` and `arm64` slices. The excerpt describes two methods: pass the hidden `--arch` flag for both architectures, which uses Xcode's XCBuild and places the artifact under `.build/apple/Products/Release`; or build separately with each Apple macOS triple and combine the outputs with `lipo`. The `lipo` approach also works on Linux, unlike `--arch`. The excerpt shows `file` confirming both architectures in the resulting binary.

## Source excerpt

If you distribute binaries for command line tools built with Swift Package Manager, you might have previously built your distribution binary with: % swift build --configuration release If you inspect the binary, you can see it was built for the current machine's architecture by default: % file .build/release/package .build/release/package: Mach-O 64-bit executable x86_64 Previously, this was sufficient since macOS only supported one architecture. Now, in order to fully utilize the native performance of Apple Silicon chips, we need to produce a fat binary that contains a slice for both x86_64 and arm64. Swift Package Manager has a few different ways to achieve this. The easiest way, as far as I can tell, is to pass the hidden --arch flag once for each architecture: % swift build --configuration release --arch arm64 --arch x86_64 This goes through a different code path in Swift Package Manager, and utilizes Xcode's underlying XCBuild tool. This results in the built binary being in a different path than usual. Inspecting the new artifact, we can see we have a binary containing both requested architectures: % file .build/apple/Products/Release/package .build/apple/Products/Release/package: Mach-O universal binary with 2 architectures: [x86_64:Mach-O 64-bit executable x86_64] [arm64:Mach-O 64-bit executable arm64] .build/apple/Products/Release/package (for architecture x86_64): Mach-O 64-bit executable x86_64 .build/apple/Products/Release/package (for architecture arm64): Mach-O 64-bit executable arm64 Another option is to build once for each architecture, and then combine the binaries using lipo. Unlike the --arch option, this approach also works on Linux. Here's an example: % swift build --configuration release --triple arm64-apple-macosx % swift build --configuration release --triple x86_64-apple-macosx % lipo -create -output package .build/arm64-apple-macosx/release/package .build/x86_64-apple-macosx/release/package Inspecting our final binary we can see it correctly