authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-10-07 14:37:25-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-10-08 16:54:31-07:00
loga232f7e8ecc9f8afb210ffd483ca59556175a33c
treeb94050e7fa1929759e695317c10d58df766d8738
parente86b7fcca3e522bede8bab06d669b75d0dca2038

finish implementing the auto-generated dependencies.zig module


2 files changed, 51 insertions(+), 20 deletions(-)

src/Package/Fetch.zig+16-2
......@@ -108,6 +108,12 @@ pub const JobQueue = struct {
108108 /// Creates the dependencies.zig file and corresponding `Module` for the
109109 /// build runner to obtain via `@import("@dependencies")`.
110110 pub fn createDependenciesModule(jq: *JobQueue, buf: *std.ArrayList(u8)) Allocator.Error!void {
111 const keys = jq.table.keys();
112 const values = jq.table.values();
113
114 if (keys.len == 0)
115 return createEmptyDependenciesModule(buf);
116
111117 try buf.appendSlice("pub const packages = struct {\n");
112118
113119 // Ensure the generated .zig file is deterministic.
......@@ -116,9 +122,9 @@ pub const JobQueue = struct {
116122 pub fn lessThan(ctx: @This(), a_index: usize, b_index: usize) bool {
117123 return std.mem.lessThan(u8, &ctx.keys[a_index], &ctx.keys[b_index]);
118124 }
119 }, .{ .keys = jq.table.keys() }));
125 }, .{ .keys = keys }));
120126
121 for (jq.table.keys()[1..], jq.table.values()[1..]) |hash, fetch| {
127 for (keys[1..], values[1..]) |hash, fetch| {
122128 try buf.writer().print(
123129 \\ pub const {} = struct {{
124130 \\ pub const build_root = "{q}";
......@@ -176,6 +182,14 @@ pub const JobQueue = struct {
176182 }
177183 try buf.appendSlice("};\n");
178184 }
185
186 pub fn createEmptyDependenciesModule(buf: *std.ArrayList(u8)) Allocator.Error!void {
187 try buf.appendSlice(
188 \\pub const packages = struct {};
189 \\pub const root_deps: []const struct { []const u8, []const u8 } = &.{};
190 \\
191 );
192 }
179193};
180194
181195pub const Location = union(enum) {
src/main.zig+35-18
......@@ -4827,13 +4827,10 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi
48274827 .root = .{ .root_dir = build_root },
48284828 .root_src_path = build_zig_basename,
48294829 };
4830 var dependencies_zig_src = std.ArrayList(u8).init(gpa);
4831 defer dependencies_zig_src.deinit();
48304832 if (build_options.only_core_functionality) {
4831 const deps_mod = try Package.createFilePkg(gpa, local_cache_directory, "dependencies.zig",
4832 \\pub const packages = struct {};
4833 \\pub const root_deps: []const struct { []const u8, []const u8 } = &.{};
4834 \\
4835 );
4836 try main_mod.deps.put(arena, "@dependencies", deps_mod);
4833 try Package.Fetch.createEmptyDependenciesModule(&dependencies_zig_src);
48374834 } else {
48384835 var http_client: std.http.Client = .{ .allocator = gpa };
48394836 defer http_client.deinit();
......@@ -4887,19 +4884,39 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi
48874884 process.exit(1);
48884885 }
48894886
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");
4887 try job_queue.createDependenciesModule(&dependencies_zig_src);
4888 }
4889 {
4890 // Atomically create the file in a directory named after the hash of its contents.
4891 const basename = "dependencies.zig";
4892 const rand_int = std.crypto.random.int(u64);
4893 const tmp_dir_sub_path = "tmp" ++ fs.path.sep_str ++ Package.Manifest.hex64(rand_int);
4894 {
4895 var tmp_dir = try local_cache_directory.handle.makeOpenPath(tmp_dir_sub_path, .{});
4896 defer tmp_dir.close();
4897 try tmp_dir.writeFile(basename, dependencies_zig_src.items);
48964898 }
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);
4899
4900 var hh: Cache.HashHelper = .{};
4901 hh.addBytes(build_options.version);
4902 hh.addBytes(dependencies_zig_src.items);
4903 const hex_digest = hh.final();
4904
4905 const o_dir_sub_path = try arena.dupe(u8, "o" ++ fs.path.sep_str ++ hex_digest);
4906 try Package.Fetch.renameTmpIntoCache(
4907 local_cache_directory.handle,
4908 tmp_dir_sub_path,
4909 o_dir_sub_path,
4910 );
4911
4912 const deps_mod = try Package.Module.create(arena, .{
4913 .root = .{
4914 .root_dir = local_cache_directory,
4915 .sub_path = o_dir_sub_path,
4916 },
4917 .root_src_path = basename,
4918 });
4919 try main_mod.deps.put(arena, "@dependencies", deps_mod);
49034920 }
49044921 try main_mod.deps.put(arena, "@build", &build_mod);
49054922