an experimental irc client

build: add version string and flag

Signed-off-by: Tim Culverhouse <tim@timculverhouse.com>

+58
+36
build.zig
··· 1 1 const std = @import("std"); 2 2 3 + /// Must be kept in sync with git tags 4 + const comlink_version: std.SemanticVersion = .{ .major = 0, .minor = 0, .patch = 0 }; 5 + 3 6 pub fn build(b: *std.Build) void { 4 7 const target = b.standardTargetOptions(.{}); 5 8 const optimize = b.standardOptimizeOption(.{}); ··· 32 35 .target = target, 33 36 .optimize = optimize, 34 37 }); 38 + const opts = b.addOptions(); 39 + const version_string = version(b) catch |err| { 40 + std.debug.print("{}", .{err}); 41 + @compileError("couldn't get version"); 42 + }; 43 + opts.addOption([]const u8, "version", version_string); 44 + 45 + exe.root_module.addOptions("build_options", opts); 35 46 exe.root_module.addImport("tls", tls_dep.module("tls")); 36 47 exe.root_module.addImport("ziglua", ziglua_dep.module("ziglua")); 37 48 exe.root_module.addImport("vaxis", vaxis_dep.module("vaxis")); ··· 66 77 const test_step = b.step("test", "Run unit tests"); 67 78 test_step.dependOn(&run_exe_unit_tests.step); 68 79 } 80 + 81 + fn version(b: *std.Build) ![]const u8 { 82 + if (!std.process.can_spawn) { 83 + std.debug.print("error: version info cannot be retrieved from git. Zig version must be provided using -Dversion-string\n", .{}); 84 + std.process.exit(1); 85 + } 86 + const version_string = b.fmt("v{d}.{d}.{d}", .{ comlink_version.major, comlink_version.minor, comlink_version.patch }); 87 + 88 + var code: u8 = undefined; 89 + const git_describe_untrimmed = b.runAllowFail(&[_][]const u8{ 90 + "git", 91 + "-C", 92 + b.build_root.path orelse ".", 93 + "describe", 94 + "--tags", 95 + "--abbrev=9", 96 + }, &code, .Ignore) catch { 97 + return version_string; 98 + }; 99 + if (!std.mem.startsWith(u8, git_describe_untrimmed, version_string)) { 100 + std.debug.print("error: tagged version does not match internal version\n", .{}); 101 + std.process.exit(1); 102 + } 103 + return std.mem.trim(u8, git_describe_untrimmed, " \n\r"); 104 + }
+22
src/main.zig
··· 1 1 const std = @import("std"); 2 + const options = @import("build_options"); 2 3 const builtin = @import("builtin"); 3 4 const comlink = @import("comlink.zig"); 4 5 const vaxis = @import("vaxis"); ··· 16 17 .{ .scope = .vaxis_parser, .level = .warn }, 17 18 }, 18 19 }; 20 + 21 + pub const version = options.version; 19 22 20 23 /// Called after receiving a terminating signal 21 24 fn cleanUp(sig: c_int) callconv(.C) void { ··· 47 50 } 48 51 const alloc = gpa.allocator(); 49 52 53 + var args = try std.process.argsWithAllocator(alloc); 54 + while (args.next()) |arg| { 55 + if (argMatch("-v", "--version", arg)) { 56 + const stdout = std.io.getStdOut(); 57 + try stdout.writer().print("comlink {s}\n", .{version}); 58 + return; 59 + } 60 + } 61 + 50 62 // Handle termination signals 51 63 switch (builtin.os.tag) { 52 64 .windows => {}, ··· 87 99 else => return err, 88 100 } 89 101 }; 102 + } 103 + 104 + fn argMatch(maybe_short: ?[]const u8, maybe_long: ?[]const u8, arg: [:0]const u8) bool { 105 + if (maybe_short) |short| { 106 + if (std.mem.eql(u8, short, arg)) return true; 107 + } 108 + if (maybe_long) |long| { 109 + if (std.mem.eql(u8, long, arg)) return true; 110 + } 111 + return false; 90 112 } 91 113 92 114 test {