Zig NEWS

LI Yu
LI Yu

Posted on

Upcoming zig 0.12 changes of writing `build.zig`

Note for myself :)

Add module dependency

In short, before version 0.12.0-dev.1828+225fe6ddb, in build.zig, add pkg modules to my exe as follows

const zcmd_dep = b.dependency("zcmd", .{});
const jstring_dep = b.dependency("jstring", .{});

...

var zlex_exe = b.addExecutable(.{
...    
});

zlex_exe.addModule("zcmd", zcmd_dep.module("zcmd"));
zlex_exe.addModule("jstring", jstring_dep.module("jstring"));
Enter fullscreen mode Exit fullscreen mode

will not work in newer versions (like master_0.12.0-dev.2818+97290e0bf), as breaking changes in zig build.

the new way is

const zcmd_dep = b.dependency("zcmd", .{});
const jstring_dep = b.dependency("jstring", .{});

...

var zlex_exe = b.addExecutable(.{
...    
});

zlex_exe.root_module.addImport("zcmd", zcmd_dep.module("zcmd"));
zlex_exe.root_module.addImport("jstring", zcmd_dep.module("jstring"));
Enter fullscreen mode Exit fullscreen mode

the new addImport function doc is here.

addModule

this one is an easy change, just need to change from

    _ = b.addModule("bison_data", .{ .source_file = .{ .path = b.pathFromRoot("bison/data") } });
Enter fullscreen mode Exit fullscreen mode

to

    _ = b.addModule("bison_data", .{ .root_source_file = .{ .path = b.pathFromRoot("bison/data") } });
Enter fullscreen mode Exit fullscreen mode

as struct field source_file renamed to root_source_file. Naming and renaming the 2 fundamental tasks when coding so this is well understood XD. Hopefully we can see a much stable interfaces in future.

Top comments (2)

Collapse
 
rofrol profile image
Roman Frołow
Collapse
 
rabbit profile image
pylang

you write many tools with zig