There’s actually an example of exactly what you’re looking for in Zig’s build.zig:
build.zig
(and another here)