From 351649685d4d7751d2fdfe316763d43101e5e173 Mon Sep 17 00:00:00 2001 From: Tim Culverhouse Date: Mon, 12 Aug 2024 16:10:17 -0500 Subject: [PATCH] build: add version string and flag Signed-off-by: Tim Culverhouse --- build.zig | 36 ++++++++++++++++++++++++++++++++++++ src/main.zig | 22 ++++++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/build.zig b/build.zig index 6d34468..05defa4 100644 --- a/build.zig +++ b/build.zig @@ -1,5 +1,8 @@ const std = @import("std"); +/// Must be kept in sync with git tags +const comlink_version: std.SemanticVersion = .{ .major = 0, .minor = 0, .patch = 0 }; + pub fn build(b: *std.Build) void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); @@ -32,6 +35,14 @@ pub fn build(b: *std.Build) void { .target = target, .optimize = optimize, }); + const opts = b.addOptions(); + const version_string = version(b) catch |err| { + std.debug.print("{}", .{err}); + @compileError("couldn't get version"); + }; + opts.addOption([]const u8, "version", version_string); + + exe.root_module.addOptions("build_options", opts); exe.root_module.addImport("tls", tls_dep.module("tls")); exe.root_module.addImport("ziglua", ziglua_dep.module("ziglua")); exe.root_module.addImport("vaxis", vaxis_dep.module("vaxis")); @@ -66,3 +77,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 version(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}", .{ comlink_version.major, comlink_version.minor, comlink_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/src/main.zig b/src/main.zig index 1c84e90..6e3cb0b 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1,4 +1,5 @@ const std = @import("std"); +const options = @import("build_options"); const builtin = @import("builtin"); const comlink = @import("comlink.zig"); const vaxis = @import("vaxis"); @@ -17,6 +18,8 @@ pub const std_options: std.Options = .{ }, }; +pub const version = options.version; + /// Called after receiving a terminating signal fn cleanUp(sig: c_int) callconv(.C) void { if (vaxis.Tty.global_tty) |gty| { @@ -47,6 +50,15 @@ pub fn main() !void { } const alloc = gpa.allocator(); + var args = try std.process.argsWithAllocator(alloc); + while (args.next()) |arg| { + if (argMatch("-v", "--version", arg)) { + const stdout = std.io.getStdOut(); + try stdout.writer().print("comlink {s}\n", .{version}); + return; + } + } + // Handle termination signals switch (builtin.os.tag) { .windows => {}, @@ -89,6 +101,16 @@ pub fn main() !void { }; } +fn argMatch(maybe_short: ?[]const u8, maybe_long: ?[]const u8, arg: [:0]const u8) bool { + if (maybe_short) |short| { + if (std.mem.eql(u8, short, arg)) return true; + } + if (maybe_long) |long| { + if (std.mem.eql(u8, long, arg)) return true; + } + return false; +} + test { _ = @import("irc.zig"); } -- 2.51.2