#mulitple executibles with single build.zig (structs with optional fields)

1 messages · Page 1 of 1 (latest)

echo junco
#

To compile multiple target programs within a single build.zig file, I've used a "Targ" struct, and created an array of these. Once created, the build file can loop around and compile each element.

Lately, been wanting to add a "libs" field that some targets may have, others may not have it. Perhaps something like this:

const Targ = struct {
    name: []const u8,
    src: []const u8,
    libs: ?????
}

const targs = [_] Targ{
    .{
        .name = "foo",
        .src = "src/foo.zig",
    },

    .{
        .name = "bar",
        .src = "src/bar.zig",
        .libs = .{"jpeg", "tiff"},
    },
};

I do not know what type the "libs" field should have. Also, the above instantiation of "targs" above will not compile.

Any hints on how to implement?

quiet torrent
#

Why the struct? It'd be nicer to just use the normal API multiple times.

echo junco
#

hmmm.... good question. I am not sure what the "normal API" alternative means

#

I pass the array of structs to the loop.

quiet torrent
#

You can just use addExecutable multiple times.

tired saddle
#

i.e. nullable array of strings

echo junco
#

aha...I see what you mean.

quiet torrent
tired saddle
#

oh yeah that might be better

echo junco
#

I use the array to pass to a function within the build.zig, this function will use the normal API of the addExecutable.

With a large number of executables to be built, copy and pasting is not as nice as a sub-function.

quiet torrent
#

Sure, use a function then if you want.

echo junco
#

Yeah, I tried the libs: []const []const u8 = &.{}, but got the error "does not support array initialization"

quiet torrent
#

paste the full error please

echo junco
#

sure ... it'll take a sec...

#
            .libs = .{ "sdl2_image", "jpeg", "libpng", "tiff", "webp", },
                    ~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/void/PiRepos/jpl_sdl2/build.zig:35:22: note: inferred array length is specified with an underscore: '[_][]const u8'
referenced by:
    runBuild__anon_7138: /home/void/zig/build/stage3/lib/zig/std/Build.zig:1601:37
    steps__anon_6911: /home/void/zig/build/stage3/lib/zig/build_runner.zig:914:20
    remaining reference traces hidden; use '-freference-trace' to see all reference traces
#

@quiet torrent it took longer than expected (it was on a different machine.)

quiet torrent
#

you're missing a & there

echo junco
#

YES! Thanks a million!