Importing custom modules in build.zig (outside fn build)

(This is a tough one for search engines.)

I think I remember seeing a build.zig along these lines somewhere:

const std = @import("std");

const non_std_module = @import("non_std_module");
// "non_std_module" does not end in ".zig"

pub fn build(b: *std.Build) void {
    // ...
}

Is there such a way to use (non-std non-.zig) modules in build.zig? If yes, how? If no, any good pointers for (human) memory exercises? :slight_smile:

1 Like

You can reference the build.zig files of your dependency using this method, so it is commenly used for things like Mach apps where they provide methods for setting up your application how that framework expects you to use it.

In order to @import("non_std_module") in your build.zig that module will need to be in your build.zig.zon. When you import it, it will contail whatever is pub in the dependencies build.zig file.

If you want to include your own files, then you will need to import the file with the .zig suffix.

4 Likes

When you import a dependency in your build.zig you are importing what was publicly declared in the build.zig of the corresponding dependency.

My go to project to look at, for these sort of things is

it uses a lot of the more advanced possibilities of the build system.

Here is an example of how it is used:

And here is the definition of the website function within Zine’s build.zig:

Note that this means that you only can import and use projects this way that have specifically defined pub declarations in their build.zig.
As far as I can tell/remember this was specifically done so that building a project could be done with less/more-specific dependencies/code.

It was implemented here:

7 Likes

Thanks for the very clear and elaborate answers!

2 Likes