Hi everybody. So recently I have been looking into the Zig source code. So far I have been able to successfully build from source on Windows and WSL with Alpine Linux. I’m relieved to see that I can do the first build in under an hour’s time on a low-end system. What I am trying to do from here is have a better understanding of what’s going on in the build process, before going into the source code. My question is about the relationship between CMakeLists.txt and build.zig. I’m assuming most of the build logic is in these two files. I’m not really familiar with CMake, but it will help to clarify what’s going on here in a general sense. Would it be correct to say that I can understand the build process by understanding CMakeLists.txt and then build.zig? Is it one after the other or is there something else going on here that I’m missing? Any useful information or advice on understanding the build process will be appreciated.
- Use system C compiler to compile zig-wasm2.c
- Use zig-wasm2.c to convert zig1.wasm.zst to zig1.c
- Use system C compiler to compile zig1.c.
- Note that zig1 only has the C backend enabled.
- Use zig1 to build the Zig compiler into zig2.c
- Use system C compiler to compile zig2.c
- This one has the correct final logic however its machine code has been optimized by the system C compiler rather than by itself. We proceed to step 6 in order to get a binary with self-hosted performance characteristics.
- zig2 build (standard build process for building Zig using an older Zig build)
If you take the output of this final step and build Zig again, it produces the same thing byte-for-byte. In other words,
zig4are identical, and therefore we are finished and name this final binary
zigwithout any suffix.
Thanks for your response. I actually already read that article. So I was specifically trying to get some clarification as to where all those steps it mentions are in the source code exactly. For example in CMakeLists.txt it says “we use cmake, not the zig build system, to build zig2.o”. And there is also the build.zig file in the root directory. So where does the CMake start and end, and where and when does zig build come into play? And does this cover everything?
CMake is responsible for creating up to zig2 (since those parts rely on the system C compiler), and it also has a custom step to build stage3 using zig2:
Note that stage3 could be built without CMake being involved, though, as it’s just invoking
zig2 build --prefix stage3 with some extra arguments based on options given to CMake. So,
build.zig is ultimately the build system for going from zig2 → stage3.
See also this section of the contributing guide. It recommends doing:
stage3/bin/zig build -p stage4 -Denable-llvm -Dno-lib
to build “stage4” (which in theory should be the same as stage3 if the same flags are passed)
Awesome. This is exactly what I was looking for. Thank you! I am hoping to be a contributor on this project, so this will help me out a lot to get familiar with everything.