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 {...@@ -108,6 +108,12 @@ pub const JobQueue = struct {
108 /// Creates the dependencies.zig file and corresponding `Module` for the108 /// Creates the dependencies.zig file and corresponding `Module` for the
109 /// build runner to obtain via `@import("@dependencies")`.109 /// build runner to obtain via `@import("@dependencies")`.
110 pub fn createDependenciesModule(jq: *JobQueue, buf: *std.ArrayList(u8)) Allocator.Error!void {110 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
111 try buf.appendSlice("pub const packages = struct {\n");117 try buf.appendSlice("pub const packages = struct {\n");
112118
113 // Ensure the generated .zig file is deterministic.119 // Ensure the generated .zig file is deterministic.
...@@ -116,9 +122,9 @@ pub const JobQueue = struct {...@@ -116,9 +122,9 @@ pub const JobQueue = struct {
116 pub fn lessThan(ctx: @This(), a_index: usize, b_index: usize) bool {122 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]);123 return std.mem.lessThan(u8, &ctx.keys[a_index], &ctx.keys[b_index]);
118 }124 }
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| {
122 try buf.writer().print(128 try buf.writer().print(
123 \\ pub const {} = struct {{129 \\ pub const {} = struct {{
124 \\ pub const build_root = "{q}";130 \\ pub const build_root = "{q}";
...@@ -176,6 +182,14 @@ pub const JobQueue = struct {...@@ -176,6 +182,14 @@ pub const JobQueue = struct {
176 }182 }
177 try buf.appendSlice("};\n");183 try buf.appendSlice("};\n");
178 }184 }
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 }
179};193};
180194
181pub const Location = union(enum) {195pub 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...@@ -4827,13 +4827,10 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi
4827 .root = .{ .root_dir = build_root },4827 .root = .{ .root_dir = build_root },
4828 .root_src_path = build_zig_basename,4828 .root_src_path = build_zig_basename,
4829 };4829 };
4830 var dependencies_zig_src = std.ArrayList(u8).init(gpa);
4831 defer dependencies_zig_src.deinit();
4830 if (build_options.only_core_functionality) {4832 if (build_options.only_core_functionality) {
4831 const deps_mod = try Package.createFilePkg(gpa, local_cache_directory, "dependencies.zig",4833 try Package.Fetch.createEmptyDependenciesModule(&dependencies_zig_src);
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);
4837 } else {4834 } else {
4838 var http_client: std.http.Client = .{ .allocator = gpa };4835 var http_client: std.http.Client = .{ .allocator = gpa };
4839 defer http_client.deinit();4836 defer http_client.deinit();
...@@ -4887,19 +4884,39 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi...@@ -4887,19 +4884,39 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi
4887 process.exit(1);4884 process.exit(1);
4888 }4885 }
48894886
4890 var buf = std.ArrayList(u8).init(gpa);4887 try job_queue.createDependenciesModule(&dependencies_zig_src);
4891 defer buf.deinit();4888 }
4892 try job_queue.createDependenciesModule(&buf);4889 {
4893 if (true) {4890 // Atomically create the file in a directory named after the hash of its contents.
4894 std.debug.print("dependencies source:\n\n{s}\n", .{buf.items});4891 const basename = "dependencies.zig";
4895 @panic("TODO");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);
4896 }4898 }
4897 //const deps_mod = try job_queue.createDependenciesModule(4899
4898 // arena,4900 var hh: Cache.HashHelper = .{};
4899 // local_cache_directory,4901 hh.addBytes(build_options.version);
4900 // "dependencies.zig",4902 hh.addBytes(dependencies_zig_src.items);
4901 //);4903 const hex_digest = hh.final();
4902 //try main_mod.deps.put(arena, "@dependencies", deps_mod);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);
4903 }4920 }
4904 try main_mod.deps.put(arena, "@build", &build_mod);4921 try main_mod.deps.put(arena, "@build", &build_mod);
49054922