authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-07-09 21:08:20-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-07-12 00:14:08-07:00
log0cc492a272ef9c03a34b57a26bf570b242615ddf
tree8b3ce5767406aeb02c16f4c3538b1ddcaf89d62d
parent956f1ebc707f8a2530e49b80357768f3bf1235ac

make more build steps integrate with the watch system


10 files changed, 35 insertions(+), 20 deletions(-)

lib/std/Build.zig+1-1
......@@ -1052,7 +1052,7 @@ pub fn addWriteFiles(b: *Build) *Step.WriteFile {
10521052 return Step.WriteFile.create(b);
10531053}
10541054
1055pub fn addRemoveDirTree(b: *Build, dir_path: []const u8) *Step.RemoveDir {
1055pub fn addRemoveDirTree(b: *Build, dir_path: LazyPath) *Step.RemoveDir {
10561056 return Step.RemoveDir.create(b, dir_path);
10571057}
10581058
lib/std/Build/Step.zig+9-6
......@@ -598,14 +598,17 @@ pub fn writeManifest(s: *Step, man: *Build.Cache.Manifest) !void {
598598 }
599599}
600600
601fn oom(err: anytype) noreturn {
602 switch (err) {
603 error.OutOfMemory => @panic("out of memory"),
604 }
601/// For steps that have a single input that never changes when re-running `make`.
602pub fn singleUnchangingWatchInput(step: *Step, lazy_path: Build.LazyPath) Allocator.Error!void {
603 if (!step.inputs.populated()) try step.addWatchInput(lazy_path);
604}
605
606pub fn clearWatchInputs(step: *Step) void {
607 const gpa = step.owner.allocator;
608 step.inputs.clear(gpa);
605609}
606610
607pub fn addWatchInput(step: *Step, lazy_path: Build.LazyPath) void {
608 errdefer |err| oom(err);
611pub fn addWatchInput(step: *Step, lazy_path: Build.LazyPath) Allocator.Error!void {
609612 switch (lazy_path) {
610613 .src_path => |src_path| try addWatchInputFromBuilder(step, src_path.owner, src_path.sub_path),
611614 .dependency => |d| try addWatchInputFromBuilder(step, d.dependency.builder, d.sub_path),
lib/std/Build/Step/CheckFile.zig+1
......@@ -50,6 +50,7 @@ fn make(step: *Step, prog_node: std.Progress.Node) !void {
5050 _ = prog_node;
5151 const b = step.owner;
5252 const check_file: *CheckFile = @fieldParentPtr("step", step);
53 try step.singleUnchangingWatchInput(check_file.source);
5354
5455 const src_path = check_file.source.getPath2(b, step);
5556 const contents = fs.cwd().readFileAlloc(b.allocator, src_path, check_file.max_bytes) catch |err| {
lib/std/Build/Step/CheckObject.zig+1
......@@ -555,6 +555,7 @@ fn make(step: *Step, prog_node: std.Progress.Node) !void {
555555 const b = step.owner;
556556 const gpa = b.allocator;
557557 const check_object: *CheckObject = @fieldParentPtr("step", step);
558 try step.singleUnchangingWatchInput(check_object.source);
558559
559560 const src_path = check_object.source.getPath2(b, step);
560561 const contents = fs.cwd().readFileAllocOptions(
lib/std/Build/Step/ConfigHeader.zig+2
......@@ -168,6 +168,8 @@ fn make(step: *Step, prog_node: std.Progress.Node) !void {
168168 _ = prog_node;
169169 const b = step.owner;
170170 const config_header: *ConfigHeader = @fieldParentPtr("step", step);
171 if (config_header.style.getPath()) |lp| try step.singleUnchangingWatchInput(lp);
172
171173 const gpa = b.allocator;
172174 const arena = b.allocator;
173175
lib/std/Build/Step/InstallFile.zig+1-3
......@@ -39,9 +39,7 @@ fn make(step: *Step, prog_node: std.Progress.Node) !void {
3939 _ = prog_node;
4040 const b = step.owner;
4141 const install_file: *InstallFile = @fieldParentPtr("step", step);
42
43 // Inputs never change when re-running `make`.
44 if (!step.inputs.populated()) step.addWatchInput(install_file.source);
42 try step.singleUnchangingWatchInput(install_file.source);
4543
4644 const full_src_path = install_file.source.getPath2(b, step);
4745 const full_dest_path = b.getInstallPath(install_file.dir, install_file.dest_rel_path);
lib/std/Build/Step/ObjCopy.zig+1
......@@ -93,6 +93,7 @@ pub fn getOutputSeparatedDebug(objcopy: *const ObjCopy) ?std.Build.LazyPath {
9393fn make(step: *Step, prog_node: std.Progress.Node) !void {
9494 const b = step.owner;
9595 const objcopy: *ObjCopy = @fieldParentPtr("step", step);
96 try step.singleUnchangingWatchInput(objcopy.input_file);
9697
9798 var man = b.graph.cache.obtain();
9899 defer man.deinit();
lib/std/Build/Step/Options.zig+3
......@@ -424,6 +424,9 @@ fn make(step: *Step, prog_node: std.Progress.Node) !void {
424424 item.path.getPath2(b, step),
425425 );
426426 }
427 if (!step.inputs.populated()) for (options.args.items) |item| {
428 try step.addWatchInput(item.path);
429 };
427430
428431 const basename = "options.zig";
429432
lib/std/Build/Step/RemoveDir.zig+13-7
......@@ -2,22 +2,23 @@ const std = @import("std");
22const fs = std.fs;
33const Step = std.Build.Step;
44const RemoveDir = @This();
5const LazyPath = std.Build.LazyPath;
56
67pub const base_id: Step.Id = .remove_dir;
78
89step: Step,
9dir_path: []const u8,
10doomed_path: LazyPath,
1011
11pub fn create(owner: *std.Build, dir_path: []const u8) *RemoveDir {
12pub fn create(owner: *std.Build, doomed_path: LazyPath) *RemoveDir {
1213 const remove_dir = owner.allocator.create(RemoveDir) catch @panic("OOM");
1314 remove_dir.* = .{
1415 .step = Step.init(.{
1516 .id = base_id,
16 .name = owner.fmt("RemoveDir {s}", .{dir_path}),
17 .name = owner.fmt("RemoveDir {s}", .{doomed_path.getDisplayName()}),
1718 .owner = owner,
1819 .makeFn = make,
1920 }),
20 .dir_path = owner.dupePath(dir_path),
21 .doomed_path = doomed_path.dupe(owner),
2122 };
2223 return remove_dir;
2324}
......@@ -30,14 +31,19 @@ fn make(step: *Step, prog_node: std.Progress.Node) !void {
3031 const b = step.owner;
3132 const remove_dir: *RemoveDir = @fieldParentPtr("step", step);
3233
33 b.build_root.handle.deleteTree(remove_dir.dir_path) catch |err| {
34 step.clearWatchInputs();
35 try step.addWatchInput(remove_dir.doomed_path);
36
37 const full_doomed_path = remove_dir.doomed_path.getPath2(b, step);
38
39 b.build_root.handle.deleteTree(full_doomed_path) catch |err| {
3440 if (b.build_root.path) |base| {
3541 return step.fail("unable to recursively delete path '{s}/{s}': {s}", .{
36 base, remove_dir.dir_path, @errorName(err),
42 base, full_doomed_path, @errorName(err),
3743 });
3844 } else {
3945 return step.fail("unable to recursively delete path '{s}': {s}", .{
40 remove_dir.dir_path, @errorName(err),
46 full_doomed_path, @errorName(err),
4147 });
4248 }
4349 };
test/tests.zig+3-3
......@@ -771,7 +771,7 @@ pub fn addCliTests(b: *std.Build) *Step {
771771 run_run.expectStdErrEqual("All your codebase are belong to us.\n");
772772 run_run.step.dependOn(&init_exe.step);
773773
774 const cleanup = b.addRemoveDirTree(tmp_path);
774 const cleanup = b.addRemoveDirTree(.{ .cwd_relative = tmp_path });
775775 cleanup.step.dependOn(&run_test.step);
776776 cleanup.step.dependOn(&run_run.step);
777777 cleanup.step.dependOn(&run_bad.step);
......@@ -816,7 +816,7 @@ pub fn addCliTests(b: *std.Build) *Step {
816816 });
817817 checkfile.setName("check godbolt.org CLI usage generating valid asm");
818818
819 const cleanup = b.addRemoveDirTree(tmp_path);
819 const cleanup = b.addRemoveDirTree(.{ .cwd_relative = tmp_path });
820820 cleanup.step.dependOn(&checkfile.step);
821821
822822 step.dependOn(&cleanup.step);
......@@ -902,7 +902,7 @@ pub fn addCliTests(b: *std.Build) *Step {
902902 });
903903 check6.step.dependOn(&run6.step);
904904
905 const cleanup = b.addRemoveDirTree(tmp_path);
905 const cleanup = b.addRemoveDirTree(.{ .cwd_relative = tmp_path });
906906 cleanup.step.dependOn(&check6.step);
907907
908908 step.dependOn(&cleanup.step);