std.Build.Watch: introduce special file "." to watch entire dir

And use it to implement InstallDir Step watch integration.

I'm not seeing any events triggered when I run `mkdir` in the watched
directory, however, and I have not yet figured out why.
This commit is contained in:
Andrew Kelley 2024-07-09 23:15:22 -07:00
parent 26bdc836d2
commit 7bccef3e4e
3 changed files with 75 additions and 21 deletions

View File

@ -160,6 +160,7 @@ pub const Inputs = struct {
};
pub const Table = std.ArrayHashMapUnmanaged(Build.Cache.Path, Files, Build.Cache.Path.TableAdapter, false);
/// The special file name "." means any changes inside the directory.
pub const Files = std.ArrayListUnmanaged([]const u8);
pub fn populated(inputs: *Inputs) bool {
@ -611,8 +612,9 @@ pub fn clearWatchInputs(step: *Step) void {
step.inputs.clear(gpa);
}
pub fn addWatchInput(step: *Step, lazy_path: Build.LazyPath) Allocator.Error!void {
switch (lazy_path) {
/// Places a *file* dependency on the path.
pub fn addWatchInput(step: *Step, lazy_file: Build.LazyPath) Allocator.Error!void {
switch (lazy_file) {
.src_path => |src_path| try addWatchInputFromBuilder(step, src_path.owner, src_path.sub_path),
.dependency => |d| try addWatchInputFromBuilder(step, d.dependency.builder, d.sub_path),
.cwd_relative => |path_string| {
@ -629,6 +631,34 @@ pub fn addWatchInput(step: *Step, lazy_path: Build.LazyPath) Allocator.Error!voi
}
}
/// Any changes inside the directory will trigger invalidation.
///
/// See also `addDirectoryWatchInputFromPath` which takes a `Build.Cache.Path` instead.
pub fn addDirectoryWatchInput(step: *Step, lazy_directory: Build.LazyPath) Allocator.Error!void {
switch (lazy_directory) {
.src_path => |src_path| try addDirectoryWatchInputFromBuilder(step, src_path.owner, src_path.sub_path),
.dependency => |d| try addDirectoryWatchInputFromBuilder(step, d.dependency.builder, d.sub_path),
.cwd_relative => |path_string| {
try addDirectoryWatchInputFromPath(step, .{
.root_dir = .{
.path = null,
.handle = std.fs.cwd(),
},
.sub_path = path_string,
});
},
// Nothing to watch because this dependency edge is modeled instead via `dependants`.
.generated => {},
}
}
/// Any changes inside the directory will trigger invalidation.
///
/// See also `addDirectoryWatchInput` which takes a `Build.LazyPath` instead.
pub fn addDirectoryWatchInputFromPath(step: *Step, path: Build.Cache.Path) !void {
return addWatchInputFromPath(step, path, ".");
}
fn addWatchInputFromBuilder(step: *Step, builder: *Build, sub_path: []const u8) !void {
return addWatchInputFromPath(step, .{
.root_dir = builder.build_root,
@ -636,6 +666,13 @@ fn addWatchInputFromBuilder(step: *Step, builder: *Build, sub_path: []const u8)
}, std.fs.path.basename(sub_path));
}
fn addDirectoryWatchInputFromBuilder(step: *Step, builder: *Build, sub_path: []const u8) !void {
return addDirectoryWatchInputFromPath(step, .{
.root_dir = builder.build_root,
.sub_path = sub_path,
});
}
fn addWatchInputFromPath(step: *Step, path: Build.Cache.Path, basename: []const u8) !void {
const gpa = step.owner.allocator;
const gop = try step.inputs.table.getOrPut(gpa, path);

View File

@ -59,12 +59,14 @@ fn make(step: *Step, prog_node: std.Progress.Node) !void {
_ = prog_node;
const b = step.owner;
const install_dir: *InstallDir = @fieldParentPtr("step", step);
step.clearWatchInputs();
const arena = b.allocator;
const dest_prefix = b.getInstallPath(install_dir.options.install_dir, install_dir.options.install_subdir);
const src_dir_path = install_dir.options.source_dir.getPath2(b, step);
var src_dir = b.build_root.handle.openDir(src_dir_path, .{ .iterate = true }) catch |err| {
return step.fail("unable to open source directory '{}{s}': {s}", .{
b.build_root, src_dir_path, @errorName(err),
const src_dir_path = install_dir.options.source_dir.getPath3(b, step);
try step.addDirectoryWatchInput(install_dir.options.source_dir);
var src_dir = src_dir_path.root_dir.handle.openDir(src_dir_path.subPathOpt() orelse ".", .{ .iterate = true }) catch |err| {
return step.fail("unable to open source directory '{}': {s}", .{
src_dir_path, @errorName(err),
});
};
defer src_dir.close();
@ -88,12 +90,16 @@ fn make(step: *Step, prog_node: std.Progress.Node) !void {
}
// relative to src build root
const src_sub_path = b.pathJoin(&.{ src_dir_path, entry.path });
const src_sub_path = try src_dir_path.join(arena, entry.path);
const dest_path = b.pathJoin(&.{ dest_prefix, entry.path });
const cwd = fs.cwd();
switch (entry.kind) {
.directory => try cwd.makePath(dest_path),
.directory => {
const subdir_path = try src_dir_path.join(arena, entry.path);
try step.addDirectoryWatchInputFromPath(subdir_path);
try cwd.makePath(dest_path);
},
.file => {
for (install_dir.options.blank_extensions) |ext| {
if (mem.endsWith(u8, entry.path, ext)) {
@ -103,14 +109,14 @@ fn make(step: *Step, prog_node: std.Progress.Node) !void {
}
const prev_status = fs.Dir.updateFile(
b.build_root.handle,
src_sub_path,
src_sub_path.root_dir.handle,
src_sub_path.sub_path,
cwd,
dest_path,
.{},
) catch |err| {
return step.fail("unable to update file from '{}{s}' to '{s}': {s}", .{
b.build_root, src_sub_path, dest_path, @errorName(err),
return step.fail("unable to update file from '{}' to '{s}': {s}", .{
src_sub_path, dest_path, @errorName(err),
});
};
all_cached = all_cached and prev_status == .fresh;

View File

@ -12,10 +12,13 @@ generation: Generation,
pub const fan_mask: std.os.linux.fanotify.MarkMask = .{
.CLOSE_WRITE = true,
.CREATE = true,
.DELETE = true,
.DELETE_SELF = true,
.EVENT_ON_CHILD = true,
.MOVED_FROM = true,
.MOVED_TO = true,
.EVENT_ON_CHILD = true,
.MOVE_SELF = true,
};
pub const init: Watch = .{
@ -32,6 +35,7 @@ pub const init: Watch = .{
const DirTable = std.ArrayHashMapUnmanaged(Cache.Path, void, Cache.Path.TableAdapter, false);
const HandleTable = std.ArrayHashMapUnmanaged(LinuxFileHandle, ReactionSet, LinuxFileHandle.Adapter, false);
/// Special key of "." means any changes in this directory trigger the steps.
const ReactionSet = std.StringArrayHashMapUnmanaged(StepSet);
const StepSet = std.AutoArrayHashMapUnmanaged(*Step, Generation);
@ -149,14 +153,10 @@ pub fn markDirtySteps(w: *Watch, gpa: Allocator) !bool {
const file_name = std.mem.span(file_name_z);
const lfh: Watch.LinuxFileHandle = .{ .handle = file_handle };
if (w.handle_table.getPtr(lfh)) |reaction_set| {
if (reaction_set.getPtr(file_name)) |step_set| {
for (step_set.keys()) |step| {
if (step.state != .precheck_done) {
step.recursiveReset(gpa);
any_dirty = true;
}
}
}
if (reaction_set.getPtr(".")) |glob_set|
any_dirty = markStepSetDirty(gpa, glob_set, any_dirty);
if (reaction_set.getPtr(file_name)) |step_set|
any_dirty = markStepSetDirty(gpa, step_set, any_dirty);
}
},
else => |t| std.log.warn("unexpected fanotify event '{s}'", .{@tagName(t)}),
@ -187,3 +187,14 @@ fn markAllFilesDirty(w: *Watch, gpa: Allocator) void {
}
}
}
fn markStepSetDirty(gpa: Allocator, step_set: *StepSet, any_dirty: bool) bool {
var this_any_dirty = false;
for (step_set.keys()) |step| {
if (step.state != .precheck_done) {
step.recursiveReset(gpa);
this_any_dirty = true;
}
}
return any_dirty or this_any_dirty;
}