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 {
99 path: ?[]const u8,
1010 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
1219 pub fn cwd() Directory {
1320 return .{
1421 .path = null,
src/Module.zig+3-3
......@@ -4074,7 +4074,7 @@ pub fn importFile(
40744074 return mod.importPkg(pkg);
40754075 }
40764076 if (!mem.endsWith(u8, import_string, ".zig")) {
4077 return error.PackageNotFound;
4077 return error.ModuleNotFound;
40784078 }
40794079 const gpa = mod.gpa;
40804080
......@@ -4120,7 +4120,7 @@ pub fn importFile(
41204120 {
41214121 break :p try gpa.dupe(u8, resolved_path);
41224122 }
4123 return error.ImportOutsidePkgPath;
4123 return error.ImportOutsideModulePath;
41244124 };
41254125 errdefer gpa.free(sub_file_path);
41264126
......@@ -4206,7 +4206,7 @@ pub fn embedFile(mod: *Module, cur_file: *File, import_string: []const u8) !*Emb
42064206 {
42074207 break :p try gpa.dupe(u8, resolved_path);
42084208 }
4209 return error.ImportOutsidePkgPath;
4209 return error.ImportOutsideModulePath;
42104210 };
42114211 errdefer gpa.free(sub_file_path);
42124212
src/Package.zig+7
......@@ -9,6 +9,13 @@ pub const Path = struct {
99 /// Empty string means the root_dir is the path.
1010 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
1219 pub fn cwd() Path {
1320 return .{ .root_dir = Cache.Directory.cwd() };
1421 }
src/Package/Fetch.zig+5-2
......@@ -109,7 +109,6 @@ pub const JobQueue = struct {
109109 /// build runner to obtain via `@import("@dependencies")`.
110110 pub fn createDependenciesModule(jq: *JobQueue, buf: *std.ArrayList(u8)) Allocator.Error!void {
111111 const keys = jq.table.keys();
112 const values = jq.table.values();
113112
114113 if (keys.len == 0)
115114 return createEmptyDependenciesModule(buf);
......@@ -124,7 +123,11 @@ pub const JobQueue = struct {
124123 }
125124 }, .{ .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 }
128131 try buf.writer().print(
129132 \\ pub const {} = struct {{
130133 \\ 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.
1307513075 const operand = inst_data.get(sema.code);
1307613076
1307713077 const result = mod.importFile(block.getFileScope(mod), operand) catch |err| switch (err) {
13078 error.ImportOutsidePkgPath => {
13079 return sema.fail(block, operand_src, "import of file outside package path: '{s}'", .{operand});
13078 error.ImportOutsideModulePath => {
13079 return sema.fail(block, operand_src, "import of file outside module path: '{s}'", .{operand});
1308013080 },
13081 error.PackageNotFound => {
13081 error.ModuleNotFound => {
1308213082 //const name = try block.getFileScope(mod).mod.getName(sema.gpa, mod.*);
1308313083 //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 '{}'", .{
1308513085 operand, block.getFileScope(mod).mod.root,
1308613086 });
1308713087 },
......@@ -13112,7 +13112,7 @@ fn zirEmbedFile(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
1311213112 }
1311313113
1311413114 const embed_file = mod.embedFile(block.getFileScope(mod), name) catch |err| switch (err) {
13115 error.ImportOutsidePkgPath => {
13115 error.ImportOutsideModulePath => {
1311613116 return sema.fail(block, operand_src, "embed of file outside package path: '{s}'", .{name});
1311713117 },
1311813118 else => {
src/main.zig+50-29
......@@ -4885,39 +4885,60 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi
48854885 }
48864886
48874887 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);
4906 try Package.Fetch.renameTmpIntoCache(
4907 local_cache_directory.handle,
4908 tmp_dir_sub_path,
4909 o_dir_sub_path,
4910 );
4889 const deps_mod = m: {
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 ++
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, .{
4913 .root = .{
4914 .root_dir = local_cache_directory,
4915 .sub_path = o_dir_sub_path,
4916 },
4917 .root_src_path = basename,
4918 });
4901 var hh: Cache.HashHelper = .{};
4902 hh.addBytes(build_options.version);
4903 hh.addBytes(dependencies_zig_src.items);
4904 const hex_digest = hh.final();
4905
4906 const o_dir_sub_path = try arena.dupe(u8, "o" ++ fs.path.sep_str ++ hex_digest);
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 }
49194939 try main_mod.deps.put(arena, "@dependencies", deps_mod);
49204940 }
4941
49214942 try main_mod.deps.put(arena, "@build", &build_mod);
49224943
49234944 const comp = Compilation.create(gpa, .{