authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-07-09 23:14:18-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-07-12 00:14:08-07:00
log26bdc836d2d9b2654f7f95fec34c6276070f2a59
tree8a28bc65b8f5a783a8b2bc3ab08e4302aa3ec12b
parent0994e22a646a797aa91602a56a6a0cddf8cdbd28

std.Build.LazyPath: add getPath3; deprecate getPath2 and getPath

The goal is to move towards using `std.Build.Cache.Path` instead of absolute path names. This was helpful for implementing file watching integration to the InstallDir Step

1 files changed, 36 insertions(+), 17 deletions(-)

lib/std/Build.zig+36-17
......@@ -2327,36 +2327,52 @@ pub const LazyPath = union(enum) {
23272327 }
23282328 }
23292329
2330 /// Returns an absolute path.
2331 /// Intended to be used during the make phase only.
2330 /// Deprecated, see `getPath3`.
23322331 pub fn getPath(lazy_path: LazyPath, src_builder: *Build) []const u8 {
23332332 return getPath2(lazy_path, src_builder, null);
23342333 }
23352334
2336 /// Returns an absolute path.
2335 /// Deprecated, see `getPath3`.
2336 pub fn getPath2(lazy_path: LazyPath, src_builder: *Build, asking_step: ?*Step) []const u8 {
2337 const p = getPath3(lazy_path, src_builder, asking_step);
2338 return src_builder.pathResolve(&.{ p.root_dir.path orelse ".", p.sub_path });
2339 }
2340
23372341 /// Intended to be used during the make phase only.
23382342 ///
23392343 /// `asking_step` is only used for debugging purposes; it's the step being
23402344 /// run that is asking for the path.
2341 pub fn getPath2(lazy_path: LazyPath, src_builder: *Build, asking_step: ?*Step) []const u8 {
2345 pub fn getPath3(lazy_path: LazyPath, src_builder: *Build, asking_step: ?*Step) Cache.Path {
23422346 switch (lazy_path) {
2343 .src_path => |sp| return sp.owner.pathFromRoot(sp.sub_path),
2344 .cwd_relative => |p| return src_builder.pathFromCwd(p),
2347 .src_path => |sp| return .{
2348 .root_dir = sp.owner.build_root,
2349 .sub_path = sp.sub_path,
2350 },
2351 .cwd_relative => |sub_path| return .{
2352 .root_dir = Cache.Directory.cwd(),
2353 .sub_path = sub_path,
2354 },
23452355 .generated => |gen| {
2346 var file_path: []const u8 = gen.file.step.owner.pathFromRoot(gen.file.path orelse {
2347 std.debug.lockStdErr();
2348 const stderr = std.io.getStdErr();
2349 dumpBadGetPathHelp(gen.file.step, stderr, src_builder, asking_step) catch {};
2350 std.debug.unlockStdErr();
2351 @panic("misconfigured build script");
2352 });
2356 // TODO make gen.file.path not be absolute and use that as the
2357 // basis for not traversing up too many directories.
2358
2359 var file_path: Cache.Path = .{
2360 .root_dir = gen.file.step.owner.build_root,
2361 .sub_path = gen.file.path orelse {
2362 std.debug.lockStdErr();
2363 const stderr = std.io.getStdErr();
2364 dumpBadGetPathHelp(gen.file.step, stderr, src_builder, asking_step) catch {};
2365 std.debug.unlockStdErr();
2366 @panic("misconfigured build script");
2367 },
2368 };
23532369
23542370 if (gen.up > 0) {
23552371 const cache_root_path = src_builder.cache_root.path orelse
23562372 (src_builder.cache_root.join(src_builder.allocator, &.{"."}) catch @panic("OOM"));
23572373
23582374 for (0..gen.up) |_| {
2359 if (mem.eql(u8, file_path, cache_root_path)) {
2375 if (mem.eql(u8, file_path.sub_path, cache_root_path)) {
23602376 // If we hit the cache root and there's still more to go,
23612377 // the script attempted to go too far.
23622378 dumpBadDirnameHelp(gen.file.step, asking_step,
......@@ -2370,7 +2386,7 @@ pub const LazyPath = union(enum) {
23702386 // path is absolute.
23712387 // dirname will return null only if we're at root.
23722388 // Typically, we'll stop well before that at the cache root.
2373 file_path = fs.path.dirname(file_path) orelse {
2389 file_path.sub_path = fs.path.dirname(file_path.sub_path) orelse {
23742390 dumpBadDirnameHelp(gen.file.step, asking_step,
23752391 \\dirname() reached root.
23762392 \\No more directories left to go up.
......@@ -2381,9 +2397,12 @@ pub const LazyPath = union(enum) {
23812397 }
23822398 }
23832399
2384 return src_builder.pathResolve(&.{ file_path, gen.sub_path });
2400 return file_path.join(src_builder.allocator, gen.sub_path) catch @panic("OOM");
2401 },
2402 .dependency => |dep| return .{
2403 .root_dir = dep.dependency.builder.build_root,
2404 .sub_path = dep.sub_path,
23852405 },
2386 .dependency => |dep| return dep.dependency.builder.pathFromRoot(dep.sub_path),
23872406 }
23882407 }
23892408