loggyrunstep.zig 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. const std = @import("std");
  2. const RunStep = std.build.RunStep;
  3. const print = std.debug.print;
  4. // This saves the RunStep.make function pointer because it is private
  5. var global_run_step_make: ?fn(step: *std.build.Step) anyerror!void = null;
  6. pub fn enable(run_step: *RunStep) void {
  7. // TODO: use an atomic operation
  8. if (global_run_step_make) |make| {
  9. std.debug.assert(run_step.step.makeFn == make);
  10. } else {
  11. global_run_step_make = run_step.step.makeFn;
  12. }
  13. run_step.step.makeFn = loggyRunStepMake;
  14. }
  15. fn printCmd(cwd: ?[]const u8, argv: []const []const u8) void {
  16. if (cwd) |yes_cwd| print("cd {s} && ", .{yes_cwd});
  17. for (argv) |arg| {
  18. print("{s} ", .{arg});
  19. }
  20. print("\n", .{});
  21. }
  22. fn loggyRunStepMake(step: *std.build.Step) !void {
  23. const self = @fieldParentPtr(RunStep, "step", step);
  24. const cwd = if (self.cwd) |cwd| self.builder.pathFromRoot(cwd) else self.builder.build_root;
  25. var argv_list = std.ArrayList([]const u8).init(self.builder.allocator);
  26. for (self.argv.items) |arg| {
  27. switch (arg) {
  28. .bytes => |bytes| try argv_list.append(bytes),
  29. .file_source => |file| try argv_list.append(file.getPath(self.builder)),
  30. .artifact => |artifact| {
  31. const executable_path = artifact.installed_path orelse artifact.getOutputSource().getPath(self.builder);
  32. try argv_list.append(executable_path);
  33. },
  34. }
  35. }
  36. printCmd(cwd, argv_list.items);
  37. return global_run_step_make.?(step);
  38. }