Skip to content

Commit cdf860c

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: * `needed` — 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 `needed` was passed. Signed-off-by: Eric Joldasov <[email protected]>
1 parent a7ff042 commit cdf860c

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
@@ -1293,7 +1293,9 @@ fn usage(b: *std.Build, out_stream: anytype) !void {
12931293
\\ -j<N> Limit concurrent jobs (default is to use all CPU cores)
12941294
\\ --maxrss <bytes> Limit memory usage (default is to use available memory)
12951295
\\ --skip-oom-steps Instead of failing, skip steps that would exceed --maxrss
1296-
\\ --fetch Exit after fetching dependency tree
1296+
\\ --fetch[=mode] Fetch dependency tree (optionally choose laziness) and exit
1297+
\\ needed (Default) Lazy dependencies are fetched as needed
1298+
\\ all Lazy dependencies are always fetched
12971299
\\ --watch Continuously rebuild when source files are modified
12981300
\\ --fuzz Continuously search for unit test failures
12991301
\\ --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
@@ -114,10 +114,18 @@ pub const JobQueue = struct {
114114
/// If this is true, `recursive` must be false.
115115
debug_hash: bool,
116116
work_around_btrfs_bug: bool,
117+
mode: Mode,
117118
/// Set of hashes that will be additionally fetched even if they are marked
118119
/// as lazy.
119120
unlazy_set: UnlazySet = .{},
120121

122+
pub const Mode = enum {
123+
/// Non-lazy dependencies are always fetched.
124+
/// Lazy dependencies are fetched only when needed.
125+
needed,
126+
/// Both non-lazy and lazy dependencies are always fetched.
127+
all,
128+
};
121129
pub const Table = std.AutoArrayHashMapUnmanaged(Package.Hash, *Fetch);
122130
pub const UnlazySet = std.AutoArrayHashMapUnmanaged(Package.Hash, void);
123131

@@ -754,7 +762,10 @@ fn queueJobsForDeps(f: *Fetch) RunError!void {
754762
.location_tok = dep.location_tok,
755763
.hash_tok = dep.hash_tok,
756764
.name_tok = dep.name_tok,
757-
.lazy_status = if (dep.lazy) .available else .eager,
765+
.lazy_status = switch (f.job_queue.mode) {
766+
.needed => if (dep.lazy) .available else .eager,
767+
.all => .eager,
768+
},
758769
.parent_package_root = f.package_root,
759770
.parent_manifest_ast = &f.manifest_ast,
760771
.prog_node = f.prog_node,
@@ -2325,6 +2336,7 @@ const TestFetchBuilder = struct {
23252336
.read_only = false,
23262337
.debug_hash = false,
23272338
.work_around_btrfs_bug = false,
2339+
.mode = .needed,
23282340
};
23292341

23302342
self.fetch = .{

src/main.zig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4843,6 +4843,7 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
48434843
var verbose_cimport = false;
48444844
var verbose_llvm_cpu_features = false;
48454845
var fetch_only = false;
4846+
var fetch_mode: Package.Fetch.JobQueue.Mode = .needed;
48464847
var system_pkg_dir_path: ?[]const u8 = null;
48474848
var debug_target: ?[]const u8 = null;
48484849

@@ -4924,6 +4925,13 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
49244925
reference_trace = 256;
49254926
} else if (mem.eql(u8, arg, "--fetch")) {
49264927
fetch_only = true;
4928+
} else if (mem.startsWith(u8, arg, "--fetch=")) {
4929+
fetch_only = true;
4930+
const sub_arg = arg["--fetch=".len..];
4931+
fetch_mode = std.meta.stringToEnum(Package.Fetch.JobQueue.Mode, sub_arg) orelse
4932+
fatal("expected [needed|all] after '--fetch=', found '{s}'", .{
4933+
sub_arg,
4934+
});
49274935
} else if (mem.eql(u8, arg, "--system")) {
49284936
if (i + 1 >= args.len) fatal("expected argument after '{s}'", .{arg});
49294937
i += 1;
@@ -5208,6 +5216,7 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
52085216
.debug_hash = false,
52095217
.work_around_btrfs_bug = work_around_btrfs_bug,
52105218
.unlazy_set = unlazy_set,
5219+
.mode = fetch_mode,
52115220
};
52125221
defer job_queue.deinit();
52135222

@@ -7130,6 +7139,7 @@ fn cmdFetch(
71307139
.read_only = false,
71317140
.debug_hash = debug_hash,
71327141
.work_around_btrfs_bug = work_around_btrfs_bug,
7142+
.mode = .all,
71337143
};
71347144
defer job_queue.deinit();
71357145

0 commit comments

Comments
 (0)