From 38265938e92e25fb676f029c4db61d99fa3aa31c Mon Sep 17 00:00:00 2001 From: Femi Agbabiaka Date: Mon, 16 Jun 2025 23:58:45 -0500 Subject: [PATCH 1/2] Update build for Zig 0.14.1 This commit updates the build system for Zig 0.14.1 compatibility, including proper module configuration, build API changes, and adds a package manifest (build.zig.zon). --- .gitignore | 3 ++- build.zig | 31 +++++++++++++++++++++++-------- build.zig.zon | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ getopt.zig | 2 +- 4 files changed, 76 insertions(+), 10 deletions(-) create mode 100644 build.zig.zon diff --git a/.gitignore b/.gitignore index 03299ae..fe5cad2 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -/zig-cache/ +.zig-cache/ +zig-out/* diff --git a/build.zig b/build.zig index ceac95a..49a5eac 100644 --- a/build.zig +++ b/build.zig @@ -1,21 +1,36 @@ const std = @import("std"); -pub fn build(b: *std.build.Builder) void { +pub fn build(b: *std.Build) void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); - _ = b.addModule("getopt", .{ - .source_file = .{ - .path = "getopt.zig", - }, + const getopt_lib_mod = b.createModule(.{ + .root_source_file = b.path("getopt.zig"), + .target = target, + .optimize = optimize, }); - const tests = b.addTest(.{ - .root_source_file = .{ .path = "getopt.zig" }, + const getopt_example_exe_mod = b.createModule(.{ + .root_source_file = b.path("example.zig"), .target = target, .optimize = optimize, }); - const run_tests = b.addRunArtifact(tests); + + const lib = b.addLibrary(.{ + .linkage = .static, + .name = "getopt", + .root_module = getopt_lib_mod, + }); + b.installArtifact(lib); + + const exe = b.addExecutable(.{ + .name = "example", + .root_module = getopt_example_exe_mod, + }); + b.installArtifact(exe); + + const getopt_lib_unit_tests = b.addTest(.{ .root_module = getopt_lib_mod }); + const run_tests = b.addRunArtifact(getopt_lib_unit_tests); const test_step = b.step("test", "Run tests"); test_step.dependOn(&run_tests.step); diff --git a/build.zig.zon b/build.zig.zon new file mode 100644 index 0000000..0650574 --- /dev/null +++ b/build.zig.zon @@ -0,0 +1,50 @@ +.{ + // This is the default name used by packages depending on this one. For + // example, when a user runs `zig fetch --save `, this field is used + // as the key in the `dependencies` table. Although the user can choose a + // different name, most users will stick with this provided value. + // + // It is redundant to include "zig" in this name because it is already + // within the Zig package namespace. + .name = .getopt, + + // This is a [Semantic Version](https://semver.org/). + // In a future version of Zig it will be used for package deduplication. + .version = "0.0.0", + + // Together with name, this represents a globally unique package + // identifier. This field is generated by the Zig toolchain when the + // package is first created, and then *never changes*. This allows + // unambiguous detection of one package being an updated version of + // another. + // + // When forking a Zig project, this id should be regenerated (delete the + // field and run `zig build`) if the upstream project is still maintained. + // Otherwise, the fork is *hostile*, attempting to take control over the + // original project's identity. Thus it is recommended to leave the comment + // on the following line intact, so that it shows up in code reviews that + // modify the field. + .fingerprint = 0x2a601179af5673be, + + // Tracks the earliest Zig version that the package considers to be a + // supported use case. + .minimum_zig_version = "0.14.1", + + // This field is optional. + // Each dependency must either provide a `url` and `hash`, or a `path`. + // `zig build --fetch` can be used to fetch all dependencies of a package, recursively. + // Once all dependencies are fetched, `zig build` no longer requires + // internet connectivity. + .dependencies = .{}, + .paths = .{ + "build.zig", + "build.zig.zon", + "example.zig", + "LICENSE", + "README.md", + "getopt.zig", + // For example... + //"LICENSE", + //"README.md", + }, +} diff --git a/getopt.zig b/getopt.zig index 0d68702..64a4351 100644 --- a/getopt.zig +++ b/getopt.zig @@ -4,7 +4,7 @@ const mem = std.mem; const os = std.os; const expect = std.testing.expect; -/// Parsed option struct. +/// Parsed option struct. pub const Option = struct { /// Option character. opt: u8, From 0bc70507782ce27c574a9929558a310b43343fe7 Mon Sep 17 00:00:00 2001 From: Femi Agbabiaka Date: Tue, 17 Jun 2025 00:11:29 -0500 Subject: [PATCH 2/2] Add cross-platform test targets --- build.zig | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/build.zig b/build.zig index 49a5eac..54092cd 100644 --- a/build.zig +++ b/build.zig @@ -1,5 +1,17 @@ const std = @import("std"); +const test_targets = [_]std.Target.Query{ + .{}, // native + .{ + .cpu_arch = .x86_64, + .os_tag = .linux, + }, + .{ + .cpu_arch = .aarch64, + .os_tag = .macos, + }, +}; + pub fn build(b: *std.Build) void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); @@ -29,9 +41,14 @@ pub fn build(b: *std.Build) void { }); b.installArtifact(exe); - const getopt_lib_unit_tests = b.addTest(.{ .root_module = getopt_lib_mod }); - const run_tests = b.addRunArtifact(getopt_lib_unit_tests); - const test_step = b.step("test", "Run tests"); - test_step.dependOn(&run_tests.step); + for (test_targets) |test_target| { + const unit_tests = b.addTest(.{ + .root_module = getopt_lib_mod, + .target = b.resolveTargetQuery(test_target), + }); + + const run_unit_tests = b.addRunArtifact(unit_tests); + test_step.dependOn(&run_unit_tests.step); + } }