authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-10-07 22:34:45-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-10-08 16:54:31-07:00
loge5c2a7dbca103b0c61bebaff95c5d5a5b777bd59
tree2f41df7e7c01286ef102dc7a814aae6c7cdf5f84
parent35d81c99c0f737f071e1ad0083342328dbb32acb

finish hooking up new dependency tree logic

* add Module instances for each package's build.zig and attach it to the dependencies.zig module with the hash digest hex string as the name. * fix incorrectly skipping the wrong packages for creating dependencies.zig * a couple more renaming of "package" to "module"

6 files changed, 77 insertions(+), 39 deletions(-)

lib/std/Build/Cache.zig+7
...@@ -9,6 +9,13 @@ pub const Directory = struct {...@@ -9,6 +9,13 @@ pub const Directory = struct {
9 path: ?[]const u8,9 path: ?[]const u8,
10 handle: fs.Dir,10 handle: fs.Dir,
1111
12 pub fn clone(d: Directory, arena: Allocator) Allocator.Error!Directory {
13 return .{
14 .path = if (d.path) |p| try arena.dupe(u8, p) else null,
15 .handle = d.handle,
16 };
17 }
18
12 pub fn cwd() Directory {19 pub fn cwd() Directory {
13 return .{20 return .{
14 .path = null,21 .path = null,
src/Module.zig+3-3
...@@ -4074,7 +4074,7 @@ pub fn importFile(...@@ -4074,7 +4074,7 @@ pub fn importFile(
4074 return mod.importPkg(pkg);4074 return mod.importPkg(pkg);
4075 }4075 }
4076 if (!mem.endsWith(u8, import_string, ".zig")) {4076 if (!mem.endsWith(u8, import_string, ".zig")) {
4077 return error.PackageNotFound;4077 return error.ModuleNotFound;
4078 }4078 }
4079 const gpa = mod.gpa;4079 const gpa = mod.gpa;
40804080
...@@ -4120,7 +4120,7 @@ pub fn importFile(...@@ -4120,7 +4120,7 @@ pub fn importFile(
4120 {4120 {
4121 break :p try gpa.dupe(u8, resolved_path);4121 break :p try gpa.dupe(u8, resolved_path);
4122 }4122 }
4123 return error.ImportOutsidePkgPath;4123 return error.ImportOutsideModulePath;
4124 };4124 };
4125 errdefer gpa.free(sub_file_path);4125 errdefer gpa.free(sub_file_path);
41264126
...@@ -4206,7 +4206,7 @@ pub fn embedFile(mod: *Module, cur_file: *File, import_string: []const u8) !*Emb...@@ -4206,7 +4206,7 @@ pub fn embedFile(mod: *Module, cur_file: *File, import_string: []const u8) !*Emb
4206 {4206 {
4207 break :p try gpa.dupe(u8, resolved_path);4207 break :p try gpa.dupe(u8, resolved_path);
4208 }4208 }
4209 return error.ImportOutsidePkgPath;4209 return error.ImportOutsideModulePath;
4210 };4210 };
4211 errdefer gpa.free(sub_file_path);4211 errdefer gpa.free(sub_file_path);
42124212
src/Package.zig+7
...@@ -9,6 +9,13 @@ pub const Path = struct {...@@ -9,6 +9,13 @@ pub const Path = struct {
9 /// Empty string means the root_dir is the path.9 /// Empty string means the root_dir is the path.
10 sub_path: []const u8 = "",10 sub_path: []const u8 = "",
1111
12 pub fn clone(p: Path, arena: Allocator) Allocator.Error!Path {
13 return .{
14 .root_dir = try p.root_dir.clone(arena),
15 .sub_path = try arena.dupe(u8, p.sub_path),
16 };
17 }
18
12 pub fn cwd() Path {19 pub fn cwd() Path {
13 return .{ .root_dir = Cache.Directory.cwd() };20 return .{ .root_dir = Cache.Directory.cwd() };
14 }21 }
src/Package/Fetch.zig+5-2
...@@ -109,7 +109,6 @@ pub const JobQueue = struct {...@@ -109,7 +109,6 @@ pub const JobQueue = struct {
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();111 const keys = jq.table.keys();
112 const values = jq.table.values();
113112
114 if (keys.len == 0)113 if (keys.len == 0)
115 return createEmptyDependenciesModule(buf);114 return createEmptyDependenciesModule(buf);
...@@ -124,7 +123,11 @@ pub const JobQueue = struct {...@@ -124,7 +123,11 @@ pub const JobQueue = struct {
124 }123 }
125 }, .{ .keys = keys }));124 }, .{ .keys = keys }));
126125
127 for (keys[1..], values[1..]) |hash, fetch| {126 for (keys, jq.table.values()) |hash, fetch| {
127 if (fetch == jq.all_fetches.items[0]) {
128 // The first one is a dummy package for the current project.
129 continue;
130 }
128 try buf.writer().print(131 try buf.writer().print(
129 \\ pub const {} = struct {{132 \\ pub const {} = struct {{
130 \\ pub const build_root = "{q}";133 \\ pub const build_root = "{q}";
src/Sema.zig+5-5
...@@ -13075,13 +13075,13 @@ fn zirImport(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air....@@ -13075,13 +13075,13 @@ fn zirImport(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.
13075 const operand = inst_data.get(sema.code);13075 const operand = inst_data.get(sema.code);
1307613076
13077 const result = mod.importFile(block.getFileScope(mod), operand) catch |err| switch (err) {13077 const result = mod.importFile(block.getFileScope(mod), operand) catch |err| switch (err) {
13078 error.ImportOutsidePkgPath => {13078 error.ImportOutsideModulePath => {
13079 return sema.fail(block, operand_src, "import of file outside package path: '{s}'", .{operand});13079 return sema.fail(block, operand_src, "import of file outside module path: '{s}'", .{operand});
13080 },13080 },
13081 error.PackageNotFound => {13081 error.ModuleNotFound => {
13082 //const name = try block.getFileScope(mod).mod.getName(sema.gpa, mod.*);13082 //const name = try block.getFileScope(mod).mod.getName(sema.gpa, mod.*);
13083 //defer sema.gpa.free(name);13083 //defer sema.gpa.free(name);
13084 return sema.fail(block, operand_src, "no package named '{s}' available within package '{}'", .{13084 return sema.fail(block, operand_src, "no module named '{s}' available within module '{}'", .{
13085 operand, block.getFileScope(mod).mod.root,13085 operand, block.getFileScope(mod).mod.root,
13086 });13086 });
13087 },13087 },
...@@ -13112,7 +13112,7 @@ fn zirEmbedFile(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A...@@ -13112,7 +13112,7 @@ fn zirEmbedFile(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
13112 }13112 }
1311313113
13114 const embed_file = mod.embedFile(block.getFileScope(mod), name) catch |err| switch (err) {13114 const embed_file = mod.embedFile(block.getFileScope(mod), name) catch |err| switch (err) {
13115 error.ImportOutsidePkgPath => {13115 error.ImportOutsideModulePath => {
13116 return sema.fail(block, operand_src, "embed of file outside package path: '{s}'", .{name});13116 return sema.fail(block, operand_src, "embed of file outside package path: '{s}'", .{name});
13117 },13117 },
13118 else => {13118 else => {
src/main.zig+50-29
...@@ -4885,39 +4885,60 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi...@@ -4885,39 +4885,60 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi
4885 }4885 }
48864886
4887 try job_queue.createDependenciesModule(&dependencies_zig_src);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);
4898 }
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();
49044888
4905 const o_dir_sub_path = try arena.dupe(u8, "o" ++ fs.path.sep_str ++ hex_digest);4889 const deps_mod = m: {
4906 try Package.Fetch.renameTmpIntoCache(4890 // Atomically create the file in a directory named after the hash of its contents.
4907 local_cache_directory.handle,4891 const basename = "dependencies.zig";
4908 tmp_dir_sub_path,4892 const rand_int = std.crypto.random.int(u64);
4909 o_dir_sub_path,4893 const tmp_dir_sub_path = "tmp" ++ fs.path.sep_str ++
4910 );4894 Package.Manifest.hex64(rand_int);
4895 {
4896 var tmp_dir = try local_cache_directory.handle.makeOpenPath(tmp_dir_sub_path, .{});
4897 defer tmp_dir.close();
4898 try tmp_dir.writeFile(basename, dependencies_zig_src.items);
4899 }
49114900
4912 const deps_mod = try Package.Module.create(arena, .{4901 var hh: Cache.HashHelper = .{};
4913 .root = .{4902 hh.addBytes(build_options.version);
4914 .root_dir = local_cache_directory,4903 hh.addBytes(dependencies_zig_src.items);
4915 .sub_path = o_dir_sub_path,4904 const hex_digest = hh.final();
4916 },4905
4917 .root_src_path = basename,4906 const o_dir_sub_path = try arena.dupe(u8, "o" ++ fs.path.sep_str ++ hex_digest);
4918 });4907 try Package.Fetch.renameTmpIntoCache(
4908 local_cache_directory.handle,
4909 tmp_dir_sub_path,
4910 o_dir_sub_path,
4911 );
4912
4913 break :m try Package.Module.create(arena, .{
4914 .root = .{
4915 .root_dir = local_cache_directory,
4916 .sub_path = o_dir_sub_path,
4917 },
4918 .root_src_path = basename,
4919 });
4920 };
4921 {
4922 // We need a Module for each package's build.zig.
4923 const hashes = job_queue.table.keys();
4924 const fetches = job_queue.table.values();
4925 try deps_mod.deps.ensureUnusedCapacity(arena, @intCast(hashes.len));
4926 for (hashes, fetches) |hash, f| {
4927 if (f == &fetch) {
4928 // The first one is a dummy package for the current project.
4929 continue;
4930 }
4931 const m = try Package.Module.create(arena, .{
4932 .root = try f.package_root.clone(arena),
4933 .root_src_path = if (f.has_build_zig) Package.build_zig_basename else "",
4934 });
4935 const hash_cloned = try arena.dupe(u8, &hash);
4936 deps_mod.deps.putAssumeCapacityNoClobber(hash_cloned, m);
4937 }
4938 }
4919 try main_mod.deps.put(arena, "@dependencies", deps_mod);4939 try main_mod.deps.put(arena, "@dependencies", deps_mod);
4920 }4940 }
4941
4921 try main_mod.deps.put(arena, "@build", &build_mod);4942 try main_mod.deps.put(arena, "@build", &build_mod);
49224943
4923 const comp = Compilation.create(gpa, .{4944 const comp = Compilation.create(gpa, .{