How to use the new "addTempFiles" and "tmpPath" build functions when dealing with temporary files?

I have the following case. In my build.zig I build my game assets. One asset type it should handle are .blend files, it should pre-render the assets into .png’s, and pack them into a atlas/spritesheet.
Only the final resulting atlas should be installed along side the executable, all the intermediate .png files are not of interest and should be cleaned up after build.

I wanted to use something like addRemoveDirTree but that one got removed recently and apparently replaced with addTempFiles and tmpPath though I’m not sure how to use these as I don’t control the tmp files created in the build.zig as it’s a result of a python script.

Here what I’ve tried:

fn addBlend(b: *std.Build, source: std.Build.LazyPath) void {
    // Step results in tmp .png files to be packed in a atlas in a later step
    const render_step = b.addSystemCommand(&.{ "blender", "--background" });
    // If .blend changes, re-render the blend file
    render_step.addFileArg(source);
    // If python script changes, re-render the blend file
    render_step.addArg("--python");
    render_step.addFileArg(b.path("build/scripts/render.py"));
    render_step.addArg("--");
    render_step.addArg("--output");
    // Crashes because path is not generated yet
    const path = b.tmpPath().getPath4(b, &render_step.step) catch @panic("No path!");
    const output_dir = render_step.addOutputDirectoryArg(path.sub_path);
    _ = output_dir;

    // const pack_step = b.addSystemCommand(&.{"Some command to pack the resulting .pngs"});
    // pack_step.addDirectoryArg(output_dir);

    b.getInstallStep().dependOn(&render_step.step);
}

Should I just use Io to create and delete a tmp directory myself? Or is there some way to get this working using the build system?

The addOutputDirectoryArg function actually creates a temporarily directory itself, so you don’t have to do that yourself.
The string that it needs is only the basename, i.e. the name of the directory it should create, not a full path.

So you should be able to just do something like this:

const output_dir = render_step.addOutputDirectoryArg("assets");

And remove the line above this creating the temp path.

3 Likes

Thnx! Tested it and it indeed seems to create a temp dir.