aboutsummaryrefslogtreecommitdiff
path: root/examples/build-options/build.zig
blob: 5412e8ddafdd7760cf2fc88fd7f4626304f00a47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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);
}