also looking at this
build: single-source the package and CLI version
#20
7 files
+40
-11
| @@ -1,19 +1,37 @@ | ||
| 1 | 1 | const std = ; |
| 2 | const manifest_source = @embedFile("build.zig.zon"); | |
| 3 | const package_version = manifestVersion(manifest_source); | |
| 4 | ||
| 5 | fn 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 | } | |
| 2 | 14 | |
| 3 | 15 | pub fn build(b: *std.Build) void { |
| 4 | 16 | const target = b.standardTargetOptions(.{}); |
| 5 | 17 | const optimize = b.standardOptimizeOption(.{}); |
| 6 | 18 | |
| 19 | const build_options = b.addOptions(); | |
| 20 | build_options.addOption([]const u8, "version", package_version); | |
| 21 | ||
| 7 | 22 | // Create the main library module |
| 8 | 23 | const lib_module = b.addModule("zig_test_framework", .{ |
| 9 | 24 | .root_source_file = b.path("src/lib.zig"), |
| 10 | 25 | .target = target, |
| 11 | 26 | .link_libc = true, |
| 12 | 27 | }); |
| 28 | lib_module.addOptions("build_options", build_options); | |
| 13 | 29 | |
| 14 | 30 | // Create the test runner executable |
| 15 | 31 | const exe = b.addExecutable(.{ |
| 16 | 32 | .name = "zig-test", |
| 33 | .version = std.SemanticVersion.parse(package_version) catch | |
| 34 | @panic("build.zig.zon contains an invalid semantic version"), | |
| 17 | 35 | .root_module = b.createModule(.{ |
| 18 | 36 | .root_source_file = b.path("src/main.zig"), |
| 19 | 37 | .target = target, |
| @@ -258,6 +276,11 @@ pub fn build(b: *std.Build) void { | ||
| 258 | 276 | run_invalid_config.expectExitCode(2); |
| 259 | 277 | test_step.dependOn(&run_invalid_config.step); |
| 260 | 278 | |
| 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 | ||
| 261 | 284 | // Examples |
| 262 | 285 | const basic_example = b.addExecutable(.{ |
| 263 | 286 | .name = "basic_example", |