Zig Compiler Bootstrapped on macOS via Linux
A developer has detailed the process of bootstrapping the Zig compiler from source on macOS. The effort involved using remote Linux runners and building on LLVM, Clang, and libc++. The project serves as a deep dive into the complexities of cross-platform compiler workflows, particularly relevant for Apple Silicon optimization efforts.
Bootstrapping is the process of writing a compiler in the language it is meant to compile. An initial compiler is created in a different, existing language to compile the first version of the new compiler's source code, solving a classic chicken-or-egg problem in language development. This milestone is often seen as a sign of a language's growing maturity. The Zig compiler is now self-hosted, meaning it's written in Zig itself. The project transitioned from an earlier compiler written in C++ to dogfood the language, reduce memory usage, and improve compilation speed. Since version 0.11.0, the legacy C++ bootstrap has been removed in favor of a new process that uses a WebAssembly build of the compiler. Cross-compiling from Linux to macOS is notoriously difficult, primarily due to dependencies on proprietary Apple SDKs and frameworks that are not readily available on non-Apple systems. This process requires intricate handling of linkers and platform-specific libraries, a complexity that Zig's built-in tooling is designed to manage automatically. This work is particularly relevant for Apple Silicon optimization. Zig's emphasis on low-level control and performance makes it a strong candidate for systems-level development on arm64 chips. In fact, deep learning frameworks written in Zig have already demonstrated up to 2.5x faster performance than PyTorch on Apple Silicon. Zig is designed with cross-compilation as a first-class feature, not an afterthought. The toolchain includes `zig cc` and `zig c++` commands that can function as drop-in cross-compilers for C/C++ code, bundling necessary libraries like libc for numerous target platforms out of the box. While the compiler currently relies on LLVM for backend optimizations, a key long-term goal is to make it an optional component. This is because LLVM can be responsible for up to 70% of the compilation time. By developing its own backends, Zig aims to significantly speed up debug builds and enable advanced features like incremental compilation via in-place binary patching.