Skip to main content

TIL: compile with a locally patched Zig standard library via --zig-lib-dir

Zig TIL Programming
alt-title: find a way to narrow down the scope of your comparisons (and compilations).

While benchmarking the impact of Zig removing inlines from their standard cryptography library, my naive initial approach compared the changes from the perspective of the Zig project as a whole, not just the standard library itself. In reality, this meant every time I modified a Zig crypto implementation while experimenting, even just to call std.debug.print, I was rebuilding the entire project from source.

The Zig build system feels powerful and I’m planning out a new project to coat myself in all the grimy details1. In the meantime, I got started with Loris Cro’s excellent Zig Build System Basics video, and the existing documentation. I followed the Zig wiki’s instructions for building from source on both mac and linux.

But, of course, there’s a flag for that:

$ zig build --help

Usage: zig build [steps] [options]

...

Advanced Options:
  ...
  --zig-lib-dir [arg]          Override path to Zig lib directory
  ...

We can input the path of the lib directory containing an instantiation of the Zig standard library and zig build will compile without extra steps or linking. Now instead of feeling stuck at the bird’s eye view of the entire Zig source, it’s much easier to quickly compare and contrast the small nudges and curiosities.

Just call:

zig build --zig-lib-dir ../zig-master/lib

Those familiar with the ziglang codebase may note that the directory for the standard library isn’t lib/, it’s lib/std/. This means that --zig-lib-dir allows us specify a lot more than just the standard library for injection, we’re just scratching the surface here2.

Benchmarking changes is much smoother now. I simply point my script to the appropriate git worktree directory for each version and away we go!


  1. Hint: it involves porting the build system of an existing (academic) C++ project I’ve long admired, with a goal of extending the code base using Zig. More soon! ↩︎

  2. Have a cool use for this flag in your workflow? Send me an message! ↩︎

Eli Grubb
Author
Eli Grubb
I am a privacy-oriented software engineer with a strong foundation in applied cryptography, reliable data systems, and secure system design.