#Add C source file build flags to dependency that has build.zig

1 messages · Page 1 of 1 (latest)

tiny plank
#

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.

GitHub

Upstream appears to be effectively dead, but there's undefined behavior happening here: Illegal instruction at address 0x4ace666 /home/andy/.cache/zig/p/1220a15af8fd1628f0adfeb43782c49cdbe0ea8d...

mortal sundial
#

there's not really any better way of accomplishing that than what you are doing currently. Artifacts resolved from dependencies should generally be considered immutable and not configurable beyond the options you pass to b.dependency. The best fix would be to open a PR to the repo that either adds -fno-sanitize=undefined or patches the bugs as mentioned in the issue. The allyourcodebase org has several repos that all disable UB sanitization so I don't think the former should be too controversial