Is there a clean way to add C source file build flags to a zig dependency that has its own build.zig file?
I'm guessing it's possible if the library author built in a custom way to pass additional build flags in, but I'm wondering if there's a generic way that zig supports, that doesn't require any skulduggery?
I have managed to accomplish it with what seems like some slightly unsavoury code, but I'm wondering if there's a better option:
const libmp3lame = b.dependency("libmp3lame", .{
.target = target,
.optimize = optimize,
});
const libmp3lame_artifact = libmp3lame.artifact("mp3lame");
for (libmp3lame_artifact.root_module.link_objects.items) |link_object| {
switch (std.meta.activeTag(link_object)) {
.c_source_files => {
const new_flags = std.mem.concat(b.allocator, []const u8, &.{
link_object.c_source_files.flags,
&.{
"-fno-sanitize=undefined",
},
}) catch @panic("OOM");
link_object.c_source_files.flags = new_flags;
},
else => {},
}
}
The particular dependency is https://github.com/allyourcodebase/libmp3lame btw.
And I need to add that flag because lame apparently has some UB in it. Unfortunately.
There's already a ticket regarding this issue, but I'm not sure what the specific resolution should be (considering the upstream might be somewhat unmaintained):
https://github.com/allyourcodebase/libmp3lame/issues/3
I'm interested in working around it without any changes upstream from my project.