authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-10-06 23:59:47-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-10-08 16:54:31-07:00
log4eb7b61daae4fe43b218eaaac0614f6bee1915d8
tree61794ebe2f922325403ad2a80eeee33ffa780b51
parent9eb21541ec9e8b64ae0d64e2851ed8d105308a35

package fetching: generate dependencies.zig file

Only problem is that it looks like `has_build_zig` is being false when it should be true. After that is fixed then main.zig needs to create the `@dependencies` module from the generated source code.

2 files changed, 84 insertions(+), 18 deletions(-)

src/Package/Fetch.zig+71-12
......@@ -61,6 +61,8 @@ oom_flag: bool,
6161/// Contains shared state among all `Fetch` tasks.
6262pub const JobQueue = struct {
6363 mutex: std.Thread.Mutex = .{},
64 /// It's an array hash map so that it can be sorted before rendering the
65 /// dependencies.zig source file.
6466 /// Protected by `mutex`.
6567 table: Table = .{},
6668 /// `table` may be missing some tasks such as ones that failed, so this
......@@ -75,7 +77,7 @@ pub const JobQueue = struct {
7577 recursive: bool,
7678 work_around_btrfs_bug: bool,
7779
78 pub const Table = std.AutoHashMapUnmanaged(Manifest.MultiHashHexDigest, *Fetch);
80 pub const Table = std.AutoArrayHashMapUnmanaged(Manifest.MultiHashHexDigest, *Fetch);
7981
8082 pub fn deinit(jq: *JobQueue) void {
8183 if (jq.all_fetches.items.len == 0) return;
......@@ -105,17 +107,74 @@ pub const JobQueue = struct {
105107
106108 /// Creates the dependencies.zig file and corresponding `Module` for the
107109 /// build runner to obtain via `@import("@dependencies")`.
108 pub fn createDependenciesModule(
109 jq: *JobQueue,
110 arena: Allocator,
111 local_cache_directory: Cache.Directory,
112 basename: []const u8,
113 ) !*Package.Module {
114 _ = jq;
115 _ = arena;
116 _ = local_cache_directory;
117 _ = basename;
118 @panic("TODO: createDependenciesModule");
110 pub fn createDependenciesModule(jq: *JobQueue, buf: *std.ArrayList(u8)) Allocator.Error!void {
111 try buf.appendSlice("pub const packages = struct {\n");
112
113 // Ensure the generated .zig file is deterministic.
114 jq.table.sortUnstable(@as(struct {
115 keys: []const Manifest.MultiHashHexDigest,
116 pub fn lessThan(ctx: @This(), a_index: usize, b_index: usize) bool {
117 return std.mem.lessThan(u8, &ctx.keys[a_index], &ctx.keys[b_index]);
118 }
119 }, .{ .keys = jq.table.keys() }));
120
121 for (jq.table.keys()[1..], jq.table.values()[1..]) |hash, fetch| {
122 try buf.writer().print(
123 \\ pub const {} = struct {{
124 \\ pub const build_root = "{q}";
125 \\
126 , .{ std.zig.fmtId(&hash), fetch.package_root });
127
128 if (fetch.has_build_zig) {
129 try buf.writer().print(
130 \\ pub const build_zig = @import("{}");
131 \\
132 , .{std.zig.fmtEscapes(&hash)});
133 }
134
135 if (fetch.manifest) |*manifest| {
136 try buf.appendSlice(
137 \\ pub const deps: []const struct { []const u8, []const u8 } = &.{
138 \\
139 );
140 for (manifest.dependencies.keys(), manifest.dependencies.values()) |name, dep| {
141 try buf.writer().print(
142 " .{{ \"{}\", \"{}\" }},\n",
143 .{ std.zig.fmtEscapes(name), std.zig.fmtEscapes(dep.hash.?) },
144 );
145 }
146
147 try buf.appendSlice(
148 \\ };
149 \\ };
150 \\
151 );
152 } else {
153 try buf.appendSlice(
154 \\ pub const deps: []const struct { []const u8, []const u8 } = &.{};
155 \\ };
156 \\
157 );
158 }
159 }
160
161 try buf.appendSlice(
162 \\};
163 \\
164 \\pub const root_deps: []const struct { []const u8, []const u8 } = &.{
165 \\
166 );
167
168 const root_fetch = jq.all_fetches.items[0];
169 const root_manifest = &root_fetch.manifest.?;
170
171 for (root_manifest.dependencies.keys(), root_manifest.dependencies.values()) |name, dep| {
172 try buf.writer().print(
173 " .{{ \"{}\", \"{}\" }},\n",
174 .{ std.zig.fmtEscapes(name), std.zig.fmtEscapes(dep.hash.?) },
175 );
176 }
177 try buf.appendSlice("};\n");
119178 }
120179};
121180
src/main.zig+13-6
......@@ -4887,12 +4887,19 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi
48874887 process.exit(1);
48884888 }
48894889
4890 const deps_mod = try job_queue.createDependenciesModule(
4891 arena,
4892 local_cache_directory,
4893 "dependencies.zig",
4894 );
4895 try main_mod.deps.put(arena, "@dependencies", deps_mod);
4890 var buf = std.ArrayList(u8).init(gpa);
4891 defer buf.deinit();
4892 try job_queue.createDependenciesModule(&buf);
4893 if (true) {
4894 std.debug.print("dependencies source:\n\n{s}\n", .{buf.items});
4895 @panic("TODO");
4896 }
4897 //const deps_mod = try job_queue.createDependenciesModule(
4898 // arena,
4899 // local_cache_directory,
4900 // "dependencies.zig",
4901 //);
4902 //try main_mod.deps.put(arena, "@dependencies", deps_mod);
48964903 }
48974904 try main_mod.deps.put(arena, "@build", &build_mod);
48984905