Skip to content

Commit cd1b1ef

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: #14597 (comment) If none is passed, behaviour is same as if `lazy` was passed. Signed-off-by: Eric Joldasov <[email protected]>
1 parent 6a65561 commit cd1b1ef

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

lib/compiler/build_runner.zig

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1123,7 +1123,9 @@ fn usage(b: *std.Build, out_stream: anytype) !void {
11231123
\\ -j<N> Limit concurrent jobs (default is to use all CPU cores)
11241124
\\ --maxrss <bytes> Limit memory usage (default is to use available memory)
11251125
\\ --skip-oom-steps Instead of failing, skip steps that would exceed --maxrss
1126-
\\ --fetch Exit after fetching dependency tree
1126+
\\ --fetch[=mode] Fetch dependency tree (optionally choose laziness) and exit
1127+
\\ lazy (Default) Lazy dependencies are fetched on need
1128+
\\ all Lazy dependencies are always fetched
11271129
\\
11281130
\\Project-Specific Options:
11291131
\\

src/Package/Fetch.zig

Lines changed: 12 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,

src/main.zig

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4730,7 +4730,9 @@ const usage_build =
47304730
\\ --build-runner [file] Override path to build runner
47314731
\\ --prominent-compile-errors Buffer compile errors and display at end
47324732
\\ --seed [integer] For shuffling dependency traversal order (default: random)
4733-
\\ --fetch Exit after fetching dependency tree
4733+
\\ --fetch[=mode] Fetch dependency tree (optionally choose laziness) and exit
4734+
\\ lazy (Default) Lazy dependencies are fetched on need
4735+
\\ all Lazy dependencies are always fetched
47344736
\\ -h, --help Print this help and exit
47354737
\\
47364738
;
@@ -4758,6 +4760,7 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
47584760
var verbose_cimport = false;
47594761
var verbose_llvm_cpu_features = false;
47604762
var fetch_only = false;
4763+
var fetch_mode: Package.Fetch.JobQueue.Mode = .lazy;
47614764
var system_pkg_dir_path: ?[]const u8 = null;
47624765

47634766
const argv_index_exe = child_argv.items.len;
@@ -4833,6 +4836,13 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
48334836
reference_trace = 256;
48344837
} else if (mem.eql(u8, arg, "--fetch")) {
48354838
fetch_only = true;
4839+
} else if (mem.startsWith(u8, arg, "--fetch=")) {
4840+
fetch_only = true;
4841+
const sub_arg = arg["--fetch=".len..];
4842+
fetch_mode = std.meta.stringToEnum(Package.Fetch.JobQueue.Mode, sub_arg) orelse
4843+
fatal("expected [lazy|all] after '--fetch=', found '{s}'", .{
4844+
sub_arg,
4845+
});
48364846
} else if (mem.eql(u8, arg, "--system")) {
48374847
if (i + 1 >= args.len) fatal("expected argument after '{s}'", .{arg});
48384848
i += 1;
@@ -5063,6 +5073,7 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
50635073
.debug_hash = false,
50645074
.work_around_btrfs_bug = work_around_btrfs_bug,
50655075
.unlazy_set = unlazy_set,
5076+
.mode = fetch_mode,
50665077
};
50675078
defer job_queue.deinit();
50685079

@@ -6984,6 +6995,7 @@ fn cmdFetch(
69846995
.read_only = false,
69856996
.debug_hash = debug_hash,
69866997
.work_around_btrfs_bug = work_around_btrfs_bug,
6998+
.mode = .all,
69876999
};
69887000
defer job_queue.deinit();
69897001

0 commit comments

Comments
 (0)