Skip to content

Commit 6acd830

Browse files
committed
zig build: allow to choose "lazy mode" for fetching process
`--fetch` flag now has additional optional parameter, which specifies how lazy dependencies should be fetched: * `lazy` — lazy dependencies are fetched only if they are required for current build configuration to work. Default and works same as old `--fetch` flag. * `all` — lazy dependencies are always fetched. If `--system` flag is used after that, it's guaranteed that **any** build configuration will not require additional download of dependencies during build. Helpful for distro packagers and CI systems: https://www.github.com/ziglang/zig/issues/14597#issuecomment-1426827495 If none is passed, behaviour is same as if `lazy` was passed. Signed-off-by: Eric Joldasov <[email protected]>
1 parent f219286 commit 6acd830

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

lib/compiler/build_runner.zig

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1243,7 +1243,9 @@ fn usage(b: *std.Build, out_stream: anytype) !void {
12431243
\\ -j<N> Limit concurrent jobs (default is to use all CPU cores)
12441244
\\ --maxrss <bytes> Limit memory usage (default is to use available memory)
12451245
\\ --skip-oom-steps Instead of failing, skip steps that would exceed --maxrss
1246-
\\ --fetch Exit after fetching dependency tree
1246+
\\ --fetch[=mode] Fetch dependency tree (optionally choose laziness) and exit
1247+
\\ lazy (Default) Lazy dependencies are fetched on need
1248+
\\ all Lazy dependencies are always fetched
12471249
\\ --watch Continuously rebuild when source files are modified
12481250
\\ --fuzz Continuously search for unit test failures
12491251
\\ --debounce <ms> Delay before rebuilding after changed file detected

src/Package/Fetch.zig

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,18 @@ pub const JobQueue = struct {
112112
/// If this is true, `recursive` must be false.
113113
debug_hash: bool,
114114
work_around_btrfs_bug: bool,
115+
mode: Mode,
115116
/// Set of hashes that will be additionally fetched even if they are marked
116117
/// as lazy.
117118
unlazy_set: UnlazySet = .{},
118119

120+
pub const Mode = enum {
121+
/// Non-lazy dependencies are always fetched.
122+
/// Lazy dependencies are fetched only when needed.
123+
lazy,
124+
/// Both non-lazy and lazy dependencies are always fetched.
125+
all,
126+
};
119127
pub const Table = std.AutoArrayHashMapUnmanaged(Manifest.MultiHashHexDigest, *Fetch);
120128
pub const UnlazySet = std.AutoArrayHashMapUnmanaged(Manifest.MultiHashHexDigest, void);
121129

@@ -698,7 +706,10 @@ fn queueJobsForDeps(f: *Fetch) RunError!void {
698706
.location_tok = dep.location_tok,
699707
.hash_tok = dep.hash_tok,
700708
.name_tok = dep.name_tok,
701-
.lazy_status = if (dep.lazy) .available else .eager,
709+
.lazy_status = switch (f.job_queue.mode) {
710+
.lazy => if (dep.lazy) .available else .eager,
711+
.all => .eager,
712+
},
702713
.parent_package_root = f.package_root,
703714
.parent_manifest_ast = &f.manifest_ast,
704715
.prog_node = f.prog_node,
@@ -2260,6 +2271,7 @@ const TestFetchBuilder = struct {
22602271
.read_only = false,
22612272
.debug_hash = false,
22622273
.work_around_btrfs_bug = false,
2274+
.mode = .lazy,
22632275
};
22642276

22652277
self.fetch = .{

src/main.zig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4751,6 +4751,7 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
47514751
var verbose_cimport = false;
47524752
var verbose_llvm_cpu_features = false;
47534753
var fetch_only = false;
4754+
var fetch_mode: Package.Fetch.JobQueue.Mode = .lazy;
47544755
var system_pkg_dir_path: ?[]const u8 = null;
47554756
var debug_target: ?[]const u8 = null;
47564757

@@ -4832,6 +4833,13 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
48324833
reference_trace = 256;
48334834
} else if (mem.eql(u8, arg, "--fetch")) {
48344835
fetch_only = true;
4836+
} else if (mem.startsWith(u8, arg, "--fetch=")) {
4837+
fetch_only = true;
4838+
const sub_arg = arg["--fetch=".len..];
4839+
fetch_mode = std.meta.stringToEnum(Package.Fetch.JobQueue.Mode, sub_arg) orelse
4840+
fatal("expected [lazy|all] after '--fetch=', found '{s}'", .{
4841+
sub_arg,
4842+
});
48354843
} else if (mem.eql(u8, arg, "--system")) {
48364844
if (i + 1 >= args.len) fatal("expected argument after '{s}'", .{arg});
48374845
i += 1;
@@ -5107,6 +5115,7 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
51075115
.debug_hash = false,
51085116
.work_around_btrfs_bug = work_around_btrfs_bug,
51095117
.unlazy_set = unlazy_set,
5118+
.mode = fetch_mode,
51105119
};
51115120
defer job_queue.deinit();
51125121

@@ -7040,6 +7049,7 @@ fn cmdFetch(
70407049
.read_only = false,
70417050
.debug_hash = debug_hash,
70427051
.work_around_btrfs_bug = work_around_btrfs_bug,
7052+
.mode = .all,
70437053
};
70447054
defer job_queue.deinit();
70457055

0 commit comments

Comments
 (0)