authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-07-30 18:19:39-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-07-30 18:42:08-07:00
log25a9487caa05c16f9f87cb6931340e1a1051a7f3
tree02fdf3a43117dc69329dabef365732f8827ca898
parentbdbd6172371543c6e1c80ea3f6abcb87702eeb08

std.Build.LazyPath: fix resolution of cwd_relative

The callsites of getPath rely on the result being absolute so that they can pass the path to a child process with the cwd set to the build root.

4 files changed, 26 insertions(+), 23 deletions(-)

build.zig+1-1
...@@ -46,7 +46,7 @@ pub fn build(b: *std.Build) !void {...@@ -46,7 +46,7 @@ pub fn build(b: *std.Build) !void {
46 docgen_cmd.addArgs(&.{ "--zig", b.zig_exe });46 docgen_cmd.addArgs(&.{ "--zig", b.zig_exe });
47 if (b.zig_lib_dir) |p| {47 if (b.zig_lib_dir) |p| {
48 docgen_cmd.addArg("--zig-lib-dir");48 docgen_cmd.addArg("--zig-lib-dir");
49 docgen_cmd.addFileArg(p);49 docgen_cmd.addDirectoryArg(p);
50 }50 }
51 docgen_cmd.addFileArg(.{ .path = "doc/langref.html.in" });51 docgen_cmd.addFileArg(.{ .path = "doc/langref.html.in" });
52 const langref_file = docgen_cmd.addOutputFileArg("langref.html");52 const langref_file = docgen_cmd.addOutputFileArg("langref.html");
doc/docgen.zig+3-1
...@@ -64,7 +64,9 @@ pub fn main() !void {...@@ -64,7 +64,9 @@ pub fn main() !void {
64 }64 }
65 } else if (mem.eql(u8, arg, "--zig-lib-dir")) {65 } else if (mem.eql(u8, arg, "--zig-lib-dir")) {
66 if (args_it.next()) |param| {66 if (args_it.next()) |param| {
67 opt_zig_lib_dir = param;67 // Convert relative to absolute because this will be passed
68 // to a child process with a different cwd.
69 opt_zig_lib_dir = try fs.realpathAlloc(allocator, param);
68 } else {70 } else {
69 fatal("expected parameter after --zig-lib-dir", .{});71 fatal("expected parameter after --zig-lib-dir", .{});
70 }72 }
lib/std/Build.zig+8-7
...@@ -1390,6 +1390,11 @@ pub fn pathFromRoot(b: *Build, p: []const u8) []u8 {...@@ -1390,6 +1390,11 @@ pub fn pathFromRoot(b: *Build, p: []const u8) []u8 {
1390 return fs.path.resolve(b.allocator, &.{ b.build_root.path orelse ".", p }) catch @panic("OOM");1390 return fs.path.resolve(b.allocator, &.{ b.build_root.path orelse ".", p }) catch @panic("OOM");
1391}1391}
13921392
1393fn pathFromCwd(b: *Build, p: []const u8) []u8 {
1394 const cwd = process.getCwdAlloc(b.allocator) catch @panic("OOM");
1395 return fs.path.resolve(b.allocator, &.{ cwd, p }) catch @panic("OOM");
1396}
1397
1393pub fn pathJoin(self: *Build, paths: []const []const u8) []u8 {1398pub fn pathJoin(self: *Build, paths: []const []const u8) []u8 {
1394 return fs.path.join(self.allocator, paths) catch @panic("OOM");1399 return fs.path.join(self.allocator, paths) catch @panic("OOM");
1395}1400}
...@@ -1706,17 +1711,13 @@ pub const LazyPath = union(enum) {...@@ -1706,17 +1711,13 @@ pub const LazyPath = union(enum) {
1706 }1711 }
1707 }1712 }
17081713
1709 /// Returns a path relative to the current process's current working directory, suitable1714 /// Returns an absolute path.
1710 /// for direct file system operations.
1711 ///
1712 /// Intended to be used during the make phase only.1715 /// Intended to be used during the make phase only.
1713 pub fn getPath(self: LazyPath, src_builder: *Build) []const u8 {1716 pub fn getPath(self: LazyPath, src_builder: *Build) []const u8 {
1714 return getPath2(self, src_builder, null);1717 return getPath2(self, src_builder, null);
1715 }1718 }
17161719
1717 /// Returns a path relative to the current process's current working directory, suitable1720 /// Returns an absolute path.
1718 /// for direct file system operations.
1719 ///
1720 /// Intended to be used during the make phase only.1721 /// Intended to be used during the make phase only.
1721 ///1722 ///
1722 /// `asking_step` is only used for debugging purposes; it's the step being1723 /// `asking_step` is only used for debugging purposes; it's the step being
...@@ -1724,7 +1725,7 @@ pub const LazyPath = union(enum) {...@@ -1724,7 +1725,7 @@ pub const LazyPath = union(enum) {
1724 pub fn getPath2(self: LazyPath, src_builder: *Build, asking_step: ?*Step) []const u8 {1725 pub fn getPath2(self: LazyPath, src_builder: *Build, asking_step: ?*Step) []const u8 {
1725 switch (self) {1726 switch (self) {
1726 .path => |p| return src_builder.pathFromRoot(p),1727 .path => |p| return src_builder.pathFromRoot(p),
1727 .cwd_relative => |p| return p,1728 .cwd_relative => |p| return src_builder.pathFromCwd(p),
1728 .generated => |gen| return gen.path orelse {1729 .generated => |gen| return gen.path orelse {
1729 std.debug.getStderrMutex().lock();1730 std.debug.getStderrMutex().lock();
1730 const stderr = std.io.getStdErr();1731 const stderr = std.io.getStdErr();
lib/std/Build/Step/Run.zig+14-14
...@@ -82,7 +82,7 @@ has_side_effects: bool = false,...@@ -82,7 +82,7 @@ has_side_effects: bool = false,
82pub const StdIn = union(enum) {82pub const StdIn = union(enum) {
83 none,83 none,
84 bytes: []const u8,84 bytes: []const u8,
85 file_source: std.Build.LazyPath,85 lazy_path: std.Build.LazyPath,
86};86};
8787
88pub const StdIo = union(enum) {88pub const StdIo = union(enum) {
...@@ -120,7 +120,7 @@ pub const StdIo = union(enum) {...@@ -120,7 +120,7 @@ pub const StdIo = union(enum) {
120120
121pub const Arg = union(enum) {121pub const Arg = union(enum) {
122 artifact: *Step.Compile,122 artifact: *Step.Compile,
123 file_source: PrefixedLazyPath,123 lazy_path: PrefixedLazyPath,
124 directory_source: PrefixedLazyPath,124 directory_source: PrefixedLazyPath,
125 bytes: []u8,125 bytes: []u8,
126 output: *Output,126 output: *Output,
...@@ -128,7 +128,7 @@ pub const Arg = union(enum) {...@@ -128,7 +128,7 @@ pub const Arg = union(enum) {
128128
129pub const PrefixedLazyPath = struct {129pub const PrefixedLazyPath = struct {
130 prefix: []const u8,130 prefix: []const u8,
131 file_source: std.Build.LazyPath,131 lazy_path: std.Build.LazyPath,
132};132};
133133
134pub const Output = struct {134pub const Output = struct {
...@@ -213,9 +213,9 @@ pub fn addPrefixedFileArg(self: *Run, prefix: []const u8, lp: std.Build.LazyPath...@@ -213,9 +213,9 @@ pub fn addPrefixedFileArg(self: *Run, prefix: []const u8, lp: std.Build.LazyPath
213213
214 const prefixed_file_source: PrefixedLazyPath = .{214 const prefixed_file_source: PrefixedLazyPath = .{
215 .prefix = b.dupe(prefix),215 .prefix = b.dupe(prefix),
216 .file_source = lp.dupe(b),216 .lazy_path = lp.dupe(b),
217 };217 };
218 self.argv.append(.{ .file_source = prefixed_file_source }) catch @panic("OOM");218 self.argv.append(.{ .lazy_path = prefixed_file_source }) catch @panic("OOM");
219 lp.addStepDependencies(&self.step);219 lp.addStepDependencies(&self.step);
220}220}
221221
...@@ -234,7 +234,7 @@ pub fn addPrefixedDirectoryArg(self: *Run, prefix: []const u8, directory_source:...@@ -234,7 +234,7 @@ pub fn addPrefixedDirectoryArg(self: *Run, prefix: []const u8, directory_source:
234234
235 const prefixed_directory_source: PrefixedLazyPath = .{235 const prefixed_directory_source: PrefixedLazyPath = .{
236 .prefix = b.dupe(prefix),236 .prefix = b.dupe(prefix),
237 .file_source = directory_source.dupe(b),237 .lazy_path = directory_source.dupe(b),
238 };238 };
239 self.argv.append(.{ .directory_source = prefixed_directory_source }) catch @panic("OOM");239 self.argv.append(.{ .directory_source = prefixed_directory_source }) catch @panic("OOM");
240 directory_source.addStepDependencies(&self.step);240 directory_source.addStepDependencies(&self.step);
...@@ -252,7 +252,7 @@ pub fn addArgs(self: *Run, args: []const []const u8) void {...@@ -252,7 +252,7 @@ pub fn addArgs(self: *Run, args: []const []const u8) void {
252252
253pub fn setStdIn(self: *Run, stdin: StdIn) void {253pub fn setStdIn(self: *Run, stdin: StdIn) void {
254 switch (stdin) {254 switch (stdin) {
255 .file_source => |file_source| file_source.addStepDependencies(&self.step),255 .lazy_path => |lazy_path| lazy_path.addStepDependencies(&self.step),
256 .bytes, .none => {},256 .bytes, .none => {},
257 }257 }
258 self.stdin = stdin;258 self.stdin = stdin;
...@@ -444,14 +444,14 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {...@@ -444,14 +444,14 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
444 try argv_list.append(bytes);444 try argv_list.append(bytes);
445 man.hash.addBytes(bytes);445 man.hash.addBytes(bytes);
446 },446 },
447 .file_source => |file| {447 .lazy_path => |file| {
448 const file_path = file.file_source.getPath(b);448 const file_path = file.lazy_path.getPath(b);
449 try argv_list.append(b.fmt("{s}{s}", .{ file.prefix, file_path }));449 try argv_list.append(b.fmt("{s}{s}", .{ file.prefix, file_path }));
450 man.hash.addBytes(file.prefix);450 man.hash.addBytes(file.prefix);
451 _ = try man.addFile(file_path, null);451 _ = try man.addFile(file_path, null);
452 },452 },
453 .directory_source => |file| {453 .directory_source => |file| {
454 const file_path = file.file_source.getPath(b);454 const file_path = file.lazy_path.getPath(b);
455 try argv_list.append(b.fmt("{s}{s}", .{ file.prefix, file_path }));455 try argv_list.append(b.fmt("{s}{s}", .{ file.prefix, file_path }));
456 man.hash.addBytes(file.prefix);456 man.hash.addBytes(file.prefix);
457 man.hash.addBytes(file_path);457 man.hash.addBytes(file_path);
...@@ -486,8 +486,8 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {...@@ -486,8 +486,8 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
486 .bytes => |bytes| {486 .bytes => |bytes| {
487 man.hash.addBytes(bytes);487 man.hash.addBytes(bytes);
488 },488 },
489 .file_source => |file_source| {489 .lazy_path => |lazy_path| {
490 const file_path = file_source.getPath(b);490 const file_path = lazy_path.getPath(b);
491 _ = try man.addFile(file_path, null);491 _ = try man.addFile(file_path, null);
492 },492 },
493 .none => {},493 .none => {},
...@@ -1186,8 +1186,8 @@ fn evalGeneric(self: *Run, child: *std.process.Child) !StdIoResult {...@@ -1186,8 +1186,8 @@ fn evalGeneric(self: *Run, child: *std.process.Child) !StdIoResult {
1186 child.stdin.?.close();1186 child.stdin.?.close();
1187 child.stdin = null;1187 child.stdin = null;
1188 },1188 },
1189 .file_source => |file_source| {1189 .lazy_path => |lazy_path| {
1190 const path = file_source.getPath(self.step.owner);1190 const path = lazy_path.getPath(self.step.owner);
1191 const file = self.step.owner.build_root.handle.openFile(path, .{}) catch |err| {1191 const file = self.step.owner.build_root.handle.openFile(path, .{}) catch |err| {
1192 return self.step.fail("unable to open stdin file: {s}", .{@errorName(err)});1192 return self.step.fail("unable to open stdin file: {s}", .{@errorName(err)});
1193 };1193 };