std.Build.InstallDirStep: avoid std.log

And better make use of open directory handles.
This commit is contained in:
Andrew Kelley 2023-03-03 17:49:14 -07:00
parent 405bf1b091
commit bb1960c2a4

View File

@ -4,7 +4,6 @@ const fs = std.fs;
const Step = std.Build.Step;
const InstallDir = std.Build.InstallDir;
const InstallDirStep = @This();
const log = std.log;
step: Step,
options: Options,
@ -57,17 +56,17 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
_ = prog_node;
const self = @fieldParentPtr(InstallDirStep, "step", step);
const dest_builder = self.dest_builder;
const arena = dest_builder.allocator;
const dest_prefix = dest_builder.getInstallPath(self.options.install_dir, self.options.install_subdir);
const src_builder = self.step.owner;
const full_src_dir = src_builder.pathFromRoot(self.options.source_dir);
var src_dir = std.fs.cwd().openIterableDir(full_src_dir, .{}) catch |err| {
log.err("InstallDirStep: unable to open source directory '{s}': {s}", .{
full_src_dir, @errorName(err),
var src_dir = src_builder.build_root.handle.openIterableDir(self.options.source_dir, .{}) catch |err| {
return step.fail("unable to open source directory '{}{s}': {s}", .{
src_builder.build_root, self.options.source_dir, @errorName(err),
});
return error.StepFailed;
};
defer src_dir.close();
var it = try src_dir.walk(dest_builder.allocator);
var it = try src_dir.walk(arena);
var all_cached = true;
next_entry: while (try it.next()) |entry| {
for (self.options.exclude_extensions) |ext| {
if (mem.endsWith(u8, entry.path, ext)) {
@ -75,11 +74,13 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
}
}
const full_path = dest_builder.pathJoin(&.{ full_src_dir, entry.path });
const dest_path = dest_builder.pathJoin(&.{ dest_prefix, entry.path });
// relative to src build root
const src_sub_path = try fs.path.join(arena, &.{ self.options.source_dir, entry.path });
const dest_path = try fs.path.join(arena, &.{ dest_prefix, entry.path });
const cwd = fs.cwd();
switch (entry.kind) {
.Directory => try fs.cwd().makePath(dest_path),
.Directory => try cwd.makePath(dest_path),
.File => {
for (self.options.blank_extensions) |ext| {
if (mem.endsWith(u8, entry.path, ext)) {
@ -88,9 +89,18 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
}
}
try dest_builder.updateFile(full_path, dest_path);
const prev_status = try fs.Dir.updateFile(
src_builder.build_root.handle,
src_sub_path,
cwd,
dest_path,
.{},
);
all_cached = all_cached and prev_status == .fresh;
},
else => continue,
}
}
step.result_cached = all_cached;
}