Skip to content

Commit abf1795

Browse files
committed
std.Build.Watch: add macOS implementation based on FSEventStream
Resolves: #21905
1 parent 7429568 commit abf1795

File tree

3 files changed

+530
-4
lines changed

3 files changed

+530
-4
lines changed

lib/compiler/build_runner.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ pub fn main() !void {
511511
// recursive dependants.
512512
var caption_buf: [std.Progress.Node.max_name_len]u8 = undefined;
513513
const caption = std.fmt.bufPrint(&caption_buf, "watching {d} directories, {d} processes", .{
514-
w.dir_table.entries.len, countSubProcesses(run.step_stack.keys()),
514+
w.dir_count, countSubProcesses(run.step_stack.keys()),
515515
}) catch &caption_buf;
516516
var debouncing_node = main_progress_node.start(caption, 0);
517517
var in_debounce = false;

lib/std/Build/Watch.zig

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
const builtin = @import("builtin");
22
const std = @import("../std.zig");
3-
const Watch = @This();
43
const Step = std.Build.Step;
54
const Allocator = std.mem.Allocator;
65
const assert = std.debug.assert;
76
const fatal = std.process.fatal;
7+
const Watch = @This();
8+
const FsEvents = @import("Watch/FsEvents.zig");
89

9-
dir_table: DirTable,
1010
os: Os,
11+
/// The number to show as the number of directories being watched.
12+
dir_count: usize,
13+
// These fields are common to most implementations so are kept here for simplicity.
14+
// They are `undefined` on implementations which do not utilize then.
15+
dir_table: DirTable,
1116
generation: Generation,
1217

1318
pub const have_impl = Os != void;
@@ -97,6 +102,7 @@ const Os = switch (builtin.os.tag) {
97102
fn init() !Watch {
98103
return .{
99104
.dir_table = .{},
105+
.dir_count = 0,
100106
.os = switch (builtin.os.tag) {
101107
.linux => .{
102108
.handle_table = .{},
@@ -273,6 +279,7 @@ const Os = switch (builtin.os.tag) {
273279
}
274280
w.generation +%= 1;
275281
}
282+
w.dir_count = w.dir_table.count();
276283
}
277284

278285
fn wait(w: *Watch, gpa: Allocator, timeout: Timeout) !WaitResult {
@@ -408,6 +415,7 @@ const Os = switch (builtin.os.tag) {
408415
fn init() !Watch {
409416
return .{
410417
.dir_table = .{},
418+
.dir_count = 0,
411419
.os = switch (builtin.os.tag) {
412420
.windows => .{
413421
.handle_table = .{},
@@ -572,6 +580,7 @@ const Os = switch (builtin.os.tag) {
572580
}
573581
w.generation +%= 1;
574582
}
583+
w.dir_count = w.dir_table.count();
575584
}
576585

577586
fn wait(w: *Watch, gpa: Allocator, timeout: Timeout) !WaitResult {
@@ -605,7 +614,7 @@ const Os = switch (builtin.os.tag) {
605614
};
606615
}
607616
},
608-
.dragonfly, .freebsd, .netbsd, .openbsd, .ios, .macos, .tvos, .visionos, .watchos => struct {
617+
.dragonfly, .freebsd, .netbsd, .openbsd, .ios, .tvos, .visionos, .watchos => struct {
609618
const posix = std.posix;
610619

611620
kq_fd: i32,
@@ -639,6 +648,7 @@ const Os = switch (builtin.os.tag) {
639648
errdefer posix.close(kq_fd);
640649
return .{
641650
.dir_table = .{},
651+
.dir_count = 0,
642652
.os = .{
643653
.kq_fd = kq_fd,
644654
.handles = .empty,
@@ -769,6 +779,7 @@ const Os = switch (builtin.os.tag) {
769779
}
770780
w.generation +%= 1;
771781
}
782+
w.dir_count = w.dir_table.count();
772783
}
773784

774785
fn wait(w: *Watch, gpa: Allocator, timeout: Timeout) !WaitResult {
@@ -812,6 +823,28 @@ const Os = switch (builtin.os.tag) {
812823
return any_dirty;
813824
}
814825
},
826+
.macos => struct {
827+
fse: FsEvents,
828+
829+
fn init() !Watch {
830+
return .{
831+
.os = .{ .fse = try .init() },
832+
.dir_count = 0,
833+
.dir_table = undefined,
834+
.generation = undefined,
835+
};
836+
}
837+
fn update(w: *Watch, gpa: Allocator, steps: []const *Step) !void {
838+
try w.os.fse.setPaths(gpa, steps);
839+
w.dir_count = w.os.fse.watch_roots.len;
840+
}
841+
fn wait(w: *Watch, gpa: Allocator, timeout: Timeout) !WaitResult {
842+
return w.os.fse.wait(gpa, switch (timeout) {
843+
.none => null,
844+
.ms => |ms| @as(u64, ms) * std.time.ns_per_ms,
845+
});
846+
}
847+
},
815848
else => void,
816849
};
817850

0 commit comments

Comments
 (0)