Skip to main content
coreydaley.dev

Vendored C in a universal Mac app: the x86_64 slice breaks last

The first time I ran xcodebuild archive on Purrchiver, the link step failed with this:

Undefined symbols for architecture x86_64:
  "_HUF_decompress4X1_usingDTable_internal_fast_asm_loop", referenced from: ...
  "_HUF_decompress4X2_usingDTable_internal_fast_asm_loop", referenced from: ...

Every local build before that had worked. Debug builds in Xcode only compile for the Mac you’re sitting at, and mine is Apple silicon. An archive for the Mac App Store builds a universal binary, so it was the first build to compile anything for Intel. Purrchiver’s archive engine is a Swift package that vendors a lot of C and C++: libarchive, zstd, UnRAR, and more. Several of those had x86_64 problems that nothing had exercised yet.

These are the ones I hit.

Update, October 25: for a few days Purrchiver 1.1 was going to be Apple silicon only, because the universal build shipped an Intel slice that no test ever ran. Instead, the Intel slice is now tested: the whole test suite also runs as Intel code under Rosetta, and it passes. So 1.1 stays universal, and the fixes below still apply.

zstd: the assembly that wasn’t vendored

zstd’s Huffman decoder has a hand-written x86-64 assembly fast path in a separate huf_decompress_amd64.S file. The C code decides whether to call it at compile time, in portability_macros.h. The asm path is enabled when all of the following are true: you haven’t defined ZSTD_DISABLE_ASM, the toolchain supports assembly, the target is x86-64, and BMI2 support is compiled in (directly or through runtime dispatch).

The vendored copy had the C sources but not the .S file. On arm64 that didn’t matter, because the macro turns the asm path off. On x86_64 the C code referenced two functions that nothing defined.

The fix is one define on the target:

.target(
    name: "Clibarchive",
    cSettings: [
        .define("ZSTD_DISABLE_ASM", to: "1"),
        // ...
    ]
)

zstd then uses its portable C decoder on both architectures. The asm path exists because it’s faster on Intel, so this gives up some decompression speed there. I haven’t measured how much. I took the trade because the C path is correct everywhere and Intel Macs are a shrinking share of Macs. Vendoring the .S file is the other option if Intel speed matters to you.

UnRAR: declare a C++ standard

The same day, a CI build of the package failed in the vendored UnRAR headers with a cascade of unknown type name 'constexpr' and unknown type name 'size_t'. Declaring the standard explicitly in the package manifest fixed it:

let package = Package(
    name: "ArchiveKit",
    // ...
    cxxLanguageStandard: .cxx17
)

I never fully pinned down the cause. My commit at the time blamed SwiftPM’s default standard for being too old, but constexpr dates to C++11, so that explanation is suspect. What I can say is that the default build failed, the explicit standard fixed it, and declaring one means local and CI builds can’t quietly disagree about it. If your package vendors C++, set cxxLanguageStandard rather than relying on whatever the toolchain picks.

xz: configure remembers the machine it ran on

Three weeks later I replaced the system liblzma with a vendored, statically compiled xz, after App Review rejected the system one. liblzma’s config.h is generated by running xz’s ./configure, and configure records what it finds on the machine that runs it. I ran it on Apple silicon, where it found the ARM64 CRC32 instruction and wrote:

#define HAVE_ARM64_CRC32 1

That define has no architecture guard, and liblzma uses it alone to decide whether to include <arm_acle.h> and compile the ARM CRC path. The x86_64 slice tried to compile ARM intrinsics and failed. configure assumes one architecture per build, and a universal build breaks that assumption.

The fix is to guard the define by hand:

#if defined(__aarch64__)
#define HAVE_ARM64_CRC32 1
#endif

Intel then uses liblzma’s portable table-based CRC. Any config.h generated by autoconf on one architecture and shared by both slices is worth checking for defines like this, especially anything with a CPU feature in its name. A test now checks that the guard is still present, because regenerating config.h for an xz update would quietly remove it.

A Rust library: Homebrew’s toolchain can’t cross-compile

Not everything in Purrchiver is C. age encryption comes from the Rust age crate, built as a static library behind a small C interface and shipped to SwiftPM as a prebuilt binaryTarget. That library needs both slices too.

The catch is that Homebrew’s Rust only ships the standard library for the host architecture, so it can’t build x86_64-apple-darwin on an arm64 Mac. The rustup toolchain can, once both targets are installed. I pointed cargo at that toolchain’s rustc explicitly so a Homebrew one earlier in PATH couldn’t win:

TC="$(dirname "$(rustup which --toolchain stable cargo)")"
export RUSTC="$TC/rustc"

"$TC/cargo" build --release --target aarch64-apple-darwin
"$TC/cargo" build --release --target x86_64-apple-darwin

lipo -create \
  target/aarch64-apple-darwin/release/libfoo.a \
  target/x86_64-apple-darwin/release/libfoo.a \
  -output libfoo.a
xcodebuild -create-xcframework -library libfoo.a -output Foo.xcframework

The build also runs nm over the result to confirm there are no networking symbols like socket or getaddrinfo in it, and records a SHA-256 of the artifact so a rebuild that changes anything shows up in review.

Check the slices, not just the build

A universal build that succeeds can still be wrong, so I check the output directly:

lipo -archs MyApp.app/Contents/MacOS/MyApp      # expect: x86_64 arm64
lipo -info  Vendor/Foo.xcframework/*/libfoo.a

Run the same check on every extension in the bundle. Each is a separate binary.

What to take away

  • Your first archive build is your first Intel build. Run xcodebuild archive (or a Release build with both architectures) early, not the night before you submit.
  • Vendored code makes per-architecture decisions you didn’t see. Assembly fast paths, CPU-feature defines in generated config.h files, and toolchains that only target the host all work on one architecture and fail on the other.
  • Pin the fixes with tests. Regenerating a config or updating a vendored library undoes hand edits without telling you.

Related posts