authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-10-08 16:08:08-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-10-08 16:54:31-07:00
log1ad33f53fea4109160ccfceec94efa2013f3b3b3
treea3f603eccd53ff84c5a74d733401a6d0e9a625e9
parentce052d89a85be02494b25ba734ff4483e846176c

give build.zig modules access to their dependencies' build.zig modules


2 files changed, 57 insertions(+), 6 deletions(-)

src/Package/Fetch.zig+32-5
...@@ -58,6 +58,12 @@ has_build_zig: bool,...@@ -58,6 +58,12 @@ has_build_zig: bool,
58/// Indicates whether the task aborted due to an out-of-memory condition.58/// Indicates whether the task aborted due to an out-of-memory condition.
59oom_flag: bool,59oom_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.
65module: ?*Package.Module,
66
61/// Contains shared state among all `Fetch` tasks.67/// Contains shared state among all `Fetch` tasks.
62pub const JobQueue = struct {68pub const JobQueue = struct {
63 mutex: std.Thread.Mutex = .{},69 mutex: std.Thread.Mutex = .{},
...@@ -147,10 +153,10 @@ pub const JobQueue = struct {...@@ -147,10 +153,10 @@ pub const JobQueue = struct {
147 \\153 \\
148 );154 );
149 for (manifest.dependencies.keys(), manifest.dependencies.values()) |name, dep| {155 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;
151 try buf.writer().print(157 try buf.writer().print(
152 " .{{ \"{}\", \"{}\" }},\n",158 " .{{ \"{}\", \"{}\" }},\n",
153 .{ std.zig.fmtEscapes(name), std.zig.fmtEscapes(h) },159 .{ std.zig.fmtEscapes(name), std.zig.fmtEscapes(&h) },
154 );160 );
155 }161 }
156162
...@@ -179,10 +185,10 @@ pub const JobQueue = struct {...@@ -179,10 +185,10 @@ pub const JobQueue = struct {
179 const root_manifest = &root_fetch.manifest.?;185 const root_manifest = &root_fetch.manifest.?;
180186
181 for (root_manifest.dependencies.keys(), root_manifest.dependencies.values()) |name, dep| {187 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;
183 try buf.writer().print(189 try buf.writer().print(
184 " .{{ \"{}\", \"{}\" }},\n",190 " .{{ \"{}\", \"{}\" }},\n",
185 .{ std.zig.fmtEscapes(name), std.zig.fmtEscapes(h) },191 .{ std.zig.fmtEscapes(name), std.zig.fmtEscapes(&h) },
186 );192 );
187 }193 }
188 try buf.appendSlice("};\n");194 try buf.appendSlice("};\n");
...@@ -258,7 +264,7 @@ pub fn run(f: *Fetch) RunError!void {...@@ -258,7 +264,7 @@ pub fn run(f: *Fetch) RunError!void {
258 }264 }
259 f.package_root = pkg_root;265 f.package_root = pkg_root;
260 try loadManifest(f, pkg_root);266 try loadManifest(f, pkg_root);
261 try checkBuildFileExistence(f);267 if (!f.has_build_zig) try checkBuildFileExistence(f);
262 if (!f.job_queue.recursive) return;268 if (!f.job_queue.recursive) return;
263 return queueJobsForDeps(f);269 return queueJobsForDeps(f);
264 },270 },
...@@ -604,6 +610,8 @@ fn queueJobsForDeps(f: *Fetch) RunError!void {...@@ -604,6 +610,8 @@ fn queueJobsForDeps(f: *Fetch) RunError!void {
604 .actual_hash = undefined,610 .actual_hash = undefined,
605 .has_build_zig = false,611 .has_build_zig = false,
606 .oom_flag = false,612 .oom_flag = false,
613
614 .module = null,
607 };615 };
608 }616 }
609617
...@@ -1400,6 +1408,25 @@ const Filter = struct {...@@ -1400,6 +1408,25 @@ const Filter = struct {
1400 }1408 }
1401};1409};
14021410
1411pub 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
1403// These are random bytes.1430// These are random bytes.
1404const package_hash_prefix_cached = [8]u8{ 0x53, 0x7e, 0xfa, 0x94, 0x65, 0xe9, 0xf8, 0x73 };1431const package_hash_prefix_cached = [8]u8{ 0x53, 0x7e, 0xfa, 0x94, 0x65, 0xe9, 0xf8, 0x73 };
1405const package_hash_prefix_project = [8]u8{ 0xe1, 0x25, 0xee, 0xfa, 0xa6, 0x17, 0x38, 0xcc };1432const package_hash_prefix_project = [8]u8{ 0xe1, 0x25, 0xee, 0xfa, 0xa6, 0x17, 0x38, 0xcc };
src/main.zig+25-1
...@@ -4870,8 +4870,10 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi...@@ -4870,8 +4870,10 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi
4870 .manifest = null,4870 .manifest = null,
4871 .manifest_ast = undefined,4871 .manifest_ast = undefined,
4872 .actual_hash = undefined,4872 .actual_hash = undefined,
4873 .has_build_zig = false,4873 .has_build_zig = true,
4874 .oom_flag = false,4874 .oom_flag = false,
4875
4876 .module = &build_mod,
4875 };4877 };
4876 job_queue.all_fetches.appendAssumeCapacity(&fetch);4878 job_queue.all_fetches.appendAssumeCapacity(&fetch);
48774879
...@@ -4922,6 +4924,26 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi...@@ -4922,6 +4924,26 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi
4922 });4924 });
4923 const hash_cloned = try arena.dupe(u8, &hash);4925 const hash_cloned = try arena.dupe(u8, &hash);
4924 deps_mod.deps.putAssumeCapacityNoClobber(hash_cloned, m);4926 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 }
4925 }4947 }
4926 }4948 }
4927 }4949 }
...@@ -6751,6 +6773,8 @@ fn cmdFetch(...@@ -6751,6 +6773,8 @@ fn cmdFetch(
6751 .actual_hash = undefined,6773 .actual_hash = undefined,
6752 .has_build_zig = false,6774 .has_build_zig = false,
6753 .oom_flag = false,6775 .oom_flag = false,
6776
6777 .module = null,
6754 };6778 };
6755 defer fetch.deinit();6779 defer fetch.deinit();
67566780