Skip to content

Commit

Permalink
zig build: allow to choose "lazy mode" for fetching process
Browse files Browse the repository at this point in the history
`--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]>
  • Loading branch information
BratishkaErik committed Jul 30, 2024
1 parent f219286 commit 6acd830
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
4 changes: 3 additions & 1 deletion lib/compiler/build_runner.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1243,7 +1243,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
\\ 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 <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 @@ -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);

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

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 @@ -4751,6 +4751,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;

Expand Down Expand Up @@ -4832,6 +4833,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;
Expand Down Expand Up @@ -5107,6 +5115,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 @@ -7040,6 +7049,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

0 comments on commit 6acd830

Please sign in to comment.