srctree

Gregory Mullen parent 3c841f7c 30248683
add semver support to build.zig

build.zig added: 35, removed: 2, total 33
@@ -13,6 +13,14 @@ pub fn build(b: *std.Build) void {
.optimize = optimize,
});
 
const exe_opt = b.addOptions();
exe.addOptions("hsh_build", exe_opt);
exe_opt.addOption(
std.SemanticVersion,
"version",
std.SemanticVersion.parse(version(b)) catch unreachable,
);
 
const log = b.createModule(.{
.source_file = .{ .path = "src/log.zig" },
});
@@ -35,9 +43,30 @@ pub fn build(b: *std.Build) void {
.target = target,
.optimize = optimize,
});
unit_tests.addOptions("hsh_build", exe_opt);
unit_tests.addModule("log", log);
const run_tests = b.addRunArtifact(unit_tests);
 
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&run_tests.step);
}
 
fn version(b: *std.Build) []const u8 {
if (!std.process.can_spawn) {
std.debug.print("Can't get a version number\n", .{});
std.process.exit(1);
}
 
var code: u8 = undefined;
var git_wide = b.execAllowFail(&[_][]const u8{
"git",
"describe",
"--dirty",
"--always",
}, &code, .Ignore) catch {
std.process.exit(2);
};
 
var git = std.mem.trim(u8, git_wide, " \r\n");
return if (std.mem.startsWith(u8, git, "v")) git[1..] else git;
}
 
src/main.zig added: 35, removed: 2, total 33
@@ -1,4 +1,5 @@
const std = @import("std");
const hsh_build = @import("hsh_build");
const Allocator = mem.Allocator;
const ArrayList = std.ArrayList;
const TTY = TTY_.TTY;
@@ -294,6 +295,9 @@ pub fn main() !void {
log.info("arg: {s}\n", .{arg});
if (std.mem.eql(u8, "debug", arg)) {
log.verbosity = .debug;
} else if (std.mem.eql(u8, "--version", arg)) {
std.debug.print("version: {}\n", .{hsh_build.version});
std.process.exit(0);
}
}