Skip to content

Commit 1ad33f5

Browse files
committed
give build.zig modules access to their dependencies' build.zig modules
1 parent ce052d8 commit 1ad33f5

File tree

2 files changed

+57
-6
lines changed

2 files changed

+57
-6
lines changed

src/Package/Fetch.zig

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ has_build_zig: bool,
5858
/// Indicates whether the task aborted due to an out-of-memory condition.
5959
oom_flag: bool,
6060

61+
// This field is used by the CLI only, untouched by this file.
62+
63+
/// The module for this `Fetch` tasks's package, which exposes `build.zig` as
64+
/// the root source file.
65+
module: ?*Package.Module,
66+
6167
/// Contains shared state among all `Fetch` tasks.
6268
pub const JobQueue = struct {
6369
mutex: std.Thread.Mutex = .{},
@@ -147,10 +153,10 @@ pub const JobQueue = struct {
147153
\\
148154
);
149155
for (manifest.dependencies.keys(), manifest.dependencies.values()) |name, dep| {
150-
const h = dep.hash orelse continue;
156+
const h = depDigest(fetch.package_root, jq.global_cache, dep) orelse continue;
151157
try buf.writer().print(
152158
" .{{ \"{}\", \"{}\" }},\n",
153-
.{ std.zig.fmtEscapes(name), std.zig.fmtEscapes(h) },
159+
.{ std.zig.fmtEscapes(name), std.zig.fmtEscapes(&h) },
154160
);
155161
}
156162

@@ -179,10 +185,10 @@ pub const JobQueue = struct {
179185
const root_manifest = &root_fetch.manifest.?;
180186

181187
for (root_manifest.dependencies.keys(), root_manifest.dependencies.values()) |name, dep| {
182-
const h = dep.hash orelse continue;
188+
const h = depDigest(root_fetch.package_root, jq.global_cache, dep) orelse continue;
183189
try buf.writer().print(
184190
" .{{ \"{}\", \"{}\" }},\n",
185-
.{ std.zig.fmtEscapes(name), std.zig.fmtEscapes(h) },
191+
.{ std.zig.fmtEscapes(name), std.zig.fmtEscapes(&h) },
186192
);
187193
}
188194
try buf.appendSlice("};\n");
@@ -258,7 +264,7 @@ pub fn run(f: *Fetch) RunError!void {
258264
}
259265
f.package_root = pkg_root;
260266
try loadManifest(f, pkg_root);
261-
try checkBuildFileExistence(f);
267+
if (!f.has_build_zig) try checkBuildFileExistence(f);
262268
if (!f.job_queue.recursive) return;
263269
return queueJobsForDeps(f);
264270
},
@@ -604,6 +610,8 @@ fn queueJobsForDeps(f: *Fetch) RunError!void {
604610
.actual_hash = undefined,
605611
.has_build_zig = false,
606612
.oom_flag = false,
613+
614+
.module = null,
607615
};
608616
}
609617

@@ -1400,6 +1408,25 @@ const Filter = struct {
14001408
}
14011409
};
14021410

1411+
pub fn depDigest(
1412+
pkg_root: Package.Path,
1413+
cache_root: Cache.Directory,
1414+
dep: Manifest.Dependency,
1415+
) ?Manifest.MultiHashHexDigest {
1416+
if (dep.hash) |h| return h[0..Manifest.multihash_hex_digest_len].*;
1417+
1418+
switch (dep.location) {
1419+
.url => return null,
1420+
.path => |rel_path| {
1421+
var buf: [fs.MAX_PATH_BYTES]u8 = undefined;
1422+
var fba = std.heap.FixedBufferAllocator.init(&buf);
1423+
const new_root = pkg_root.resolvePosix(fba.allocator(), rel_path) catch
1424+
return null;
1425+
return relativePathDigest(new_root, cache_root);
1426+
},
1427+
}
1428+
}
1429+
14031430
// These are random bytes.
14041431
const package_hash_prefix_cached = [8]u8{ 0x53, 0x7e, 0xfa, 0x94, 0x65, 0xe9, 0xf8, 0x73 };
14051432
const package_hash_prefix_project = [8]u8{ 0xe1, 0x25, 0xee, 0xfa, 0xa6, 0x17, 0x38, 0xcc };

src/main.zig

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4870,8 +4870,10 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi
48704870
.manifest = null,
48714871
.manifest_ast = undefined,
48724872
.actual_hash = undefined,
4873-
.has_build_zig = false,
4873+
.has_build_zig = true,
48744874
.oom_flag = false,
4875+
4876+
.module = &build_mod,
48754877
};
48764878
job_queue.all_fetches.appendAssumeCapacity(&fetch);
48774879

@@ -4922,6 +4924,26 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi
49224924
});
49234925
const hash_cloned = try arena.dupe(u8, &hash);
49244926
deps_mod.deps.putAssumeCapacityNoClobber(hash_cloned, m);
4927+
f.module = m;
4928+
}
4929+
4930+
// Each build.zig module needs access to each of its
4931+
// dependencies' build.zig modules by name.
4932+
for (fetches) |f| {
4933+
const mod = f.module orelse continue;
4934+
const man = f.manifest orelse continue;
4935+
const dep_names = man.dependencies.keys();
4936+
try mod.deps.ensureUnusedCapacity(arena, @intCast(dep_names.len));
4937+
for (dep_names, man.dependencies.values()) |name, dep| {
4938+
const dep_digest = Package.Fetch.depDigest(
4939+
f.package_root,
4940+
global_cache_directory,
4941+
dep,
4942+
) orelse continue;
4943+
const dep_mod = job_queue.table.get(dep_digest).?.module orelse continue;
4944+
const name_cloned = try arena.dupe(u8, name);
4945+
mod.deps.putAssumeCapacityNoClobber(name_cloned, dep_mod);
4946+
}
49254947
}
49264948
}
49274949
}
@@ -6751,6 +6773,8 @@ fn cmdFetch(
67516773
.actual_hash = undefined,
67526774
.has_build_zig = false,
67536775
.oom_flag = false,
6776+
6777+
.module = null,
67546778
};
67556779
defer fetch.deinit();
67566780

0 commit comments

Comments
 (0)