Skip to content

Commit a1a68d0

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 53d7a7b commit a1a68d0

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
@@ -1279,7 +1279,9 @@ fn usage(b: *std.Build, out_stream: anytype) !void {
12791279
\\ -j<N> Limit concurrent jobs (default is to use all CPU cores)
12801280
\\ --maxrss <bytes> Limit memory usage (default is to use available memory)
12811281
\\ --skip-oom-steps Instead of failing, skip steps that would exceed --maxrss
1282-
\\ --fetch Exit after fetching dependency tree
1282+
\\ --fetch[=mode] Fetch dependency tree (optionally choose laziness) and exit
1283+
\\ lazy (Default) Lazy dependencies are fetched on need
1284+
\\ all Lazy dependencies are always fetched
12831285
\\ --watch Continuously rebuild when source files are modified
12841286
\\ --fuzz Continuously search for unit test failures
12851287
\\ --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,
@@ -2262,6 +2273,7 @@ const TestFetchBuilder = struct {
22622273
.read_only = false,
22632274
.debug_hash = false,
22642275
.work_around_btrfs_bug = false,
2276+
.mode = .lazy,
22652277
};
22662278

22672279
self.fetch = .{

src/main.zig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4692,6 +4692,7 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
46924692
var verbose_cimport = false;
46934693
var verbose_llvm_cpu_features = false;
46944694
var fetch_only = false;
4695+
var fetch_mode: Package.Fetch.JobQueue.Mode = .lazy;
46954696
var system_pkg_dir_path: ?[]const u8 = null;
46964697
var debug_target: ?[]const u8 = null;
46974698

@@ -4773,6 +4774,13 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
47734774
reference_trace = 256;
47744775
} else if (mem.eql(u8, arg, "--fetch")) {
47754776
fetch_only = true;
4777+
} else if (mem.startsWith(u8, arg, "--fetch=")) {
4778+
fetch_only = true;
4779+
const sub_arg = arg["--fetch=".len..];
4780+
fetch_mode = std.meta.stringToEnum(Package.Fetch.JobQueue.Mode, sub_arg) orelse
4781+
fatal("expected [lazy|all] after '--fetch=', found '{s}'", .{
4782+
sub_arg,
4783+
});
47764784
} else if (mem.eql(u8, arg, "--system")) {
47774785
if (i + 1 >= args.len) fatal("expected argument after '{s}'", .{arg});
47784786
i += 1;
@@ -5048,6 +5056,7 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
50485056
.debug_hash = false,
50495057
.work_around_btrfs_bug = work_around_btrfs_bug,
50505058
.unlazy_set = unlazy_set,
5059+
.mode = fetch_mode,
50515060
};
50525061
defer job_queue.deinit();
50535062

@@ -6906,6 +6915,7 @@ fn cmdFetch(
69066915
.read_only = false,
69076916
.debug_hash = debug_hash,
69086917
.work_around_btrfs_bug = work_around_btrfs_bug,
6918+
.mode = .all,
69096919
};
69106920
defer job_queue.deinit();
69116921

0 commit comments

Comments
 (0)