Here's a near-minimal build.zig for a 'hello world' with two custom build options. Legend:
b.option()adds an option to 'zig build --help'. It takes a type, name, and description. This is the outward-facing option.b.addOptions()creates astd.Build.Step.Optionsto manage options to be added to build artifacts as an@import()able module..addOptionadds an option tostd.Build.Step.Options. It takes a type, name, and value. This is the inward-facing option, that the artifact actually uses.- This line adds the options as a Zig module to the given artifact, to be imported with the given name (here: build_options).
const std = @import("std");
pub fn build(b: *std.Build) void {
// (1)
const opt_color: ?bool = b.option(bool, "color", "use color codes?");
const opt_message: ?[]const u8 = b.option([]const u8, "message", "set custom message");
// (2)
const opts = b.addOptions();
// (3)
opts.addOption(bool, "colors", opt_color orelse false);
opts.addOption(?[]const u8, "msg", opt_message);
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const exe = b.addExecutable(.{
.name = "hello",
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});
// (4)
exe.root_module.addImport("build_options", opts.createModule());
b.installArtifact(exe);
}
and a brief src/main.zig:
const std = @import("std");
const opts = @import("build_options");
const RED = if (opts.colors) "\x1b[31;1m" else "";
const RESET = if (opts.colors) "\x1b[0m" else "";
const MSG = RED ++ (opts.msg orelse "Hello, world!") ++ RESET;
pub fn main() !void {
const stdout = std.io.getStdOut().writer();
try stdout.print("{s}\n", .{MSG});
}