diff --git a/build.zig b/build.zig index 389dff5..b432ca3 100644 --- a/build.zig +++ b/build.zig @@ -6,6 +6,12 @@ const zbs = std.build; const ScanProtocolsStep = @import("deps/zig-wayland/build.zig").ScanProtocolsStep; +/// While a river release is in development, this string should contain the version in development +/// with the "-dev" suffix. +/// When a release is tagged, the "-dev" suffix should be removed for the commit that gets tagged. +/// Directly after the tagged commit, the version should be bumped and the "-dev" suffix added. +const version = "0.1.0-dev"; + pub fn build(b: *zbs.Builder) !void { const target = b.standardTargetOptions(.{}); const mode = b.standardReleaseOptions(); @@ -48,6 +54,21 @@ pub fn build(b: *zbs.Builder) !void { const examples = b.option(bool, "examples", "Set to true to build examples") orelse false; + const full_version = blk: { + if (mem.endsWith(u8, version, "-dev")) { + var ret: u8 = undefined; + const git_dir = try fs.path.join(b.allocator, &[_][]const u8{ b.build_root, ".git" }); + const git_commit_hash = b.execAllowFail( + &[_][]const u8{ "git", "--git-dir", git_dir, "--work-tree", b.build_root, "rev-parse", "--short", "HEAD" }, + &ret, + .Inherit, + ) catch break :blk version; + break :blk try std.fmt.allocPrintZ(b.allocator, "{s}-{s}", .{ version, git_commit_hash }); + } else { + break :blk version; + } + }; + const scanner = ScanProtocolsStep.create(b); scanner.addSystemProtocol("stable/xdg-shell/xdg-shell.xml"); scanner.addSystemProtocol("unstable/pointer-gestures/pointer-gestures-unstable-v1.xml"); @@ -64,6 +85,7 @@ pub fn build(b: *zbs.Builder) !void { river.setTarget(target); river.setBuildMode(mode); river.addBuildOption(bool, "xwayland", xwayland); + river.addBuildOption([:0]const u8, "version", full_version); addServerDeps(river, scanner); @@ -74,6 +96,7 @@ pub fn build(b: *zbs.Builder) !void { const riverctl = b.addExecutable("riverctl", "riverctl/main.zig"); riverctl.setTarget(target); riverctl.setBuildMode(mode); + riverctl.addBuildOption([:0]const u8, "version", full_version); riverctl.step.dependOn(&scanner.step); riverctl.addPackage(scanner.getPkg()); @@ -89,6 +112,7 @@ pub fn build(b: *zbs.Builder) !void { const rivertile = b.addExecutable("rivertile", "rivertile/main.zig"); rivertile.setTarget(target); rivertile.setBuildMode(mode); + rivertile.addBuildOption([:0]const u8, "version", full_version); rivertile.step.dependOn(&scanner.step); rivertile.addPackage(scanner.getPkg()); diff --git a/river/main.zig b/river/main.zig index 30b2f78..212cc69 100644 --- a/river/main.zig +++ b/river/main.zig @@ -42,6 +42,7 @@ const usage: []const u8 = \\ -h Print this help message and exit. \\ -c Run `sh -c ` on startup. \\ -l Set the log level to a value from 0 to 7. + \\ -version Print the version number and exit. \\ ; @@ -73,6 +74,9 @@ pub fn main() anyerror!void { } else { printErrorExit("Error: flag '-l' requires exactly one argument", .{}); } + } else if (std.mem.eql(u8, arg, "-version")) { + try std.io.getStdOut().writeAll(build_options.version); + os.exit(0); } else { const stderr = std.io.getStdErr().writer(); try stderr.print(usage, .{}); diff --git a/riverctl/main.zig b/riverctl/main.zig index 9d4c8b6..31ca44e 100644 --- a/riverctl/main.zig +++ b/riverctl/main.zig @@ -61,9 +61,15 @@ fn _main() !void { const control = globals.control orelse return error.RiverControlNotAdvertised; const seat = globals.seat orelse return error.SeatNotAdverstised; - // Skip our name, send all other args // This next line is needed cause of https://github.com/ziglang/zig/issues/2622 const args = os.argv; + + if (mem.eql(u8, mem.span(args[1]), "-version")) { + try std.io.getStdOut().writeAll(@import("build_options").version); + std.os.exit(0); + } + + // Skip our name, send all other args for (args[1..]) |arg| control.addArgument(arg); const callback = try control.runCommand(seat); diff --git a/rivertile/main.zig b/rivertile/main.zig index 61eba08..ff57475 100644 --- a/rivertile/main.zig +++ b/rivertile/main.zig @@ -52,6 +52,7 @@ const usage = \\Usage: rivertile [options] \\ \\ -h, --help Print this help message and exit. + \\ -version Print the version number and exit. \\ -view-padding Set the padding around views in pixels. (Default 6) \\ -outer-padding Set the padding around the edge of the layout area in \\ pixels. (Default 6) @@ -315,6 +316,7 @@ pub fn main() !void { const args = Args(0, &[_]FlagDef{ .{ .name = "-h", .kind = .boolean }, .{ .name = "--help", .kind = .boolean }, + .{ .name = "-version", .kind = .boolean }, .{ .name = "-view-padding", .kind = .arg }, .{ .name = "-outer-padding", .kind = .arg }, .{ .name = "-main-location", .kind = .arg }, @@ -326,6 +328,10 @@ pub fn main() !void { try std.io.getStdOut().writeAll(usage); std.os.exit(0); } + if (args.boolFlag("-version")) { + try std.io.getStdOut().writeAll(@import("build_options").version); + std.os.exit(0); + } if (args.argFlag("-view-padding")) |raw| { view_padding = std.fmt.parseUnsigned(u32, mem.span(raw), 10) catch fatal("invalid value '{s}' provided to -view-padding", .{raw});