diff --git a/build.zig b/build.zig index 9fd86f93ab..95761a2fd3 100644 --- a/build.zig +++ b/build.zig @@ -10,6 +10,8 @@ const io = std.io; const fs = std.fs; const InstallDirectoryOptions = std.build.InstallDirectoryOptions; +const zig_version = std.builtin.Version{ .major = 0, .minor = 6, .patch = 0 }; + pub fn build(b: *Builder) !void { b.setPreferredReleaseMode(.ReleaseFast); const mode = b.standardReleaseOptions(); @@ -79,6 +81,24 @@ pub fn build(b: *Builder) !void { const log_scopes = b.option([]const []const u8, "log", "Which log scopes to enable") orelse &[0][]const u8{}; + const opt_version_string = b.option([]const u8, "version-string", "Override Zig version string. Default is to find out with git."); + const version = if (opt_version_string) |version| version else v: { + var code: u8 = undefined; + const version_untrimmed = b.execAllowFail(&[_][]const u8{ + "git", "-C", b.build_root, "name-rev", "HEAD", + "--tags", "--name-only", "--no-undefined", "--always", + }, &code, .Ignore) catch |err| { + std.debug.print( + \\Unable to determine zig version string: {} + \\Provide the zig version string explicitly using the `version-string` build option. + , .{err}); + std.process.exit(1); + }; + const trimmed = mem.trim(u8, version_untrimmed, " \n\r"); + break :v b.fmt("{}.{}.{}+{}", .{ zig_version.major, zig_version.minor, zig_version.patch, trimmed }); + }; + exe.addBuildOption([]const u8, "version", version); + exe.addBuildOption([]const []const u8, "log_scopes", log_scopes); exe.addBuildOption(bool, "enable_tracy", tracy != null); if (tracy) |tracy_path| { diff --git a/src-self-hosted/link.zig b/src-self-hosted/link.zig index 1faec19b60..76d889c581 100644 --- a/src-self-hosted/link.zig +++ b/src-self-hosted/link.zig @@ -15,6 +15,9 @@ const leb128 = std.debug.leb; const Package = @import("Package.zig"); const Value = @import("value.zig").Value; const Type = @import("type.zig").Type; +const build_options = @import("build_options"); + +const producer_string = if (std.builtin.is_test) "zig test" else "zig " ++ build_options.version; // TODO Turn back on zig fmt when https://github.com/ziglang/zig/issues/5948 is implemented. // zig fmt: off @@ -1132,7 +1135,7 @@ pub const File = struct { // Write the form for the compile unit, which must match the abbrev table above. const name_strp = try self.makeDebugString(self.base.options.root_pkg.root_src_path); const comp_dir_strp = try self.makeDebugString(self.base.options.root_pkg.root_src_dir_path); - const producer_strp = try self.makeDebugString("zig (TODO version here)"); + const producer_strp = try self.makeDebugString(producer_string); // Currently only one compilation unit is supported, so the address range is simply // identical to the main program header virtual address and memory size. const text_phdr = &self.program_headers.items[self.phdr_load_re_index.?]; diff --git a/src-self-hosted/main.zig b/src-self-hosted/main.zig index 76d8651646..b9b4872d6b 100644 --- a/src-self-hosted/main.zig +++ b/src-self-hosted/main.zig @@ -95,11 +95,8 @@ pub fn main() !void { const stdout = io.getStdOut().outStream(); return @import("print_targets.zig").cmdTargets(arena, cmd_args, stdout, info.target); } else if (mem.eql(u8, cmd, "version")) { - // Need to set up the build script to give the version as a comptime value. - // TODO when you solve this, also take a look at link.zig, there is a placeholder - // that says "TODO version here". - std.debug.print("TODO version command not implemented yet\n", .{}); - return error.Unimplemented; + std.io.getStdOut().writeAll(build_options.version ++ "\n") catch process.exit(1); + return; } else if (mem.eql(u8, cmd, "zen")) { try io.getStdOut().writeAll(info_zen); } else if (mem.eql(u8, cmd, "help")) {