Repository navigation
Open
Labels
enhancementSolving this issue will likely involve adding new logic or components to the codebase.
Milestone
Description
You know what's really pleasant to work on? The build system. The zig build system is shipped in source form, and when you run zig build, zig compiles the build runner from source, and then runs it. In order to do that, the compiler needs only 2 capabilities: compiling source into a native executable, and executing child processes.
Here are some components that could be shipped in source form rather than compiled into the zig binary:
- zig fmt
- zig reduce
- translate-c
- aro
- package fetching
- Autodoc
- building glibc stubs
- building libc++, libcxxabi, libtsan, libunwind
- building musl, mingw, and wasi-libc
- libc installation detection
- msvc installation detection
- objcopy
- resinator
-
all the machine code backends other than the one used to produce native executablesseparate proposal
All of these components have simple interfaces that could be communicated over IPC or the file system. I admit however that last one is a bit spicy.
Implementing this issue would accomplish the following things:
- Significantly decrease compiler build times
- Make it easier to contribute to zig because no compiler rebuild would be needed in order to test edits to any of these components.
- Smaller installation size. Source code is surprisingly compact and compresses extraordinarily well. For example, the xz-compressed zig compiler executable built with
-Denable-llvm=false -Doptimize=ReleaseSmallon x86_64 is 2.9 MiB (9.9 MiB uncompressed), while the compiler source code catted together into an xz stream is 1.6 MiB. - The lazily built executables can take full advantage of native CPU features. We can have AOT cake and eat JIT too!
Reacted by Jarrod M., Visen, February, ominitay, Victorien Elvinger, Jae B, cn, steven-vd, GalaxySnail, Niles Salter and 32 more
Reacted by square, Visen, Chris, lin72h, Mitchell Hashimoto, Paperdev, Tesseract22 and 16hournaps
Reacted by Meghan Denny, Krzysztof Wolicki, Josh Wolfe, Nathan Craddock, Visen, February, Niles Salter, Mathew Gordon, SeriousBusiness101, McSinyx and 11 more
Reacted by Alex Kladov, Rohlem, Matheus C. França, SeriousBusiness101, svercl, dmngd, lin72h, Mitchell Hashimoto, Paperdev, AdamGoertz and 5 more
Reacted by Yuri, Ryan Liptak, Meredith Oleander, Carl Åstholm, Sean Dewar, Ali Cheraghi, Robin Voetter, Krzysztof Wolicki, Robbie Lyman, Ian Johnson and 15 more
Metadata
Metadata
Assignees
Labels
enhancementSolving this issue will likely involve adding new logic or components to the codebase.
Activity
VisenDev commentedon Feb 24, 2024
Possibly related proposal
#18834.
This proposal would allow other zig projects to ship zig source code rather than compiled binaries as well.
der-teufel-programming commentedon Feb 24, 2024
Shipping current version of Autodoc in source form would require also shipping ZIR and parts of InternPool
mikdusan commentedon Feb 25, 2024
I like this. It's always irked me that we have things like
src/musl.zigthat has sort of poor-mans build logic when the build system already exists.nektro commentedon Feb 25, 2024
because that logic uses the cache but needs to be callable by
build-exeetc. making them use the actual build system is a good idea imo but will require a medium amount of work.nektro commentedon Feb 25, 2024
does this proposal being implemented mean for example that
zig reducewould start shipping aszig-reduceand/or thezigbinary should check PATH for binaries to ensure the space version still works?andrewrk commentedon Feb 26, 2024
No. It would be the same CLI.
jayrod246 commentedon Feb 26, 2024
So same CLI, and it would re-build the relevant source files if necessary - like how
zig buildworks. Nice!compiler: JIT zig fmt
compiler: JIT zig reduce
move `zig libc` command to be lazily built
move `zig libc` command to be lazily built
move `zig libc` command to be lazily built
10 remaining items
compiler: JIT zig fmt
compiler: JIT zig reduce
move `zig libc` command to be lazily built
make aro-based translate-c lazily built from source
move `zig objcopy` command to be lazily built
Lazily compile the `zig rc` subcommand and use it during `zig build-exe`
tecanec commentedon Apr 3, 2024
Well, that explains it...
This is just speculation, but I feel like JIT would synergize really well with comptime. Especially for things like
std.simd.suggestVectorLength, whose goal is specifically to automatically optimize for the given hardware.I was thinking something similar. Custom backends sound like a big deal for non-mainstream platforms, including proprietary, niche, or even in-development architectures, including experiments and pet projects.
Overall, I am a bit concerned about compile times, though. I know caching is a thing, but it's not perfect.
compiler: un-jit `zig fmt`
moosichu commentedon Oct 12, 2025
I was having a think just now about
zig fmtbeing un-jitted due to the worse user experience it creates, and was wondering if it could be still be shipped as source without make the user experience of runningzig fmtfor the first time itself bad.One idea that could work would be to eagerly compile all the code that ships with zig instead of lazily compiling it.
I think this could work as a "one time" setup step for anyone downloading zig - because at that point you are already going through a process of waiting it to download and decompress, so having the very first invokation of zig compile at least all the code needed to run the frequently used subcommands would be quite reasonable?
It also keeps the benefit that if you are going to be modifying that code (triggering a recompile) during compiler development, it's probably because you are testing that feature anyway. So I think it's only a boon for those working on the compiler itself?
The only downside is that the very first time you run zig it will be a little bit slower as it will always be building zig fmt even if you end up never using it, but:
zig fmtat some point, so compiling it eagerly just improves the overall experience for themTBH thinking about it more, I don't really see much of a benefit to JITing any of the subcommands (as-in, waiting to compile them until they are invoked), as I think across the board it's a better user experience to know that you can "warm up" the whole zig compiler by invoking it just once when you are already accepting a wait for parts of it to compile anyway. However, for
fmtin particular the eager compilation strategy could work well even if it's decided it doesn't make sense for less-frequently used subcommands.