When hacking on the compiler, when will `zig build` not work for building the compiler?

I can build the zig compiler by just downloading nightly and running zig build in the zig repo. This takes 5 minutes. When will this not work when modifying the compiler and what are the limitations of this?

1 Like

The first thing to note is that the compiler this gives you is missing some functionality: namely, it doesn’t link LLVM. This means you lose:

  • Optimized binaries on x86_64
  • Any binaries at all on other targets (except via -ofmt=c)
  • zig cc and zig c++
  • zig translate-c (it will still run, but it will use the highly incomplete Aro implementation which will almost certainly crash)

Assuming you’re fine with that, zig build in the compiler repo should pretty much always get you a working compiler build. One caveat: if you modify the standard library in lib/, and use your modified std in the compiler, you’ll need to also pass --zig-lib-dir lib, to tell your system-installed zig to find the standard library (and some other stuff, e.g. libc) in the repo’s lib directory.

Additionally, if your system installation of zig is too far behind (or ahead of) the version you’re trying to build, it’s possible to run into incompatibilities where neither your system std nor the repo’s std will work – this usually arises due to changes in std.builtin.

In these situations, the best thing to do is to bootstrap a new Zig compiler build from the ground up. I won’t explain the bootstrap process in detail here, but if you’re fine with an LLVM-free build, you can essentially just run cc bootstrap.c -o bootstrap && ./bootstrap and let it work (it’ll take a while!).

2 Likes