Skip to content

Commit a8c82b7

Browse files
a-khabarovuser.name
authored andcommitted
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 d71b118 commit a8c82b7

File tree

4 files changed

+17
-5
lines changed

4 files changed

+17
-5
lines changed

lib/std/Build/Step.zig

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,7 @@ pub fn addError(step: *Step, comptime fmt: []const u8, args: anytype) error{OutO
316316
/// is the zig compiler - the same version that compiled the build runner.
317317
pub fn evalZigProcess(
318318
s: *Step,
319+
opt_cwd: ?[]const u8,
319320
argv: []const []const u8,
320321
prog_node: std.Progress.Node,
321322
) !?[]const u8 {
@@ -324,8 +325,8 @@ pub fn evalZigProcess(
324325
const arena = b.allocator;
325326
const gpa = arena;
326327

327-
try handleChildProcUnsupported(s, null, argv);
328-
try handleVerbose(s.owner, null, argv);
328+
try handleChildProcUnsupported(s, opt_cwd, argv);
329+
try handleVerbose(s.owner, opt_cwd, argv);
329330

330331
var child = std.process.Child.init(argv, arena);
331332
child.env_map = &b.graph.env_map;
@@ -334,6 +335,7 @@ pub fn evalZigProcess(
334335
child.stderr_behavior = .Pipe;
335336
child.request_resource_usage_statistics = true;
336337
child.progress_node = prog_node;
338+
child.cwd = opt_cwd;
337339

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

lib/std/Build/Step/Compile.zig

Lines changed: 11 additions & 1 deletion
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;
@@ -214,6 +215,9 @@ is_linking_libcpp: bool = false,
214215

215216
no_builtin: bool = false,
216217

218+
/// Use `setCwd` to set the initial current working directory
219+
cwd: ?Build.LazyPath = null,
220+
217221
pub const ExpectedCompileErrors = union(enum) {
218222
contains: []const u8,
219223
exact: []const []const u8,
@@ -1739,7 +1743,8 @@ fn make(step: *Step, prog_node: std.Progress.Node) !void {
17391743
try zig_args.append(resolved_args_file);
17401744
}
17411745

1742-
const maybe_output_bin_path = step.evalZigProcess(zig_args.items, prog_node) catch |err| switch (err) {
1746+
const cwd: ?[]const u8 = if (compile.cwd) |lazy_cwd| lazy_cwd.getPath(b) else null;
1747+
const maybe_output_bin_path = step.evalZigProcess(cwd, zig_args.items, prog_node) catch |err| switch (err) {
17431748
error.NeedCompileErrorCheck => {
17441749
assert(compile.expect_errors != null);
17451750
try checkCompileErrors(compile);
@@ -1982,3 +1987,8 @@ fn moduleNeedsCliArg(mod: *const Module) bool {
19821987
else => continue,
19831988
} else false;
19841989
}
1990+
1991+
pub fn setCwd(self: *Compile, cwd: Build.LazyPath) void {
1992+
cwd.addStepDependencies(&self.step);
1993+
self.cwd = cwd;
1994+
}

lib/std/Build/Step/ObjCopy.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ fn make(step: *Step, prog_node: std.Progress.Node) !void {
161161
try argv.appendSlice(&.{ full_src_path, full_dest_path });
162162

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

166166
objcopy.output_file.path = full_dest_path;
167167
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
@@ -154,7 +154,7 @@ fn make(step: *Step, prog_node: std.Progress.Node) !void {
154154

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

157-
const output_path = try step.evalZigProcess(argv_list.items, prog_node);
157+
const output_path = try step.evalZigProcess(null, argv_list.items, prog_node);
158158

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

0 commit comments

Comments
 (0)