setcap and sudo args

This commit is contained in:
Motiejus Jakštys 2024-10-15 20:19:37 +03:00
parent 4946963af3
commit fe4f09d546

View File

@ -10,7 +10,8 @@ pub fn build(b: *std.Build) void {
.optimize = optimize, .optimize = optimize,
}); });
const sudo = b.option([]const u8, "sudo", "sudo command") orelse null; const setcap = b.option(bool, "setcap", "run setcap before executing the test binary") orelse false;
const sudo = b.option([]const u8, "sudo", "sudo command for setcap command") orelse null;
// This declares intent for the executable to be installed into the // This declares intent for the executable to be installed into the
// standard location when the user invokes the "install" step (the default // standard location when the user invokes the "install" step (the default
@ -46,15 +47,20 @@ pub fn build(b: *std.Build) void {
.optimize = optimize, .optimize = optimize,
}); });
const set_cap = b.addSystemCommand(if (sudo) |cmd| const run_unit_tests = b.addRunArtifact(unit_tests);
if (setcap and sudo == null) {
std.log.warn("non-null setcap and no sudo command probably won't work. Add sudo.", .{});
}
if (setcap) {
const set_cap_cmd = b.addSystemCommand(if (sudo) |cmd|
&.{ cmd, "setcap", "cap_net_admin=+ep" } &.{ cmd, "setcap", "cap_net_admin=+ep" }
else else
&.{ "setcap", "cap_net_admin=+ep" }); &.{ "setcap", "cap_net_admin=+ep" });
set_cap.addArtifactArg(unit_tests); set_cap_cmd.addArtifactArg(unit_tests);
run_unit_tests.step.dependOn(&set_cap_cmd.step);
const run_unit_tests = b.addRunArtifact(unit_tests); }
run_unit_tests.step.dependOn(&set_cap.step);
// Similar to creating the run step earlier, this exposes a `test` step to // Similar to creating the run step earlier, this exposes a `test` step to
// the `zig build --help` menu, providing a way for the user to request // the `zig build --help` menu, providing a way for the user to request