Creating a zig module to be used a build time

I am busy learning Zig and tinkering with a monorepo type structure where I have many libraries in subfolders with their own build.zig and build.zig.zon files.

I currently have some build logic that I am having to duplicate between all of these subfolders and I was wondering if there is a way to create a build-tasks module that can be used by other modules at build time but as far as I can tell to use a zig file at build time you need to @import it’s path.

Is there a way to create a module for use at build time?

in build.zig you can @import("dep_name") to import the build.zig of a dependency.

you can even do b.lazyImport for lazy dependencies.

3 Likes

Thanks. That is entirely obvious. I think I still have a bit of a mental block about build.zig and stuff happening at comptime still being “just plain zig”

It actually isn’t obvious, this works because when zig builds your build.zig it passes the build.zigs of dependencies as imports.

It’s something you wouldn’t know unless told, or read the relevant docs.

I think you are also confusing comptime, with running build.zig.

‘comptime’ refers to the language feature where the compiler can interpret zig code while compiling to do various things.

running build.zig is 2 steps before that, and is referred to as the ‘configure phase’ as you are configuring the build graph.

The graph is then executed in the ‘make phase’ during which code might be compiled, only then is ‘comptime’.

8 Likes