# 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: article

Language: en

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

Topics: [Swift](<https://devfeed.tech/topics/swift.md>), [Package manager](<https://devfeed.tech/topics/package-manager.md>), [macOS](<https://devfeed.tech/topics/macos.md>), [x86](<https://devfeed.tech/topics/x86.md>), [Xcode](<https://devfeed.tech/topics/xcode.md>), [Command-line interface](<https://devfeed.tech/topics/cli.md>), [Linux](<https://devfeed.tech/topics/linux.md>)

Tags: [apple](<https://devfeed.tech/tags/apple.md>), [architecture](<https://devfeed.tech/tags/architecture.md>), [architectures](<https://devfeed.tech/tags/architectures.md>), [binaries](<https://devfeed.tech/tags/binaries.md>), [build](<https://devfeed.tech/tags/build.md>), [command-line](<https://devfeed.tech/tags/command-line.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](<https://devfeed.tech/tags/swift-package.md>), [swift-package-manager](<https://devfeed.tech/tags/swift-package-manager.md>), [x86](<https://devfeed.tech/tags/x86.md>), [x86-64](<https://devfeed.tech/tags/x86-64.md>), [xcode](<https://devfeed.tech/tags/xcode.md>)

## AI overview

This article explains how to cross-compile Swift Package Manager command-line binaries for Apple Silicon. It shows how to create a universal macOS binary containing both arm64 and x86_64 slices using the hidden --arch flag, and how to build separately for each architecture and combine the results with lipo, including an approach that also works on Linux.

## 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