Skip to content

Commit e76c3bd

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 497592c commit e76c3bd

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
@@ -1280,7 +1280,9 @@ fn usage(b: *std.Build, out_stream: anytype) !void {
12801280
\\ -j<N> Limit concurrent jobs (default is to use all CPU cores)
12811281
\\ --maxrss <bytes> Limit memory usage (default is to use available memory)
12821282
\\ --skip-oom-steps Instead of failing, skip steps that would exceed --maxrss
1283-
\\ --fetch Exit after fetching dependency tree
1283+
\\ --fetch[=mode] Fetch dependency tree (optionally choose laziness) and exit
1284+
\\ lazy (Default) Lazy dependencies are fetched on need
1285+
\\ all Lazy dependencies are always fetched
12841286
\\ --watch Continuously rebuild when source files are modified
12851287
\\ --fuzz Continuously search for unit test failures
12861288
\\ --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

@@ -711,7 +719,10 @@ fn queueJobsForDeps(f: *Fetch) RunError!void {
711719
.location_tok = dep.location_tok,
712720
.hash_tok = dep.hash_tok,
713721
.name_tok = dep.name_tok,
714-
.lazy_status = if (dep.lazy) .available else .eager,
722+
.lazy_status = switch (f.job_queue.mode) {
723+
.lazy => if (dep.lazy) .available else .eager,
724+
.all => .eager,
725+
},
715726
.parent_package_root = f.package_root,
716727
.parent_manifest_ast = &f.manifest_ast,
717728
.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
@@ -4731,6 +4731,7 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
47314731
var verbose_cimport = false;
47324732
var verbose_llvm_cpu_features = false;
47334733
var fetch_only = false;
4734+
var fetch_mode: Package.Fetch.JobQueue.Mode = .lazy;
47344735
var system_pkg_dir_path: ?[]const u8 = null;
47354736
var debug_target: ?[]const u8 = null;
47364737

@@ -4812,6 +4813,13 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
48124813
reference_trace = 256;
48134814
} else if (mem.eql(u8, arg, "--fetch")) {
48144815
fetch_only = true;
4816+
} else if (mem.startsWith(u8, arg, "--fetch=")) {
4817+
fetch_only = true;
4818+
const sub_arg = arg["--fetch=".len..];
4819+
fetch_mode = std.meta.stringToEnum(Package.Fetch.JobQueue.Mode, sub_arg) orelse
4820+
fatal("expected [lazy|all] after '--fetch=', found '{s}'", .{
4821+
sub_arg,
4822+
});
48154823
} else if (mem.eql(u8, arg, "--system")) {
48164824
if (i + 1 >= args.len) fatal("expected argument after '{s}'", .{arg});
48174825
i += 1;
@@ -5087,6 +5095,7 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
50875095
.debug_hash = false,
50885096
.work_around_btrfs_bug = work_around_btrfs_bug,
50895097
.unlazy_set = unlazy_set,
5098+
.mode = fetch_mode,
50905099
};
50915100
defer job_queue.deinit();
50925101

@@ -7011,6 +7020,7 @@ fn cmdFetch(
70117020
.read_only = false,
70127021
.debug_hash = debug_hash,
70137022
.work_around_btrfs_bug = work_around_btrfs_bug,
7023+
.mode = .all,
70147024
};
70157025
defer job_queue.deinit();
70167026

0 commit comments

Comments
 (0)