Print output from external tools when bulding

Hey, does anyone know how you can print the output from external tools when building? In my case I am compiling shaders and cannot see the error messages from the tool (glslangValidator)

fn add_shader(b: *std.Build, exe: *std.Build.Step.Compile, name: []const u8) void {
    const source = std.fmt.allocPrint(b.allocator, "src/shaders/{s}", .{name}) catch @panic("OOM");
    const outpath = std.fmt.allocPrint(b.allocator, "src/shaders/{s}.spv", .{name}) catch @panic("OOM");

    const shader_compilation = b.addSystemCommand(&.{"glslangValidator"});
    shader_compilation.addArg("--target-env");
    shader_compilation.addArg("vulkan1.3");
    shader_compilation.addArg("--target-env");
    shader_compilation.addArg("spirv1.5");
    shader_compilation.addArg("-V");
    shader_compilation.addArg("-o");
    const output = shader_compilation.addOutputFileArg(outpath);
    shader_compilation.addFileArg(b.path(source));
    _ = shader_compilation.captureStdOut();
    const captured_output = shader_compilation.captured_stdout.?.generated_file.getPath();

    std.log.info("Shader Compilation Output Path: {s}", .{captured_output});
    exe.root_module.addAnonymousImport(name, .{ .root_source_file = output });
}

I tried to get the path of the output files since i know the stdout gets stored in .zig-cache but i get this error

thread 14924 panic: getPath() was called on a GeneratedFile that wasn't built yet. Is there a missing Step dependency on step 'run glslangValidator (src/shaders/mesh.vert.spv)'?

Zig does not run the command or any other steps during build; it is actually a configure phase.
captureStdOut returns a generated file specification and not the actual output.

You can save the contents of captureStdOut as a file; search for captureStdOut in Zig Build System for an example that saves the command output to a file.

I think a better way is to add a check in the system command using expectStdOutEqual.

    shader_compilation.expectStdOutEqual("");

If the resulted output is not empty you get the failed check report:

========= expected this stdout: =========

========= but found: ====================
HERE GOES THE OUTPUT...
========= from the following command: ===
glslangValidator --target-env ...
1 Like

Cool, thanks. Works well enough i guess :grinning: