Skip to content

Commit 67441e6

Browse files
committed
lib: allow setting current directory for compile steps
We need this to implement the new test in #19818 This commits adds: * `setCwd` for `Build.Step.Compile`. Allows setting current directory for compile steps. * `opt_cwd` for `evalZigProcess` in `Build.Step`. Allows running `evalZigProcess` in a specific directory. Needed for setting current directory for compile steps. `--zig-lib-dir` is now appended differently in `Build.Step.Compile`. This is needed for compatibility with `setCwd`.
1 parent d708c4c commit 67441e6

File tree

4 files changed

+23
-9
lines changed

4 files changed

+23
-9
lines changed

lib/std/Build/Step.zig

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,7 @@ pub const ZigProcess = struct {
371371
/// is the zig compiler - the same version that compiled the build runner.
372372
pub fn evalZigProcess(
373373
s: *Step,
374+
opt_cwd: ?[]const u8,
374375
argv: []const []const u8,
375376
prog_node: std.Progress.Node,
376377
watch: bool,
@@ -401,7 +402,7 @@ pub fn evalZigProcess(
401402
};
402403
s.result_peak_rss = zp.child.resource_usage_statistics.getMaxRss() orelse 0;
403404
s.clearZigProcess();
404-
try handleChildProcessTerm(s, term, null, argv);
405+
try handleChildProcessTerm(s, term, opt_cwd, argv);
405406
return error.MakeFailed;
406407
}
407408

@@ -412,8 +413,8 @@ pub fn evalZigProcess(
412413
const arena = b.allocator;
413414
const gpa = arena;
414415

415-
try handleChildProcUnsupported(s, null, argv);
416-
try handleVerbose(s.owner, null, argv);
416+
try handleChildProcUnsupported(s, opt_cwd, argv);
417+
try handleVerbose(s.owner, opt_cwd, argv);
417418

418419
var child = std.process.Child.init(argv, arena);
419420
child.env_map = &b.graph.env_map;
@@ -422,6 +423,7 @@ pub fn evalZigProcess(
422423
child.stderr_behavior = .Pipe;
423424
child.request_resource_usage_statistics = true;
424425
child.progress_node = prog_node;
426+
child.cwd = opt_cwd;
425427

426428
child.spawn() catch |err| return s.fail("failed to spawn zig compiler {s}: {s}", .{
427429
argv[0], @errorName(err),
@@ -463,15 +465,15 @@ pub fn evalZigProcess(
463465
else => {},
464466
};
465467

466-
try handleChildProcessTerm(s, term, null, argv);
468+
try handleChildProcessTerm(s, term, opt_cwd, argv);
467469
}
468470

469471
// This is intentionally printed for failure on the first build but not for
470472
// subsequent rebuilds.
471473
if (s.result_error_bundle.errorMessageCount() > 0) {
472474
return s.fail("the following command failed with {d} compilation errors:\n{s}", .{
473475
s.result_error_bundle.errorMessageCount(),
474-
try allocPrintCmd(arena, null, argv),
476+
try allocPrintCmd(arena, opt_cwd, argv),
475477
});
476478
}
477479

lib/std/Build/Step/Compile.zig

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const fs = std.fs;
55
const assert = std.debug.assert;
66
const panic = std.debug.panic;
77
const ArrayList = std.ArrayList;
8+
const Build = std.Build;
89
const StringHashMap = std.StringHashMap;
910
const Sha256 = std.crypto.hash.sha2.Sha256;
1011
const Allocator = mem.Allocator;
@@ -243,6 +244,9 @@ zig_process: ?*Step.ZigProcess,
243244
/// builtin fuzzer, see the `fuzz` flag in `Module`.
244245
sanitize_coverage_trace_pc_guard: ?bool = null,
245246

247+
/// Use `setCwd` to set the initial current working directory
248+
cwd: ?Build.LazyPath = null,
249+
246250
pub const ExpectedCompileErrors = union(enum) {
247251
contains: []const u8,
248252
exact: []const []const u8,
@@ -1705,7 +1709,7 @@ fn getZigArgs(compile: *Compile, fuzz: bool) ![][]const u8 {
17051709

17061710
if (opt_zig_lib_dir) |zig_lib_dir| {
17071711
try zig_args.append("--zig-lib-dir");
1708-
try zig_args.append(zig_lib_dir);
1712+
try zig_args.append(try fs.cwd().realpathAlloc(arena, zig_lib_dir));
17091713
}
17101714

17111715
try addFlag(&zig_args, "PIE", compile.pie);
@@ -1803,9 +1807,11 @@ fn make(step: *Step, options: Step.MakeOptions) !void {
18031807
const b = step.owner;
18041808
const compile: *Compile = @fieldParentPtr("step", step);
18051809

1810+
const cwd: ?[]const u8 = if (compile.cwd) |lazy_cwd| lazy_cwd.getPath(b) else null;
18061811
const zig_args = try getZigArgs(compile, false);
18071812

18081813
const maybe_output_dir = step.evalZigProcess(
1814+
cwd,
18091815
zig_args,
18101816
options.progress_node,
18111817
(b.graph.incremental == true) and options.watch,
@@ -1888,8 +1894,9 @@ pub fn rebuildInFuzzMode(c: *Compile, progress_node: std.Progress.Node) !Path {
18881894
c.step.result_error_bundle.deinit(gpa);
18891895
c.step.result_error_bundle = std.zig.ErrorBundle.empty;
18901896

1897+
const cwd: ?[]const u8 = if (c.cwd) |lazy_cwd| lazy_cwd.getPath(c.step.owner) else null;
18911898
const zig_args = try getZigArgs(c, true);
1892-
const maybe_output_bin_path = try c.step.evalZigProcess(zig_args, progress_node, false);
1899+
const maybe_output_bin_path = try c.step.evalZigProcess(cwd, zig_args, progress_node, false);
18931900
return maybe_output_bin_path.?;
18941901
}
18951902

@@ -2130,3 +2137,8 @@ pub fn getCompileDependencies(start: *Compile, chase_dynamic: bool) []const *Com
21302137

21312138
return compiles.keys();
21322139
}
2140+
2141+
pub fn setCwd(self: *Compile, cwd: Build.LazyPath) void {
2142+
cwd.addStepDependencies(&self.step);
2143+
self.cwd = cwd;
2144+
}

lib/std/Build/Step/ObjCopy.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ fn make(step: *Step, options: Step.MakeOptions) !void {
236236
try argv.appendSlice(&.{ full_src_path, full_dest_path });
237237

238238
try argv.append("--listen=-");
239-
_ = try step.evalZigProcess(argv.items, prog_node, false);
239+
_ = try step.evalZigProcess(null, argv.items, prog_node, false);
240240

241241
objcopy.output_file.path = full_dest_path;
242242
if (objcopy.output_file_debug) |*file| file.path = full_dest_path_debug;

lib/std/Build/Step/TranslateC.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ fn make(step: *Step, options: Step.MakeOptions) !void {
200200
const c_source_path = translate_c.source.getPath2(b, step);
201201
try argv_list.append(c_source_path);
202202

203-
const output_dir = try step.evalZigProcess(argv_list.items, prog_node, false);
203+
const output_dir = try step.evalZigProcess(null, argv_list.items, prog_node, false);
204204

205205
const basename = std.fs.path.stem(std.fs.path.basename(c_source_path));
206206
translate_c.out_basename = b.fmt("{s}.zig", .{basename});

0 commit comments

Comments
 (0)