ReviewOS

also looking at this

zig-utils/zig-test-framework

build: single-source the package and CLI version

#20
Merged glennmichael123 wants to merge codex/issue-13-version into main
7 files +40 -11
build.zigmodified+23-0
Changes to build.zig
@@ -1,19 +1,37 @@
11const std = ;
2const manifest_source = @embedFile("build.zig.zon");
3const package_version = manifestVersion(manifest_source);
4
5fn manifestVersion(source: []const u8) []const u8 {
6 const marker = ".version = \"";
7 const marker_start = std.mem.indexOf(u8, source, marker) orelse
8 @compileError("build.zig.zon must declare .version");
9 const start = marker_start + marker.len;
10 const end_offset = std.mem.indexOfScalar(u8, source[start..], '"') orelse
11 @compileError("build.zig.zon contains an unterminated .version");
12 return source[start .. start + end_offset];
13}
214
315pub fn build(b: *std.Build) void {
416 const target = b.standardTargetOptions(.{});
517 const optimize = b.standardOptimizeOption(.{});
618
19 const build_options = b.addOptions();
20 build_options.addOption([]const u8, "version", package_version);
21
722 // Create the main library module
823 const lib_module = b.addModule("zig_test_framework", .{
924 .root_source_file = b.path("src/lib.zig"),
1025 .target = target,
1126 .link_libc = true,
1227 });
28 lib_module.addOptions("build_options", build_options);
1329
1430 // Create the test runner executable
1531 const exe = b.addExecutable(.{
1632 .name = "zig-test",
33 .version = std.SemanticVersion.parse(package_version) catch
34 @panic("build.zig.zon contains an invalid semantic version"),
1735 .root_module = b.createModule(.{
1836 .root_source_file = b.path("src/main.zig"),
1937 .target = target,
@@ -258,6 +276,11 @@ pub fn build(b: *std.Build) void {
258276 run_invalid_config.expectExitCode(2);
259277 test_step.dependOn(&run_invalid_config.step);
260278
279 const run_version = b.addRunArtifact(exe);
280 run_version.addArg("--version");
281 run_version.expectStdErrEqual(b.fmt("Zig Test Framework v{s}\n", .{package_version}));
282 test_step.dependOn(&run_version.step);
283
261284 // Examples
262285 const basic_example = b.addExecutable(.{
263286 .name = "basic_example",