Skip to content

Commit 66f960a

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 a31fe8a commit 66f960a

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
@@ -4653,7 +4653,9 @@ const usage_build =
46534653
\\ --build-runner [file] Override path to build runner
46544654
\\ --prominent-compile-errors Buffer compile errors and display at end
46554655
\\ --seed [integer] For shuffling dependency traversal order (default: random)
4656-
\\ --fetch Exit after fetching dependency tree
4656+
\\ --fetch[=mode] Fetch dependency tree (optionally choose laziness) and exit
4657+
\\ lazy (Default) Lazy dependencies are fetched on need
4658+
\\ all Lazy dependencies are always fetched
46574659
\\ -h, --help Print this help and exit
46584660
\\
46594661
;
@@ -4679,6 +4681,7 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
46794681
var verbose_cimport = false;
46804682
var verbose_llvm_cpu_features = false;
46814683
var fetch_only = false;
4684+
var fetch_mode: Package.Fetch.JobQueue.Mode = .lazy;
46824685
var system_pkg_dir_path: ?[]const u8 = null;
46834686

46844687
const argv_index_exe = child_argv.items.len;
@@ -4756,6 +4759,13 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
47564759
reference_trace = 256;
47574760
} else if (mem.eql(u8, arg, "--fetch")) {
47584761
fetch_only = true;
4762+
} else if (mem.startsWith(u8, arg, "--fetch=")) {
4763+
fetch_only = true;
4764+
const sub_arg = arg["--fetch=".len..];
4765+
fetch_mode = std.meta.stringToEnum(Package.Fetch.JobQueue.Mode, sub_arg) orelse
4766+
fatal("expected [lazy|all] after '--fetch=', found '{s}'", .{
4767+
sub_arg,
4768+
});
47594769
} else if (mem.eql(u8, arg, "--system")) {
47604770
if (i + 1 >= args.len) fatal("expected argument after '{s}'", .{arg});
47614771
i += 1;
@@ -4998,6 +5008,7 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
49985008
.debug_hash = false,
49995009
.work_around_btrfs_bug = work_around_btrfs_bug,
50005010
.unlazy_set = unlazy_set,
5011+
.mode = fetch_mode,
50015012
};
50025013
defer job_queue.deinit();
50035014

@@ -6907,6 +6918,7 @@ fn cmdFetch(
69076918
.read_only = false,
69086919
.debug_hash = debug_hash,
69096920
.work_around_btrfs_bug = work_around_btrfs_bug,
6921+
.mode = .all,
69106922
};
69116923
defer job_queue.deinit();
69126924

0 commit comments

Comments
 (0)