From 27c1f2b3a04960d9e8701f420560cc53e8e5f1dd Mon Sep 17 00:00:00 2001 From: Eric Joldasov Date: Wed, 15 May 2024 18:48:29 +0500 Subject: [PATCH] zig build: allow to choose "lazy mode" for fetching process MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `--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 --- lib/compiler/build_runner.zig | 4 +++- src/Package/Fetch.zig | 14 +++++++++++++- src/main.zig | 10 ++++++++++ 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/lib/compiler/build_runner.zig b/lib/compiler/build_runner.zig index 8702acb329..aacd26b82d 100644 --- a/lib/compiler/build_runner.zig +++ b/lib/compiler/build_runner.zig @@ -1293,7 +1293,9 @@ fn usage(b: *std.Build, out_stream: anytype) !void { \\ -j Limit concurrent jobs (default is to use all CPU cores) \\ --maxrss Limit memory usage (default is to use available memory) \\ --skip-oom-steps Instead of failing, skip steps that would exceed --maxrss - \\ --fetch Exit after fetching dependency tree + \\ --fetch[=mode] Fetch dependency tree (optionally choose laziness) and exit + \\ needed (Default) Lazy dependencies are fetched as needed + \\ all Lazy dependencies are always fetched \\ --watch Continuously rebuild when source files are modified \\ --fuzz Continuously search for unit test failures \\ --debounce Delay before rebuilding after changed file detected diff --git a/src/Package/Fetch.zig b/src/Package/Fetch.zig index bb70518910..4e386be2d5 100644 --- a/src/Package/Fetch.zig +++ b/src/Package/Fetch.zig @@ -114,10 +114,18 @@ pub const JobQueue = struct { /// If this is true, `recursive` must be false. debug_hash: bool, work_around_btrfs_bug: bool, + mode: Mode, /// Set of hashes that will be additionally fetched even if they are marked /// as lazy. unlazy_set: UnlazySet = .{}, + pub const Mode = enum { + /// Non-lazy dependencies are always fetched. + /// Lazy dependencies are fetched only when needed. + needed, + /// Both non-lazy and lazy dependencies are always fetched. + all, + }; pub const Table = std.AutoArrayHashMapUnmanaged(Package.Hash, *Fetch); pub const UnlazySet = std.AutoArrayHashMapUnmanaged(Package.Hash, void); @@ -754,7 +762,10 @@ fn queueJobsForDeps(f: *Fetch) RunError!void { .location_tok = dep.location_tok, .hash_tok = dep.hash_tok, .name_tok = dep.name_tok, - .lazy_status = if (dep.lazy) .available else .eager, + .lazy_status = switch (f.job_queue.mode) { + .needed => if (dep.lazy) .available else .eager, + .all => .eager, + }, .parent_package_root = f.package_root, .parent_manifest_ast = &f.manifest_ast, .prog_node = f.prog_node, @@ -2325,6 +2336,7 @@ const TestFetchBuilder = struct { .read_only = false, .debug_hash = false, .work_around_btrfs_bug = false, + .mode = .needed, }; self.fetch = .{ diff --git a/src/main.zig b/src/main.zig index 865ce78b47..2687546e41 100644 --- a/src/main.zig +++ b/src/main.zig @@ -4843,6 +4843,7 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !void { var verbose_cimport = false; var verbose_llvm_cpu_features = false; var fetch_only = false; + var fetch_mode: Package.Fetch.JobQueue.Mode = .needed; var system_pkg_dir_path: ?[]const u8 = null; var debug_target: ?[]const u8 = null; @@ -4924,6 +4925,13 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !void { reference_trace = 256; } else if (mem.eql(u8, arg, "--fetch")) { fetch_only = true; + } else if (mem.startsWith(u8, arg, "--fetch=")) { + fetch_only = true; + const sub_arg = arg["--fetch=".len..]; + fetch_mode = std.meta.stringToEnum(Package.Fetch.JobQueue.Mode, sub_arg) orelse + fatal("expected [needed|all] after '--fetch=', found '{s}'", .{ + sub_arg, + }); } else if (mem.eql(u8, arg, "--system")) { if (i + 1 >= args.len) fatal("expected argument after '{s}'", .{arg}); i += 1; @@ -5208,6 +5216,7 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !void { .debug_hash = false, .work_around_btrfs_bug = work_around_btrfs_bug, .unlazy_set = unlazy_set, + .mode = fetch_mode, }; defer job_queue.deinit(); @@ -7130,6 +7139,7 @@ fn cmdFetch( .read_only = false, .debug_hash = debug_hash, .work_around_btrfs_bug = work_around_btrfs_bug, + .mode = .all, }; defer job_queue.deinit();