| ... | ... | @@ -560,10 +560,11 @@ pub fn main(init: process.Init.Minimal) !void { |
| 560 | 560 | const cwd_path = std.zig.getResolvedCwd(io, arena) catch |err| |
| 561 | 561 | fatal("resolving current directory path failed: {t}", .{err}); |
| 562 | 562 | |
| 563 | | const build_root = try findBuildRoot(arena, io, .{ |
| 563 | var build_root = try findBuildRoot(arena, io, .{ |
| 564 | 564 | .cwd_path = cwd_path, |
| 565 | 565 | .build_file = build_file, |
| 566 | 566 | }); |
| 567 | defer build_root.deinit(io); |
| 567 | 568 | |
| 568 | 569 | graph.build_root_directory = build_root.directory; |
| 569 | 570 | graph.local_cache_root = if (override_local_cache_dir) |unresolved_path| std.zig.Directories.openUnresolved( |
| ... | ... | @@ -3335,22 +3336,21 @@ inline fn debugMakerLeaks() bool { |
| 3335 | 3336 | |
| 3336 | 3337 | const BuildRoot = struct { |
| 3337 | 3338 | directory: Cache.Directory, |
| 3339 | close_directory: bool, |
| 3338 | 3340 | build_zig_basename: []const u8, |
| 3339 | | cleanup_build_dir: ?Io.Dir, |
| 3340 | 3341 | |
| 3341 | 3342 | fn deinit(br: *BuildRoot, io: Io) void { |
| 3342 | | if (br.cleanup_build_dir) |*dir| dir.close(io); |
| 3343 | if (br.close_directory) br.directory.handle.close(io); |
| 3343 | 3344 | br.* = undefined; |
| 3344 | 3345 | } |
| 3345 | 3346 | }; |
| 3346 | 3347 | |
| 3347 | 3348 | const FindBuildRootOptions = struct { |
| 3348 | 3349 | build_file: ?[]const u8 = null, |
| 3349 | | cwd_path: ?[]const u8 = null, |
| 3350 | cwd_path: []const u8, |
| 3350 | 3351 | }; |
| 3351 | 3352 | |
| 3352 | 3353 | fn findBuildRoot(arena: Allocator, io: Io, options: FindBuildRootOptions) !BuildRoot { |
| 3353 | | const cwd_path = options.cwd_path orelse try std.zig.getResolvedCwd(io, arena); |
| 3354 | 3354 | const build_zig_basename = if (options.build_file) |bf| |
| 3355 | 3355 | Dir.path.basename(bf) |
| 3356 | 3356 | else |
| ... | ... | @@ -3364,35 +3364,41 @@ fn findBuildRoot(arena: Allocator, io: Io, options: FindBuildRootOptions) !Build |
| 3364 | 3364 | return .{ |
| 3365 | 3365 | .build_zig_basename = build_zig_basename, |
| 3366 | 3366 | .directory = .{ .path = dirname, .handle = dir }, |
| 3367 | | .cleanup_build_dir = dir, |
| 3367 | .close_directory = true, |
| 3368 | 3368 | }; |
| 3369 | 3369 | } |
| 3370 | 3370 | |
| 3371 | 3371 | return .{ |
| 3372 | 3372 | .build_zig_basename = build_zig_basename, |
| 3373 | | .directory = .{ .path = null, .handle = Io.Dir.cwd() }, |
| 3374 | | .cleanup_build_dir = null, |
| 3373 | .directory = .cwd(), |
| 3374 | .close_directory = false, |
| 3375 | 3375 | }; |
| 3376 | 3376 | } |
| 3377 | 3377 | // Search up parent directories until we find build.zig. |
| 3378 | | var dirname: []const u8 = cwd_path; |
| 3378 | var dirname: ?[]const u8 = null; |
| 3379 | 3379 | while (true) { |
| 3380 | | const joined_path = try Dir.path.join(arena, &[_][]const u8{ dirname, build_zig_basename }); |
| 3380 | const joined_path = if (dirname) |d| |
| 3381 | try Dir.path.join(arena, &.{ d, build_zig_basename }) |
| 3382 | else |
| 3383 | build_zig_basename; |
| 3381 | 3384 | if (Io.Dir.cwd().access(io, joined_path, .{})) |_| { |
| 3382 | | const dir = Io.Dir.cwd().openDir(io, dirname, .{}) catch |err| { |
| 3383 | | fatal("unable to open directory while searching for build.zig file, {q}: {t}", .{ dirname, err }); |
| 3384 | | }; |
| 3385 | const dir = if (dirname) |d| |
| 3386 | Io.Dir.cwd().openDir(io, d, .{}) catch |err| { |
| 3387 | fatal("unable to open directory while searching for build.zig file, {q}: {t}", .{ d, err }); |
| 3388 | } |
| 3389 | else |
| 3390 | Io.Dir.cwd(); |
| 3385 | 3391 | return .{ |
| 3386 | 3392 | .build_zig_basename = build_zig_basename, |
| 3387 | 3393 | .directory = .{ |
| 3388 | 3394 | .path = dirname, |
| 3389 | 3395 | .handle = dir, |
| 3390 | 3396 | }, |
| 3391 | | .cleanup_build_dir = dir, |
| 3397 | .close_directory = dirname != null, |
| 3392 | 3398 | }; |
| 3393 | 3399 | } else |err| switch (err) { |
| 3394 | 3400 | error.FileNotFound => { |
| 3395 | | dirname = Dir.path.dirname(dirname) orelse { |
| 3401 | dirname = Dir.path.dirname(dirname orelse options.cwd_path) orelse { |
| 3396 | 3402 | log.info("initialize {s} template file with \"zig init\"", .{std.zig.build_zig_basename}); |
| 3397 | 3403 | log.info("see \"zig --help\" for more options", .{}); |
| 3398 | 3404 | fatal("no build.zig file found, in the current directory or any parent directories", .{}); |