From 328284fb34e86ab076c5c1061252fc6975bc76f9 Mon Sep 17 00:00:00 2001 From: Tim Culverhouse Date: Tue, 6 May 2025 08:36:16 -0500 Subject: [PATCH] add version info --- build.zig | 37 +++++++++++++++++++++++++++++++++++++ build.zig.zon | 2 +- src/main.zig | 5 +++++ 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/build.zig b/build.zig index 185e9c4..8a5ea7b 100644 --- a/build.zig +++ b/build.zig @@ -1,5 +1,8 @@ const std = @import("std"); +/// Must be kept in sync with git tags +const version: std.SemanticVersion = .{ .major = 0, .minor = 0, .patch = 0 }; + pub fn build(b: *std.Build) void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); @@ -18,6 +21,15 @@ pub fn build(b: *std.Build) void { const zeit_mod = zeit_dep.module("zeit"); exe_mod.addImport("zeit", zeit_mod); + const opts = b.addOptions(); + const version_string = genVersion(b) catch |err| { + std.debug.print("{}", .{err}); + @compileError("couldn't get version"); + }; + opts.addOption([]const u8, "version", version_string); + + exe_mod.addOptions("build_options", opts); + const exe = b.addExecutable(.{ .name = "lsr", .root_module = exe_mod, @@ -45,3 +57,28 @@ pub fn build(b: *std.Build) void { const test_step = b.step("test", "Run unit tests"); test_step.dependOn(&run_exe_unit_tests.step); } + +fn genVersion(b: *std.Build) ![]const u8 { + if (!std.process.can_spawn) { + std.debug.print("error: version info cannot be retrieved from git. Zig version must be provided using -Dversion-string\n", .{}); + std.process.exit(1); + } + const version_string = b.fmt("v{d}.{d}.{d}", .{ version.major, version.minor, version.patch }); + + var code: u8 = undefined; + const git_describe_untrimmed = b.runAllowFail(&[_][]const u8{ + "git", + "-C", + b.build_root.path orelse ".", + "describe", + "--tags", + "--abbrev=9", + }, &code, .Ignore) catch { + return version_string; + }; + if (!std.mem.startsWith(u8, git_describe_untrimmed, version_string)) { + std.debug.print("error: tagged version does not match internal version\n", .{}); + std.process.exit(1); + } + return std.mem.trim(u8, git_describe_untrimmed, " \n\r"); +} diff --git a/build.zig.zon b/build.zig.zon index f1b21fa..9559aa0 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -1,6 +1,6 @@ .{ .name = .lsr, - .version = "0.0.0", + .version = "0.1.0", .fingerprint = 0x495d173f6002e86, // Changing this has security and trust implications. .minimum_zig_version = "0.14.0", diff --git a/src/main.zig b/src/main.zig index 02f788d..b8eec25 100644 --- a/src/main.zig +++ b/src/main.zig @@ -2,6 +2,7 @@ const std = @import("std"); const builtin = @import("builtin"); const ourio = @import("ourio"); const zeit = @import("zeit"); +const build_options = @import("build_options"); const posix = std.posix; @@ -200,6 +201,10 @@ pub fn main() !void { cmd.opts.shortview = if (o) .oneline else .columns; } else if (eql(opt, "help")) { return stderr.writeAll(usage); + } else if (eql(opt, "version")) { + try bw.writer().print("lsr {s}", .{build_options.version}); + try bw.flush(); + return; } else { try stderr.print("Invalid opt: '{s}'", .{opt}); std.process.exit(1); -- 2.51.2