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 {...@@ -1052,7 +1052,7 @@ pub fn addWriteFiles(b: *Build) *Step.WriteFile {
1052 return Step.WriteFile.create(b);1052 return Step.WriteFile.create(b);
1053}1053}
10541054
1055pub fn addRemoveDirTree(b: *Build, dir_path: []const u8) *Step.RemoveDir {1055pub fn addRemoveDirTree(b: *Build, dir_path: LazyPath) *Step.RemoveDir {
1056 return Step.RemoveDir.create(b, dir_path);1056 return Step.RemoveDir.create(b, dir_path);
1057}1057}
10581058
lib/std/Build/Step.zig+9-6
...@@ -598,14 +598,17 @@ pub fn writeManifest(s: *Step, man: *Build.Cache.Manifest) !void {...@@ -598,14 +598,17 @@ pub fn writeManifest(s: *Step, man: *Build.Cache.Manifest) !void {
598 }598 }
599}599}
600600
601fn oom(err: anytype) noreturn {601/// For steps that have a single input that never changes when re-running `make`.
602 switch (err) {602pub fn singleUnchangingWatchInput(step: *Step, lazy_path: Build.LazyPath) Allocator.Error!void {
603 error.OutOfMemory => @panic("out of memory"),603 if (!step.inputs.populated()) try step.addWatchInput(lazy_path);
604 }604}
605
606pub fn clearWatchInputs(step: *Step) void {
607 const gpa = step.owner.allocator;
608 step.inputs.clear(gpa);
605}609}
606610
607pub fn addWatchInput(step: *Step, lazy_path: Build.LazyPath) void {611pub fn addWatchInput(step: *Step, lazy_path: Build.LazyPath) Allocator.Error!void {
608 errdefer |err| oom(err);
609 switch (lazy_path) {612 switch (lazy_path) {
610 .src_path => |src_path| try addWatchInputFromBuilder(step, src_path.owner, src_path.sub_path),613 .src_path => |src_path| try addWatchInputFromBuilder(step, src_path.owner, src_path.sub_path),
611 .dependency => |d| try addWatchInputFromBuilder(step, d.dependency.builder, d.sub_path),614 .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 {...@@ -50,6 +50,7 @@ fn make(step: *Step, prog_node: std.Progress.Node) !void {
50 _ = prog_node;50 _ = prog_node;
51 const b = step.owner;51 const b = step.owner;
52 const check_file: *CheckFile = @fieldParentPtr("step", step);52 const check_file: *CheckFile = @fieldParentPtr("step", step);
53 try step.singleUnchangingWatchInput(check_file.source);
5354
54 const src_path = check_file.source.getPath2(b, step);55 const src_path = check_file.source.getPath2(b, step);
55 const contents = fs.cwd().readFileAlloc(b.allocator, src_path, check_file.max_bytes) catch |err| {56 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 {...@@ -555,6 +555,7 @@ fn make(step: *Step, prog_node: std.Progress.Node) !void {
555 const b = step.owner;555 const b = step.owner;
556 const gpa = b.allocator;556 const gpa = b.allocator;
557 const check_object: *CheckObject = @fieldParentPtr("step", step);557 const check_object: *CheckObject = @fieldParentPtr("step", step);
558 try step.singleUnchangingWatchInput(check_object.source);
558559
559 const src_path = check_object.source.getPath2(b, step);560 const src_path = check_object.source.getPath2(b, step);
560 const contents = fs.cwd().readFileAllocOptions(561 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 {...@@ -168,6 +168,8 @@ fn make(step: *Step, prog_node: std.Progress.Node) !void {
168 _ = prog_node;168 _ = prog_node;
169 const b = step.owner;169 const b = step.owner;
170 const config_header: *ConfigHeader = @fieldParentPtr("step", step);170 const config_header: *ConfigHeader = @fieldParentPtr("step", step);
171 if (config_header.style.getPath()) |lp| try step.singleUnchangingWatchInput(lp);
172
171 const gpa = b.allocator;173 const gpa = b.allocator;
172 const arena = b.allocator;174 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 {...@@ -39,9 +39,7 @@ fn make(step: *Step, prog_node: std.Progress.Node) !void {
39 _ = prog_node;39 _ = prog_node;
40 const b = step.owner;40 const b = step.owner;
41 const install_file: *InstallFile = @fieldParentPtr("step", step);41 const install_file: *InstallFile = @fieldParentPtr("step", step);
4242 try step.singleUnchangingWatchInput(install_file.source);
43 // Inputs never change when re-running `make`.
44 if (!step.inputs.populated()) step.addWatchInput(install_file.source);
4543
46 const full_src_path = install_file.source.getPath2(b, step);44 const full_src_path = install_file.source.getPath2(b, step);
47 const full_dest_path = b.getInstallPath(install_file.dir, install_file.dest_rel_path);45 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 {...@@ -93,6 +93,7 @@ pub fn getOutputSeparatedDebug(objcopy: *const ObjCopy) ?std.Build.LazyPath {
93fn make(step: *Step, prog_node: std.Progress.Node) !void {93fn make(step: *Step, prog_node: std.Progress.Node) !void {
94 const b = step.owner;94 const b = step.owner;
95 const objcopy: *ObjCopy = @fieldParentPtr("step", step);95 const objcopy: *ObjCopy = @fieldParentPtr("step", step);
96 try step.singleUnchangingWatchInput(objcopy.input_file);
9697
97 var man = b.graph.cache.obtain();98 var man = b.graph.cache.obtain();
98 defer man.deinit();99 defer man.deinit();
lib/std/Build/Step/Options.zig+3
...@@ -424,6 +424,9 @@ fn make(step: *Step, prog_node: std.Progress.Node) !void {...@@ -424,6 +424,9 @@ fn make(step: *Step, prog_node: std.Progress.Node) !void {
424 item.path.getPath2(b, step),424 item.path.getPath2(b, step),
425 );425 );
426 }426 }
427 if (!step.inputs.populated()) for (options.args.items) |item| {
428 try step.addWatchInput(item.path);
429 };
427430
428 const basename = "options.zig";431 const basename = "options.zig";
429432
lib/std/Build/Step/RemoveDir.zig+13-7
...@@ -2,22 +2,23 @@ const std = @import("std");...@@ -2,22 +2,23 @@ const std = @import("std");
2const fs = std.fs;2const fs = std.fs;
3const Step = std.Build.Step;3const Step = std.Build.Step;
4const RemoveDir = @This();4const RemoveDir = @This();
5const LazyPath = std.Build.LazyPath;
56
6pub const base_id: Step.Id = .remove_dir;7pub const base_id: Step.Id = .remove_dir;
78
8step: Step,9step: 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 {
12 const remove_dir = owner.allocator.create(RemoveDir) catch @panic("OOM");13 const remove_dir = owner.allocator.create(RemoveDir) catch @panic("OOM");
13 remove_dir.* = .{14 remove_dir.* = .{
14 .step = Step.init(.{15 .step = Step.init(.{
15 .id = base_id,16 .id = base_id,
16 .name = owner.fmt("RemoveDir {s}", .{dir_path}),17 .name = owner.fmt("RemoveDir {s}", .{doomed_path.getDisplayName()}),
17 .owner = owner,18 .owner = owner,
18 .makeFn = make,19 .makeFn = make,
19 }),20 }),
20 .dir_path = owner.dupePath(dir_path),21 .doomed_path = doomed_path.dupe(owner),
21 };22 };
22 return remove_dir;23 return remove_dir;
23}24}
...@@ -30,14 +31,19 @@ fn make(step: *Step, prog_node: std.Progress.Node) !void {...@@ -30,14 +31,19 @@ fn make(step: *Step, prog_node: std.Progress.Node) !void {
30 const b = step.owner;31 const b = step.owner;
31 const remove_dir: *RemoveDir = @fieldParentPtr("step", step);32 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| {
34 if (b.build_root.path) |base| {40 if (b.build_root.path) |base| {
35 return step.fail("unable to recursively delete path '{s}/{s}': {s}", .{41 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),
37 });43 });
38 } else {44 } else {
39 return step.fail("unable to recursively delete path '{s}': {s}", .{45 return step.fail("unable to recursively delete path '{s}': {s}", .{
40 remove_dir.dir_path, @errorName(err),46 full_doomed_path, @errorName(err),
41 });47 });
42 }48 }
43 };49 };
test/tests.zig+3-3
...@@ -771,7 +771,7 @@ pub fn addCliTests(b: *std.Build) *Step {...@@ -771,7 +771,7 @@ pub fn addCliTests(b: *std.Build) *Step {
771 run_run.expectStdErrEqual("All your codebase are belong to us.\n");771 run_run.expectStdErrEqual("All your codebase are belong to us.\n");
772 run_run.step.dependOn(&init_exe.step);772 run_run.step.dependOn(&init_exe.step);
773773
774 const cleanup = b.addRemoveDirTree(tmp_path);774 const cleanup = b.addRemoveDirTree(.{ .cwd_relative = tmp_path });
775 cleanup.step.dependOn(&run_test.step);775 cleanup.step.dependOn(&run_test.step);
776 cleanup.step.dependOn(&run_run.step);776 cleanup.step.dependOn(&run_run.step);
777 cleanup.step.dependOn(&run_bad.step);777 cleanup.step.dependOn(&run_bad.step);
...@@ -816,7 +816,7 @@ pub fn addCliTests(b: *std.Build) *Step {...@@ -816,7 +816,7 @@ pub fn addCliTests(b: *std.Build) *Step {
816 });816 });
817 checkfile.setName("check godbolt.org CLI usage generating valid asm");817 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 });
820 cleanup.step.dependOn(&checkfile.step);820 cleanup.step.dependOn(&checkfile.step);
821821
822 step.dependOn(&cleanup.step);822 step.dependOn(&cleanup.step);
...@@ -902,7 +902,7 @@ pub fn addCliTests(b: *std.Build) *Step {...@@ -902,7 +902,7 @@ pub fn addCliTests(b: *std.Build) *Step {
902 });902 });
903 check6.step.dependOn(&run6.step);903 check6.step.dependOn(&run6.step);
904904
905 const cleanup = b.addRemoveDirTree(tmp_path);905 const cleanup = b.addRemoveDirTree(.{ .cwd_relative = tmp_path });
906 cleanup.step.dependOn(&check6.step);906 cleanup.step.dependOn(&check6.step);
907907
908 step.dependOn(&cleanup.step);908 step.dependOn(&cleanup.step);