Build.addOptions: use of undeclared identifier 'build'

I’m trying to declare an enum type in my build.zig, create a build flag for it with Build.option(), and pass the flag down to the module with Build.addOptions()Module.addOptions().

        const UnixWindowingSystem = enum { x11, wayland };
        const linux_window_hint = b.option(
            UnixWindowingSystem,
            "linux_windowing",
            "If targeting Linux, hint the windowing system.",
        );
        const options = b.addOptions();
        options.addOption(
            ?UnixWindowingSystem,
            "linux_windowing",
            linux_window_hint,
        );

        module.addOptions("options", options);

When I compile:

.zig-cache/c/2e7c8fa2ad0f40ba4fb243e0c52d0261/options.zig:1:29: error: use of undeclared identifier 'build'
pub const linux_windowing: ?build.build.UnixWindowingSystem = null;
                            ^~~~~

I see the issue is that the generated options.zig expects a missing build module where the type should have been passed down.

How do I give it the type? I’ve already tried putting the identifier at every scope of build.zig and searched through the std docs… sorry if I missed the right function. Is this not something I’m able to do?

std.Build.Step.Options generating invalid code for arrays/slices/optionals of custom types is a known issue:

There are two open pull requests (1, 2) that attempt to fix this but they are not passing CI or have been abandoned.

As a workaround you could define the enum as enum { none, x11, wayland }, define the option as non-optional and use .none in place of null.

2 Likes