From 185770625b06cb4b18f7dd6351d0325d4114df39 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: * `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 --- 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 690c93754553..bd0d533398b4 100644 --- a/lib/compiler/build_runner.zig +++ b/lib/compiler/build_runner.zig @@ -1278,7 +1278,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 + \\ lazy (Default) Lazy dependencies are fetched on need + \\ 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 28030e3879cf..3004b4a7105b 100644 --- a/src/Package/Fetch.zig +++ b/src/Package/Fetch.zig @@ -112,10 +112,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. + lazy, + /// Both non-lazy and lazy dependencies are always fetched. + all, + }; pub const Table = std.AutoArrayHashMapUnmanaged(Manifest.MultiHashHexDigest, *Fetch); pub const UnlazySet = std.AutoArrayHashMapUnmanaged(Manifest.MultiHashHexDigest, void); @@ -698,7 +706,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) { + .lazy => 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, @@ -2260,6 +2271,7 @@ const TestFetchBuilder = struct { .read_only = false, .debug_hash = false, .work_around_btrfs_bug = false, + .mode = .lazy, }; self.fetch = .{ diff --git a/src/main.zig b/src/main.zig index 226ec7946723..30632dfc71e9 100644 --- a/src/main.zig +++ b/src/main.zig @@ -4731,6 +4731,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 = .lazy; var system_pkg_dir_path: ?[]const u8 = null; var debug_target: ?[]const u8 = null; @@ -4812,6 +4813,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 [lazy|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; @@ -5087,6 +5095,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(); @@ -7020,6 +7029,7 @@ fn cmdFetch( .read_only = false, .debug_hash = debug_hash, .work_around_btrfs_bug = work_around_btrfs_bug, + .mode = .all, }; defer job_queue.deinit();