-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathloggyrunstep.zig
55 lines (49 loc) · 2.06 KB
/
loggyrunstep.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
const builtin = @import("builtin");
const std = @import("std");
const RunStep = std.build.RunStep;
const print = std.debug.print;
// This saves the RunStep.make function pointer because it is private
var global_run_step_make: switch (builtin.zig_backend) {
.stage1 => ?fn(step: *std.build.Step, prog_node: *std.Progress.Node) anyerror!void,
else => ?*const fn(step: *std.build.Step, prog_node: *std.Progress.Node) anyerror!void,
} = null;
pub fn enable(run_step: *RunStep) void {
// TODO: use an atomic operation
if (global_run_step_make) |make| {
std.debug.assert(run_step.step.makeFn == make);
} else {
global_run_step_make = run_step.step.makeFn;
}
run_step.step.makeFn = loggyRunStepMake;
}
fn printCmd(cwd: ?[]const u8, argv: []const []const u8) void {
if (cwd) |yes_cwd| print("cd {s} && ", .{yes_cwd});
for (argv) |arg| {
print("{s} ", .{arg});
}
print("\n", .{});
}
fn loggyRunStepMake(step: *std.build.Step, prog_node: *std.Progress.Node) !void {
const self = @fieldParentPtr(RunStep, "step", step);
const cwd = if (self.cwd) |cwd| self.step.owner.pathFromRoot(cwd) else self.step.owner.build_root.path.?;
var argv_list = std.ArrayList([]const u8).init(self.step.owner.allocator);
for (self.argv.items) |arg| {
switch (arg) {
.bytes => |bytes| try argv_list.append(bytes),
.lazy_path => |file| {
if (file.prefix.len != 0) @panic("todo");
try argv_list.append(file.lazy_path.getPath(self.step.owner));
},
.directory_source => @panic("todo"),
.artifact => |artifact| {
const executable_path = artifact.installed_path orelse artifact.getOutputSource().getPath(self.step.owner);
try argv_list.append(executable_path);
},
.output => |output| {
std.debug.panic("todo: convert output '{}' to argv string", .{output});
},
}
}
printCmd(cwd, argv_list.items);
return global_run_step_make.?(step, prog_node);
}