diff options
Diffstat (limited to 'build.zig')
| -rw-r--r-- | build.zig | 31 |
1 files changed, 31 insertions, 0 deletions
@@ -1,6 +1,9 @@ const std = @import("std"); const builtin = @import("builtin"); +const HEAD_location = ".git/HEAD"; +const commit_location = ".git/refs/heads/master"; + pub fn build(b: *std.Build) void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); @@ -11,6 +14,11 @@ pub fn build(b: *std.Build) void { .link_libc = true, }); + + // Adding a build version + + root.addCMacro("BUILD_VERSION", GetVersionString(b, optimize) catch "Build UNTRACKED"); + root.addCSourceFiles(.{ .files = &[_][]const u8 { "src/main.c", @@ -62,3 +70,26 @@ pub fn build(b: *std.Build) void { run_cmd.addArgs(args); } } + +// Returns a build version. Returns an empty string if build in release mode +fn GetVersionString(b: *std.Build, optimization: std.builtin.OptimizeMode) ![]u8 { + if (optimization != .Debug) { + return ""; + } + + const allocator = b.allocator; + var threaded = std.Io.Threaded.init(allocator, .{}); + defer threaded.deinit(); + + const headContents = try std.Io.Dir.cwd().readFileAlloc(threaded.io(), HEAD_location, allocator, .limited(std.math.maxInt(usize))); + var headIter = std.mem.tokenizeAny(u8, headContents[0..headContents.len - 1], "/"); + var branchName: []const u8 = ""; + while (headIter.next()) |token| { + branchName = token; + } + const commitContents = try std.Io.Dir.cwd().readFileAlloc(threaded.io(), commit_location, allocator, .limited(std.math.maxInt(usize))); + return try std.fmt.allocPrint(allocator, "\"Build {s}:{s}\"", .{ + branchName, + commitContents[0..@min(commitContents.len-1, 10)] + }); +} |
