diff options
Diffstat (limited to 'examples/build-options/build.zig')
| -rw-r--r-- | examples/build-options/build.zig | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/examples/build-options/build.zig b/examples/build-options/build.zig new file mode 100644 index 0000000..5412e8d --- /dev/null +++ b/examples/build-options/build.zig @@ -0,0 +1,43 @@ +const std = @import("std"); + +pub fn build(b: *std.Build) void { + const target = b.standardTargetOptions(.{}); + const optimize = b.standardOptimizeOption(.{}); + + // Boolean flag: -Dverbose + const verbose = b.option(bool, "verbose", "Enable verbose logging") orelse false; + + // Integer option: -Dport=<n> + const port = b.option(u16, "port", "TCP port to listen on") orelse 8080; + + // Enum option: -Dbackend=<name> + const Backend = enum { memory, sqlite, postgres }; + const backend = b.option(Backend, "backend", "Storage backend") orelse .memory; + + // Hyphenated key option: -Dmax-conn=<n> (tests USER_OPTION_REGEX with hyphens) + const max_conn = b.option(u32, "max-conn", "Maximum simultaneous connections") orelse 128; + + const options = b.addOptions(); + options.addOption(bool, "verbose", verbose); + options.addOption(u16, "port", port); + options.addOption(Backend, "backend", backend); + options.addOption(u32, "max_conn", max_conn); + + const exe = b.addExecutable(.{ + .name = "build-options-demo", + .root_module = b.createModule(.{ + .root_source_file = b.path("src/main.zig"), + .target = target, + .optimize = optimize, + .imports = &.{ + .{ .name = "build_options", .module = options.createModule() }, + }, + }), + }); + b.installArtifact(exe); + + const run_cmd = b.addRunArtifact(exe); + if (b.args) |args| run_cmd.addArgs(args); + const run_step = b.step("run", "Run the build-options demo"); + run_step.dependOn(&run_cmd.step); +} |
