Generating fat binary on macOS

Is there a way for the zig toolchain to directly generate a fat macos binary, or does it require multiple steps and lipo like so:

git clone https://github.com/allyourcodebase/box2d
cd box2d
zig build -Dtarget=aarch64-macos --release=fast -p zig-out-aarch64 && \
zig build -Dtarget=x86_64-macos --release=fast -p zig-out-x64 && \
lipo -create -output ../libbox2d-fat.a zig-out-aarch64/lib/libbox2d.a zig-out-x64/lib/libbox2d.a

The above commands work, I’m just wondering if there is a way for me to not use lipo

I’m not sure if there is a way to avoid running lipo, but Ghostty has a custom build system step for running it through the zig build system.

ghostty/src/build/LipoStep.zig at main · ghostty-org/ghostty · GitHub. Looks like it’s used here ghostty/src/build/GhosttyLib.zig at main · ghostty-org/ghostty · GitHub

That’s unfortunate if true — it prevents cross compiling from platforms that don’t have lipo. clang can generate fat binaries directly, and I was hoping that there were flags that would make zig do that too

clang -arch x86_64 -arch arm64 main.c 

There is a cross platform lipo: GitHub - konoui/lipo: This lipo is designed to be compatible with macOS lipo, written in golang.

I think cooking universal binary by hand is not that hard, we do that for TigerBeetle’s own build:

The code can’t be re-used as-is, as we are doing extra things there (stuffing old binaries in the same Mach-O executable under dead CPU arches to provide seamless upgrades), but the result is a multi version binary.

7 Likes