diff --git a/build.zig b/build.zig index b3885e4..a2e4db0 100644 --- a/build.zig +++ b/build.zig @@ -23,6 +23,15 @@ pub fn build(b: *std.Build) void { const srf_mod = srf_dep.module("srf"); + // Build-time info: version string (from git describe) and build timestamp. + // Exposed to application code as `@import("build_info")`. + // + // The version string is derived from `git describe --tags --always --dirty` + // so dev builds show the nearest tag plus commit hash + dirty flag. If git + // is unavailable (e.g. building from a source tarball), falls back to the + // `.version` in build.zig.zon. + const build_info = buildInfoOptions(b); + // Library module -- the public API for downstream consumers of zfin. // Internal code (CLI, TUI) uses file-path imports instead. _ = b.addModule("zfin", .{ @@ -30,6 +39,7 @@ pub fn build(b: *std.Build) void { .target = target, .imports = &.{ .{ .name = "srf", .module = srf_mod }, + .{ .name = "build_info", .module = build_info }, }, }); @@ -40,6 +50,7 @@ pub fn build(b: *std.Build) void { .{ .name = "srf", .module = srf_mod }, .{ .name = "vaxis", .module = vaxis_dep.module("vaxis") }, .{ .name = "z2d", .module = z2d_dep.module("z2d") }, + .{ .name = "build_info", .module = build_info }, }; // Unified executable (CLI + TUI in one binary) @@ -83,6 +94,7 @@ pub fn build(b: *std.Build) void { .optimize = optimize, .imports = &.{ .{ .name = "srf", .module = srf_mod }, + .{ .name = "build_info", .module = build_info }, }, }), }); @@ -104,3 +116,70 @@ pub fn build(b: *std.Build) void { }), "zfin"); } } + +/// Produce the `build_info` module exposing `version` (derived from `git +/// describe`) and `build_timestamp` (Unix epoch seconds at build time). +/// Consumed as `@import("build_info")` from `src/version.zig`. +fn buildInfoOptions(b: *std.Build) *std.Build.Module { + const opts = b.addOptions(); + + // `git describe --tags --always --dirty` gives the closest tag, with a + // distance-+-hash suffix when HEAD isn't exactly on a tag, plus a + // `-dirty` marker if the working tree has uncommitted changes. Falls + // back to the build.zig.zon `.version` when git is unavailable. + const version = gitDescribe(b) orelse fallbackVersion(); + opts.addOption([]const u8, "version", version); + + // Seconds since the Unix epoch at build time. Rendered by `zfin version + // --verbose` as a human-readable date. + opts.addOption(i64, "build_timestamp", std.time.timestamp()); + + return opts.createModule(); +} + +/// Run `git describe --tags --always --dirty` in the repo root and return +/// the trimmed output. Returns null on any error (git missing, not a repo, +/// non-zero exit). +fn gitDescribe(b: *std.Build) ?[]const u8 { + var child = std.process.Child.init(&.{ "git", "describe", "--tags", "--always", "--dirty" }, b.allocator); + child.cwd = b.build_root.path; + child.stdout_behavior = .Pipe; + child.stderr_behavior = .Ignore; + child.spawn() catch return null; + + const stdout_file = child.stdout orelse { + _ = child.wait() catch {}; + return null; + }; + const stdout_bytes = stdout_file.readToEndAlloc(b.allocator, 4096) catch { + _ = child.wait() catch {}; + return null; + }; + + const term = child.wait() catch return null; + switch (term) { + .Exited => |code| if (code != 0) return null, + else => return null, + } + + const trimmed = std.mem.trim(u8, stdout_bytes, " \t\r\n"); + if (trimmed.len == 0) return null; + return b.dupe(trimmed); +} + +/// Read `.version` from `build.zig.zon` as a fallback when git describe +/// fails (e.g. when building from a source tarball with no .git directory). +/// Returns a static string if even that fails. +fn fallbackVersion() []const u8 { + // `build.zig.zon` is embedded at compile time so the fallback never + // requires runtime filesystem access in the built binary — we only do + // this lookup at build time, on the build host. + const zon_contents = @embedFile("build.zig.zon"); + if (std.mem.indexOf(u8, zon_contents, ".version = \"")) |start| { + const after = zon_contents[start + ".version = \"".len ..]; + if (std.mem.indexOfScalar(u8, after, '"')) |end| { + return after[0..end]; + } + } + return "unknown"; +}