Recusively adding a directory and sub-directories to include path

I don’t think this is currently possible, but I am trying to compile a C++ project with zig build and would like it to be able to re-build things when I add .cpp files or add new sub-directories with .cpp files. Ideally, this can all happen while under zig build --watch. I know I can manually list source files and sub-directories for include paths, but I would really rather not do that as I’m more used to the makefile-style glob syntax for compiling an entire directory and sub-directories of cpp files.

Is this at all feasible? In my understanding, I can just dir.iterate() in the build.zig script, but since that only runs once during configuration, it doesnt update under --watch mode.

the word “include path” is i think a red herring here. include paths are for header files included by your source files. to me it sounds like what you is to gather all the cpp source files in a directory as source files. i believe you can do that by writing normal Zig code in your build.zig file. if the logic is sufficiently complicated you should consider writing an external script.

I think zig 0.17.0 will let you do it with Configure Cache Poisoning, specifically std.Build.dependOnDirectoryMetadata. That tells the ‘maker’ to rerun the ‘configurer’ if the directory contents changes. NB: it is not recursive.

That said, I recommend listing individual files. Unlike zig, C & C++ don’t have a mechanism for the compiler to determine what code is and isn’t used, so by listing them individually:

  1. It makes it easy for other people reading your project to see what it’s doing.
  2. Your build won’t fail because of a compile error in code you don’t use.

See

Just here to ask without testing for myself (sorry!), but this approach will avoid rebuilding the builder or avoid doing something heavy on the builder side, right?

Maybe related or not, but I used depend on program (system provided zip and ndk provided aapt2 for example) function and it explicitly says in the doc that it’ll poison the cache every time. It does feel slow, maybe I am doing something wrong. The reason was to describe my build pipeline more explicitly. Sorry, should have read more source code :sweat_smile:

I am assuming you’re talking about findProgram. Depending on what you’re doing, you could consider switching to findProgramLazy/addRunFile instead of findProgram/addSystemCommand.

However, if you truly need findProgram, there’s nothing wrong with poisoning the configuration cache; you do need that findProgram to be re-run every time, since what it returns could change at any point and therefore change what the configuration result looks like. It’s just worth avoiding re-running the configuration phase when possible to do so.