Skip to content

Commit b186999

Browse files
committed
build: split --verbose to specific flags
Usage (excluding initialization) of `build.verbose` appears in two files: 1. lib/std/Build.zig 2. lib/std/Build/Step.zig As it turns out only Step.zig is relevant in the new context of `--verbose-spawn`. The verbose references in Build.zig are through `makeUninstall` and `truncateFile`. The first of which is a bunch of deletion operations, and the second of which only got called from InstallDir.zig. The `truncateFile` operation in itself is general enough to not depend on a specific flag and the log call is therefore hoisted to the InstallDir.zig callsite. The logging in InstallDir is then enabled by a new parameter `--verbose-install`. I also took the liberty of adding logging to the result of updateFile operations as well under this flag. The updateFile logic is also used in InstallFile and InstallArtifact, so the helper `handleVerboseInstallUpdateFile` was created and added to all call site results. Regarding audit of child process invocations in Build.zig and default Steps: `execAllowFail` is the child process spawning part of the Build API that did not log invocations, so a call to `handleVerboseSpawn2` was added. The other APIs are `evalZigProcess` and `evalChildProcess`, but they already use the `handleVerboseSpawn` helper. Closes ziglang#14970
1 parent 1bf16b1 commit b186999

File tree

7 files changed

+56
-21
lines changed

7 files changed

+56
-21
lines changed

lib/build_runner.zig

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,12 @@ pub fn main() !void {
114114
usageAndErr(builder, false, stderr_stream);
115115
}
116116
} else if (mem.startsWith(u8, arg, "-")) {
117-
if (mem.eql(u8, arg, "--verbose")) {
118-
builder.verbose = true;
117+
if (mem.eql(u8, arg, "--verbose-spawn")) {
118+
builder.verbose_spawn = true;
119+
} else if (mem.eql(u8, arg, "--verbose-install")) {
120+
builder.verbose_install = true;
121+
} else if (mem.eql(u8, arg, "--verbose-uninstall")) {
122+
builder.verbose_uninstall = true;
119123
} else if (mem.eql(u8, arg, "-h") or mem.eql(u8, arg, "--help")) {
120124
return usage(builder, false, stdout_stream);
121125
} else if (mem.eql(u8, arg, "-p") or mem.eql(u8, arg, "--prefix")) {
@@ -970,7 +974,9 @@ fn usage(builder: *std.Build, already_ran_build: bool, out_stream: anytype) !voi
970974
\\
971975
\\ -h, --help Print this help and exit
972976
\\ -l, --list-steps Print available steps
973-
\\ --verbose Print commands before executing them
977+
\\ --verbose-spawn Print to stderr the commandline before spawning any child process
978+
\\ --verbose-install Print to stderr more info during install step (copying build artifacts)
979+
\\ --verbose-uninstall Print to stderr more info during uninstall step (removing build artifacts)
974980
\\ --color [auto|off|on] Enable or disable colored error messages
975981
\\ --summary [mode] Control the printing of the build summary
976982
\\ all Print the build summary in its entirety

lib/std/Build.zig

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ allocator: Allocator,
6464
user_input_options: UserInputOptionsMap,
6565
available_options_map: AvailableOptionsMap,
6666
available_options_list: ArrayList(AvailableOption),
67-
verbose: bool,
67+
verbose_spawn: bool,
68+
verbose_install: bool,
69+
verbose_uninstall: bool,
6870
verbose_link: bool,
6971
verbose_cc: bool,
7072
verbose_air: bool,
@@ -222,7 +224,9 @@ pub fn create(
222224
.cache_root = cache_root,
223225
.global_cache_root = global_cache_root,
224226
.cache = cache,
225-
.verbose = false,
227+
.verbose_spawn = false,
228+
.verbose_install = false,
229+
.verbose_uninstall = false,
226230
.verbose_link = false,
227231
.verbose_cc = false,
228232
.verbose_air = false,
@@ -311,7 +315,9 @@ fn createChildOnly(parent: *Build, dep_name: []const u8, build_root: Cache.Direc
311315
.user_input_options = UserInputOptionsMap.init(allocator),
312316
.available_options_map = AvailableOptionsMap.init(allocator),
313317
.available_options_list = ArrayList(AvailableOption).init(allocator),
314-
.verbose = parent.verbose,
318+
.verbose_spawn = parent.verbose_spawn,
319+
.verbose_install = parent.verbose_install,
320+
.verbose_uninstall = parent.verbose_uninstall,
315321
.verbose_link = parent.verbose_link,
316322
.verbose_cc = parent.verbose_cc,
317323
.verbose_air = parent.verbose_air,
@@ -796,7 +802,8 @@ fn makeUninstall(uninstall_step: *Step, prog_node: *std.Progress.Node) anyerror!
796802

797803
for (self.installed_files.items) |installed_file| {
798804
const full_path = self.getInstallPath(installed_file.dir, installed_file.path);
799-
if (self.verbose) {
805+
if (self.verbose_uninstall) {
806+
// log implementation must acquire stdErr mutex during parallel builds
800807
log.info("rm {s}", .{full_path});
801808
}
802809
fs.cwd().deleteTree(full_path) catch {};
@@ -1336,10 +1343,7 @@ pub fn pushInstalledFile(self: *Build, dir: InstallDir, dest_rel_path: []const u
13361343
self.installed_files.append(file.dupe(self)) catch @panic("OOM");
13371344
}
13381345

1339-
pub fn truncateFile(self: *Build, dest_path: []const u8) !void {
1340-
if (self.verbose) {
1341-
log.info("truncate {s}", .{dest_path});
1342-
}
1346+
pub fn truncateFile(dest_path: []const u8) !void {
13431347
const cwd = fs.cwd();
13441348
var src_file = cwd.createFile(dest_path, .{}) catch |err| switch (err) {
13451349
error.FileNotFound => blk: {
@@ -1429,6 +1433,7 @@ pub fn execAllowFail(
14291433
child.stderr_behavior = stderr_behavior;
14301434
child.env_map = self.env_map;
14311435

1436+
try Step.handleVerboseSpawn2(self, child.cwd, child.env_map, argv);
14321437
try child.spawn();
14331438

14341439
const stdout = child.stdout.?.reader().readAllAlloc(self.allocator, max_output_size) catch {

lib/std/Build/Step.zig

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ pub fn evalChildProcess(s: *Step, argv: []const []const u8) !void {
263263
const arena = s.owner.allocator;
264264

265265
try handleChildProcUnsupported(s, null, argv);
266-
try handleVerbose(s.owner, null, argv);
266+
try handleVerboseSpawn(s.owner, null, argv);
267267

268268
const result = std.ChildProcess.exec(.{
269269
.allocator = arena,
@@ -301,7 +301,7 @@ pub fn evalZigProcess(
301301
const gpa = arena;
302302

303303
try handleChildProcUnsupported(s, null, argv);
304-
try handleVerbose(s.owner, null, argv);
304+
try handleVerboseSpawn(s.owner, null, argv);
305305

306306
var child = std.ChildProcess.init(argv, arena);
307307
child.env_map = b.env_map;
@@ -437,22 +437,22 @@ fn sendMessage(file: std.fs.File, tag: std.zig.Client.Message.Tag) !void {
437437
try file.writeAll(std.mem.asBytes(&header));
438438
}
439439

440-
pub fn handleVerbose(
440+
pub fn handleVerboseSpawn(
441441
b: *Build,
442442
opt_cwd: ?[]const u8,
443443
argv: []const []const u8,
444444
) error{OutOfMemory}!void {
445-
return handleVerbose2(b, opt_cwd, null, argv);
445+
return handleVerboseSpawn2(b, opt_cwd, null, argv);
446446
}
447447

448-
pub fn handleVerbose2(
448+
pub fn handleVerboseSpawn2(
449449
b: *Build,
450450
opt_cwd: ?[]const u8,
451451
opt_env: ?*const std.process.EnvMap,
452452
argv: []const []const u8,
453453
) error{OutOfMemory}!void {
454-
if (b.verbose) {
455-
// Intention of verbose is to print all sub-process command lines to
454+
if (b.verbose_spawn) {
455+
// Intention of verbose_spawn is to print all sub-process command lines to
456456
// stderr before spawning them.
457457
const text = try allocPrintCmd2(b.allocator, opt_cwd, opt_env, argv);
458458
std.debug.print("{s}\n", .{text});
@@ -496,6 +496,20 @@ pub fn handleChildProcessTerm(
496496
},
497497
}
498498
}
499+
pub fn handleVerboseInstallUpdateFile(
500+
b: *Build,
501+
status_result: std.fs.PrevStatus,
502+
dest_path: []const u8,
503+
) void {
504+
if (b.verbose_install) {
505+
// log implementation responsible for acquiring stdErr mutex
506+
const action_was = switch (status_result) {
507+
.fresh => "cached",
508+
.stale => "update",
509+
};
510+
std.log.info("{s} {s}", .{ action_was, dest_path });
511+
}
512+
}
499513

500514
pub fn allocPrintCmd(
501515
arena: Allocator,

lib/std/Build/Step/InstallArtifact.zig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
8282
full_src_path, full_dest_path, @errorName(err),
8383
});
8484
};
85+
Step.handleVerboseInstallUpdateFile(dest_builder, p, full_dest_path);
8586
all_cached = all_cached and p == .fresh;
8687
}
8788

@@ -102,6 +103,7 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
102103
full_src_path, full_implib_path, @errorName(err),
103104
});
104105
};
106+
Step.handleVerboseInstallUpdateFile(dest_builder, p, full_implib_path);
105107
all_cached = all_cached and p == .fresh;
106108
}
107109
if (self.pdb_dir) |pdb_dir| {
@@ -112,6 +114,7 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
112114
full_src_path, full_pdb_path, @errorName(err),
113115
});
114116
};
117+
Step.handleVerboseInstallUpdateFile(dest_builder, p, full_pdb_path);
115118
all_cached = all_cached and p == .fresh;
116119
}
117120
if (self.h_dir) |h_dir| {
@@ -122,6 +125,7 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
122125
full_src_path, full_h_path, @errorName(err),
123126
});
124127
};
128+
Step.handleVerboseInstallUpdateFile(dest_builder, p, full_h_path);
125129
all_cached = all_cached and p == .fresh;
126130
}
127131
self.artifact.installed_path = full_dest_path;

lib/std/Build/Step/InstallDir.zig

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,11 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
8989
.file => {
9090
for (self.options.blank_extensions) |ext| {
9191
if (mem.endsWith(u8, entry.path, ext)) {
92-
try dest_builder.truncateFile(dest_path);
92+
if (dest_builder.verbose_install) {
93+
// log implementation responsible for acquiring stdErr mutex
94+
std.log.info("truncate {s}", .{dest_path});
95+
}
96+
try std.Build.truncateFile(dest_path);
9397
continue :next_entry;
9498
}
9599
}
@@ -105,6 +109,7 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
105109
src_builder.build_root, src_sub_path, dest_path, @errorName(err),
106110
});
107111
};
112+
Step.handleVerboseInstallUpdateFile(dest_builder, prev_status, dest_path);
108113
all_cached = all_cached and prev_status == .fresh;
109114
},
110115
else => continue,

lib/std/Build/Step/InstallFile.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,6 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
5353
full_src_path, full_dest_path, @errorName(err),
5454
});
5555
};
56+
Step.handleVerboseInstallUpdateFile(dest_builder, prev, full_dest_path);
5657
step.result_cached = prev == .fresh;
5758
}

lib/std/Build/Step/Run.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,7 @@ fn runCommand(
607607
const arena = b.allocator;
608608

609609
try step.handleChildProcUnsupported(self.cwd, argv);
610-
try Step.handleVerbose2(step.owner, self.cwd, self.env_map, argv);
610+
try Step.handleVerboseSpawn2(step.owner, self.cwd, self.env_map, argv);
611611

612612
const allow_skip = switch (self.stdio) {
613613
.check, .zig_test => self.skip_foreign_checks,
@@ -734,7 +734,7 @@ fn runCommand(
734734
self.addPathForDynLibs(exe);
735735
}
736736

737-
try Step.handleVerbose2(step.owner, self.cwd, self.env_map, interp_argv.items);
737+
try Step.handleVerboseSpawn2(step.owner, self.cwd, self.env_map, interp_argv.items);
738738

739739
break :term spawnChildAndCollect(self, interp_argv.items, has_side_effects, prog_node) catch |e| {
740740
if (!self.failing_to_execute_foreign_is_an_error) return error.MakeSkipped;

0 commit comments

Comments
 (0)