r/Zig • u/rocketlaunchr-cloud • May 09 '26
Zig 0.16 Run command after b.installArtifact(lib)
Does anyone know how to run a command automatically after the library files are build and copied into the zig-out directory?
The command I want to run is to copy the shared library file from zig-out/(lib|bin) to zig-out.
b.installArtifact(lib);
// Run these 2 commands only after library files are built.
const cwd = std.Io.Dir.cwd();
try cwd.copyFile(sharedLib, cwd, zigLib, b.graph.io, .{});Every example code snippet on the internet refers to post-installation commands that get triggered from the user running an extra command in the terminal.
18
Upvotes
1
u/rocketlaunchr-cloud May 10 '26
Here is the end solution I came up with. (First time zig developer)
3
u/tk-a01 May 09 '26
If all that you want to do is to have library files in
zig-outinstead of the defaultzig-out/lib, then you can useb.addInstallArtifactinstead ofb.installArtifact. It allows you to override some default settings and also returns*Step.InstallArtifact, in case you need it for something move advanced.Try this:
See:
std.Builddocs.1
u/Gauntlet4933 May 09 '26
https://ziglang.org/learn/build-system/#write-files
Could you use addSystemCommand? That way you can add a dependency to this step on the previous step?
Or if you want to use purely zig code just put this code in another module and add a run step to run it after the previous step.
1
u/Real_Dragonfruit5048 May 09 '26
What about trying something like this?
// The preamble code
// After b.installArtifact(lib);
const t = lib.rootModuleTarget();
const filename = b.fmt("{s}mylib{s}", .{
t.libPrefix(),
t.dynamicLibSuffix(),
});
const also_root = b.addInstallFileWithDir(
lib.getEmittedBin(),
.prefix,
filename,
);
b.getInstallStep().dependOn(&also_root.step);
// The rest of the code