build.zig

2.4 KB · raw

1const std = @import("std");23pub fn build(b: *std.Build) void {4    // When built by Kepr Builds, KEPR_TARGET carries the cross-compile triple5    // so the same command works across all platforms without shell expansion.6    const target = if (b.graph.environ_map.get("KEPR_TARGET")) |t|7        b.resolveTargetQuery(std.Target.Query.parse(8            .{ .arch_os_abi = t },9        ) catch std.Target.Query{})10    else11        b.standardTargetOptions(.{});12    const optimize = b.standardOptimizeOption(.{});1314    // VERSION file → build_options.version, accessible from version.zig.15    const version_raw = @embedFile("VERSION");16    const version = std.mem.trim(u8, version_raw, " \r\n\t");17    const options = b.addOptions();18    options.addOption([]const u8, "version", version);19    // Absolute path to README.md, baked in at build time so tests can read it20    // regardless of the cwd `zig build test` happens to run from. README.md21    // lives outside src/, so it can't be @embedFile'd from within the src/22    // module's package boundary.23    options.addOption([]const u8, "readme_path", b.pathJoin(&.{ b.build_root.path.?, "README.md" }));24    // Absolute path to build.zig.zon, baked in the same way as readme_path so25    // a test can confirm its hand-maintained `.version` field (a kepr-publishing26    // fallback if VERSION is ever lost, per NINA-018) hasn't drifted from VERSION.27    options.addOption([]const u8, "zon_path", b.pathJoin(&.{ b.build_root.path.?, "build.zig.zon" }));2829    const exe = b.addExecutable(.{30        .name = "nina",31        .root_module = b.createModule(.{32            .root_source_file = b.path("src/main.zig"),33            .target = target,34            .optimize = optimize,35        }),36    });37    exe.root_module.addOptions("build_options", options);38    b.installArtifact(exe);3940    const run_cmd = b.addRunArtifact(exe);41    run_cmd.step.dependOn(b.getInstallStep());42    if (b.args) |args| run_cmd.addArgs(args);43    const run_step = b.step("run", "Run nina");44    run_step.dependOn(&run_cmd.step);4546    const tests = b.addTest(.{47        .root_module = b.createModule(.{48            .root_source_file = b.path("src/main.zig"),49            .target = target,50            .optimize = optimize,51        }),52    });53    tests.root_module.addOptions("build_options", options);54    const test_step = b.step("test", "Run unit tests");55    test_step.dependOn(&b.addRunArtifact(tests).step);56}