diff --git a/lib/std/build.zig b/lib/std/build.zig index 30cbd0422a..c2ffcc941f 100644 --- a/lib/std/build.zig +++ b/lib/std/build.zig @@ -61,6 +61,7 @@ pub const Builder = struct { installed_files: ArrayList(InstalledFile), build_root: []const u8, cache_root: []const u8, + global_cache_root: []const u8, release_mode: ?builtin.Mode, is_release: bool, override_lib_dir: ?[]const u8, @@ -126,6 +127,7 @@ pub const Builder = struct { zig_exe: []const u8, build_root: []const u8, cache_root: []const u8, + global_cache_root: []const u8, ) !*Builder { const env_map = try allocator.create(BufMap); env_map.* = try process.getEnvMap(allocator); @@ -135,6 +137,7 @@ pub const Builder = struct { .zig_exe = zig_exe, .build_root = build_root, .cache_root = try fs.path.relative(allocator, build_root, cache_root), + .global_cache_root = global_cache_root, .verbose = false, .verbose_tokenize = false, .verbose_ast = false, @@ -1128,7 +1131,13 @@ test "builder.findProgram compiles" { var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); defer arena.deinit(); - const builder = try Builder.create(&arena.allocator, "zig", "zig-cache", "zig-cache"); + const builder = try Builder.create( + &arena.allocator, + "zig", + "zig-cache", + "zig-cache", + "zig-cache", + ); defer builder.destroy(); _ = builder.findProgram(&[_][]const u8{}, &[_][]const u8{}) catch null; } @@ -2124,6 +2133,9 @@ pub const LibExeObjStep = struct { try zig_args.append("--cache-dir"); try zig_args.append(builder.pathFromRoot(builder.cache_root)); + try zig_args.append("--global-cache-dir"); + try zig_args.append(builder.pathFromRoot(builder.global_cache_root)); + zig_args.append("--name") catch unreachable; zig_args.append(self.name) catch unreachable; @@ -2735,6 +2747,7 @@ test "Builder.dupePkg()" { "test", "test", "test", + "test", ); defer builder.destroy(); @@ -2778,6 +2791,7 @@ test "LibExeObjStep.addBuildOption" { "test", "test", "test", + "test", ); defer builder.destroy(); @@ -2815,6 +2829,7 @@ test "LibExeObjStep.addPackage" { "test", "test", "test", + "test", ); defer builder.destroy(); diff --git a/lib/std/special/build_runner.zig b/lib/std/special/build_runner.zig index 95ad8ec646..26b7c5fab8 100644 --- a/lib/std/special/build_runner.zig +++ b/lib/std/special/build_runner.zig @@ -41,8 +41,18 @@ pub fn main() !void { warn("Expected third argument to be cache root directory path\n", .{}); return error.InvalidArgs; }; + const global_cache_root = nextArg(args, &arg_idx) orelse { + warn("Expected third argument to be global cache root directory path\n", .{}); + return error.InvalidArgs; + }; - const builder = try Builder.create(allocator, zig_exe, build_root, cache_root); + const builder = try Builder.create( + allocator, + zig_exe, + build_root, + cache_root, + global_cache_root, + ); defer builder.destroy(); var targets = ArrayList([]const u8).init(allocator); diff --git a/src/main.zig b/src/main.zig index b60555ab19..1bc718e8ee 100644 --- a/src/main.zig +++ b/src/main.zig @@ -2228,6 +2228,9 @@ pub fn cmdBuild(gpa: *Allocator, arena: *Allocator, args: []const []const u8) !v const argv_index_cache_dir = child_argv.items.len; _ = try child_argv.addOne(); + const argv_index_global_cache_dir = child_argv.items.len; + _ = try child_argv.addOne(); + { var i: usize = 0; while (i < args.len) : (i += 1) { @@ -2248,13 +2251,11 @@ pub fn cmdBuild(gpa: *Allocator, arena: *Allocator, args: []const []const u8) !v if (i + 1 >= args.len) fatal("expected argument after '{}'", .{arg}); i += 1; override_local_cache_dir = args[i]; - try child_argv.appendSlice(&[_][]const u8{ arg, args[i] }); continue; } else if (mem.eql(u8, arg, "--global-cache-dir")) { if (i + 1 >= args.len) fatal("expected argument after '{}'", .{arg}); i += 1; override_global_cache_dir = args[i]; - try child_argv.appendSlice(&[_][]const u8{ arg, args[i] }); continue; } } @@ -2339,6 +2340,8 @@ pub fn cmdBuild(gpa: *Allocator, arena: *Allocator, args: []const []const u8) !v }; defer global_cache_directory.handle.close(); + child_argv.items[argv_index_global_cache_dir] = global_cache_directory.path orelse cwd_path; + var local_cache_directory: Compilation.Directory = l: { if (override_local_cache_dir) |local_cache_dir_path| { break :l .{