authorgravatar for bratishkaerik@landless-city.netEric Joldasov <bratishkaerik@landless-city.net> 2024-05-15 18:48:29+05:00
committergravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2025-03-26 19:00:33+01:00
log27c1f2b3a04960d9e8701f420560cc53e8e5f1dd
tree26c93802f5ede792cb5c1111e8b270a777b5d9d2
parentb4b1daf0012c3007b762df31028fa35f5f68bd09

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: * `needed` — 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 `needed` was passed. Signed-off-by: Eric Joldasov <bratishkaerik@landless-city.net>

3 files changed, 26 insertions(+), 2 deletions(-)

lib/compiler/build_runner.zig+3-1
...@@ -1293,7 +1293,9 @@ fn usage(b: *std.Build, out_stream: anytype) !void {...@@ -1293,7 +1293,9 @@ fn usage(b: *std.Build, out_stream: anytype) !void {
1293 \\ -j<N> Limit concurrent jobs (default is to use all CPU cores)1293 \\ -j<N> Limit concurrent jobs (default is to use all CPU cores)
1294 \\ --maxrss <bytes> Limit memory usage (default is to use available memory)1294 \\ --maxrss <bytes> Limit memory usage (default is to use available memory)
1295 \\ --skip-oom-steps Instead of failing, skip steps that would exceed --maxrss1295 \\ --skip-oom-steps Instead of failing, skip steps that would exceed --maxrss
1296 \\ --fetch Exit after fetching dependency tree1296 \\ --fetch[=mode] Fetch dependency tree (optionally choose laziness) and exit
1297 \\ needed (Default) Lazy dependencies are fetched as needed
1298 \\ all Lazy dependencies are always fetched
1297 \\ --watch Continuously rebuild when source files are modified1299 \\ --watch Continuously rebuild when source files are modified
1298 \\ --fuzz Continuously search for unit test failures1300 \\ --fuzz Continuously search for unit test failures
1299 \\ --debounce <ms> Delay before rebuilding after changed file detected1301 \\ --debounce <ms> Delay before rebuilding after changed file detected
src/Package/Fetch.zig+13-1
...@@ -114,10 +114,18 @@ pub const JobQueue = struct {...@@ -114,10 +114,18 @@ pub const JobQueue = struct {
114 /// If this is true, `recursive` must be false.114 /// If this is true, `recursive` must be false.
115 debug_hash: bool,115 debug_hash: bool,
116 work_around_btrfs_bug: bool,116 work_around_btrfs_bug: bool,
117 mode: Mode,
117 /// Set of hashes that will be additionally fetched even if they are marked118 /// Set of hashes that will be additionally fetched even if they are marked
118 /// as lazy.119 /// as lazy.
119 unlazy_set: UnlazySet = .{},120 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 needed,
126 /// Both non-lazy and lazy dependencies are always fetched.
127 all,
128 };
121 pub const Table = std.AutoArrayHashMapUnmanaged(Package.Hash, *Fetch);129 pub const Table = std.AutoArrayHashMapUnmanaged(Package.Hash, *Fetch);
122 pub const UnlazySet = std.AutoArrayHashMapUnmanaged(Package.Hash, void);130 pub const UnlazySet = std.AutoArrayHashMapUnmanaged(Package.Hash, void);
123131
...@@ -754,7 +762,10 @@ fn queueJobsForDeps(f: *Fetch) RunError!void {...@@ -754,7 +762,10 @@ fn queueJobsForDeps(f: *Fetch) RunError!void {
754 .location_tok = dep.location_tok,762 .location_tok = dep.location_tok,
755 .hash_tok = dep.hash_tok,763 .hash_tok = dep.hash_tok,
756 .name_tok = dep.name_tok,764 .name_tok = dep.name_tok,
757 .lazy_status = if (dep.lazy) .available else .eager,765 .lazy_status = switch (f.job_queue.mode) {
766 .needed => if (dep.lazy) .available else .eager,
767 .all => .eager,
768 },
758 .parent_package_root = f.package_root,769 .parent_package_root = f.package_root,
759 .parent_manifest_ast = &f.manifest_ast,770 .parent_manifest_ast = &f.manifest_ast,
760 .prog_node = f.prog_node,771 .prog_node = f.prog_node,
...@@ -2325,6 +2336,7 @@ const TestFetchBuilder = struct {...@@ -2325,6 +2336,7 @@ const TestFetchBuilder = struct {
2325 .read_only = false,2336 .read_only = false,
2326 .debug_hash = false,2337 .debug_hash = false,
2327 .work_around_btrfs_bug = false,2338 .work_around_btrfs_bug = false,
2339 .mode = .needed,
2328 };2340 };
23292341
2330 self.fetch = .{2342 self.fetch = .{
src/main.zig+10
...@@ -4843,6 +4843,7 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {...@@ -4843,6 +4843,7 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
4843 var verbose_cimport = false;4843 var verbose_cimport = false;
4844 var verbose_llvm_cpu_features = false;4844 var verbose_llvm_cpu_features = false;
4845 var fetch_only = false;4845 var fetch_only = false;
4846 var fetch_mode: Package.Fetch.JobQueue.Mode = .needed;
4846 var system_pkg_dir_path: ?[]const u8 = null;4847 var system_pkg_dir_path: ?[]const u8 = null;
4847 var debug_target: ?[]const u8 = null;4848 var debug_target: ?[]const u8 = null;
48484849
...@@ -4924,6 +4925,13 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {...@@ -4924,6 +4925,13 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
4924 reference_trace = 256;4925 reference_trace = 256;
4925 } else if (mem.eql(u8, arg, "--fetch")) {4926 } else if (mem.eql(u8, arg, "--fetch")) {
4926 fetch_only = true;4927 fetch_only = true;
4928 } else if (mem.startsWith(u8, arg, "--fetch=")) {
4929 fetch_only = true;
4930 const sub_arg = arg["--fetch=".len..];
4931 fetch_mode = std.meta.stringToEnum(Package.Fetch.JobQueue.Mode, sub_arg) orelse
4932 fatal("expected [needed|all] after '--fetch=', found '{s}'", .{
4933 sub_arg,
4934 });
4927 } else if (mem.eql(u8, arg, "--system")) {4935 } else if (mem.eql(u8, arg, "--system")) {
4928 if (i + 1 >= args.len) fatal("expected argument after '{s}'", .{arg});4936 if (i + 1 >= args.len) fatal("expected argument after '{s}'", .{arg});
4929 i += 1;4937 i += 1;
...@@ -5208,6 +5216,7 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {...@@ -5208,6 +5216,7 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
5208 .debug_hash = false,5216 .debug_hash = false,
5209 .work_around_btrfs_bug = work_around_btrfs_bug,5217 .work_around_btrfs_bug = work_around_btrfs_bug,
5210 .unlazy_set = unlazy_set,5218 .unlazy_set = unlazy_set,
5219 .mode = fetch_mode,
5211 };5220 };
5212 defer job_queue.deinit();5221 defer job_queue.deinit();
52135222
...@@ -7130,6 +7139,7 @@ fn cmdFetch(...@@ -7130,6 +7139,7 @@ fn cmdFetch(
7130 .read_only = false,7139 .read_only = false,
7131 .debug_hash = debug_hash,7140 .debug_hash = debug_hash,
7132 .work_around_btrfs_bug = work_around_btrfs_bug,7141 .work_around_btrfs_bug = work_around_btrfs_bug,
7142 .mode = .all,
7133 };7143 };
7134 defer job_queue.deinit();7144 defer job_queue.deinit();
71357145