Skip to content

zig build: allow to choose "lazy mode" for fetching process #19975

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/compiler/build_runner.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1293,7 +1293,9 @@ fn usage(b: *std.Build, out_stream: anytype) !void {
\\ -j<N> Limit concurrent jobs (default is to use all CPU cores)
\\ --maxrss <bytes> 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 <ms> Delay before rebuilding after changed file detected
Expand Down
14 changes: 13 additions & 1 deletion src/Package/Fetch.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -2325,6 +2336,7 @@ const TestFetchBuilder = struct {
.read_only = false,
.debug_hash = false,
.work_around_btrfs_bug = false,
.mode = .needed,
};

self.fetch = .{
Expand Down
10 changes: 10 additions & 0 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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();

Expand Down
Loading