Ad

Hot To Compile A Swift Script To A Universal Binary

- 1 answer

We can easily compile a Swift script with:

$ swiftc /path/to/script.swift -o /path/to/binary

However, that only compiles for the current architecture.

$ lipo -archs /path/to/binary
arm64

I found some commands to build for multiple architectures, but they seem to require setting up a new project. I don’t want or need to do that yet, for now I just want to compile a single script file easily, hence swiftc. Is there a way to do this?

As a bonus, does Rosetta 2 need to be installed to generate universal binaries, or is it possible to make them without it?

Ad

Answer

You would need to build the binary two times, for example, for a project that's targeting macOS, you would compile once with -target x86_64-apple-macos10.15 and the other one with -target arm64-apple-macos10.15.

After that, you would use lipo -create to stitch those together into a single file like this lipo -create <path/to/arm64-slice> <path/to/x86_64-slice> -output <path/to/universal/binary>.

This is how I did it:

➜  UniversalBinaryTest swiftc source.swift -target x86_64-apple-macos10.15 -o binary_x86-64
➜  UniversalBinaryTest lipo -archs binary_x86-64
x86_64
➜  UniversalBinaryTest swiftc source.swift -target arm64-apple-macos10.15 -o binary_arm64
➜  UniversalBinaryTest lipo -archs binary_arm64
arm64
➜  UniversalBinaryTest lipo -create binary_x86-64 binary_arm64 -output binary_universal
➜  UniversalBinaryTest lipo -archs binary_universal
x86_64 arm64

After all of that, you would probably want to re-sign the new binary.

Edit: Actually, it looks like lipo handles signing for you if both slices are signed:

➜  UniversalBinaryTest codesign -s - binary_x86-64
➜  UniversalBinaryTest codesign -vvv binary_x86-64
binary_x86-64: valid on disk
binary_x86-64: satisfies its Designated Requirement
➜  UniversalBinaryTest codesign -vvvvv binary_x86-64
binary_x86-64: valid on disk
binary_x86-64: satisfies its Designated Requirement
➜  UniversalBinaryTest codesign -s - binary_arm64
➜  UniversalBinaryTest lipo -create binary_x86-64 binary_arm64 -output binary_universal
➜  UniversalBinaryTest codesign -vvv binary_universal
binary_universal: valid on disk
binary_universal: satisfies its Designated Requirement
Ad
source: stackoverflow.com
Ad