Skip to content

Commit fe94629

Browse files
committed
Allow setting working directory for compile steps
Current working directory affects the linking behavior when `-L` is a relative path. We need this to add a test case to `test/link/elf.zig` for checking `-l`/`-L` behavior compatibility with Clang/GCC for ELF.
1 parent 594d224 commit fe94629

File tree

4 files changed

+17
-4
lines changed

4 files changed

+17
-4
lines changed

lib/std/Build/Step.zig

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,7 @@ pub const ZigProcess = struct {
370370
/// is the zig compiler - the same version that compiled the build runner.
371371
pub fn evalZigProcess(
372372
s: *Step,
373+
opt_cwd: ?[]const u8,
373374
argv: []const []const u8,
374375
prog_node: std.Progress.Node,
375376
watch: bool,
@@ -411,8 +412,8 @@ pub fn evalZigProcess(
411412
const arena = b.allocator;
412413
const gpa = arena;
413414

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

417418
var child = std.process.Child.init(argv, arena);
418419
child.env_map = &b.graph.env_map;
@@ -421,6 +422,7 @@ pub fn evalZigProcess(
421422
child.stderr_behavior = .Pipe;
422423
child.request_resource_usage_statistics = true;
423424
child.progress_node = prog_node;
425+
child.cwd = opt_cwd;
424426

425427
child.spawn() catch |err| return s.fail("unable to spawn {s}: {s}", .{
426428
argv[0], @errorName(err),

lib/std/Build/Step/Compile.zig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const builtin = @import("builtin");
22
const std = @import("std");
3+
const Build = std.Build;
34
const mem = std.mem;
45
const fs = std.fs;
56
const assert = std.debug.assert;
@@ -226,6 +227,9 @@ zig_process: ?*Step.ZigProcess,
226227
/// flag in `Module`.
227228
sanitize_coverage_trace_pc_guard: ?bool = null,
228229

230+
/// Use `setCwd` to set the initial current working directory
231+
cwd: ?Build.LazyPath = null,
232+
229233
pub const ExpectedCompileErrors = union(enum) {
230234
contains: []const u8,
231235
exact: []const []const u8,
@@ -1778,9 +1782,11 @@ fn make(step: *Step, options: Step.MakeOptions) !void {
17781782
const b = step.owner;
17791783
const compile: *Compile = @fieldParentPtr("step", step);
17801784

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

17831788
const maybe_output_bin_path = step.evalZigProcess(
1789+
cwd,
17841790
zig_args,
17851791
options.progress_node,
17861792
(b.graph.incremental == true) and options.watch,
@@ -2042,3 +2048,8 @@ fn moduleNeedsCliArg(mod: *const Module) bool {
20422048
else => continue,
20432049
} else false;
20442050
}
2051+
2052+
pub fn setCwd(self: *Compile, cwd: Build.LazyPath) void {
2053+
cwd.addStepDependencies(&self.step);
2054+
self.cwd = cwd;
2055+
}

lib/std/Build/Step/ObjCopy.zig

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

161161
try argv.append("--listen=-");
162-
_ = try step.evalZigProcess(argv.items, prog_node, false);
162+
_ = try step.evalZigProcess(null, argv.items, prog_node, false);
163163

164164
objcopy.output_file.path = full_dest_path;
165165
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
@@ -155,7 +155,7 @@ fn make(step: *Step, options: Step.MakeOptions) !void {
155155

156156
try argv_list.append(translate_c.source.getPath2(b, step));
157157

158-
const output_path = try step.evalZigProcess(argv_list.items, prog_node, false);
158+
const output_path = try step.evalZigProcess(null, argv_list.items, prog_node, false);
159159

160160
translate_c.out_basename = fs.path.basename(output_path.?);
161161
const output_dir = fs.path.dirname(output_path.?).?;

0 commit comments

Comments
 (0)