authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-03-05 16:11:04-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-03-15 10:48:13-07:00
loga4c35a62454a3970dd44ac8b842be91a46bb896c
treed71aee352f75881373aa1658d031ff6ef765163a
parent1e63573d359f15042ebdcc9b0c32d7ce4662899f

std.Build: audit use of updateFile

* remove std.Build.updateFile. I noticed some people use it from build.zig (declare phase) when it is intended only for use in the make phase. - This also was incorrectly reporting errors with std.log. * std.Build.InstallArtifactStep - report better errors on failure - report whether the step was cached or not * std.Build.InstallDirStep: report better error on failure * std.Build.InstallFileStep: report better error on failure

5 files changed, 60 insertions(+), 27 deletions(-)

lib/std/Build.zig-12
...@@ -1235,18 +1235,6 @@ pub fn pushInstalledFile(self: *Build, dir: InstallDir, dest_rel_path: []const u...@@ -1235,18 +1235,6 @@ pub fn pushInstalledFile(self: *Build, dir: InstallDir, dest_rel_path: []const u
1235 self.installed_files.append(file.dupe(self)) catch @panic("OOM");1235 self.installed_files.append(file.dupe(self)) catch @panic("OOM");
1236}1236}
12371237
1238pub fn updateFile(self: *Build, source_path: []const u8, dest_path: []const u8) !void {
1239 if (self.verbose) {
1240 log.info("cp {s} {s} ", .{ source_path, dest_path });
1241 }
1242 const cwd = fs.cwd();
1243 const prev_status = try fs.Dir.updateFile(cwd, source_path, cwd, dest_path, .{});
1244 if (self.verbose) switch (prev_status) {
1245 .stale => log.info("# installed", .{}),
1246 .fresh => log.info("# up-to-date", .{}),
1247 };
1248}
1249
1250pub fn truncateFile(self: *Build, dest_path: []const u8) !void {1238pub fn truncateFile(self: *Build, dest_path: []const u8) !void {
1251 if (self.verbose) {1239 if (self.verbose) {
1252 log.info("truncate {s}", .{dest_path});1240 log.info("truncate {s}", .{dest_path});
lib/std/Build/CompileStep.zig+3-3
...@@ -1910,13 +1910,13 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {...@@ -1910,13 +1910,13 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
1910 const build_output_dir = fs.path.dirname(output_bin_path).?;1910 const build_output_dir = fs.path.dirname(output_bin_path).?;
19111911
1912 if (self.output_dir) |output_dir| {1912 if (self.output_dir) |output_dir| {
1913 var src_dir = try std.fs.cwd().openIterableDir(build_output_dir, .{});1913 var src_dir = try fs.cwd().openIterableDir(build_output_dir, .{});
1914 defer src_dir.close();1914 defer src_dir.close();
19151915
1916 // Create the output directory if it doesn't exist.1916 // Create the output directory if it doesn't exist.
1917 try std.fs.cwd().makePath(output_dir);1917 try fs.cwd().makePath(output_dir);
19181918
1919 var dest_dir = try std.fs.cwd().openDir(output_dir, .{});1919 var dest_dir = try fs.cwd().openDir(output_dir, .{});
1920 defer dest_dir.close();1920 defer dest_dir.close();
19211921
1922 var it = src_dir.iterate();1922 var it = src_dir.iterate();
lib/std/Build/InstallArtifactStep.zig+44-9
...@@ -3,6 +3,7 @@ const Step = std.Build.Step;...@@ -3,6 +3,7 @@ const Step = std.Build.Step;
3const CompileStep = std.Build.CompileStep;3const CompileStep = std.Build.CompileStep;
4const InstallDir = std.Build.InstallDir;4const InstallDir = std.Build.InstallDir;
5const InstallArtifactStep = @This();5const InstallArtifactStep = @This();
6const fs = std.fs;
67
7pub const base_id = .install_artifact;8pub const base_id = .install_artifact;
89
...@@ -77,25 +78,59 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {...@@ -77,25 +78,59 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
7778
78 const dest_sub_path = if (self.dest_sub_path) |sub_path| sub_path else self.artifact.out_filename;79 const dest_sub_path = if (self.dest_sub_path) |sub_path| sub_path else self.artifact.out_filename;
79 const full_dest_path = dest_builder.getInstallPath(self.dest_dir, dest_sub_path);80 const full_dest_path = dest_builder.getInstallPath(self.dest_dir, dest_sub_path);
81 const cwd = fs.cwd();
8082
81 try src_builder.updateFile(83 var all_cached = true;
82 self.artifact.getOutputSource().getPath(src_builder),84
83 full_dest_path,85 {
84 );86 const full_src_path = self.artifact.getOutputSource().getPath(src_builder);
85 if (self.artifact.isDynamicLibrary() and self.artifact.version != null and self.artifact.target.wantSharedLibSymLinks()) {87 const p = fs.Dir.updateFile(cwd, full_src_path, cwd, full_dest_path, .{}) catch |err| {
88 return step.fail("unable to update file from '{s}' to '{s}': {s}", .{
89 full_src_path, full_dest_path, @errorName(err),
90 });
91 };
92 all_cached = all_cached and p == .fresh;
93 }
94
95 if (self.artifact.isDynamicLibrary() and
96 self.artifact.version != null and
97 self.artifact.target.wantSharedLibSymLinks())
98 {
86 try CompileStep.doAtomicSymLinks(step, full_dest_path, self.artifact.major_only_filename.?, self.artifact.name_only_filename.?);99 try CompileStep.doAtomicSymLinks(step, full_dest_path, self.artifact.major_only_filename.?, self.artifact.name_only_filename.?);
87 }100 }
88 if (self.artifact.isDynamicLibrary() and self.artifact.target.isWindows() and self.artifact.emit_implib != .no_emit) {101 if (self.artifact.isDynamicLibrary() and
102 self.artifact.target.isWindows() and
103 self.artifact.emit_implib != .no_emit)
104 {
105 const full_src_path = self.artifact.getOutputLibSource().getPath(src_builder);
89 const full_implib_path = dest_builder.getInstallPath(self.dest_dir, self.artifact.out_lib_filename);106 const full_implib_path = dest_builder.getInstallPath(self.dest_dir, self.artifact.out_lib_filename);
90 try src_builder.updateFile(self.artifact.getOutputLibSource().getPath(src_builder), full_implib_path);107 const p = fs.Dir.updateFile(cwd, full_src_path, cwd, full_implib_path, .{}) catch |err| {
108 return step.fail("unable to update file from '{s}' to '{s}': {s}", .{
109 full_src_path, full_implib_path, @errorName(err),
110 });
111 };
112 all_cached = all_cached and p == .fresh;
91 }113 }
92 if (self.pdb_dir) |pdb_dir| {114 if (self.pdb_dir) |pdb_dir| {
115 const full_src_path = self.artifact.getOutputPdbSource().getPath(src_builder);
93 const full_pdb_path = dest_builder.getInstallPath(pdb_dir, self.artifact.out_pdb_filename);116 const full_pdb_path = dest_builder.getInstallPath(pdb_dir, self.artifact.out_pdb_filename);
94 try src_builder.updateFile(self.artifact.getOutputPdbSource().getPath(src_builder), full_pdb_path);117 const p = fs.Dir.updateFile(cwd, full_src_path, cwd, full_pdb_path, .{}) catch |err| {
118 return step.fail("unable to update file from '{s}' to '{s}': {s}", .{
119 full_src_path, full_pdb_path, @errorName(err),
120 });
121 };
122 all_cached = all_cached and p == .fresh;
95 }123 }
96 if (self.h_dir) |h_dir| {124 if (self.h_dir) |h_dir| {
125 const full_src_path = self.artifact.getOutputHSource().getPath(src_builder);
97 const full_h_path = dest_builder.getInstallPath(h_dir, self.artifact.out_h_filename);126 const full_h_path = dest_builder.getInstallPath(h_dir, self.artifact.out_h_filename);
98 try src_builder.updateFile(self.artifact.getOutputHSource().getPath(src_builder), full_h_path);127 const p = fs.Dir.updateFile(cwd, full_src_path, cwd, full_h_path, .{}) catch |err| {
128 return step.fail("unable to update file from '{s}' to '{s}': {s}", .{
129 full_src_path, full_h_path, @errorName(err),
130 });
131 };
132 all_cached = all_cached and p == .fresh;
99 }133 }
100 self.artifact.installed_path = full_dest_path;134 self.artifact.installed_path = full_dest_path;
135 step.result_cached = all_cached;
101}136}
lib/std/Build/InstallDirStep.zig+6-2
...@@ -89,13 +89,17 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {...@@ -89,13 +89,17 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
89 }89 }
90 }90 }
9191
92 const prev_status = try fs.Dir.updateFile(92 const prev_status = fs.Dir.updateFile(
93 src_builder.build_root.handle,93 src_builder.build_root.handle,
94 src_sub_path,94 src_sub_path,
95 cwd,95 cwd,
96 dest_path,96 dest_path,
97 .{},97 .{},
98 );98 ) catch |err| {
99 return step.fail("unable to update file from '{}{s}' to '{s}': {s}", .{
100 src_builder.build_root, src_sub_path, dest_path, @errorName(err),
101 });
102 };
99 all_cached = all_cached and prev_status == .fresh;103 all_cached = all_cached and prev_status == .fresh;
100 },104 },
101 else => continue,105 else => continue,
lib/std/Build/InstallFileStep.zig+7-1
...@@ -42,5 +42,11 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {...@@ -42,5 +42,11 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
42 const dest_builder = self.dest_builder;42 const dest_builder = self.dest_builder;
43 const full_src_path = self.source.getPath2(src_builder, step);43 const full_src_path = self.source.getPath2(src_builder, step);
44 const full_dest_path = dest_builder.getInstallPath(self.dir, self.dest_rel_path);44 const full_dest_path = dest_builder.getInstallPath(self.dir, self.dest_rel_path);
45 try dest_builder.updateFile(full_src_path, full_dest_path);45 const cwd = std.fs.cwd();
46 const prev = std.fs.Dir.updateFile(cwd, full_src_path, cwd, full_dest_path, .{}) catch |err| {
47 return step.fail("unable to update file from '{s}' to '{s}': {s}", .{
48 full_src_path, full_dest_path, @errorName(err),
49 });
50 };
51 step.result_cached = prev == .fresh;
46}52}