Skip to content

Commit 275088b

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 79460d4 commit 275088b

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+
\\ lazy (Default) Lazy dependencies are fetched on need
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+
lazy,
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

@@ -753,7 +761,10 @@ fn queueJobsForDeps(f: *Fetch) RunError!void {
753761
.location_tok = dep.location_tok,
754762
.hash_tok = dep.hash_tok,
755763
.name_tok = dep.name_tok,
756-
.lazy_status = if (dep.lazy) .available else .eager,
764+
.lazy_status = switch (f.job_queue.mode) {
765+
.lazy => if (dep.lazy) .available else .eager,
766+
.all => .eager,
767+
},
757768
.parent_package_root = f.package_root,
758769
.parent_manifest_ast = &f.manifest_ast,
759770
.prog_node = f.prog_node,
@@ -2316,6 +2327,7 @@ const TestFetchBuilder = struct {
23162327
.read_only = false,
23172328
.debug_hash = false,
23182329
.work_around_btrfs_bug = false,
2330+
.mode = .lazy,
23192331
};
23202332

23212333
self.fetch = .{

src/main.zig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4834,6 +4834,7 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
48344834
var verbose_cimport = false;
48354835
var verbose_llvm_cpu_features = false;
48364836
var fetch_only = false;
4837+
var fetch_mode: Package.Fetch.JobQueue.Mode = .lazy;
48374838
var system_pkg_dir_path: ?[]const u8 = null;
48384839
var debug_target: ?[]const u8 = null;
48394840

@@ -4915,6 +4916,13 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
49154916
reference_trace = 256;
49164917
} else if (mem.eql(u8, arg, "--fetch")) {
49174918
fetch_only = true;
4919+
} else if (mem.startsWith(u8, arg, "--fetch=")) {
4920+
fetch_only = true;
4921+
const sub_arg = arg["--fetch=".len..];
4922+
fetch_mode = std.meta.stringToEnum(Package.Fetch.JobQueue.Mode, sub_arg) orelse
4923+
fatal("expected [lazy|all] after '--fetch=', found '{s}'", .{
4924+
sub_arg,
4925+
});
49184926
} else if (mem.eql(u8, arg, "--system")) {
49194927
if (i + 1 >= args.len) fatal("expected argument after '{s}'", .{arg});
49204928
i += 1;
@@ -5199,6 +5207,7 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
51995207
.debug_hash = false,
52005208
.work_around_btrfs_bug = work_around_btrfs_bug,
52015209
.unlazy_set = unlazy_set,
5210+
.mode = fetch_mode,
52025211
};
52035212
defer job_queue.deinit();
52045213

@@ -7119,6 +7128,7 @@ fn cmdFetch(
71197128
.read_only = false,
71207129
.debug_hash = debug_hash,
71217130
.work_around_btrfs_bug = work_around_btrfs_bug,
7131+
.mode = .all,
71227132
};
71237133
defer job_queue.deinit();
71247134

0 commit comments

Comments
 (0)