I want to migrate a C project’s build system from Autotools to Zig Build because I plan to incrementally rewrite the project in Zig. There’s one part that’s problematic.
The project has a port directory which contains files with implementations of functions which might not be present on the target OS. For example there’s sockaddr_snprintf.c. Autotools checks if the function is present on the OS and if it’s not it gets added as a source of all the artifacts. AC_REPLACE_FUNCS is used.
How can I do the same idiomatically with Zig Build?
Unfortunately, the “check if some system library symbol exists or if some code compiles and conditionally add a file or define a config header value depending on the result” workflow is not (currently) very well supported by the Zig build system, and it can be a big blocker when trying to migrate a project from a different build system.
For now, I would personally suggest hard-coding a configuration, or having your build.zig define b.options that can be used to manually control certain values.
Keep in mind that Zig provides libcs for some targets, so if you only need to support Windows/Linux/macOS you can reasonably assume that certain symbols will always be available. When you explicitly specify a target with -target/-Dtarget, Zig performs a hermetic/host-independent build that doesn’t let host system paths like /usr/include leak into the build, so you can use this to your advantage learn which symbols are always provided by Zig.
Start by setting up your build.zig such that it assumes that everything is available, then build the project with -Dtarget=x86_64-linux-gnu (or whatever your target is) and let the compile errors guide you into progressively setting config header values or providing your own replacements for missing functionality until the project finally compiles.