Sze
June 5, 2025, 1:52pm
3
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
Zine is a collection of tools (eg markdown to html renderer, templating engine, etc) orchestrated via the Zig Build system.
Zine also features its own templating language called Super (which currently lives in the same repository but that will eventually become a standalone project).
It’s still super alpha but it’s complete enough to build my personal blog which was ported to Zine from Hugo a while ago.
I develop it on stream quite often so if you catch me live you can watch me fix the bugs a…
it uses a lot of the more advanced possibilities of the build system.
Here is an example of how it is used:
const std = @import("std");
const zine = @import("zine");
pub fn build(b: *std.Build) !void {
zine.website(b, .{
.title = "Loris Cro's Blog",
.host_url = "https://kristoff.it",
.layouts_dir_path = "layouts",
.content_dir_path = "content",
.assets_dir_path = "assets",
.static_assets = &.{
"favicon.ico",
"CNAME",
".well-known/atproto-did",
},
// .image_size_attributes = true,
.debug = true,
});
}
And here is the definition of the website
function within Zine’s build.zig
:
/// Builds a Zine website.
pub fn website(project: *std.Build, opts: Options) *std.Build.Step.Run {
const zine_dep = project.dependencyFromBuildZig(@This(), .{
.optimize = opts.debug.optimize,
.scope = opts.debug.scopes,
});
const run_zine = switch (opts.zine) {
.source => project.addRunArtifact(zine_dep.artifact("zine")),
.path => |path| project.addSystemCommand(&.{path orelse "zine"}),
};
run_zine.setCwd(opts.website_root orelse project.path("."));
run_zine.addArg("release");
const full_install_path = project.pathJoin(&.{
project.install_prefix,
opts.install_path,
});
run_zine.addArg(project.fmt("--install={s}", .{full_install_path}));
This file has been truncated. show original
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:
opened 11:52PM - 12 Jan 23 UTC
closed 03:38AM - 10 Mar 23 UTC
enhancement
frontend
zig build system
Extracted from #14265.
Within a build.zig file, `@import` should be allowed, … and it should refer to the build.zig file of any dependencies listed in the manifest file.
Example:
build.zig.zon
```zig
.dependencies = .{
.@"android-sdk" = .{
.url = "...",
.hash = "...",
},
},
```
build.zig
```zig
const std = @import("std");
pub fn build(b: *std.build.Builder) void {
const exe = b.addExecutable("app", "src/main.zig");
@import("android-sdk").helperFunction(exe);
}
```
build.zig of android-sdk:
```zig
const std = @import("std");
pub fn build(b: *std.build.Builder) void {
// ...
}
pub fn helperFunction(artifact: *std.build.LibExeObjStep) void {
// ...
}
```
cc @MasterQ32
7 Likes