Skip to content

Commit b4d58e9

Browse files
committed
make docgen accept --zig-lib-dir
1 parent b3af5d0 commit b4d58e9

File tree

2 files changed

+64
-26
lines changed

2 files changed

+64
-26
lines changed

build.zig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ pub fn build(b: *std.Build) !void {
4747

4848
const docgen_cmd = b.addRunArtifact(docgen_exe);
4949
docgen_cmd.addArgs(&.{ "--zig", b.zig_exe });
50+
if (b.zig_lib_dir) |p| {
51+
docgen_cmd.addArgs(&.{ "--zig-lib-dir", p });
52+
}
5053
docgen_cmd.addFileSourceArg(.{ .path = "doc/langref.html.in" });
5154
const langref_file = docgen_cmd.addOutputFileArg("langref.html");
5255
const install_langref = b.addInstallFileWithDir(langref_file, .prefix, "doc/langref.html");

doc/docgen.zig

Lines changed: 61 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ const usage =
2828
\\
2929
;
3030

31-
fn errorf(comptime format: []const u8, args: anytype) noreturn {
31+
fn fatal(comptime format: []const u8, args: anytype) noreturn {
3232
const stderr = io.getStdErr().writer();
3333

34-
stderr.print("error: " ++ format, args) catch {};
34+
stderr.print("error: " ++ format ++ "\n", args) catch {};
3535
process.exit(1);
3636
}
3737

@@ -45,6 +45,7 @@ pub fn main() !void {
4545
if (!args_it.skip()) @panic("expected self arg");
4646

4747
var zig_exe: []const u8 = "zig";
48+
var opt_zig_lib_dir: ?[]const u8 = null;
4849
var do_code_tests = true;
4950
var files = [_][]const u8{ "", "" };
5051

@@ -59,24 +60,29 @@ pub fn main() !void {
5960
if (args_it.next()) |param| {
6061
zig_exe = param;
6162
} else {
62-
errorf("expected parameter after --zig\n", .{});
63+
fatal("expected parameter after --zig", .{});
64+
}
65+
} else if (mem.eql(u8, arg, "--zig-lib-dir")) {
66+
if (args_it.next()) |param| {
67+
opt_zig_lib_dir = param;
68+
} else {
69+
fatal("expected parameter after --zig-lib-dir", .{});
6370
}
6471
} else if (mem.eql(u8, arg, "--skip-code-tests")) {
6572
do_code_tests = false;
6673
} else {
67-
errorf("unrecognized option: '{s}'\n", .{arg});
74+
fatal("unrecognized option: '{s}'", .{arg});
6875
}
6976
} else {
7077
if (i > 1) {
71-
errorf("too many arguments\n", .{});
78+
fatal("too many arguments", .{});
7279
}
7380
files[i] = arg;
7481
i += 1;
7582
}
7683
}
7784
if (i < 2) {
78-
errorf("not enough arguments\n", .{});
79-
process.exit(1);
85+
fatal("not enough arguments", .{});
8086
}
8187

8288
var in_file = try fs.cwd().openFile(files[0], .{ .mode = .read_only });
@@ -95,7 +101,7 @@ pub fn main() !void {
95101
try fs.cwd().makePath(tmp_dir_name);
96102
defer fs.cwd().deleteTree(tmp_dir_name) catch {};
97103

98-
try genHtml(allocator, &tokenizer, &toc, buffered_writer.writer(), zig_exe, do_code_tests);
104+
try genHtml(allocator, &tokenizer, &toc, buffered_writer.writer(), zig_exe, opt_zig_lib_dir, do_code_tests);
99105
try buffered_writer.flush();
100106
}
101107

@@ -1268,6 +1274,7 @@ fn genHtml(
12681274
toc: *Toc,
12691275
out: anytype,
12701276
zig_exe: []const u8,
1277+
opt_zig_lib_dir: ?[]const u8,
12711278
do_code_tests: bool,
12721279
) !void {
12731280
var progress = Progress{ .dont_print_on_dumb = true };
@@ -1278,7 +1285,7 @@ fn genHtml(
12781285
try env_map.put("ZIG_DEBUG_COLOR", "1");
12791286

12801287
const host = try std.zig.system.NativeTargetInfo.detect(.{});
1281-
const builtin_code = try getBuiltinCode(allocator, &env_map, zig_exe);
1288+
const builtin_code = try getBuiltinCode(allocator, &env_map, zig_exe, opt_zig_lib_dir);
12821289

12831290
for (toc.nodes) |node| {
12841291
defer root_node.completeOne();
@@ -1370,6 +1377,9 @@ fn genHtml(
13701377
"--color", "on",
13711378
"--enable-cache", tmp_source_file_name,
13721379
});
1380+
if (opt_zig_lib_dir) |zig_lib_dir| {
1381+
try build_args.appendSlice(&.{ "--zig-lib-dir", zig_lib_dir });
1382+
}
13731383

13741384
try shell_out.print("$ zig build-exe {s} ", .{name_plus_ext});
13751385

@@ -1512,8 +1522,12 @@ fn genHtml(
15121522
defer test_args.deinit();
15131523

15141524
try test_args.appendSlice(&[_][]const u8{
1515-
zig_exe, "test", tmp_source_file_name,
1525+
zig_exe, "test",
1526+
tmp_source_file_name,
15161527
});
1528+
if (opt_zig_lib_dir) |zig_lib_dir| {
1529+
try test_args.appendSlice(&.{ "--zig-lib-dir", zig_lib_dir });
1530+
}
15171531
try shell_out.print("$ zig test {s}.zig ", .{code.name});
15181532

15191533
switch (code.mode) {
@@ -1564,12 +1578,13 @@ fn genHtml(
15641578
defer test_args.deinit();
15651579

15661580
try test_args.appendSlice(&[_][]const u8{
1567-
zig_exe,
1568-
"test",
1569-
"--color",
1570-
"on",
1581+
zig_exe, "test",
1582+
"--color", "on",
15711583
tmp_source_file_name,
15721584
});
1585+
if (opt_zig_lib_dir) |zig_lib_dir| {
1586+
try test_args.appendSlice(&.{ "--zig-lib-dir", zig_lib_dir });
1587+
}
15731588
try shell_out.print("$ zig test {s}.zig ", .{code.name});
15741589

15751590
switch (code.mode) {
@@ -1624,8 +1639,12 @@ fn genHtml(
16241639
defer test_args.deinit();
16251640

16261641
try test_args.appendSlice(&[_][]const u8{
1627-
zig_exe, "test", tmp_source_file_name,
1642+
zig_exe, "test",
1643+
tmp_source_file_name,
16281644
});
1645+
if (opt_zig_lib_dir) |zig_lib_dir| {
1646+
try test_args.appendSlice(&.{ "--zig-lib-dir", zig_lib_dir });
1647+
}
16291648
var mode_arg: []const u8 = "";
16301649
switch (code.mode) {
16311650
.Debug => {},
@@ -1684,17 +1703,17 @@ fn genHtml(
16841703
defer build_args.deinit();
16851704

16861705
try build_args.appendSlice(&[_][]const u8{
1687-
zig_exe,
1688-
"build-obj",
1706+
zig_exe, "build-obj",
1707+
"--color", "on",
1708+
"--name", code.name,
16891709
tmp_source_file_name,
1690-
"--color",
1691-
"on",
1692-
"--name",
1693-
code.name,
16941710
try std.fmt.allocPrint(allocator, "-femit-bin={s}{c}{s}", .{
16951711
tmp_dir_name, fs.path.sep, name_plus_obj_ext,
16961712
}),
16971713
});
1714+
if (opt_zig_lib_dir) |zig_lib_dir| {
1715+
try build_args.appendSlice(&.{ "--zig-lib-dir", zig_lib_dir });
1716+
}
16981717

16991718
try shell_out.print("$ zig build-obj {s}.zig ", .{code.name});
17001719

@@ -1758,13 +1777,15 @@ fn genHtml(
17581777
defer test_args.deinit();
17591778

17601779
try test_args.appendSlice(&[_][]const u8{
1761-
zig_exe,
1762-
"build-lib",
1780+
zig_exe, "build-lib",
17631781
tmp_source_file_name,
17641782
try std.fmt.allocPrint(allocator, "-femit-bin={s}{s}{s}", .{
17651783
tmp_dir_name, fs.path.sep_str, bin_basename,
17661784
}),
17671785
});
1786+
if (opt_zig_lib_dir) |zig_lib_dir| {
1787+
try test_args.appendSlice(&.{ "--zig-lib-dir", zig_lib_dir });
1788+
}
17681789
try shell_out.print("$ zig build-lib {s}.zig ", .{code.name});
17691790

17701791
switch (code.mode) {
@@ -1829,9 +1850,23 @@ fn exec(allocator: Allocator, env_map: *process.EnvMap, args: []const []const u8
18291850
return result;
18301851
}
18311852

1832-
fn getBuiltinCode(allocator: Allocator, env_map: *process.EnvMap, zig_exe: []const u8) ![]const u8 {
1833-
const result = try exec(allocator, env_map, &[_][]const u8{ zig_exe, "build-obj", "--show-builtin" });
1834-
return result.stdout;
1853+
fn getBuiltinCode(
1854+
allocator: Allocator,
1855+
env_map: *process.EnvMap,
1856+
zig_exe: []const u8,
1857+
opt_zig_lib_dir: ?[]const u8,
1858+
) ![]const u8 {
1859+
if (opt_zig_lib_dir) |zig_lib_dir| {
1860+
const result = try exec(allocator, env_map, &.{
1861+
zig_exe, "build-obj", "--show-builtin", "--zig-lib-dir", zig_lib_dir,
1862+
});
1863+
return result.stdout;
1864+
} else {
1865+
const result = try exec(allocator, env_map, &.{
1866+
zig_exe, "build-obj", "--show-builtin",
1867+
});
1868+
return result.stdout;
1869+
}
18351870
}
18361871

18371872
fn dumpArgs(args: []const []const u8) void {

0 commit comments

Comments
 (0)