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
12351235 self.installed_files.append(file.dupe(self)) catch @panic("OOM");
12361236}
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
12501238pub fn truncateFile(self: *Build, dest_path: []const u8) !void {
12511239 if (self.verbose) {
12521240 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 {
19101910 const build_output_dir = fs.path.dirname(output_bin_path).?;
19111911
19121912 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, .{});
19141914 defer src_dir.close();
19151915
19161916 // 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, .{});
19201920 defer dest_dir.close();
19211921
19221922 var it = src_dir.iterate();
lib/std/Build/InstallArtifactStep.zig+44-9
......@@ -3,6 +3,7 @@ const Step = std.Build.Step;
33const CompileStep = std.Build.CompileStep;
44const InstallDir = std.Build.InstallDir;
55const InstallArtifactStep = @This();
6const fs = std.fs;
67
78pub const base_id = .install_artifact;
89
......@@ -77,25 +78,59 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
7778
7879 const dest_sub_path = if (self.dest_sub_path) |sub_path| sub_path else self.artifact.out_filename;
7980 const full_dest_path = dest_builder.getInstallPath(self.dest_dir, dest_sub_path);
81 const cwd = fs.cwd();
8082
81 try src_builder.updateFile(
82 self.artifact.getOutputSource().getPath(src_builder),
83 full_dest_path,
84 );
85 if (self.artifact.isDynamicLibrary() and self.artifact.version != null and self.artifact.target.wantSharedLibSymLinks()) {
83 var all_cached = true;
84
85 {
86 const full_src_path = self.artifact.getOutputSource().getPath(src_builder);
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 {
8699 try CompileStep.doAtomicSymLinks(step, full_dest_path, self.artifact.major_only_filename.?, self.artifact.name_only_filename.?);
87100 }
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);
89106 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;
91113 }
92114 if (self.pdb_dir) |pdb_dir| {
115 const full_src_path = self.artifact.getOutputPdbSource().getPath(src_builder);
93116 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;
95123 }
96124 if (self.h_dir) |h_dir| {
125 const full_src_path = self.artifact.getOutputHSource().getPath(src_builder);
97126 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;
99133 }
100134 self.artifact.installed_path = full_dest_path;
135 step.result_cached = all_cached;
101136}
lib/std/Build/InstallDirStep.zig+6-2
......@@ -89,13 +89,17 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
8989 }
9090 }
9191
92 const prev_status = try fs.Dir.updateFile(
92 const prev_status = fs.Dir.updateFile(
9393 src_builder.build_root.handle,
9494 src_sub_path,
9595 cwd,
9696 dest_path,
9797 .{},
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 };
99103 all_cached = all_cached and prev_status == .fresh;
100104 },
101105 else => continue,
lib/std/Build/InstallFileStep.zig+7-1
......@@ -42,5 +42,11 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
4242 const dest_builder = self.dest_builder;
4343 const full_src_path = self.source.getPath2(src_builder, step);
4444 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;
4652}