Skip to content

Commit

Permalink
Added global-cache argument to build system + removed extra args.
Browse files Browse the repository at this point in the history
* Field global_cache_root was added to Builder struct along with
mandatory argument for build_runner.zig. Logic for using the custom
global cache was also added.

* The arguments --cache-dir and --global-cache-dir are no longer passed
directly through to build_runner.zig and are instead only passed through the
mandatory cache_root and global_cache_root arguments.
  • Loading branch information
antlilja authored and aarvay committed Jan 4, 2021
1 parent ebe3524 commit 2dbd261
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
17 changes: 16 additions & 1 deletion lib/std/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand All @@ -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,
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -2196,6 +2205,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;

Expand Down Expand Up @@ -2826,6 +2838,7 @@ test "Builder.dupePkg()" {
"test",
"test",
"test",
"test",
);
defer builder.destroy();

Expand Down Expand Up @@ -2869,6 +2882,7 @@ test "LibExeObjStep.addBuildOption" {
"test",
"test",
"test",
"test",
);
defer builder.destroy();

Expand Down Expand Up @@ -2906,6 +2920,7 @@ test "LibExeObjStep.addPackage" {
"test",
"test",
"test",
"test",
);
defer builder.destroy();

Expand Down
12 changes: 11 additions & 1 deletion lib/std/special/build_runner.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
7 changes: 5 additions & 2 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2260,6 +2260,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) {
Expand All @@ -2280,13 +2283,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;
}
}
Expand Down Expand Up @@ -2371,6 +2372,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 .{
Expand Down

0 comments on commit 2dbd261

Please sign in to comment.