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) {...@@ -2327,36 +2327,52 @@ pub const LazyPath = union(enum) {
2327 }2327 }
2328 }2328 }
23292329
2330 /// Returns an absolute path.2330 /// Deprecated, see `getPath3`.
2331 /// Intended to be used during the make phase only.
2332 pub fn getPath(lazy_path: LazyPath, src_builder: *Build) []const u8 {2331 pub fn getPath(lazy_path: LazyPath, src_builder: *Build) []const u8 {
2333 return getPath2(lazy_path, src_builder, null);2332 return getPath2(lazy_path, src_builder, null);
2334 }2333 }
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
2337 /// Intended to be used during the make phase only.2341 /// Intended to be used during the make phase only.
2338 ///2342 ///
2339 /// `asking_step` is only used for debugging purposes; it's the step being2343 /// `asking_step` is only used for debugging purposes; it's the step being
2340 /// run that is asking for the path.2344 /// 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 {
2342 switch (lazy_path) {2346 switch (lazy_path) {
2343 .src_path => |sp| return sp.owner.pathFromRoot(sp.sub_path),2347 .src_path => |sp| return .{
2344 .cwd_relative => |p| return src_builder.pathFromCwd(p),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 },
2345 .generated => |gen| {2355 .generated => |gen| {
2346 var file_path: []const u8 = gen.file.step.owner.pathFromRoot(gen.file.path orelse {2356 // TODO make gen.file.path not be absolute and use that as the
2347 std.debug.lockStdErr();2357 // basis for not traversing up too many directories.
2348 const stderr = std.io.getStdErr();2358
2349 dumpBadGetPathHelp(gen.file.step, stderr, src_builder, asking_step) catch {};2359 var file_path: Cache.Path = .{
2350 std.debug.unlockStdErr();2360 .root_dir = gen.file.step.owner.build_root,
2351 @panic("misconfigured build script");2361 .sub_path = gen.file.path orelse {
2352 });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
2354 if (gen.up > 0) {2370 if (gen.up > 0) {
2355 const cache_root_path = src_builder.cache_root.path orelse2371 const cache_root_path = src_builder.cache_root.path orelse
2356 (src_builder.cache_root.join(src_builder.allocator, &.{"."}) catch @panic("OOM"));2372 (src_builder.cache_root.join(src_builder.allocator, &.{"."}) catch @panic("OOM"));
23572373
2358 for (0..gen.up) |_| {2374 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)) {
2360 // If we hit the cache root and there's still more to go,2376 // If we hit the cache root and there's still more to go,
2361 // the script attempted to go too far.2377 // the script attempted to go too far.
2362 dumpBadDirnameHelp(gen.file.step, asking_step,2378 dumpBadDirnameHelp(gen.file.step, asking_step,
...@@ -2370,7 +2386,7 @@ pub const LazyPath = union(enum) {...@@ -2370,7 +2386,7 @@ pub const LazyPath = union(enum) {
2370 // path is absolute.2386 // path is absolute.
2371 // dirname will return null only if we're at root.2387 // dirname will return null only if we're at root.
2372 // Typically, we'll stop well before that at the cache root.2388 // 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 {
2374 dumpBadDirnameHelp(gen.file.step, asking_step,2390 dumpBadDirnameHelp(gen.file.step, asking_step,
2375 \\dirname() reached root.2391 \\dirname() reached root.
2376 \\No more directories left to go up.2392 \\No more directories left to go up.
...@@ -2381,9 +2397,12 @@ pub const LazyPath = union(enum) {...@@ -2381,9 +2397,12 @@ pub const LazyPath = union(enum) {
2381 }2397 }
2382 }2398 }
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,
2385 },2405 },
2386 .dependency => |dep| return dep.dependency.builder.pathFromRoot(dep.sub_path),
2387 }2406 }
2388 }2407 }
23892408