I need to execute a command (currently via std.Build.addSystemCommand) and use the file it generates as the input to another build step. Unfortunately, the system command does not take an output argument; it just dumps its output into a hard-coded filename in whatever its cwd is.
I have a solution that seems to work, but I’m not happy with it:
pub fn build(b: *std.Build) void {
// ...
const generate = b.addSystemCommand(&.{
// ... command arguments ...
});
// Create a directory in the cache that this can use as its cwd.
const first = b.addWriteFiles();
const dir = first.getDirectory();
generate.setCwd(dir);
// Get the path of the file it outputs. This depends on the
// addWriteFiles step, not the addSystemCommand step.
const tmp = dir.join(b.graph.arena, "hard-coded-file-name") catch @panic("OOM");
// Create a new addWriteFiles step that _does_ depend on
// the addSystemCommand step to ensure that it actually
// gets run before anything that depends on the file.
const second = b.addWriteFiles();
second.step.dependOn(&generate.step);
const file = second.addCopyFile(tmp, "hard-coded-file-name");
// Pass `file` to steps that need it ...
}
Is there a better way to do this? Using two addWriteFiles steps seems needless, but my attempts to use fewer steps lead to circular dependencies, and trying to change tmp.generated.index to point to generate’s step causes an “attempt to use null value” panic. I would prefer to not manually add the dependency on generate to every step that needs file.
I know roughly how this could have been solved with a custom build step in earlier versions, but it looks like that might not be possible in 0.17.0 anymore. If we are still able to make custom steps, are there any resources on how to do it? This would be the best solution IMO, since I could include the output of the command with the --version flag in the step’s hash to ensure that it gets re-run if the version changes.
What’s the best way to make sure the command gets re-run if its version changes? From what I can see in Maker/Step/Run.zig, the addSystemCommand’s initial arguments are hashed as plain bytes. It wouldn’t detect if the underlying command was updated, which could mean that the cached file is no longer valid.
Running <command> --version at configure-time and passing it’s output to a dummy environment variable looks like it might work, but that feels like a hack.
The other thought I had was to use Build.addRunFile with the output of Build.addFindProgramLazy so it picks up changes. This feels more robust, but I don’t know if there’s a better way to do it.
I ended up doing what @grurbo suggested. I created a short Zig executable that takes the following arguments:
The directory to use as the cwd (passed via Run.addOutputDirectoryArg2)
The path to the command (from Build.findProgramLazy and passed via Run.addFileArg2)
The additional flags to forward to the command (passed via `Run.addArg’)
The executable creates the directory from the first argument and executes the command with the new directory as its cwd.
Then I can use LazyPath.path on the path returned by Run.addOutputDirectoryArg2 to get the actual file. This does not hold onto expired cache files when the underlying command is updated, and does not require an extra copy of the output file to get the dependencies to work.