Skip to content

Commit e485cd0

Browse files
committed
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).
1 parent 51a2d30 commit e485cd0

File tree

4 files changed

+79
-15
lines changed

4 files changed

+79
-15
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
/zig-cache/
1+
.zig-cache/
2+
zig-out/*

build.zig

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,36 @@
11
const std = @import("std");
22

3-
pub fn build(b: *std.build.Builder) void {
3+
pub fn build(b: *std.Build) void {
44
const target = b.standardTargetOptions(.{});
55
const optimize = b.standardOptimizeOption(.{});
66

7-
_ = b.addModule("getopt", .{
8-
.source_file = .{
9-
.path = "getopt.zig",
10-
},
7+
const getopt_lib_mod = b.createModule(.{
8+
.root_source_file = b.path("getopt.zig"),
9+
.target = target,
10+
.optimize = optimize,
1111
});
1212

13-
const tests = b.addTest(.{
14-
.root_source_file = .{ .path = "getopt.zig" },
13+
const getopt_example_exe_mod = b.createModule(.{
14+
.root_source_file = b.path("example.zig"),
1515
.target = target,
1616
.optimize = optimize,
1717
});
18-
const run_tests = b.addRunArtifact(tests);
18+
19+
const lib = b.addLibrary(.{
20+
.linkage = .static,
21+
.name = "getopt",
22+
.root_module = getopt_lib_mod,
23+
});
24+
b.installArtifact(lib);
25+
26+
const exe = b.addExecutable(.{
27+
.name = "example",
28+
.root_module = getopt_example_exe_mod,
29+
});
30+
b.installArtifact(exe);
31+
32+
const getopt_lib_unit_tests = b.addTest(.{ .root_module = getopt_lib_mod });
33+
const run_tests = b.addRunArtifact(getopt_lib_unit_tests);
1934

2035
const test_step = b.step("test", "Run tests");
2136
test_step.dependOn(&run_tests.step);

build.zig.zon

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
.{
2+
// This is the default name used by packages depending on this one. For
3+
// example, when a user runs `zig fetch --save <url>`, this field is used
4+
// as the key in the `dependencies` table. Although the user can choose a
5+
// different name, most users will stick with this provided value.
6+
//
7+
// It is redundant to include "zig" in this name because it is already
8+
// within the Zig package namespace.
9+
.name = .getopt,
10+
11+
// This is a [Semantic Version](https://semver.org/).
12+
// In a future version of Zig it will be used for package deduplication.
13+
.version = "0.0.0",
14+
15+
// Together with name, this represents a globally unique package
16+
// identifier. This field is generated by the Zig toolchain when the
17+
// package is first created, and then *never changes*. This allows
18+
// unambiguous detection of one package being an updated version of
19+
// another.
20+
//
21+
// When forking a Zig project, this id should be regenerated (delete the
22+
// field and run `zig build`) if the upstream project is still maintained.
23+
// Otherwise, the fork is *hostile*, attempting to take control over the
24+
// original project's identity. Thus it is recommended to leave the comment
25+
// on the following line intact, so that it shows up in code reviews that
26+
// modify the field.
27+
.fingerprint = 0x2a601179af5673be,
28+
29+
// Tracks the earliest Zig version that the package considers to be a
30+
// supported use case.
31+
.minimum_zig_version = "0.14.1",
32+
33+
// This field is optional.
34+
// Each dependency must either provide a `url` and `hash`, or a `path`.
35+
// `zig build --fetch` can be used to fetch all dependencies of a package, recursively.
36+
// Once all dependencies are fetched, `zig build` no longer requires
37+
// internet connectivity.
38+
.dependencies = .{},
39+
.paths = .{
40+
"build.zig",
41+
"build.zig.zon",
42+
"example.zig",
43+
"LICENSE",
44+
"README.md",
45+
"getopt.zig",
46+
// For example...
47+
//"LICENSE",
48+
//"README.md",
49+
},
50+
}

getopt.zig

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub const Option = struct {
1616
pub const Error = error{ InvalidOption, MissingArgument };
1717

1818
pub const OptionsIterator = struct {
19-
argv: [][*:0]const u8,
19+
argv: [][*:0]u8,
2020
opts: []const u8,
2121

2222
/// Index of the current element of the argv vector.
@@ -75,15 +75,15 @@ pub const OptionsIterator = struct {
7575
}
7676

7777
/// Return remaining arguments, if any.
78-
pub fn args(self: *OptionsIterator) ?[][*:0]const u8 {
78+
pub fn args(self: *OptionsIterator) ?[][*:0]u8 {
7979
if (self.optind < self.argv.len)
8080
return self.argv[self.optind..]
8181
else
8282
return null;
8383
}
8484
};
8585

86-
fn getoptArgv(argv: [][*:0]const u8, optstring: []const u8) OptionsIterator {
86+
fn getoptArgv(argv: [][*:0]u8, optstring: []const u8) OptionsIterator {
8787
return OptionsIterator{
8888
.argv = argv,
8989
.opts = optstring,
@@ -92,9 +92,7 @@ fn getoptArgv(argv: [][*:0]const u8, optstring: []const u8) OptionsIterator {
9292

9393
/// Parse os.argv according to the optstring.
9494
pub fn getopt(optstring: []const u8) OptionsIterator {
95-
// https://github.com/ziglang/zig/issues/8808
96-
const argv: [][*:0]const u8 = os.argv;
97-
return getoptArgv(argv, optstring);
95+
return getoptArgv(os.argv, optstring);
9896
}
9997

10098
test "no args separate" {

0 commit comments

Comments
 (0)