cmake_build will produce a static library named libzmq.a. I can already link and use this library. However, every time I run zig build, zig will rerun cmake_build step again (it’s instant but it’s annoying). Is there a way for me to tell zig that it does not have to rerun that step if libzmq.a is already produced?
In addition to plain string arguments, run steps (std.Build.Step.Run) can take special arguments representing input/output paths via std.Build.Step.Run.addFileArg and related methods. These methods return a lazy path which you can subsequently pass around to other build steps.
Run steps that only take string arguments are considered impure and will be re-executed every time they are invoked. However, if a run step takes an input/output path argument, it is considered pure, will participate in the cache system and will only be rerun when any of its inputs change.
So you likely want to rewrite your build script as such:
build_dir now points to a path in .zig-cache, and any steps that needs to access anything in that directory should do so via the build_dir lazy path variable. By doing so, the build system will automatically set up step dependencies and ensure the CMake command is rerun when it needs to.
(Edit: I realize that this is the build phase of CMake and not the configure step, so this directory is an input arg and not an output arg. But for the configure phase, the same principles apply.)
Never use getPath/2/3 from your build function. These APIs are intended to be used internally by the build system after it has verified that all the correct prerequisite steps have been run. Using it from your own code is almost always a bug.
This is the wrong fix. You should not use any of the std.fs APIs that accesses the file system from your build script either. Your build function is supposed to be (mostly) pure function that declares a graph of build step dependencies without interacting with the host system.
Thank you for the write up. I see a glimpse of how this should be done, but I’m not sure how I should declare the path to libzmq.a to make zig aware of it.
Another project can use the libzmq module fine, but the cmake_build step is still always rerun. I guess it’s because there is no way to specify its output files.