authorgravatar for jan.hafer@rwth-aachen.deJan Philipp Hafer <jan.hafer@rwth-aachen.de> 2022-05-28 05:06:32+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-09-14 19:39:25-04:00
log8cab0762d86d405f21877045e67bd1858c75e258
tree123fde3fd3c96e918c74ec78b68191fc84a9c0cc
parent3250b20ceabea044bd4c7b1653d76d7069840058

place zig-cache directory next to build.zig

* search upwards, if no build.zig is found * if this fails or any error occurs, fallback to global zig-cache Closes #11672

1 files changed, 22 insertions(+), 7 deletions(-)

src/main.zig+22-7
...@@ -2825,13 +2825,28 @@ fn buildOutputType(...@@ -2825,13 +2825,28 @@ fn buildOutputType(
2825 break :l global_cache_directory;2825 break :l global_cache_directory;
2826 }2826 }
2827 if (main_pkg) |pkg| {2827 if (main_pkg) |pkg| {
2828 const cache_dir_path = try pkg.root_src_directory.join(arena, &[_][]const u8{"zig-cache"});2828 // search upwards from cwd until we find directory with build.zig
2829 const dir = try pkg.root_src_directory.handle.makeOpenPath("zig-cache", .{});2829 const cwd_path = try process.getCwdAlloc(arena);
2830 cleanup_local_cache_dir = dir;2830 const build_zig = "build.zig";
2831 break :l .{2831 const zig_cache = "zig-cache";
2832 .handle = dir,2832 var dirname: []const u8 = cwd_path;
2833 .path = cache_dir_path,2833 while (true) {
2834 };2834 const joined_path = try fs.path.join(arena, &[_][]const u8{ dirname, build_zig });
2835 if (fs.cwd().access(joined_path, .{})) |_| {
2836 const cache_dir_path = try fs.path.join(arena, &[_][]const u8{ dirname, zig_cache });
2837 const dir = try pkg.root_src_directory.handle.makeOpenPath(cache_dir_path, .{});
2838 cleanup_local_cache_dir = dir;
2839 break :l .{ .handle = dir, .path = cache_dir_path };
2840 } else |err| switch (err) {
2841 error.FileNotFound => {
2842 dirname = fs.path.dirname(dirname) orelse {
2843 break :l global_cache_directory;
2844 };
2845 continue;
2846 },
2847 else => break :l global_cache_directory,
2848 }
2849 }
2835 }2850 }
2836 // Otherwise we really don't have a reasonable place to put the local cache directory,2851 // Otherwise we really don't have a reasonable place to put the local cache directory,
2837 // so we utilize the global one.2852 // so we utilize the global one.