authorgravatar for thejoshwolfe@gmail.comJosh Wolfe <thejoshwolfe@gmail.com> 2025-08-30 22:15:37-04:00
committergravatar for thejoshwolfe@gmail.comJosh Wolfe <thejoshwolfe@gmail.com> 2025-08-30 23:18:20-04:00
log69c1dbc9ff9f5031f7262d6079b83285f5fc0c86
treeae9f64d3910e9bb79ed4f5bf0334030ac09a4d67
parent473c1d6fa5bea6dcb03151a304af645910826be8

update tools/ to use std.cli.parse


21 files changed, 214 insertions(+), 398 deletions(-)

tools/docgen.zig+15-42
......@@ -16,16 +16,17 @@ const max_doc_file_size = 10 * 1024 * 1024;
1616
1717const obj_ext = builtin.object_format.fileExt(builtin.cpu.arch);
1818
19const usage =
20 \\Usage: docgen [options] input output
21 \\
22 \\ Generates an HTML document from a docgen template.
23 \\
24 \\Options:
25 \\ --code-dir dir Path to directory containing code example outputs
26 \\ -h, --help Print this help and exit
27 \\
28;
19const Args = struct {
20 pub const description = "Generates an HTML document from a docgen template.";
21 named: struct {
22 @"code-dir": [:0]const u8,
23 pub const @"code-dir_help" = "Path to directory containing code example outputs";
24 },
25 positional: struct {
26 input: [:0]const u8,
27 output: [:0]const u8,
28 },
29};
2930
3031pub fn main() !void {
3132 var arena_instance = std.heap.ArenaAllocator.init(std.heap.page_allocator);
......@@ -33,38 +34,10 @@ pub fn main() !void {
3334
3435 const arena = arena_instance.allocator();
3536
36 var args_it = try process.argsWithAllocator(arena);
37 if (!args_it.skip()) @panic("expected self arg");
38
39 var opt_code_dir: ?[]const u8 = null;
40 var opt_input: ?[]const u8 = null;
41 var opt_output: ?[]const u8 = null;
42
43 while (args_it.next()) |arg| {
44 if (mem.startsWith(u8, arg, "-")) {
45 if (mem.eql(u8, arg, "-h") or mem.eql(u8, arg, "--help")) {
46 try fs.File.stdout().writeAll(usage);
47 process.exit(0);
48 } else if (mem.eql(u8, arg, "--code-dir")) {
49 if (args_it.next()) |param| {
50 opt_code_dir = param;
51 } else {
52 fatal("expected parameter after --code-dir", .{});
53 }
54 } else {
55 fatal("unrecognized option: '{s}'", .{arg});
56 }
57 } else if (opt_input == null) {
58 opt_input = arg;
59 } else if (opt_output == null) {
60 opt_output = arg;
61 } else {
62 fatal("unexpected positional argument: '{s}'", .{arg});
63 }
64 }
65 const input_path = opt_input orelse fatal("missing input file", .{});
66 const output_path = opt_output orelse fatal("missing output file", .{});
67 const code_dir_path = opt_code_dir orelse fatal("missing --code-dir argument", .{});
37 const args = try std.cli.parse(Args, arena, .{});
38 const input_path = args.positional.input;
39 const output_path = args.positional.output;
40 const code_dir_path = args.named.@"code-dir";
6841
6942 var in_file = try fs.cwd().openFile(input_path, .{});
7043 defer in_file.close();
tools/dump-cov.zig+9-3
......@@ -16,9 +16,15 @@ pub fn main() !void {
1616 defer arena_instance.deinit();
1717 const arena = arena_instance.allocator();
1818
19 const args = try std.process.argsAlloc(arena);
20 const exe_file_name = args[1];
21 const cov_file_name = args[2];
19 const args = try std.cli.parse(struct {
20 named: struct {},
21 positional: struct {
22 exe_file: [:0]const u8,
23 cov_file: [:0]const u8,
24 },
25 }, arena, .{});
26 const exe_file_name = args.positional.exe_file;
27 const cov_file_name = args.positional.cov_file;
2228
2329 const exe_path: Path = .{
2430 .root_dir = std.Build.Cache.Directory.cwd(),
tools/fetch_them_macos_headers.zig+14-44
......@@ -55,36 +55,24 @@ const Target = struct {
5555
5656const headers_source_prefix: []const u8 = "headers";
5757
58const usage =
59 \\fetch_them_macos_headers [options] [cc args]
60 \\
61 \\Options:
62 \\ --sysroot Path to macOS SDK
63 \\
64 \\General Options:
65 \\-h, --help Print this help and exit
66;
58const Args = struct {
59 named: struct {
60 sysroot: []const u8 = "",
61 pub const sysroot_help = "Path to macOS SDK";
62 },
63 positional: struct {
64 cc_args: []const [:0]const u8 = &.{},
65 },
66};
6767
6868pub fn main() anyerror!void {
6969 var arena = std.heap.ArenaAllocator.init(gpa);
7070 defer arena.deinit();
7171 const allocator = arena.allocator();
7272
73 const args = try std.process.argsAlloc(allocator);
74
75 var argv = std.array_list.Managed([]const u8).init(allocator);
76 var sysroot: ?[]const u8 = null;
77
78 var args_iter = ArgsIterator{ .args = args[1..] };
79 while (args_iter.next()) |arg| {
80 if (mem.eql(u8, arg, "--help") or mem.eql(u8, arg, "-h")) {
81 return info(usage, .{});
82 } else if (mem.eql(u8, arg, "--sysroot")) {
83 sysroot = args_iter.nextOrFatal();
84 } else try argv.append(arg);
85 }
73 const args = try std.cli.parse(Args, allocator, .{});
8674
87 const sysroot_path = sysroot orelse blk: {
75 const sysroot_path = if (args.named.sysroot.len > 0) args.named.sysroot else blk: {
8876 const target = try std.zig.system.resolveTargetQuery(.{});
8977 break :blk std.zig.system.darwin.getSdk(allocator, &target) orelse
9078 fatal("no SDK found; you can provide one explicitly with '--sysroot' flag", .{});
......@@ -121,13 +109,13 @@ pub fn main() anyerror!void {
121109 .arch = arch,
122110 .os_ver = os_ver,
123111 };
124 try fetchTarget(allocator, argv.items, sysroot_path, target, version, tmp);
112 try fetchTarget(allocator, args.positional.cc_args, sysroot_path, target, version, tmp);
125113 }
126114}
127115
128116fn fetchTarget(
129117 arena: Allocator,
130 args: []const []const u8,
118 cc_args: []const []const u8,
131119 sysroot: []const u8,
132120 target: Target,
133121 ver: Version,
......@@ -165,7 +153,7 @@ fn fetchTarget(
165153 "-MF",
166154 headers_list_path,
167155 });
168 try cc_argv.appendSlice(args);
156 try cc_argv.appendSlice(cc_args);
169157
170158 const res = try std.process.Child.run(.{
171159 .allocator = arena,
......@@ -229,24 +217,6 @@ fn fetchTarget(
229217 }
230218}
231219
232const ArgsIterator = struct {
233 args: []const []const u8,
234 i: usize = 0,
235
236 fn next(it: *@This()) ?[]const u8 {
237 if (it.i >= it.args.len) {
238 return null;
239 }
240 defer it.i += 1;
241 return it.args[it.i];
242 }
243
244 fn nextOrFatal(it: *@This()) []const u8 {
245 const arg = it.next() orelse fatal("expected parameter after '{s}'", .{it.args[it.i - 1]});
246 return arg;
247 }
248};
249
250220const Version = struct {
251221 major: u16,
252222 minor: u8,
tools/gen_macos_headers_c.zig+6-20
......@@ -8,32 +8,18 @@ const Allocator = std.mem.Allocator;
88var general_purpose_allocator = std.heap.GeneralPurposeAllocator(.{}){};
99const gpa = general_purpose_allocator.allocator();
1010
11const usage =
12 \\gen_macos_headers_c [dir]
13 \\
14 \\General Options:
15 \\-h, --help Print this help and exit
16;
17
1811pub fn main() anyerror!void {
1912 var arena_allocator = std.heap.ArenaAllocator.init(gpa);
2013 defer arena_allocator.deinit();
2114 const arena = arena_allocator.allocator();
2215
23 const args = try std.process.argsAlloc(arena);
24 if (args.len == 1) fatal("no command or option specified", .{});
25
26 var positionals = std.array_list.Managed([]const u8).init(arena);
27
28 for (args[1..]) |arg| {
29 if (std.mem.eql(u8, arg, "--help") or std.mem.eql(u8, arg, "-h")) {
30 return info(usage, .{});
31 } else try positionals.append(arg);
32 }
33
34 if (positionals.items.len != 1) fatal("expected one positional argument: [dir]", .{});
16 const args = try std.cli.parse(struct {
17 positional: struct {
18 dir: []const u8,
19 },
20 }, arena, .{});
3521
36 var dir = try std.fs.cwd().openDir(positionals.items[0], .{ .no_follow = true });
22 var dir = try std.fs.cwd().openDir(args.positional.dir, .{ .no_follow = true });
3723 defer dir.close();
3824 var paths = std.array_list.Managed([]const u8).init(arena);
3925 try findHeaders(arena, dir, "", &paths);
tools/gen_outline_atomics.zig+1-1
......@@ -15,7 +15,7 @@ pub fn main() !void {
1515 defer arena_instance.deinit();
1616 const arena = arena_instance.allocator();
1717
18 //const args = try std.process.argsAlloc(arena);
18 _ = try std.cli.parse(struct {}, arena, .{});
1919
2020 var stdout_buffer: [2000]u8 = undefined;
2121 var stdout_writer = std.fs.File.stdout().writerStreaming(&stdout_buffer);
tools/gen_spirv_spec.zig+15-23
......@@ -58,12 +58,20 @@ const allocator = arena.allocator();
5858pub fn main() !void {
5959 defer arena.deinit();
6060
61 const args = try std.process.argsAlloc(allocator);
62 if (args.len != 3) {
63 usageAndExit(args[0], 1);
64 }
65
66 const json_path = try std.fs.path.join(allocator, &.{ args[1], "include/spirv/unified1/" });
61 const args = try std.cli.parse(struct {
62 pub const description =
63 \\Generates Zig bindings for SPIR-V specifications found in the SPIRV-Headers
64 \\repository. The result, printed to stdout, should be used to update
65 \\files in src/codegen/spirv. Don't forget to format the output.
66 ;
67 positional: struct {
68 pub const @"path/to/SPIRV-Headers_help" = "should point to a clone of https://github.com/KhronosGroup/SPIRV-Headers/";
69 @"path/to/SPIRV-Headers": [:0]const u8,
70 @"path/to/zig/src/codegen/spirv/extinst.zig.grammar.json": [:0]const u8,
71 },
72 }, allocator, .{});
73
74 const json_path = try std.fs.path.join(allocator, &.{ args.positional.@"path/to/SPIRV-Headers", "include/spirv/unified1/" });
6775 const dir = try std.fs.cwd().openDir(json_path, .{ .iterate = true });
6876
6977 const core_spec = try readRegistry(CoreRegistry, dir, "spirv.core.grammar.json");
......@@ -80,7 +88,7 @@ pub fn main() !void {
8088 try readExtRegistry(&exts, dir, entry.name);
8189 }
8290
83 try readExtRegistry(&exts, std.fs.cwd(), args[2]);
91 try readExtRegistry(&exts, std.fs.cwd(), args.positional.@"path/to/zig/src/codegen/spirv/extinst.zig.grammar.json");
8492
8593 var allocating: std.Io.Writer.Allocating = .init(allocator);
8694 defer allocating.deinit();
......@@ -929,19 +937,3 @@ fn parseHexInt(text: []const u8) !u31 {
929937 return error.InvalidHexInt;
930938 return try std.fmt.parseInt(u31, text[prefix.len..], 16);
931939}
932
933fn usageAndExit(arg0: []const u8, code: u8) noreturn {
934 const stderr = std.debug.lockStderrWriter(&.{});
935 stderr.print(
936 \\Usage: {s} <SPIRV-Headers repository path> <path/to/zig/src/codegen/spirv/extinst.zig.grammar.json>
937 \\
938 \\Generates Zig bindings for SPIR-V specifications found in the SPIRV-Headers
939 \\repository. The result, printed to stdout, should be used to update
940 \\files in src/codegen/spirv. Don't forget to format the output.
941 \\
942 \\<SPIRV-Headers repository path> should point to a clone of
943 \\https://github.com/KhronosGroup/SPIRV-Headers/
944 \\
945 , .{arg0}) catch std.process.exit(1);
946 std.process.exit(code);
947}
tools/gen_stubs.zig+6-2
......@@ -284,8 +284,12 @@ pub fn main() !void {
284284 defer arena_instance.deinit();
285285 const arena = arena_instance.allocator();
286286
287 const args = try std.process.argsAlloc(arena);
288 const build_all_path = args[1];
287 const args = try std.cli.parse(struct {
288 positional: struct {
289 build_all_path: [:0]const u8,
290 },
291 }, arena, .{});
292 const build_all_path = args.positional.build_all_path;
289293
290294 var build_all_dir = try std.fs.cwd().openDir(build_all_path, .{});
291295
tools/generate_JSONTestSuite.zig+5-1
......@@ -1,4 +1,6 @@
1// zig run this file inside the test_parsing/ directory of this repo: https://github.com/nst/JSONTestSuite
1const Args = struct {
2 pub const description = "zig run this file inside the test_parsing/ directory of this repo: https://github.com/nst/JSONTestSuite";
3};
24
35const std = @import("std");
46
......@@ -6,6 +8,8 @@ pub fn main() !void {
68 var gpa: std.heap.GeneralPurposeAllocator(.{}) = .init;
79 var allocator = gpa.allocator();
810
11 _ = try std.cli.parse(Args, allocator, .{});
12
913 var stdout_buffer: [2000]u8 = undefined;
1014 var stdout_writer = std.fs.File.stdout().writerStreaming(&stdout_buffer);
1115 const output = &stdout_writer.interface;
tools/generate_c_size_and_align_checks.zig+11-10
......@@ -25,21 +25,22 @@ fn cName(ty: std.Target.CType) []const u8 {
2525 };
2626}
2727
28var general_purpose_allocator: std.heap.GeneralPurposeAllocator(.{}) = .init;
29
3028pub fn main() !void {
31 const gpa = general_purpose_allocator.allocator();
29 var general_purpose_allocator: std.heap.GeneralPurposeAllocator(.{}) = .init;
3230 defer std.debug.assert(general_purpose_allocator.deinit() == .ok);
31 const gpa = general_purpose_allocator.allocator();
3332
34 const args = try std.process.argsAlloc(gpa);
35 defer std.process.argsFree(gpa, args);
33 var arena_instance = std.heap.ArenaAllocator.init(gpa);
34 defer arena_instance.deinit();
35 const arena = arena_instance.allocator();
3636
37 if (args.len != 2) {
38 std.debug.print("Usage: {s} [target_triple]\n", .{args[0]});
39 std.process.exit(1);
40 }
37 const args = try std.cli.parse(struct {
38 positional: struct {
39 target_triple: [:0]const u8,
40 },
41 }, arena, .{});
4142
42 const query = try std.Target.Query.parse(.{ .arch_os_abi = args[1] });
43 const query = try std.Target.Query.parse(.{ .arch_os_abi = args.positional.target_triple });
4344 const target = try std.zig.system.resolveTargetQuery(query);
4445
4546 var buffer: [2000]u8 = undefined;
tools/generate_linux_syscalls.zig+12-17
......@@ -11,6 +11,16 @@
1111//!
1212//! Everything after `name` is ignored for the purposes of this tool.
1313
14const Args = struct {
15 pub const description =
16 \\Generates the list of Linux syscalls for each supported cpu arch, using the Linux development tree.
17 \\Prints to stdout Zig code which you can use to replace the file lib/std/os/linux/syscalls.zig.
18 ;
19 positional: struct {
20 @"/path/to/linux": [:0]const u8,
21 },
22};
23
1424const std = @import("std");
1525const Io = std.Io;
1626const mem = std.mem;
......@@ -175,12 +185,8 @@ pub fn main() !void {
175185 defer arena.deinit();
176186 const gpa = arena.allocator();
177187
178 const args = try std.process.argsAlloc(gpa);
179 if (args.len < 2 or mem.eql(u8, args[1], "--help")) {
180 usage(std.debug.lockStderrWriter(&.{}), args[0]) catch std.process.exit(2);
181 std.process.exit(1);
182 }
183 const linux_path = args[1];
188 const args = try std.cli.parse(Args, gpa, .{});
189 const linux_path = args.positional.@"/path/to/linux";
184190
185191 var stdout_buffer: [2048]u8 = undefined;
186192 var stdout_writer = std.fs.File.stdout().writerStreaming(&stdout_buffer);
......@@ -247,14 +253,3 @@ pub fn main() !void {
247253
248254 try Io.Writer.flush(stdout);
249255}
250
251fn usage(w: *std.Io.Writer, arg0: []const u8) std.Io.Writer.Error!void {
252 try w.print(
253 \\Usage: {s} /path/to/zig /path/to/linux
254 \\Alternative Usage: zig run /path/to/git/zig/tools/generate_linux_syscalls.zig -- /path/to/zig /path/to/linux
255 \\
256 \\Generates the list of Linux syscalls for each supported cpu arch, using the Linux development tree.
257 \\Prints to stdout Zig code which you can use to replace the file lib/std/os/linux/syscalls.zig.
258 \\
259 , .{arg0});
260}
tools/incr-check.zig+23-42
......@@ -2,8 +2,6 @@ const std = @import("std");
22const Allocator = std.mem.Allocator;
33const Cache = std.Build.Cache;
44
5const usage = "usage: incr-check <zig binary path> <input file> [--zig-lib-dir lib] [--debug-zcu] [--debug-dwarf] [--debug-link] [--preserve-tmp] [--zig-cc-binary /path/to/zig]";
6
75pub fn main() !void {
86 const fatal = std.process.fatal;
97
......@@ -11,46 +9,29 @@ pub fn main() !void {
119 defer arena_instance.deinit();
1210 const arena = arena_instance.allocator();
1311
14 var opt_zig_exe: ?[]const u8 = null;
15 var opt_input_file_name: ?[]const u8 = null;
16 var opt_lib_dir: ?[]const u8 = null;
17 var opt_cc_zig: ?[]const u8 = null;
18 var debug_zcu = false;
19 var debug_dwarf = false;
20 var debug_link = false;
21 var preserve_tmp = false;
22
23 var arg_it = try std.process.argsWithAllocator(arena);
24 _ = arg_it.skip();
25 while (arg_it.next()) |arg| {
26 if (arg.len > 0 and arg[0] == '-') {
27 if (std.mem.eql(u8, arg, "--zig-lib-dir")) {
28 opt_lib_dir = arg_it.next() orelse fatal("expected arg after '--zig-lib-dir'\n{s}", .{usage});
29 } else if (std.mem.eql(u8, arg, "--debug-zcu")) {
30 debug_zcu = true;
31 } else if (std.mem.eql(u8, arg, "--debug-dwarf")) {
32 debug_dwarf = true;
33 } else if (std.mem.eql(u8, arg, "--debug-link")) {
34 debug_link = true;
35 } else if (std.mem.eql(u8, arg, "--preserve-tmp")) {
36 preserve_tmp = true;
37 } else if (std.mem.eql(u8, arg, "--zig-cc-binary")) {
38 opt_cc_zig = arg_it.next() orelse fatal("expect arg after '--zig-cc-binary'\n{s}", .{usage});
39 } else {
40 fatal("unknown option '{s}'\n{s}", .{ arg, usage });
41 }
42 continue;
43 }
44 if (opt_zig_exe == null) {
45 opt_zig_exe = arg;
46 } else if (opt_input_file_name == null) {
47 opt_input_file_name = arg;
48 } else {
49 fatal("unknown argument '{s}'\n{s}", .{ arg, usage });
50 }
51 }
52 const zig_exe = opt_zig_exe orelse fatal("missing path to zig\n{s}", .{usage});
53 const input_file_name = opt_input_file_name orelse fatal("missing input file\n{s}", .{usage});
12 const args = try std.cli.parse(struct {
13 positional: struct {
14 @"zig-binary-path": []const u8,
15 @"input-file": []const u8,
16 },
17 named: struct {
18 @"zig-lib-dir": []const u8 = "",
19 @"debug-zcu": bool = false,
20 @"debug-dwarf": bool = false,
21 @"debug-link": bool = false,
22 preserve_tmp: bool = false,
23 @"zig-cc-binary": []const u8 = "",
24 },
25 }, arena, .{});
26
27 const opt_lib_dir: ?[]const u8 = if (args.named.@"zig-lib-dir".len > 0) args.named.@"zig-lib-dir" else null;
28 const opt_cc_zig: ?[]const u8 = if (args.named.@"zig-cc-binary".len > 0) args.named.@"zig-cc-binary" else null;
29 const debug_zcu = args.named.@"debug-zcu";
30 const debug_dwarf = args.named.@"debug-dwarf";
31 const debug_link = args.named.@"debug-link";
32 const preserve_tmp = args.named.preserve_tmp;
33 const zig_exe = args.positional.@"zig-binary-path";
34 const input_file_name = args.positional.@"input-file";
5435
5536 const input_file_bytes = try std.fs.cwd().readFileAlloc(input_file_name, arena, .limited(std.math.maxInt(u32)));
5637 const case = try Case.parse(arena, input_file_bytes);
tools/migrate_langref.zig+8-3
......@@ -13,9 +13,14 @@ pub fn main() !void {
1313 defer arena_instance.deinit();
1414 const arena = arena_instance.allocator();
1515
16 const args = try std.process.argsAlloc(arena);
17 const input_file = args[1];
18 const output_file = args[2];
16 const args = try std.cli.parse(struct {
17 positional: struct {
18 input_file: [:0]const u8,
19 output_file: [:0]const u8,
20 },
21 }, arena, .{});
22 const input_file = args.positional.input_file;
23 const output_file = args.positional.output_file;
1924
2025 var in_file = try fs.cwd().openFile(input_file, .{ .mode = .read_only });
2126 defer in_file.close();
tools/process_headers.zig+14-51
......@@ -119,52 +119,24 @@ const HashToContents = std.StringHashMap(Contents);
119119const TargetToHash = std.StringArrayHashMap([]const u8);
120120const PathTable = std.StringHashMap(*TargetToHash);
121121
122const LibCVendor = enum {
123 musl,
124 glibc,
125 freebsd,
126 netbsd,
127};
128
129122pub fn main() !void {
130123 var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
131124 const allocator = arena.allocator();
132 const args = try std.process.argsAlloc(allocator);
133 var search_paths = std.array_list.Managed([]const u8).init(allocator);
134 var opt_out_dir: ?[]const u8 = null;
135 var opt_abi: ?[]const u8 = null;
136125
137 var arg_i: usize = 1;
138 while (arg_i < args.len) : (arg_i += 1) {
139 if (std.mem.eql(u8, args[arg_i], "--help"))
140 usageAndExit(args[0]);
141 if (arg_i + 1 >= args.len) {
142 std.debug.print("expected argument after '{s}'\n", .{args[arg_i]});
143 usageAndExit(args[0]);
144 }
126 const args = try std.cli.parse(struct {
127 named: struct {
128 @"search-path": []const []const u8 = &.{},
129 out: []const u8,
130 abi: enum { musl, glibc, freebsd, netbsd },
145131
146 if (std.mem.eql(u8, args[arg_i], "--search-path")) {
147 try search_paths.append(args[arg_i + 1]);
148 } else if (std.mem.eql(u8, args[arg_i], "--out")) {
149 assert(opt_out_dir == null);
150 opt_out_dir = args[arg_i + 1];
151 } else if (std.mem.eql(u8, args[arg_i], "--abi")) {
152 assert(opt_abi == null);
153 opt_abi = args[arg_i + 1];
154 } else {
155 std.debug.print("unrecognized argument: {s}\n", .{args[arg_i]});
156 usageAndExit(args[0]);
157 }
158
159 arg_i += 1;
160 }
161
162 const out_dir = opt_out_dir orelse usageAndExit(args[0]);
163 const abi_name = opt_abi orelse usageAndExit(args[0]);
164 const vendor = std.meta.stringToEnum(LibCVendor, abi_name) orelse {
165 std.debug.print("unrecognized C ABI: {s}\n", .{abi_name});
166 usageAndExit(args[0]);
167 };
132 pub const @"search-path_help" = "subdirectories of search paths look like, e.g. x86_64-linux-gnu";
133 pub const out_help = "a dir that will be created, and populated with the results";
134 },
135 }, allocator, .{});
136 const search_paths = args.named.@"search-path";
137 const out_dir = args.named.out;
138 const vendor = args.named.abi;
139 const abi_name = @tagName(vendor);
168140
169141 const generic_name = try std.fmt.allocPrint(allocator, "generic-{s}", .{abi_name});
170142 const libc_targets = switch (vendor) {
......@@ -225,7 +197,7 @@ pub fn main() !void {
225197 @tagName(libc_target.abi),
226198 });
227199
228 search: for (search_paths.items) |search_path| {
200 search: for (search_paths) |search_path| {
229201 const sub_path = switch (vendor) {
230202 .glibc,
231203 .freebsd,
......@@ -362,12 +334,3 @@ pub fn main() !void {
362334 }
363335 }
364336}
365
366fn usageAndExit(arg0: []const u8) noreturn {
367 std.debug.print("Usage: {s} [--search-path <dir>] --out <dir> --abi <name>\n", .{arg0});
368 std.debug.print("--search-path can be used any number of times.\n", .{});
369 std.debug.print(" subdirectories of search paths look like, e.g. x86_64-linux-gnu\n", .{});
370 std.debug.print("--out is a dir that will be created, and populated with the results\n", .{});
371 std.debug.print("--abi is either glibc, musl, freebsd, or netbsd\n", .{});
372 std.process.exit(1);
373}
tools/update-linux-headers.zig+11-34
......@@ -142,33 +142,18 @@ const PathTable = std.StringHashMap(*TargetToHash);
142142pub fn main() !void {
143143 var arena_state = std.heap.ArenaAllocator.init(std.heap.page_allocator);
144144 const arena = arena_state.allocator();
145 const args = try std.process.argsAlloc(arena);
146 var search_paths = std.array_list.Managed([]const u8).init(arena);
147 var opt_out_dir: ?[]const u8 = null;
148145
149 var arg_i: usize = 1;
150 while (arg_i < args.len) : (arg_i += 1) {
151 if (std.mem.eql(u8, args[arg_i], "--help"))
152 usageAndExit(args[0]);
153 if (arg_i + 1 >= args.len) {
154 std.debug.print("expected argument after '{s}'\n", .{args[arg_i]});
155 usageAndExit(args[0]);
156 }
157
158 if (std.mem.eql(u8, args[arg_i], "--search-path")) {
159 try search_paths.append(args[arg_i + 1]);
160 } else if (std.mem.eql(u8, args[arg_i], "--out")) {
161 assert(opt_out_dir == null);
162 opt_out_dir = args[arg_i + 1];
163 } else {
164 std.debug.print("unrecognized argument: {s}\n", .{args[arg_i]});
165 usageAndExit(args[0]);
166 }
146 const args = try std.cli.parse(struct {
147 named: struct {
148 @"search-path": []const []const u8 = &.{},
149 out: []const u8,
167150
168 arg_i += 1;
169 }
170
171 const out_dir = opt_out_dir orelse usageAndExit(args[0]);
151 pub const @"search-path_help" = "subdirectories of search paths look like, e.g. x86_64-linux-gnu";
152 pub const out_help = "a dir that will be created, and populated with the results";
153 },
154 }, arena, .{});
155 const search_paths = args.named.@"search-path";
156 const out_dir = args.named.out;
172157 const generic_name = "any-linux-any";
173158
174159 var path_table = PathTable.init(arena);
......@@ -182,7 +167,7 @@ pub fn main() !void {
182167 const dest_target = DestTarget{
183168 .arch = linux_target.arch,
184169 };
185 search: for (search_paths.items) |search_path| {
170 search: for (search_paths) |search_path| {
186171 const target_include_dir = try std.fs.path.join(arena, &.{
187172 search_path, linux_target.name, "include",
188173 });
......@@ -320,11 +305,3 @@ pub fn main() !void {
320305 try std.fs.cwd().deleteFile(full_path);
321306 }
322307}
323
324fn usageAndExit(arg0: []const u8) noreturn {
325 std.debug.print("Usage: {s} [--search-path <dir>] --out <dir> --abi <name>\n", .{arg0});
326 std.debug.print("--search-path can be used any number of times.\n", .{});
327 std.debug.print(" subdirectories of search paths look like, e.g. x86_64-linux-gnu\n", .{});
328 std.debug.print("--out is a dir that will be created, and populated with the results\n", .{});
329 std.process.exit(1);
330}
tools/update_clang_options.zig+9-31
......@@ -630,29 +630,22 @@ const cpu_targets = struct {
630630pub fn main() anyerror!void {
631631 var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
632632 defer arena.deinit();
633
634633 const allocator = arena.allocator();
635 const args = try std.process.argsAlloc(allocator);
636634
637635 var stdout_buffer: [4000]u8 = undefined;
638636 var stdout_writer = fs.File.stdout().writerStreaming(&stdout_buffer);
639637 const stdout = &stdout_writer.interface;
640638
641 if (args.len <= 1) printUsageAndExit(args[0]);
642
643 if (std.mem.eql(u8, args[1], "--help")) {
644 printUsage(stdout, args[0]) catch std.process.exit(2);
645 stdout.flush() catch std.process.exit(2);
646 std.process.exit(0);
647 }
648
649 if (args.len < 3) printUsageAndExit(args[0]);
650
651 const llvm_tblgen_exe = args[1];
652 if (std.mem.startsWith(u8, llvm_tblgen_exe, "-")) printUsageAndExit(args[0]);
639 const args = try std.cli.parse(struct {
640 pub const description = "Prints to stdout Zig code which you can use to replace the file src/clang_options_data.zig.";
641 positional: struct {
642 @"/path/to/llvm-tblgen": [:0]const u8,
643 @"/path/to/git/llvm/llvm-project": [:0]const u8,
644 },
645 }, allocator, .{});
653646
654 const llvm_src_root = args[2];
655 if (std.mem.startsWith(u8, llvm_src_root, "-")) printUsageAndExit(args[0]);
647 const llvm_tblgen_exe = args.positional.@"/path/to/llvm-tblgen";
648 const llvm_src_root = args.positional.@"/path/to/git/llvm/llvm-project";
656649
657650 var llvm_to_zig_cpu_features = std.StringHashMap([]const u8).init(allocator);
658651
......@@ -959,18 +952,3 @@ fn objectLessThan(context: void, a: *json.ObjectMap, b: *json.ObjectMap) bool {
959952 const b_key = b.get("!name").?.string;
960953 return std.mem.lessThan(u8, a_key, b_key);
961954}
962
963fn printUsageAndExit(arg0: []const u8) noreturn {
964 printUsage(std.debug.lockStderrWriter(&.{}), arg0) catch std.process.exit(2);
965 std.process.exit(1);
966}
967
968fn printUsage(w: *std.Io.Writer, arg0: []const u8) std.Io.Writer.Error!void {
969 try w.print(
970 \\Usage: {s} /path/to/llvm-tblgen /path/to/git/llvm/llvm-project
971 \\Alternative Usage: zig run /path/to/git/zig/tools/update_clang_options.zig -- /path/to/llvm-tblgen /path/to/git/llvm/llvm-project
972 \\
973 \\Prints to stdout Zig code which you can use to replace the file src/clang_options_data.zig.
974 \\
975 , .{arg0});
976}
tools/update_cpu_features.zig+17-45
......@@ -1567,38 +1567,23 @@ pub fn main() anyerror!void {
15671567 defer arena_state.deinit();
15681568 const arena = arena_state.allocator();
15691569
1570 var args = try std.process.argsWithAllocator(arena);
1571 const args0 = args.next().?;
1572
1573 const llvm_tblgen_exe = args.next() orelse
1574 usageAndExit(args0, 1);
1575
1576 if (std.mem.eql(u8, llvm_tblgen_exe, "--help")) {
1577 usageAndExit(args0, 0);
1578 }
1579 if (std.mem.startsWith(u8, llvm_tblgen_exe, "-")) {
1580 usageAndExit(args0, 1);
1581 }
1582
1583 const llvm_src_root = args.next() orelse
1584 usageAndExit(args0, 1);
1585
1586 if (std.mem.startsWith(u8, llvm_src_root, "-")) {
1587 usageAndExit(args0, 1);
1588 }
1589
1590 const zig_src_root = args.next() orelse
1591 usageAndExit(args0, 1);
1592
1593 if (std.mem.startsWith(u8, zig_src_root, "-")) {
1594 usageAndExit(args0, 1);
1595 }
1596
1597 var filter: ?[]const u8 = null;
1598 if (args.next()) |arg| filter = arg;
1599
1600 // there shouldn't be any more argument after the optional filter
1601 if (args.skip()) usageAndExit(args0, 1);
1570 const args = try std.cli.parse(struct {
1571 pub const description =
1572 \\Updates lib/std/target/<target>.zig from llvm/lib/Target/<Target>/<Target>.td .
1573 \\
1574 \\On a less beefy system, or when debugging, compile with -fsingle-threaded.
1575 ;
1576 positional: struct {
1577 @"/path/to/llvm-tblgen": [:0]const u8,
1578 @"/path/git/llvm-project": [:0]const u8,
1579 @"/path/git/zig": [:0]const u8,
1580 zig_name_filter: []const u8 = "",
1581 },
1582 }, arena, .{});
1583 const llvm_tblgen_exe = args.positional.@"/path/to/llvm-tblgen";
1584 const llvm_src_root = args.positional.@"/path/git/llvm-project";
1585 const zig_src_root = args.positional.@"/path/git/zig";
1586 const filter: ?[]const u8 = if (args.positional.zig_name_filter.len > 0) args.positional.zig_name_filter else null;
16021587
16031588 var zig_src_dir = try fs.cwd().openDir(zig_src_root, .{});
16041589 defer zig_src_dir.close();
......@@ -2104,19 +2089,6 @@ fn processOneTarget(job: Job) void {
21042089 render_progress.end();
21052090}
21062091
2107fn usageAndExit(arg0: []const u8, code: u8) noreturn {
2108 const stderr = std.debug.lockStderrWriter(&.{});
2109 stderr.print(
2110 \\Usage: {s} /path/to/llvm-tblgen /path/git/llvm-project /path/git/zig [zig_name filter]
2111 \\
2112 \\Updates lib/std/target/<target>.zig from llvm/lib/Target/<Target>/<Target>.td .
2113 \\
2114 \\On a less beefy system, or when debugging, compile with -fsingle-threaded.
2115 \\
2116 , .{arg0}) catch std.process.exit(1);
2117 std.process.exit(code);
2118}
2119
21202092fn featureLessThan(_: void, a: Feature, b: Feature) bool {
21212093 return std.ascii.lessThanIgnoreCase(a.zig_name, b.zig_name);
21222094}
tools/update_crc_catalog.zig+6-17
......@@ -10,11 +10,12 @@ pub fn main() anyerror!void {
1010 defer arena_state.deinit();
1111 const arena = arena_state.allocator();
1212
13 const args = try std.process.argsAlloc(arena);
14 if (args.len <= 1) printUsageAndExit(args[0]);
15
16 const zig_src_root = args[1];
17 if (mem.startsWith(u8, zig_src_root, "-")) printUsageAndExit(args[0]);
13 const args = try std.cli.parse(struct {
14 positional: struct {
15 @"/path/git/zig": [:0]const u8,
16 },
17 }, arena, .{});
18 const zig_src_root = args.positional.@"/path/git/zig";
1819
1920 var zig_src_dir = try fs.cwd().openDir(zig_src_root, .{});
2021 defer zig_src_dir.close();
......@@ -188,15 +189,3 @@ pub fn main() anyerror!void {
188189 try code_writer.flush();
189190 try test_writer.flush();
190191}
191
192fn printUsageAndExit(arg0: []const u8) noreturn {
193 printUsage(std.debug.lockStderrWriter(&.{}), arg0) catch std.process.exit(2);
194 std.process.exit(1);
195}
196
197fn printUsage(w: *std.Io.Writer, arg0: []const u8) std.Io.Writer.Error!void {
198 return w.print(
199 \\Usage: {s} /path/git/zig
200 \\
201 , .{arg0});
202}
tools/update_freebsd_libc.zig+8-3
......@@ -16,9 +16,14 @@ pub fn main() !void {
1616 defer arena_instance.deinit();
1717 const arena = arena_instance.allocator();
1818
19 const args = try std.process.argsAlloc(arena);
20 const freebsd_src_path = args[1];
21 const zig_src_path = args[2];
19 const args = try std.cli.parse(struct {
20 positional: struct {
21 freebsd_src_path: [:0]const u8,
22 zig_src_path: [:0]const u8,
23 },
24 }, arena, .{});
25 const freebsd_src_path = args.positional.freebsd_src_path;
26 const zig_src_path = args.positional.zig_src_path;
2227
2328 const dest_dir_path = try std.fmt.allocPrint(arena, "{s}/lib/libc/freebsd", .{zig_src_path});
2429
tools/update_glibc.zig+8-3
......@@ -41,9 +41,14 @@ pub fn main() !void {
4141 defer arena_instance.deinit();
4242 const arena = arena_instance.allocator();
4343
44 const args = try std.process.argsAlloc(arena);
45 const glibc_src_path = args[1];
46 const zig_src_path = args[2];
44 const args = try std.cli.parse(struct {
45 positional: struct {
46 glibc_src_path: [:0]const u8,
47 zig_src_path: [:0]const u8,
48 },
49 }, arena, .{});
50 const glibc_src_path = args.positional.glibc_src_path;
51 const zig_src_path = args.positional.zig_src_path;
4752
4853 const dest_dir_path = try std.fmt.allocPrint(arena, "{s}/lib/libc/glibc", .{zig_src_path});
4954
tools/update_mingw.zig+8-3
......@@ -5,9 +5,14 @@ pub fn main() !void {
55 defer arena_instance.deinit();
66 const arena = arena_instance.allocator();
77
8 const args = try std.process.argsAlloc(arena);
9 const zig_src_lib_path = args[1];
10 const mingw_src_path = args[2];
8 const args = try std.cli.parse(struct {
9 positional: struct {
10 zig_src_lib_path: [:0]const u8,
11 mingw_src_path: [:0]const u8,
12 },
13 }, arena, .{});
14 const zig_src_lib_path = args.positional.zig_src_lib_path;
15 const mingw_src_path = args.positional.mingw_src_path;
1116
1217 const dest_mingw_crt_path = try std.fs.path.join(arena, &.{
1318 zig_src_lib_path, "libc", "mingw",
tools/update_netbsd_libc.zig+8-3
......@@ -16,9 +16,14 @@ pub fn main() !void {
1616 defer arena_instance.deinit();
1717 const arena = arena_instance.allocator();
1818
19 const args = try std.process.argsAlloc(arena);
20 const netbsd_src_path = args[1];
21 const zig_src_path = args[2];
19 const args = try std.cli.parse(struct {
20 positional: struct {
21 netbsd_src_path: [:0]const u8,
22 zig_src_path: [:0]const u8,
23 },
24 }, arena, .{});
25 const netbsd_src_path = args.positional.netbsd_src_path;
26 const zig_src_path = args.positional.zig_src_path;
2227
2328 const dest_dir_path = try std.fmt.allocPrint(arena, "{s}/lib/libc/netbsd", .{zig_src_path});
2429