authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-10-06 21:29:08-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-10-08 16:54:31-07:00
logf708c5fafc5087a2518e0dc7acf986b59673dddd
tree0520ae635c4a0b643ba9b33cb6fcdf1ff08e115e
parent1c0d6f9c0020188326560e058baf36b8034d76e5

CLI: finish updating module API usage

Finish the work started in 4c4fb839972f66f55aa44fc0aca5f80b0608c731. Now the compiler compiles again. Wire up dependency tree fetching code in the CLI for `zig build`. Everything is hooked up except for `createDependenciesModule` is not yet implemented.

18 files changed, 321 insertions(+), 285 deletions(-)

lib/std/process.zig+1-1
...@@ -46,7 +46,7 @@ pub fn getCwdAlloc(allocator: Allocator) ![]u8 {...@@ -46,7 +46,7 @@ pub fn getCwdAlloc(allocator: Allocator) ![]u8 {
46 }46 }
47}47}
4848
49test "getCwdAlloc" {49test getCwdAlloc {
50 if (builtin.os.tag == .wasi) return error.SkipZigTest;50 if (builtin.os.tag == .wasi) return error.SkipZigTest;
5151
52 const cwd = try getCwdAlloc(testing.allocator);52 const cwd = try getCwdAlloc(testing.allocator);
src/Autodoc.zig+17-18
...@@ -6,7 +6,7 @@ const Autodoc = @This();...@@ -6,7 +6,7 @@ const Autodoc = @This();
6const Compilation = @import("Compilation.zig");6const Compilation = @import("Compilation.zig");
7const CompilationModule = @import("Module.zig");7const CompilationModule = @import("Module.zig");
8const File = CompilationModule.File;8const File = CompilationModule.File;
9const Module = @import("Package.zig");9const Module = @import("Package.zig").Module;
10const Tokenizer = std.zig.Tokenizer;10const Tokenizer = std.zig.Tokenizer;
11const InternPool = @import("InternPool.zig");11const InternPool = @import("InternPool.zig");
12const Zir = @import("Zir.zig");12const Zir = @import("Zir.zig");
...@@ -98,9 +98,8 @@ pub fn generate(cm: *CompilationModule, output_dir: std.fs.Dir) !void {...@@ -98,9 +98,8 @@ pub fn generate(cm: *CompilationModule, output_dir: std.fs.Dir) !void {
98}98}
9999
100fn generateZirData(self: *Autodoc, output_dir: std.fs.Dir) !void {100fn generateZirData(self: *Autodoc, output_dir: std.fs.Dir) !void {
101 const root_src_dir = self.comp_module.main_pkg.root_src_directory;101 const root_src_path = self.comp_module.main_mod.root_src_path;
102 const root_src_path = self.comp_module.main_pkg.root_src_path;102 const joined_src_path = try self.comp_module.main_mod.root.joinString(self.arena, root_src_path);
103 const joined_src_path = try root_src_dir.join(self.arena, &.{root_src_path});
104 defer self.arena.free(joined_src_path);103 defer self.arena.free(joined_src_path);
105104
106 const abs_root_src_path = try std.fs.path.resolve(self.arena, &.{ ".", joined_src_path });105 const abs_root_src_path = try std.fs.path.resolve(self.arena, &.{ ".", joined_src_path });
...@@ -295,20 +294,20 @@ fn generateZirData(self: *Autodoc, output_dir: std.fs.Dir) !void {...@@ -295,20 +294,20 @@ fn generateZirData(self: *Autodoc, output_dir: std.fs.Dir) !void {
295 }294 }
296295
297 const rootName = blk: {296 const rootName = blk: {
298 const rootName = std.fs.path.basename(self.comp_module.main_pkg.root_src_path);297 const rootName = std.fs.path.basename(self.comp_module.main_mod.root_src_path);
299 break :blk rootName[0 .. rootName.len - 4];298 break :blk rootName[0 .. rootName.len - 4];
300 };299 };
301300
302 const main_type_index = self.types.items.len;301 const main_type_index = self.types.items.len;
303 {302 {
304 try self.modules.put(self.arena, self.comp_module.main_pkg, .{303 try self.modules.put(self.arena, self.comp_module.main_mod, .{
305 .name = rootName,304 .name = rootName,
306 .main = main_type_index,305 .main = main_type_index,
307 .table = .{},306 .table = .{},
308 });307 });
309 try self.modules.entries.items(.value)[0].table.put(308 try self.modules.entries.items(.value)[0].table.put(
310 self.arena,309 self.arena,
311 self.comp_module.main_pkg,310 self.comp_module.main_mod,
312 .{311 .{
313 .name = rootName,312 .name = rootName,
314 .value = 0,313 .value = 0,
...@@ -412,7 +411,7 @@ fn generateZirData(self: *Autodoc, output_dir: std.fs.Dir) !void {...@@ -412,7 +411,7 @@ fn generateZirData(self: *Autodoc, output_dir: std.fs.Dir) !void {
412411
413 while (files_iterator.next()) |entry| {412 while (files_iterator.next()) |entry| {
414 const sub_file_path = entry.key_ptr.*.sub_file_path;413 const sub_file_path = entry.key_ptr.*.sub_file_path;
415 const file_module = entry.key_ptr.*.pkg;414 const file_module = entry.key_ptr.*.mod;
416 const module_name = (self.modules.get(file_module) orelse continue).name;415 const module_name = (self.modules.get(file_module) orelse continue).name;
417416
418 const file_path = std.fs.path.dirname(sub_file_path) orelse "";417 const file_path = std.fs.path.dirname(sub_file_path) orelse "";
...@@ -986,12 +985,12 @@ fn walkInstruction(...@@ -986,12 +985,12 @@ fn walkInstruction(
986985
987 // importFile cannot error out since all files986 // importFile cannot error out since all files
988 // are already loaded at this point987 // are already loaded at this point
989 if (file.pkg.table.get(path)) |other_module| {988 if (file.mod.deps.get(path)) |other_module| {
990 const result = try self.modules.getOrPut(self.arena, other_module);989 const result = try self.modules.getOrPut(self.arena, other_module);
991990
992 // Immediately add this module to the import table of our991 // Immediately add this module to the import table of our
993 // current module, regardless of wether it's new or not.992 // current module, regardless of wether it's new or not.
994 if (self.modules.getPtr(file.pkg)) |current_module| {993 if (self.modules.getPtr(file.mod)) |current_module| {
995 // TODO: apparently, in the stdlib a file gets analyzed before994 // TODO: apparently, in the stdlib a file gets analyzed before
996 // its module gets added. I guess we're importing a file995 // its module gets added. I guess we're importing a file
997 // that belongs to another module through its file path?996 // that belongs to another module through its file path?
...@@ -1025,12 +1024,12 @@ fn walkInstruction(...@@ -1025,12 +1024,12 @@ fn walkInstruction(
1025 // TODO: Add this module as a dependency to the current module1024 // TODO: Add this module as a dependency to the current module
1026 // TODO: this seems something that could be done in bulk1025 // TODO: this seems something that could be done in bulk
1027 // at the beginning or the end, or something.1026 // at the beginning or the end, or something.
1028 const root_src_dir = other_module.root_src_directory;1027 const abs_root_src_path = try std.fs.path.resolve(self.arena, &.{
1029 const root_src_path = other_module.root_src_path;1028 ".",
1030 const joined_src_path = try root_src_dir.join(self.arena, &.{root_src_path});1029 other_module.root.root_dir.path orelse ".",
1031 defer self.arena.free(joined_src_path);1030 other_module.root.sub_path,
10321031 other_module.root_src_path,
1033 const abs_root_src_path = try std.fs.path.resolve(self.arena, &.{ ".", joined_src_path });1032 });
1034 defer self.arena.free(abs_root_src_path);1033 defer self.arena.free(abs_root_src_path);
10351034
1036 const new_file = self.comp_module.import_table.get(abs_root_src_path).?;1035 const new_file = self.comp_module.import_table.get(abs_root_src_path).?;
...@@ -5683,7 +5682,7 @@ fn writeFileTableToJson(...@@ -5683,7 +5682,7 @@ fn writeFileTableToJson(
5683 while (it.next()) |entry| {5682 while (it.next()) |entry| {
5684 try jsw.beginArray();5683 try jsw.beginArray();
5685 try jsw.write(entry.key_ptr.*.sub_file_path);5684 try jsw.write(entry.key_ptr.*.sub_file_path);
5686 try jsw.write(mods.getIndex(entry.key_ptr.*.pkg) orelse 0);5685 try jsw.write(mods.getIndex(entry.key_ptr.*.mod) orelse 0);
5687 try jsw.endArray();5686 try jsw.endArray();
5688 }5687 }
5689 try jsw.endArray();5688 try jsw.endArray();
...@@ -5840,7 +5839,7 @@ fn addGuide(self: *Autodoc, file: *File, guide_path: []const u8, section: *Secti...@@ -5840,7 +5839,7 @@ fn addGuide(self: *Autodoc, file: *File, guide_path: []const u8, section: *Secti
5840 file.sub_file_path, "..", guide_path,5839 file.sub_file_path, "..", guide_path,
5841 });5840 });
58425841
5843 var guide_file = try file.pkg.root_src_directory.handle.openFile(resolved_path, .{});5842 var guide_file = try file.mod.root.openFile(resolved_path, .{});
5844 defer guide_file.close();5843 defer guide_file.close();
58455844
5846 const guide = guide_file.reader().readAllAlloc(self.arena, 1 * 1024 * 1024) catch |err| switch (err) {5845 const guide = guide_file.reader().readAllAlloc(self.arena, 1 * 1024 * 1024) catch |err| switch (err) {
src/Compilation.zig+61-80
...@@ -772,12 +772,14 @@ fn addModuleTableToCacheHash(...@@ -772,12 +772,14 @@ fn addModuleTableToCacheHash(
772 switch (hash_type) {772 switch (hash_type) {
773 .path_bytes => {773 .path_bytes => {
774 hash.addBytes(mod.value.root_src_path);774 hash.addBytes(mod.value.root_src_path);
775 hash.addOptionalBytes(mod.value.root_src_directory.path);775 hash.addOptionalBytes(mod.value.root.root_dir.path);
776 hash.addBytes(mod.value.root.sub_path);
776 },777 },
777 .files => |man| {778 .files => |man| {
778 const pkg_zig_file = try mod.value.root_src_directory.join(allocator, &[_][]const u8{779 const pkg_zig_file = try mod.value.root.joinString(
780 allocator,
779 mod.value.root_src_path,781 mod.value.root_src_path,
780 });782 );
781 _ = try man.addFile(pkg_zig_file, null);783 _ = try man.addFile(pkg_zig_file, null);
782 },784 },
783 }785 }
...@@ -1310,53 +1312,50 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {...@@ -1310,53 +1312,50 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
1310 const std_mod = if (main_mod_is_std)1312 const std_mod = if (main_mod_is_std)
1311 main_mod1313 main_mod
1312 else1314 else
1313 try Package.createWithDir(1315 try Package.Module.create(arena, .{
1314 gpa,1316 .root = .{
1315 options.zig_lib_directory,1317 .root_dir = options.zig_lib_directory,
1316 "std",1318 .sub_path = "std",
1317 "std.zig",1319 },
1318 );1320 .root_src_path = "std.zig",
13191321 });
1320 errdefer if (!main_mod_is_std) std_mod.destroy(gpa);
13211322
1322 const root_mod = if (options.is_test) root_mod: {1323 const root_mod = if (options.is_test) root_mod: {
1323 const test_pkg = if (options.test_runner_path) |test_runner| test_pkg: {1324 const test_mod = if (options.test_runner_path) |test_runner| test_mod: {
1324 const test_dir = std.fs.path.dirname(test_runner);1325 const pkg = try Package.Module.create(arena, .{
1325 const basename = std.fs.path.basename(test_runner);1326 .root = .{
1326 const pkg = try Package.create(gpa, test_dir, basename);1327 .root_dir = Directory.cwd(),
13271328 .sub_path = std.fs.path.dirname(test_runner) orelse "",
1328 // copy module table from main_mod to root_mod1329 },
1329 pkg.deps = try main_mod.deps.clone(gpa);1330 .root_src_path = std.fs.path.basename(test_runner),
1330 break :test_pkg pkg;1331 });
1331 } else try Package.createWithDir(1332
1332 gpa,1333 pkg.deps = try main_mod.deps.clone(arena);
1333 options.zig_lib_directory,1334 break :test_mod pkg;
1334 null,1335 } else try Package.Module.create(arena, .{
1335 "test_runner.zig",1336 .root = .{
1336 );1337 .root_dir = options.zig_lib_directory,
1337 errdefer test_pkg.destroy(gpa);1338 },
1339 .root_src_path = "test_runner.zig",
1340 });
13381341
1339 break :root_mod test_pkg;1342 break :root_mod test_mod;
1340 } else main_mod;1343 } else main_mod;
1341 errdefer if (options.is_test) root_mod.destroy(gpa);
13421344
1343 const compiler_rt_mod = if (include_compiler_rt and options.output_mode == .Obj) compiler_rt_mod: {1345 const compiler_rt_mod = if (include_compiler_rt and options.output_mode == .Obj) compiler_rt_mod: {
1344 break :compiler_rt_mod try Package.createWithDir(1346 break :compiler_rt_mod try Package.Module.create(arena, .{
1345 gpa,1347 .root = .{
1346 options.zig_lib_directory,1348 .root_dir = options.zig_lib_directory,
1347 null,1349 },
1348 "compiler_rt.zig",1350 .root_src_path = "compiler_rt.zig",
1349 );1351 });
1350 } else null;1352 } else null;
1351 errdefer if (compiler_rt_mod) |p| p.destroy(gpa);
1352
1353 try main_mod.add(gpa, "builtin", builtin_mod);
1354 try main_mod.add(gpa, "root", root_mod);
1355 try main_mod.add(gpa, "std", std_mod);
13561353
1357 if (compiler_rt_mod) |p| {1354 try main_mod.deps.put(gpa, "builtin", builtin_mod);
1358 try main_mod.add(gpa, "compiler_rt", p);1355 try main_mod.deps.put(gpa, "root", root_mod);
1359 }1356 try main_mod.deps.put(gpa, "std", std_mod);
1357 if (compiler_rt_mod) |m|
1358 try main_mod.deps.put(gpa, "compiler_rt", m);
13601359
1361 // Pre-open the directory handles for cached ZIR code so that it does not need1360 // Pre-open the directory handles for cached ZIR code so that it does not need
1362 // to redundantly happen for each AstGen operation.1361 // to redundantly happen for each AstGen operation.
...@@ -2004,7 +2003,7 @@ fn restorePrevZigCacheArtifactDirectory(comp: *Compilation, directory: *Director...@@ -2004,7 +2003,7 @@ fn restorePrevZigCacheArtifactDirectory(comp: *Compilation, directory: *Director
2004 // on the handle of zig_cache_artifact_directory.2003 // on the handle of zig_cache_artifact_directory.
2005 if (comp.bin_file.options.module) |module| {2004 if (comp.bin_file.options.module) |module| {
2006 const builtin_mod = module.main_mod.deps.get("builtin").?;2005 const builtin_mod = module.main_mod.deps.get("builtin").?;
2007 module.zig_cache_artifact_directory = builtin_mod.root_src_directory;2006 module.zig_cache_artifact_directory = builtin_mod.root.root_dir;
2008 }2007 }
2009}2008}
20102009
...@@ -2418,9 +2417,7 @@ fn addNonIncrementalStuffToCacheManifest(comp: *Compilation, man: *Cache.Manifes...@@ -2418,9 +2417,7 @@ fn addNonIncrementalStuffToCacheManifest(comp: *Compilation, man: *Cache.Manifes
2418 comptime assert(link_hash_implementation_version == 10);2417 comptime assert(link_hash_implementation_version == 10);
24192418
2420 if (comp.bin_file.options.module) |mod| {2419 if (comp.bin_file.options.module) |mod| {
2421 const main_zig_file = try mod.main_mod.root_src_directory.join(arena, &[_][]const u8{2420 const main_zig_file = try mod.main_mod.root.joinString(arena, mod.main_mod.root_src_path);
2422 mod.main_mod.root_src_path,
2423 });
2424 _ = try man.addFile(main_zig_file, null);2421 _ = try man.addFile(main_zig_file, null);
2425 {2422 {
2426 var seen_table = std.AutoHashMap(*Package.Module, void).init(arena);2423 var seen_table = std.AutoHashMap(*Package.Module, void).init(arena);
...@@ -2614,23 +2611,23 @@ fn reportMultiModuleErrors(mod: *Module) !void {...@@ -2614,23 +2611,23 @@ fn reportMultiModuleErrors(mod: *Module) !void {
2614 errdefer for (notes[0..i]) |*n| n.deinit(mod.gpa);2611 errdefer for (notes[0..i]) |*n| n.deinit(mod.gpa);
2615 note.* = switch (ref) {2612 note.* = switch (ref) {
2616 .import => |loc| blk: {2613 .import => |loc| blk: {
2617 const name = try loc.file_scope.pkg.getName(mod.gpa, mod.*);2614 //const name = try loc.file_scope.mod.getName(mod.gpa, mod.*);
2618 defer mod.gpa.free(name);2615 //defer mod.gpa.free(name);
2619 break :blk try Module.ErrorMsg.init(2616 break :blk try Module.ErrorMsg.init(
2620 mod.gpa,2617 mod.gpa,
2621 loc,2618 loc,
2622 "imported from module {s}",2619 "imported from module {}",
2623 .{name},2620 .{loc.file_scope.mod.root},
2624 );2621 );
2625 },2622 },
2626 .root => |pkg| blk: {2623 .root => |pkg| blk: {
2627 const name = try pkg.getName(mod.gpa, mod.*);2624 //const name = try pkg.getName(mod.gpa, mod.*);
2628 defer mod.gpa.free(name);2625 //defer mod.gpa.free(name);
2629 break :blk try Module.ErrorMsg.init(2626 break :blk try Module.ErrorMsg.init(
2630 mod.gpa,2627 mod.gpa,
2631 .{ .file_scope = file, .parent_decl_node = 0, .lazy = .entire_file },2628 .{ .file_scope = file, .parent_decl_node = 0, .lazy = .entire_file },
2632 "root of module {s}",2629 "root of module {}",
2633 .{name},2630 .{pkg.root},
2634 );2631 );
2635 },2632 },
2636 };2633 };
...@@ -4212,17 +4209,9 @@ fn reportRetryableAstGenError(...@@ -4212,17 +4209,9 @@ fn reportRetryableAstGenError(
4212 },4209 },
4213 };4210 };
42144211
4215 const err_msg = if (file.pkg.root_src_directory.path) |dir_path|4212 const err_msg = try Module.ErrorMsg.create(gpa, src_loc, "unable to load '{}{s}': {s}", .{
4216 try Module.ErrorMsg.create(4213 file.mod.root, file.sub_file_path, @errorName(err),
4217 gpa,4214 });
4218 src_loc,
4219 "unable to load '{s}" ++ std.fs.path.sep_str ++ "{s}': {s}",
4220 .{ dir_path, file.sub_file_path, @errorName(err) },
4221 )
4222 else
4223 try Module.ErrorMsg.create(gpa, src_loc, "unable to load '{s}': {s}", .{
4224 file.sub_file_path, @errorName(err),
4225 });
4226 errdefer err_msg.destroy(gpa);4215 errdefer err_msg.destroy(gpa);
42274216
4228 {4217 {
...@@ -4242,17 +4231,10 @@ fn reportRetryableEmbedFileError(...@@ -4242,17 +4231,10 @@ fn reportRetryableEmbedFileError(
42424231
4243 const src_loc: Module.SrcLoc = mod.declPtr(embed_file.owner_decl).srcLoc(mod);4232 const src_loc: Module.SrcLoc = mod.declPtr(embed_file.owner_decl).srcLoc(mod);
42444233
4245 const err_msg = if (embed_file.pkg.root_src_directory.path) |dir_path|4234 const err_msg = try Module.ErrorMsg.create(gpa, src_loc, "unable to load '{}{s}': {s}", .{
4246 try Module.ErrorMsg.create(4235 embed_file.mod.root, embed_file.sub_file_path, @errorName(err),
4247 gpa,4236 });
4248 src_loc,4237
4249 "unable to load '{s}" ++ std.fs.path.sep_str ++ "{s}': {s}",
4250 .{ dir_path, embed_file.sub_file_path, @errorName(err) },
4251 )
4252 else
4253 try Module.ErrorMsg.create(gpa, src_loc, "unable to load '{s}': {s}", .{
4254 embed_file.sub_file_path, @errorName(err),
4255 });
4256 errdefer err_msg.destroy(gpa);4238 errdefer err_msg.destroy(gpa);
42574239
4258 {4240 {
...@@ -6375,13 +6357,12 @@ fn buildOutputFromZig(...@@ -6375,13 +6357,12 @@ fn buildOutputFromZig(
6375 const tracy_trace = trace(@src());6357 const tracy_trace = trace(@src());
6376 defer tracy_trace.end();6358 defer tracy_trace.end();
63776359
6378 std.debug.assert(output_mode != .Exe);6360 assert(output_mode != .Exe);
63796361
6380 var main_mod: Package = .{6362 var main_mod: Package.Module = .{
6381 .root_src_directory = comp.zig_lib_directory,6363 .root = .{ .root_dir = comp.zig_lib_directory },
6382 .root_src_path = src_basename,6364 .root_src_path = src_basename,
6383 };6365 };
6384 defer main_mod.deinitTable(comp.gpa);
6385 const root_name = src_basename[0 .. src_basename.len - std.fs.path.extension(src_basename).len];6366 const root_name = src_basename[0 .. src_basename.len - std.fs.path.extension(src_basename).len];
6386 const target = comp.getTarget();6367 const target = comp.getTarget();
6387 const bin_basename = try std.zig.binNameAlloc(comp.gpa, .{6368 const bin_basename = try std.zig.binNameAlloc(comp.gpa, .{
src/Module.zig+75-89
...@@ -998,8 +998,8 @@ pub const File = struct {...@@ -998,8 +998,8 @@ pub const File = struct {
998 pub const Reference = union(enum) {998 pub const Reference = union(enum) {
999 /// The file is imported directly (i.e. not as a package) with @import.999 /// The file is imported directly (i.e. not as a package) with @import.
1000 import: SrcLoc,1000 import: SrcLoc,
1001 /// The file is the root of a package.1001 /// The file is the root of a module.
1002 root: *Package,1002 root: *Package.Module,
1003 };1003 };
10041004
1005 pub fn unload(file: *File, gpa: Allocator) void {1005 pub fn unload(file: *File, gpa: Allocator) void {
...@@ -1174,10 +1174,10 @@ pub const File = struct {...@@ -1174,10 +1174,10 @@ pub const File = struct {
1174 }1174 }
11751175
1176 const pkg = switch (ref) {1176 const pkg = switch (ref) {
1177 .import => |loc| loc.file_scope.pkg,1177 .import => |loc| loc.file_scope.mod,
1178 .root => |pkg| pkg,1178 .root => |pkg| pkg,
1179 };1179 };
1180 if (pkg != file.pkg) file.multi_pkg = true;1180 if (pkg != file.mod) file.multi_pkg = true;
1181 }1181 }
11821182
1183 /// Mark this file and every file referenced by it as multi_pkg and report an1183 /// Mark this file and every file referenced by it as multi_pkg and report an
...@@ -1219,7 +1219,7 @@ pub const EmbedFile = struct {...@@ -1219,7 +1219,7 @@ pub const EmbedFile = struct {
1219 bytes: [:0]const u8,1219 bytes: [:0]const u8,
1220 stat: Cache.File.Stat,1220 stat: Cache.File.Stat,
1221 /// Package that this file is a part of, managed externally.1221 /// Package that this file is a part of, managed externally.
1222 pkg: *Package,1222 mod: *Package.Module,
1223 /// The Decl that was created from the `@embedFile` to own this resource.1223 /// The Decl that was created from the `@embedFile` to own this resource.
1224 /// This is how zig knows what other Decl objects to invalidate if the file1224 /// This is how zig knows what other Decl objects to invalidate if the file
1225 /// changes on disk.1225 /// changes on disk.
...@@ -2535,28 +2535,6 @@ pub fn deinit(mod: *Module) void {...@@ -2535,28 +2535,6 @@ pub fn deinit(mod: *Module) void {
2535 }2535 }
25362536
2537 mod.deletion_set.deinit(gpa);2537 mod.deletion_set.deinit(gpa);
2538
2539 // The callsite of `Compilation.create` owns the `main_mod`, however
2540 // Module owns the builtin and std packages that it adds.
2541 if (mod.main_mod.table.fetchRemove("builtin")) |kv| {
2542 gpa.free(kv.key);
2543 kv.value.destroy(gpa);
2544 }
2545 if (mod.main_mod.table.fetchRemove("std")) |kv| {
2546 gpa.free(kv.key);
2547 // It's possible for main_mod to be std when running 'zig test'! In this case, we must not
2548 // destroy it, since it would lead to a double-free.
2549 if (kv.value != mod.main_mod) {
2550 kv.value.destroy(gpa);
2551 }
2552 }
2553 if (mod.main_mod.table.fetchRemove("root")) |kv| {
2554 gpa.free(kv.key);
2555 }
2556 if (mod.root_mod != mod.main_mod) {
2557 mod.root_mod.destroy(gpa);
2558 }
2559
2560 mod.compile_log_text.deinit(gpa);2538 mod.compile_log_text.deinit(gpa);
25612539
2562 mod.zig_cache_artifact_directory.handle.close();2540 mod.zig_cache_artifact_directory.handle.close();
...@@ -2703,18 +2681,19 @@ pub fn astGenFile(mod: *Module, file: *File) !void {...@@ -2703,18 +2681,19 @@ pub fn astGenFile(mod: *Module, file: *File) !void {
2703 const gpa = mod.gpa;2681 const gpa = mod.gpa;
27042682
2705 // In any case we need to examine the stat of the file to determine the course of action.2683 // In any case we need to examine the stat of the file to determine the course of action.
2706 var source_file = try file.pkg.root_src_directory.handle.openFile(file.sub_file_path, .{});2684 var source_file = try file.mod.root.openFile(file.sub_file_path, .{});
2707 defer source_file.close();2685 defer source_file.close();
27082686
2709 const stat = try source_file.stat();2687 const stat = try source_file.stat();
27102688
2711 const want_local_cache = file.pkg == mod.main_mod;2689 const want_local_cache = file.mod == mod.main_mod;
2712 const digest = hash: {2690 const digest = hash: {
2713 var path_hash: Cache.HashHelper = .{};2691 var path_hash: Cache.HashHelper = .{};
2714 path_hash.addBytes(build_options.version);2692 path_hash.addBytes(build_options.version);
2715 path_hash.add(builtin.zig_backend);2693 path_hash.add(builtin.zig_backend);
2716 if (!want_local_cache) {2694 if (!want_local_cache) {
2717 path_hash.addOptionalBytes(file.pkg.root_src_directory.path);2695 path_hash.addOptionalBytes(file.mod.root.root_dir.path);
2696 path_hash.addBytes(file.mod.root.sub_path);
2718 }2697 }
2719 path_hash.addBytes(file.sub_file_path);2698 path_hash.addBytes(file.sub_file_path);
2720 break :hash path_hash.final();2699 break :hash path_hash.final();
...@@ -2939,10 +2918,8 @@ pub fn astGenFile(mod: *Module, file: *File) !void {...@@ -2939,10 +2918,8 @@ pub fn astGenFile(mod: *Module, file: *File) !void {
2939 },2918 },
2940 };2919 };
2941 cache_file.writevAll(&iovecs) catch |err| {2920 cache_file.writevAll(&iovecs) catch |err| {
2942 const pkg_path = file.pkg.root_src_directory.path orelse ".";2921 log.warn("unable to write cached ZIR code for {}{s} to {}{s}: {s}", .{
2943 const cache_path = cache_directory.path orelse ".";2922 file.mod.root, file.sub_file_path, cache_directory, &digest, @errorName(err),
2944 log.warn("unable to write cached ZIR code for {s}/{s} to {s}/{s}: {s}", .{
2945 pkg_path, file.sub_file_path, cache_path, &digest, @errorName(err),
2946 });2923 });
2947 };2924 };
29482925
...@@ -3147,34 +3124,24 @@ pub fn populateBuiltinFile(mod: *Module) !void {...@@ -3147,34 +3124,24 @@ pub fn populateBuiltinFile(mod: *Module) !void {
3147 defer tracy.end();3124 defer tracy.end();
31483125
3149 const comp = mod.comp;3126 const comp = mod.comp;
3150 const pkg_and_file = blk: {3127 const builtin_mod, const file = blk: {
3151 comp.mutex.lock();3128 comp.mutex.lock();
3152 defer comp.mutex.unlock();3129 defer comp.mutex.unlock();
31533130
3154 const builtin_mod = mod.main_mod.table.get("builtin").?;3131 const builtin_mod = mod.main_mod.deps.get("builtin").?;
3155 const result = try mod.importPkg(builtin_mod);3132 const result = try mod.importPkg(builtin_mod);
3156 break :blk .{3133 break :blk .{ builtin_mod, result.file };
3157 .file = result.file,
3158 .pkg = builtin_mod,
3159 };
3160 };3134 };
3161 const file = pkg_and_file.file;
3162 const builtin_mod = pkg_and_file.pkg;
3163 const gpa = mod.gpa;3135 const gpa = mod.gpa;
3164 file.source = try comp.generateBuiltinZigSource(gpa);3136 file.source = try comp.generateBuiltinZigSource(gpa);
3165 file.source_loaded = true;3137 file.source_loaded = true;
31663138
3167 if (builtin_mod.root_src_directory.handle.statFile(builtin_mod.root_src_path)) |stat| {3139 if (builtin_mod.root.statFile(builtin_mod.root_src_path)) |stat| {
3168 if (stat.size != file.source.len) {3140 if (stat.size != file.source.len) {
3169 const full_path = try builtin_mod.root_src_directory.join(gpa, &.{
3170 builtin_mod.root_src_path,
3171 });
3172 defer gpa.free(full_path);
3173
3174 log.warn(3141 log.warn(
3175 "the cached file '{s}' had the wrong size. Expected {d}, found {d}. " ++3142 "the cached file '{}{s}' had the wrong size. Expected {d}, found {d}. " ++
3176 "Overwriting with correct file contents now",3143 "Overwriting with correct file contents now",
3177 .{ full_path, file.source.len, stat.size },3144 .{ builtin_mod.root, builtin_mod.root_src_path, file.source.len, stat.size },
3178 );3145 );
31793146
3180 try writeBuiltinFile(file, builtin_mod);3147 try writeBuiltinFile(file, builtin_mod);
...@@ -3206,7 +3173,7 @@ pub fn populateBuiltinFile(mod: *Module) !void {...@@ -3206,7 +3173,7 @@ pub fn populateBuiltinFile(mod: *Module) !void {
3206}3173}
32073174
3208fn writeBuiltinFile(file: *File, builtin_mod: *Package.Module) !void {3175fn writeBuiltinFile(file: *File, builtin_mod: *Package.Module) !void {
3209 var af = try builtin_mod.root_src_directory.handle.atomicFile(builtin_mod.root_src_path, .{});3176 var af = try builtin_mod.root.atomicFile(builtin_mod.root_src_path, .{});
3210 defer af.deinit();3177 defer af.deinit();
3211 try af.file.writeAll(file.source);3178 try af.file.writeAll(file.source);
3212 try af.finish();3179 try af.finish();
...@@ -3602,7 +3569,8 @@ pub fn updateEmbedFile(mod: *Module, embed_file: *EmbedFile) SemaError!void {...@@ -3602,7 +3569,8 @@ pub fn updateEmbedFile(mod: *Module, embed_file: *EmbedFile) SemaError!void {
3602 }3569 }
3603}3570}
36043571
3605pub fn semaPkg(mod: *Module, pkg: *Package) !void {3572/// https://github.com/ziglang/zig/issues/14307
3573pub fn semaPkg(mod: *Module, pkg: *Package.Module) !void {
3606 const file = (try mod.importPkg(pkg)).file;3574 const file = (try mod.importPkg(pkg)).file;
3607 return mod.semaFile(file);3575 return mod.semaFile(file);
3608}3576}
...@@ -3704,13 +3672,11 @@ pub fn semaFile(mod: *Module, file: *File) SemaError!void {...@@ -3704,13 +3672,11 @@ pub fn semaFile(mod: *Module, file: *File) SemaError!void {
3704 return error.AnalysisFail;3672 return error.AnalysisFail;
3705 };3673 };
37063674
3707 const resolved_path = std.fs.path.resolve(3675 const resolved_path = std.fs.path.resolve(gpa, &.{
3708 gpa,3676 file.mod.root.root_dir.path orelse ".",
3709 if (file.pkg.root_src_directory.path) |pkg_path|3677 file.mod.root.sub_path,
3710 &[_][]const u8{ pkg_path, file.sub_file_path }3678 file.sub_file_path,
3711 else3679 }) catch |err| {
3712 &[_][]const u8{file.sub_file_path},
3713 ) catch |err| {
3714 try reportRetryableFileError(mod, file, "unable to resolve path: {s}", .{@errorName(err)});3680 try reportRetryableFileError(mod, file, "unable to resolve path: {s}", .{@errorName(err)});
3715 return error.AnalysisFail;3681 return error.AnalysisFail;
3716 };3682 };
...@@ -3741,8 +3707,8 @@ fn semaDecl(mod: *Module, decl_index: Decl.Index) !bool {...@@ -3741,8 +3707,8 @@ fn semaDecl(mod: *Module, decl_index: Decl.Index) !bool {
37413707
3742 // TODO: figure out how this works under incremental changes to builtin.zig!3708 // TODO: figure out how this works under incremental changes to builtin.zig!
3743 const builtin_type_target_index: InternPool.Index = blk: {3709 const builtin_type_target_index: InternPool.Index = blk: {
3744 const std_mod = mod.main_mod.table.get("std").?;3710 const std_mod = mod.main_mod.deps.get("std").?;
3745 if (decl.getFileScope(mod).pkg != std_mod) break :blk .none;3711 if (decl.getFileScope(mod).mod != std_mod) break :blk .none;
3746 // We're in the std module.3712 // We're in the std module.
3747 const std_file = (try mod.importPkg(std_mod)).file;3713 const std_file = (try mod.importPkg(std_mod)).file;
3748 const std_decl = mod.declPtr(std_file.root_decl.unwrap().?);3714 const std_decl = mod.declPtr(std_file.root_decl.unwrap().?);
...@@ -4035,14 +4001,17 @@ pub const ImportFileResult = struct {...@@ -4035,14 +4001,17 @@ pub const ImportFileResult = struct {
4035 is_pkg: bool,4001 is_pkg: bool,
4036};4002};
40374003
4038pub fn importPkg(mod: *Module, pkg: *Package) !ImportFileResult {4004/// https://github.com/ziglang/zig/issues/14307
4005pub fn importPkg(mod: *Module, pkg: *Package.Module) !ImportFileResult {
4039 const gpa = mod.gpa;4006 const gpa = mod.gpa;
40404007
4041 // The resolved path is used as the key in the import table, to detect if4008 // The resolved path is used as the key in the import table, to detect if
4042 // an import refers to the same as another, despite different relative paths4009 // an import refers to the same as another, despite different relative paths
4043 // or differently mapped package names.4010 // or differently mapped package names.
4044 const resolved_path = try std.fs.path.resolve(gpa, &[_][]const u8{4011 const resolved_path = try std.fs.path.resolve(gpa, &.{
4045 pkg.root_src_directory.path orelse ".", pkg.root_src_path,4012 pkg.root.root_dir.path orelse ".",
4013 pkg.root.sub_path,
4014 pkg.root_src_path,
4046 });4015 });
4047 var keep_resolved_path = false;4016 var keep_resolved_path = false;
4048 defer if (!keep_resolved_path) gpa.free(resolved_path);4017 defer if (!keep_resolved_path) gpa.free(resolved_path);
...@@ -4076,7 +4045,7 @@ pub fn importPkg(mod: *Module, pkg: *Package) !ImportFileResult {...@@ -4076,7 +4045,7 @@ pub fn importPkg(mod: *Module, pkg: *Package) !ImportFileResult {
4076 .tree = undefined,4045 .tree = undefined,
4077 .zir = undefined,4046 .zir = undefined,
4078 .status = .never_loaded,4047 .status = .never_loaded,
4079 .pkg = pkg,4048 .mod = pkg,
4080 .root_decl = .none,4049 .root_decl = .none,
4081 };4050 };
4082 try new_file.addReference(mod.*, .{ .root = pkg });4051 try new_file.addReference(mod.*, .{ .root = pkg });
...@@ -4093,15 +4062,15 @@ pub fn importFile(...@@ -4093,15 +4062,15 @@ pub fn importFile(
4093 import_string: []const u8,4062 import_string: []const u8,
4094) !ImportFileResult {4063) !ImportFileResult {
4095 if (std.mem.eql(u8, import_string, "std")) {4064 if (std.mem.eql(u8, import_string, "std")) {
4096 return mod.importPkg(mod.main_mod.table.get("std").?);4065 return mod.importPkg(mod.main_mod.deps.get("std").?);
4097 }4066 }
4098 if (std.mem.eql(u8, import_string, "builtin")) {4067 if (std.mem.eql(u8, import_string, "builtin")) {
4099 return mod.importPkg(mod.main_mod.table.get("builtin").?);4068 return mod.importPkg(mod.main_mod.deps.get("builtin").?);
4100 }4069 }
4101 if (std.mem.eql(u8, import_string, "root")) {4070 if (std.mem.eql(u8, import_string, "root")) {
4102 return mod.importPkg(mod.root_mod);4071 return mod.importPkg(mod.root_mod);
4103 }4072 }
4104 if (cur_file.pkg.table.get(import_string)) |pkg| {4073 if (cur_file.mod.deps.get(import_string)) |pkg| {
4105 return mod.importPkg(pkg);4074 return mod.importPkg(pkg);
4106 }4075 }
4107 if (!mem.endsWith(u8, import_string, ".zig")) {4076 if (!mem.endsWith(u8, import_string, ".zig")) {
...@@ -4112,10 +4081,14 @@ pub fn importFile(...@@ -4112,10 +4081,14 @@ pub fn importFile(
4112 // The resolved path is used as the key in the import table, to detect if4081 // The resolved path is used as the key in the import table, to detect if
4113 // an import refers to the same as another, despite different relative paths4082 // an import refers to the same as another, despite different relative paths
4114 // or differently mapped package names.4083 // or differently mapped package names.
4115 const cur_pkg_dir_path = cur_file.pkg.root_src_directory.path orelse ".";4084 const resolved_path = try std.fs.path.resolve(gpa, &.{
4116 const resolved_path = try std.fs.path.resolve(gpa, &[_][]const u8{4085 cur_file.mod.root.root_dir.path orelse ".",
4117 cur_pkg_dir_path, cur_file.sub_file_path, "..", import_string,4086 cur_file.mod.root.sub_path,
4087 cur_file.sub_file_path,
4088 "..",
4089 import_string,
4118 });4090 });
4091
4119 var keep_resolved_path = false;4092 var keep_resolved_path = false;
4120 defer if (!keep_resolved_path) gpa.free(resolved_path);4093 defer if (!keep_resolved_path) gpa.free(resolved_path);
41214094
...@@ -4130,7 +4103,10 @@ pub fn importFile(...@@ -4130,7 +4103,10 @@ pub fn importFile(
4130 const new_file = try gpa.create(File);4103 const new_file = try gpa.create(File);
4131 errdefer gpa.destroy(new_file);4104 errdefer gpa.destroy(new_file);
41324105
4133 const resolved_root_path = try std.fs.path.resolve(gpa, &[_][]const u8{cur_pkg_dir_path});4106 const resolved_root_path = try std.fs.path.resolve(gpa, &.{
4107 cur_file.mod.root.root_dir.path orelse ".",
4108 cur_file.mod.root.sub_path,
4109 });
4134 defer gpa.free(resolved_root_path);4110 defer gpa.free(resolved_root_path);
41354111
4136 const sub_file_path = p: {4112 const sub_file_path = p: {
...@@ -4164,7 +4140,7 @@ pub fn importFile(...@@ -4164,7 +4140,7 @@ pub fn importFile(
4164 .tree = undefined,4140 .tree = undefined,
4165 .zir = undefined,4141 .zir = undefined,
4166 .status = .never_loaded,4142 .status = .never_loaded,
4167 .pkg = cur_file.pkg,4143 .mod = cur_file.mod,
4168 .root_decl = .none,4144 .root_decl = .none,
4169 };4145 };
4170 return ImportFileResult{4146 return ImportFileResult{
...@@ -4177,9 +4153,11 @@ pub fn importFile(...@@ -4177,9 +4153,11 @@ pub fn importFile(
4177pub fn embedFile(mod: *Module, cur_file: *File, import_string: []const u8) !*EmbedFile {4153pub fn embedFile(mod: *Module, cur_file: *File, import_string: []const u8) !*EmbedFile {
4178 const gpa = mod.gpa;4154 const gpa = mod.gpa;
41794155
4180 if (cur_file.pkg.table.get(import_string)) |pkg| {4156 if (cur_file.mod.deps.get(import_string)) |pkg| {
4181 const resolved_path = try std.fs.path.resolve(gpa, &[_][]const u8{4157 const resolved_path = try std.fs.path.resolve(gpa, &.{
4182 pkg.root_src_directory.path orelse ".", pkg.root_src_path,4158 pkg.root.root_dir.path orelse ".",
4159 pkg.root.sub_path,
4160 pkg.root_src_path,
4183 });4161 });
4184 var keep_resolved_path = false;4162 var keep_resolved_path = false;
4185 defer if (!keep_resolved_path) gpa.free(resolved_path);4163 defer if (!keep_resolved_path) gpa.free(resolved_path);
...@@ -4196,10 +4174,14 @@ pub fn embedFile(mod: *Module, cur_file: *File, import_string: []const u8) !*Emb...@@ -4196,10 +4174,14 @@ pub fn embedFile(mod: *Module, cur_file: *File, import_string: []const u8) !*Emb
41964174
4197 // The resolved path is used as the key in the table, to detect if a file4175 // The resolved path is used as the key in the table, to detect if a file
4198 // refers to the same as another, despite different relative paths.4176 // refers to the same as another, despite different relative paths.
4199 const cur_pkg_dir_path = cur_file.pkg.root_src_directory.path orelse ".";4177 const resolved_path = try std.fs.path.resolve(gpa, &.{
4200 const resolved_path = try std.fs.path.resolve(gpa, &[_][]const u8{4178 cur_file.mod.root.root_dir.path orelse ".",
4201 cur_pkg_dir_path, cur_file.sub_file_path, "..", import_string,4179 cur_file.mod.root.sub_path,
4180 cur_file.sub_file_path,
4181 "..",
4182 import_string,
4202 });4183 });
4184
4203 var keep_resolved_path = false;4185 var keep_resolved_path = false;
4204 defer if (!keep_resolved_path) gpa.free(resolved_path);4186 defer if (!keep_resolved_path) gpa.free(resolved_path);
42054187
...@@ -4207,7 +4189,10 @@ pub fn embedFile(mod: *Module, cur_file: *File, import_string: []const u8) !*Emb...@@ -4207,7 +4189,10 @@ pub fn embedFile(mod: *Module, cur_file: *File, import_string: []const u8) !*Emb
4207 errdefer assert(mod.embed_table.remove(resolved_path));4189 errdefer assert(mod.embed_table.remove(resolved_path));
4208 if (gop.found_existing) return gop.value_ptr.*;4190 if (gop.found_existing) return gop.value_ptr.*;
42094191
4210 const resolved_root_path = try std.fs.path.resolve(gpa, &[_][]const u8{cur_pkg_dir_path});4192 const resolved_root_path = try std.fs.path.resolve(gpa, &.{
4193 cur_file.mod.root.root_dir.path orelse ".",
4194 cur_file.mod.root.sub_path,
4195 });
4211 defer gpa.free(resolved_root_path);4196 defer gpa.free(resolved_root_path);
42124197
4213 const sub_file_path = p: {4198 const sub_file_path = p: {
...@@ -4225,12 +4210,13 @@ pub fn embedFile(mod: *Module, cur_file: *File, import_string: []const u8) !*Emb...@@ -4225,12 +4210,13 @@ pub fn embedFile(mod: *Module, cur_file: *File, import_string: []const u8) !*Emb
4225 };4210 };
4226 errdefer gpa.free(sub_file_path);4211 errdefer gpa.free(sub_file_path);
42274212
4228 return newEmbedFile(mod, cur_file.pkg, sub_file_path, resolved_path, &keep_resolved_path, gop);4213 return newEmbedFile(mod, cur_file.mod, sub_file_path, resolved_path, &keep_resolved_path, gop);
4229}4214}
42304215
4216/// https://github.com/ziglang/zig/issues/14307
4231fn newEmbedFile(4217fn newEmbedFile(
4232 mod: *Module,4218 mod: *Module,
4233 pkg: *Package,4219 pkg: *Package.Module,
4234 sub_file_path: []const u8,4220 sub_file_path: []const u8,
4235 resolved_path: []const u8,4221 resolved_path: []const u8,
4236 keep_resolved_path: *bool,4222 keep_resolved_path: *bool,
...@@ -4241,7 +4227,7 @@ fn newEmbedFile(...@@ -4241,7 +4227,7 @@ fn newEmbedFile(
4241 const new_file = try gpa.create(EmbedFile);4227 const new_file = try gpa.create(EmbedFile);
4242 errdefer gpa.destroy(new_file);4228 errdefer gpa.destroy(new_file);
42434229
4244 var file = try pkg.root_src_directory.handle.openFile(sub_file_path, .{});4230 var file = try pkg.root.openFile(sub_file_path, .{});
4245 defer file.close();4231 defer file.close();
42464232
4247 const actual_stat = try file.stat();4233 const actual_stat = try file.stat();
...@@ -4268,14 +4254,14 @@ fn newEmbedFile(...@@ -4268,14 +4254,14 @@ fn newEmbedFile(
4268 .sub_file_path = sub_file_path,4254 .sub_file_path = sub_file_path,
4269 .bytes = bytes,4255 .bytes = bytes,
4270 .stat = stat,4256 .stat = stat,
4271 .pkg = pkg,4257 .mod = pkg,
4272 .owner_decl = undefined, // Set by Sema immediately after this function returns.4258 .owner_decl = undefined, // Set by Sema immediately after this function returns.
4273 };4259 };
4274 return new_file;4260 return new_file;
4275}4261}
42764262
4277pub fn detectEmbedFileUpdate(mod: *Module, embed_file: *EmbedFile) !void {4263pub fn detectEmbedFileUpdate(mod: *Module, embed_file: *EmbedFile) !void {
4278 var file = try embed_file.pkg.root_src_directory.handle.openFile(embed_file.sub_file_path, .{});4264 var file = try embed_file.mod.root.openFile(embed_file.sub_file_path, .{});
4279 defer file.close();4265 defer file.close();
42804266
4281 const stat = try file.stat();4267 const stat = try file.stat();
...@@ -4448,21 +4434,21 @@ fn scanDecl(iter: *ScanDeclIter, decl_sub_index: usize, flags: u4) Allocator.Err...@@ -4448,21 +4434,21 @@ fn scanDecl(iter: *ScanDeclIter, decl_sub_index: usize, flags: u4) Allocator.Err
4448 gop.key_ptr.* = new_decl_index;4434 gop.key_ptr.* = new_decl_index;
4449 // Exported decls, comptime decls, usingnamespace decls, and4435 // Exported decls, comptime decls, usingnamespace decls, and
4450 // test decls if in test mode, get analyzed.4436 // test decls if in test mode, get analyzed.
4451 const decl_pkg = namespace.file_scope.pkg;4437 const decl_mod = namespace.file_scope.mod;
4452 const want_analysis = is_exported or switch (decl_name_index) {4438 const want_analysis = is_exported or switch (decl_name_index) {
4453 0 => true, // comptime or usingnamespace decl4439 0 => true, // comptime or usingnamespace decl
4454 1 => blk: {4440 1 => blk: {
4455 // test decl with no name. Skip the part where we check against4441 // test decl with no name. Skip the part where we check against
4456 // the test name filter.4442 // the test name filter.
4457 if (!comp.bin_file.options.is_test) break :blk false;4443 if (!comp.bin_file.options.is_test) break :blk false;
4458 if (decl_pkg != mod.main_mod) break :blk false;4444 if (decl_mod != mod.main_mod) break :blk false;
4459 try mod.test_functions.put(gpa, new_decl_index, {});4445 try mod.test_functions.put(gpa, new_decl_index, {});
4460 break :blk true;4446 break :blk true;
4461 },4447 },
4462 else => blk: {4448 else => blk: {
4463 if (!is_named_test) break :blk false;4449 if (!is_named_test) break :blk false;
4464 if (!comp.bin_file.options.is_test) break :blk false;4450 if (!comp.bin_file.options.is_test) break :blk false;
4465 if (decl_pkg != mod.main_mod) break :blk false;4451 if (decl_mod != mod.main_mod) break :blk false;
4466 if (comp.test_filter) |test_filter| {4452 if (comp.test_filter) |test_filter| {
4467 if (mem.indexOf(u8, ip.stringToSlice(decl_name), test_filter) == null) {4453 if (mem.indexOf(u8, ip.stringToSlice(decl_name), test_filter) == null) {
4468 break :blk false;4454 break :blk false;
...@@ -5589,7 +5575,7 @@ pub fn populateTestFunctions(...@@ -5589,7 +5575,7 @@ pub fn populateTestFunctions(
5589) !void {5575) !void {
5590 const gpa = mod.gpa;5576 const gpa = mod.gpa;
5591 const ip = &mod.intern_pool;5577 const ip = &mod.intern_pool;
5592 const builtin_mod = mod.main_mod.table.get("builtin").?;5578 const builtin_mod = mod.main_mod.deps.get("builtin").?;
5593 const builtin_file = (mod.importPkg(builtin_mod) catch unreachable).file;5579 const builtin_file = (mod.importPkg(builtin_mod) catch unreachable).file;
5594 const root_decl = mod.declPtr(builtin_file.root_decl.unwrap().?);5580 const root_decl = mod.declPtr(builtin_file.root_decl.unwrap().?);
5595 const builtin_namespace = mod.namespacePtr(root_decl.src_namespace);5581 const builtin_namespace = mod.namespacePtr(root_decl.src_namespace);
src/Package.zig+29-4
...@@ -13,12 +13,13 @@ pub const Path = struct {...@@ -13,12 +13,13 @@ pub const Path = struct {
13 return .{ .root_dir = Cache.Directory.cwd() };13 return .{ .root_dir = Cache.Directory.cwd() };
14 }14 }
1515
16 pub fn join(p: Path, allocator: Allocator, sub_path: []const u8) Allocator.Error!Path {16 pub fn join(p: Path, arena: Allocator, sub_path: []const u8) Allocator.Error!Path {
17 if (sub_path.len == 0) return p;
17 const parts: []const []const u8 =18 const parts: []const []const u8 =
18 if (p.sub_path.len == 0) &.{sub_path} else &.{ p.sub_path, sub_path };19 if (p.sub_path.len == 0) &.{sub_path} else &.{ p.sub_path, sub_path };
19 return .{20 return .{
20 .root_dir = p.root_dir,21 .root_dir = p.root_dir,
21 .sub_path = try fs.path.join(allocator, parts),22 .sub_path = try fs.path.join(arena, parts),
22 };23 };
23 }24 }
2425
...@@ -28,7 +29,7 @@ pub const Path = struct {...@@ -28,7 +29,7 @@ pub const Path = struct {
28 return p.root_dir.join(allocator, parts);29 return p.root_dir.join(allocator, parts);
29 }30 }
3031
31 pub fn joinStringZ(p: Path, allocator: Allocator, sub_path: []const u8) Allocator.Error![]u8 {32 pub fn joinStringZ(p: Path, allocator: Allocator, sub_path: []const u8) Allocator.Error![:0]u8 {
32 const parts: []const []const u8 =33 const parts: []const []const u8 =
33 if (p.sub_path.len == 0) &.{sub_path} else &.{ p.sub_path, sub_path };34 if (p.sub_path.len == 0) &.{sub_path} else &.{ p.sub_path, sub_path };
34 return p.root_dir.joinZ(allocator, parts);35 return p.root_dir.joinZ(allocator, parts);
...@@ -38,7 +39,7 @@ pub const Path = struct {...@@ -38,7 +39,7 @@ pub const Path = struct {
38 p: Path,39 p: Path,
39 sub_path: []const u8,40 sub_path: []const u8,
40 flags: fs.File.OpenFlags,41 flags: fs.File.OpenFlags,
41 ) fs.File.OpenError!fs.File {42 ) !fs.File {
42 var buf: [fs.MAX_PATH_BYTES]u8 = undefined;43 var buf: [fs.MAX_PATH_BYTES]u8 = undefined;
43 const joined_path = if (p.sub_path.len == 0) sub_path else p: {44 const joined_path = if (p.sub_path.len == 0) sub_path else p: {
44 break :p std.fmt.bufPrint(&buf, "{s}" ++ fs.path.sep_str ++ "{s}", .{45 break :p std.fmt.bufPrint(&buf, "{s}" ++ fs.path.sep_str ++ "{s}", .{
...@@ -58,6 +59,30 @@ pub const Path = struct {...@@ -58,6 +59,30 @@ pub const Path = struct {
58 return p.root_dir.handle.makeOpenPath(joined_path, opts);59 return p.root_dir.handle.makeOpenPath(joined_path, opts);
59 }60 }
6061
62 pub fn statFile(p: Path, sub_path: []const u8) !fs.Dir.Stat {
63 var buf: [fs.MAX_PATH_BYTES]u8 = undefined;
64 const joined_path = if (p.sub_path.len == 0) sub_path else p: {
65 break :p std.fmt.bufPrint(&buf, "{s}" ++ fs.path.sep_str ++ "{s}", .{
66 p.sub_path, sub_path,
67 }) catch return error.NameTooLong;
68 };
69 return p.root_dir.handle.statFile(joined_path);
70 }
71
72 pub fn atomicFile(
73 p: Path,
74 sub_path: []const u8,
75 options: fs.Dir.AtomicFileOptions,
76 ) !fs.AtomicFile {
77 var buf: [fs.MAX_PATH_BYTES]u8 = undefined;
78 const joined_path = if (p.sub_path.len == 0) sub_path else p: {
79 break :p std.fmt.bufPrint(&buf, "{s}" ++ fs.path.sep_str ++ "{s}", .{
80 p.sub_path, sub_path,
81 }) catch return error.NameTooLong;
82 };
83 return p.root_dir.handle.atomicFile(joined_path, options);
84 }
85
61 pub fn format(86 pub fn format(
62 self: Path,87 self: Path,
63 comptime fmt_string: []const u8,88 comptime fmt_string: []const u8,
src/Package/Fetch.zig+28-2
...@@ -49,7 +49,7 @@ allow_missing_paths_field: bool,...@@ -49,7 +49,7 @@ allow_missing_paths_field: bool,
49/// This will either be relative to `global_cache`, or to the build root of49/// This will either be relative to `global_cache`, or to the build root of
50/// the root package.50/// the root package.
51package_root: Package.Path,51package_root: Package.Path,
52error_bundle: std.zig.ErrorBundle.Wip,52error_bundle: ErrorBundle.Wip,
53manifest: ?Manifest,53manifest: ?Manifest,
54manifest_ast: std.zig.Ast,54manifest_ast: std.zig.Ast,
55actual_hash: Manifest.Digest,55actual_hash: Manifest.Digest,
...@@ -89,6 +89,31 @@ pub const JobQueue = struct {...@@ -89,6 +89,31 @@ pub const JobQueue = struct {
89 jq.all_fetches.deinit(gpa);89 jq.all_fetches.deinit(gpa);
90 jq.* = undefined;90 jq.* = undefined;
91 }91 }
92
93 /// Dumps all subsequent error bundles into the first one.
94 pub fn consolidateErrors(jq: *JobQueue) !void {
95 const root = &jq.all_fetches.items[0].error_bundle;
96 for (jq.all_fetches.items[1..]) |fetch| {
97 if (fetch.error_bundle.root_list.items.len > 0) {
98 try root.addBundleAsRoots(fetch.error_bundle.tmpBundle());
99 }
100 }
101 }
102
103 /// Creates the dependencies.zig file and corresponding `Module` for the
104 /// build runner to obtain via `@import("@dependencies")`.
105 pub fn createDependenciesModule(
106 jq: *JobQueue,
107 arena: Allocator,
108 local_cache_directory: Cache.Directory,
109 basename: []const u8,
110 ) !*Package.Module {
111 _ = jq;
112 _ = arena;
113 _ = local_cache_directory;
114 _ = basename;
115 @panic("TODO: createDependenciesModule");
116 }
92};117};
93118
94pub const Location = union(enum) {119pub const Location = union(enum) {
...@@ -502,7 +527,7 @@ pub fn workerRun(f: *Fetch) void {...@@ -502,7 +527,7 @@ pub fn workerRun(f: *Fetch) void {
502fn srcLoc(527fn srcLoc(
503 f: *Fetch,528 f: *Fetch,
504 tok: std.zig.Ast.TokenIndex,529 tok: std.zig.Ast.TokenIndex,
505) Allocator.Error!std.zig.ErrorBundle.SourceLocationIndex {530) Allocator.Error!ErrorBundle.SourceLocationIndex {
506 const ast = f.parent_manifest_ast orelse return .none;531 const ast = f.parent_manifest_ast orelse return .none;
507 const eb = &f.error_bundle;532 const eb = &f.error_bundle;
508 const token_starts = ast.tokens.items(.start);533 const token_starts = ast.tokens.items(.start);
...@@ -1258,3 +1283,4 @@ const Fetch = @This();...@@ -1258,3 +1283,4 @@ const Fetch = @This();
1258const main = @import("../main.zig");1283const main = @import("../main.zig");
1259const git = @import("../git.zig");1284const git = @import("../git.zig");
1260const Package = @import("../Package.zig");1285const Package = @import("../Package.zig");
1286const ErrorBundle = std.zig.ErrorBundle;
src/Package/Module.zig+1-1
...@@ -8,7 +8,7 @@ root: Package.Path,...@@ -8,7 +8,7 @@ root: Package.Path,
8root_src_path: []const u8,8root_src_path: []const u8,
9/// The dependency table of this module. Shared dependencies such as 'std',9/// The dependency table of this module. Shared dependencies such as 'std',
10/// 'builtin', and 'root' are not specified in every dependency table, but10/// 'builtin', and 'root' are not specified in every dependency table, but
11/// instead only in the table of `main_pkg`. `Module.importFile` is11/// instead only in the table of `main_mod`. `Module.importFile` is
12/// responsible for detecting these names and using the correct package.12/// responsible for detecting these names and using the correct package.
13deps: Deps = .{},13deps: Deps = .{},
1414
src/Sema.zig+7-5
...@@ -13075,9 +13075,11 @@ fn zirImport(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air....@@ -13075,9 +13075,11 @@ fn zirImport(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.
13075 return sema.fail(block, operand_src, "import of file outside package path: '{s}'", .{operand});13075 return sema.fail(block, operand_src, "import of file outside package path: '{s}'", .{operand});
13076 },13076 },
13077 error.PackageNotFound => {13077 error.PackageNotFound => {
13078 const name = try block.getFileScope(mod).pkg.getName(sema.gpa, mod.*);13078 //const name = try block.getFileScope(mod).mod.getName(sema.gpa, mod.*);
13079 defer sema.gpa.free(name);13079 //defer sema.gpa.free(name);
13080 return sema.fail(block, operand_src, "no package named '{s}' available within package '{s}'", .{ operand, name });13080 return sema.fail(block, operand_src, "no package named '{s}' available within package '{}'", .{
13081 operand, block.getFileScope(mod).mod.root,
13082 });
13081 },13083 },
13082 else => {13084 else => {
13083 // TODO: these errors are file system errors; make sure an update() will13085 // TODO: these errors are file system errors; make sure an update() will
...@@ -36415,8 +36417,8 @@ fn getBuiltinDecl(sema: *Sema, block: *Block, name: []const u8) CompileError!Mod...@@ -36415,8 +36417,8 @@ fn getBuiltinDecl(sema: *Sema, block: *Block, name: []const u8) CompileError!Mod
3641536417
36416 const mod = sema.mod;36418 const mod = sema.mod;
36417 const ip = &mod.intern_pool;36419 const ip = &mod.intern_pool;
36418 const std_pkg = mod.main_pkg.table.get("std").?;36420 const std_mod = mod.main_mod.deps.get("std").?;
36419 const std_file = (mod.importPkg(std_pkg) catch unreachable).file;36421 const std_file = (mod.importPkg(std_mod) catch unreachable).file;
36420 const opt_builtin_inst = (try sema.namespaceLookupRef(36422 const opt_builtin_inst = (try sema.namespaceLookupRef(
36421 block,36423 block,
36422 src,36424 src,
src/codegen/llvm.zig+16-13
...@@ -892,21 +892,24 @@ pub const Object = struct {...@@ -892,21 +892,24 @@ pub const Object = struct {
892 build_options.semver.patch,892 build_options.semver.patch,
893 });893 });
894894
895 // We fully resolve all paths at this point to avoid lack of source line info in stack895 // We fully resolve all paths at this point to avoid lack of
896 // traces or lack of debugging information which, if relative paths were used, would896 // source line info in stack traces or lack of debugging
897 // be very location dependent.897 // information which, if relative paths were used, would be
898 // very location dependent.
898 // TODO: the only concern I have with this is WASI as either host or target, should899 // TODO: the only concern I have with this is WASI as either host or target, should
899 // we leave the paths as relative then?900 // we leave the paths as relative then?
900 var buf: [std.fs.MAX_PATH_BYTES]u8 = undefined;901 var buf: [std.fs.MAX_PATH_BYTES]u8 = undefined;
901 const compile_unit_dir = blk: {902 const compile_unit_dir_z = blk: {
902 const path = d: {903 if (options.module) |mod| {
903 const mod = options.module orelse break :d ".";904 const d = try mod.root_mod.root.joinStringZ(builder.gpa, "");
904 break :d mod.root_pkg.root_src_directory.path orelse ".";905 if (std.fs.path.isAbsolute(d)) break :blk d;
905 };906 const abs = std.fs.realpath(d, &buf) catch break :blk d;
906 if (std.fs.path.isAbsolute(path)) break :blk path;907 builder.gpa.free(d);
907 break :blk std.os.realpath(path, &buf) catch path; // If realpath fails, fallback to whatever path was908 break :blk try builder.gpa.dupeZ(u8, abs);
909 }
910 const cwd = try std.process.getCwd(&buf);
911 break :blk try builder.gpa.dupeZ(u8, cwd);
908 };912 };
909 const compile_unit_dir_z = try builder.gpa.dupeZ(u8, compile_unit_dir);
910 defer builder.gpa.free(compile_unit_dir_z);913 defer builder.gpa.free(compile_unit_dir_z);
911914
912 builder.llvm.di_compile_unit = builder.llvm.di_builder.?.createCompileUnit(915 builder.llvm.di_compile_unit = builder.llvm.di_builder.?.createCompileUnit(
...@@ -2828,8 +2831,8 @@ pub const Object = struct {...@@ -2828,8 +2831,8 @@ pub const Object = struct {
2828 fn getStackTraceType(o: *Object) Allocator.Error!Type {2831 fn getStackTraceType(o: *Object) Allocator.Error!Type {
2829 const mod = o.module;2832 const mod = o.module;
28302833
2831 const std_pkg = mod.main_pkg.table.get("std").?;2834 const std_mod = mod.main_mod.deps.get("std").?;
2832 const std_file = (mod.importPkg(std_pkg) catch unreachable).file;2835 const std_file = (mod.importPkg(std_mod) catch unreachable).file;
28332836
2834 const builtin_str = try mod.intern_pool.getOrPutString(mod.gpa, "builtin");2837 const builtin_str = try mod.intern_pool.getOrPutString(mod.gpa, "builtin");
2835 const std_namespace = mod.namespacePtr(mod.declPtr(std_file.root_decl.unwrap().?).src_namespace);2838 const std_namespace = mod.namespacePtr(mod.declPtr(std_file.root_decl.unwrap().?).src_namespace);
src/glibc.zig+1-1
...@@ -1074,7 +1074,7 @@ fn buildSharedLib(...@@ -1074,7 +1074,7 @@ fn buildSharedLib(
1074 .cache_mode = .whole,1074 .cache_mode = .whole,
1075 .target = comp.getTarget(),1075 .target = comp.getTarget(),
1076 .root_name = lib.name,1076 .root_name = lib.name,
1077 .main_pkg = null,1077 .main_mod = null,
1078 .output_mode = .Lib,1078 .output_mode = .Lib,
1079 .link_mode = .Dynamic,1079 .link_mode = .Dynamic,
1080 .thread_pool = comp.thread_pool,1080 .thread_pool = comp.thread_pool,
src/libcxx.zig+2-2
...@@ -233,7 +233,7 @@ pub fn buildLibCXX(comp: *Compilation, prog_node: *std.Progress.Node) !void {...@@ -233,7 +233,7 @@ pub fn buildLibCXX(comp: *Compilation, prog_node: *std.Progress.Node) !void {
233 .cache_mode = .whole,233 .cache_mode = .whole,
234 .target = target,234 .target = target,
235 .root_name = root_name,235 .root_name = root_name,
236 .main_pkg = null,236 .main_mod = null,
237 .output_mode = output_mode,237 .output_mode = output_mode,
238 .thread_pool = comp.thread_pool,238 .thread_pool = comp.thread_pool,
239 .libc_installation = comp.bin_file.options.libc_installation,239 .libc_installation = comp.bin_file.options.libc_installation,
...@@ -396,7 +396,7 @@ pub fn buildLibCXXABI(comp: *Compilation, prog_node: *std.Progress.Node) !void {...@@ -396,7 +396,7 @@ pub fn buildLibCXXABI(comp: *Compilation, prog_node: *std.Progress.Node) !void {
396 .cache_mode = .whole,396 .cache_mode = .whole,
397 .target = target,397 .target = target,
398 .root_name = root_name,398 .root_name = root_name,
399 .main_pkg = null,399 .main_mod = null,
400 .output_mode = output_mode,400 .output_mode = output_mode,
401 .thread_pool = comp.thread_pool,401 .thread_pool = comp.thread_pool,
402 .libc_installation = comp.bin_file.options.libc_installation,402 .libc_installation = comp.bin_file.options.libc_installation,
src/libtsan.zig+1-1
...@@ -202,7 +202,7 @@ pub fn buildTsan(comp: *Compilation, prog_node: *std.Progress.Node) !void {...@@ -202,7 +202,7 @@ pub fn buildTsan(comp: *Compilation, prog_node: *std.Progress.Node) !void {
202 .cache_mode = .whole,202 .cache_mode = .whole,
203 .target = target,203 .target = target,
204 .root_name = root_name,204 .root_name = root_name,
205 .main_pkg = null,205 .main_mod = null,
206 .output_mode = output_mode,206 .output_mode = output_mode,
207 .thread_pool = comp.thread_pool,207 .thread_pool = comp.thread_pool,
208 .libc_installation = comp.bin_file.options.libc_installation,208 .libc_installation = comp.bin_file.options.libc_installation,
src/libunwind.zig+1-1
...@@ -89,7 +89,7 @@ pub fn buildStaticLib(comp: *Compilation, prog_node: *std.Progress.Node) !void {...@@ -89,7 +89,7 @@ pub fn buildStaticLib(comp: *Compilation, prog_node: *std.Progress.Node) !void {
89 .cache_mode = .whole,89 .cache_mode = .whole,
90 .target = target,90 .target = target,
91 .root_name = root_name,91 .root_name = root_name,
92 .main_pkg = null,92 .main_mod = null,
93 .output_mode = output_mode,93 .output_mode = output_mode,
94 .thread_pool = comp.thread_pool,94 .thread_pool = comp.thread_pool,
95 .libc_installation = comp.bin_file.options.libc_installation,95 .libc_installation = comp.bin_file.options.libc_installation,
src/link/Dwarf.zig+13-5
...@@ -1880,7 +1880,7 @@ pub fn writeDbgInfoHeader(self: *Dwarf, module: *Module, low_pc: u64, high_pc: u...@@ -1880,7 +1880,7 @@ pub fn writeDbgInfoHeader(self: *Dwarf, module: *Module, low_pc: u64, high_pc: u
1880 },1880 },
1881 }1881 }
1882 // Write the form for the compile unit, which must match the abbrev table above.1882 // Write the form for the compile unit, which must match the abbrev table above.
1883 const name_strp = try self.strtab.insert(self.allocator, module.root_pkg.root_src_path);1883 const name_strp = try self.strtab.insert(self.allocator, module.root_mod.root_src_path);
1884 var compile_unit_dir_buffer: [std.fs.MAX_PATH_BYTES]u8 = undefined;1884 var compile_unit_dir_buffer: [std.fs.MAX_PATH_BYTES]u8 = undefined;
1885 const compile_unit_dir = resolveCompilationDir(module, &compile_unit_dir_buffer);1885 const compile_unit_dir = resolveCompilationDir(module, &compile_unit_dir_buffer);
1886 const comp_dir_strp = try self.strtab.insert(self.allocator, compile_unit_dir);1886 const comp_dir_strp = try self.strtab.insert(self.allocator, compile_unit_dir);
...@@ -1940,9 +1940,17 @@ fn resolveCompilationDir(module: *Module, buffer: *[std.fs.MAX_PATH_BYTES]u8) []...@@ -1940,9 +1940,17 @@ fn resolveCompilationDir(module: *Module, buffer: *[std.fs.MAX_PATH_BYTES]u8) []
1940 // be very location dependent.1940 // be very location dependent.
1941 // TODO: the only concern I have with this is WASI as either host or target, should1941 // TODO: the only concern I have with this is WASI as either host or target, should
1942 // we leave the paths as relative then?1942 // we leave the paths as relative then?
1943 const comp_dir_path = module.root_pkg.root_src_directory.path orelse ".";1943 const root_dir_path = module.root_mod.root.root_dir.path orelse ".";
1944 if (std.fs.path.isAbsolute(comp_dir_path)) return comp_dir_path;1944 const sub_path = module.root_mod.root.sub_path;
1945 return std.os.realpath(comp_dir_path, buffer) catch comp_dir_path; // If realpath fails, fallback to whatever comp_dir_path was1945 const realpath = if (std.fs.path.isAbsolute(root_dir_path)) r: {
1946 @memcpy(buffer[0..root_dir_path.len], root_dir_path);
1947 break :r root_dir_path;
1948 } else std.fs.realpath(root_dir_path, buffer) catch return root_dir_path;
1949 const len = realpath.len + 1 + sub_path.len;
1950 if (buffer.len < len) return root_dir_path;
1951 buffer[realpath.len] = '/';
1952 @memcpy(buffer[realpath.len + 1 ..][0..sub_path.len], sub_path);
1953 return buffer[0..len];
1946}1954}
19471955
1948fn writeAddrAssumeCapacity(self: *Dwarf, buf: *std.ArrayList(u8), addr: u64) void {1956fn writeAddrAssumeCapacity(self: *Dwarf, buf: *std.ArrayList(u8), addr: u64) void {
...@@ -2664,7 +2672,7 @@ fn genIncludeDirsAndFileNames(self: *Dwarf, arena: Allocator) !struct {...@@ -2664,7 +2672,7 @@ fn genIncludeDirsAndFileNames(self: *Dwarf, arena: Allocator) !struct {
2664 for (self.di_files.keys()) |dif| {2672 for (self.di_files.keys()) |dif| {
2665 const dir_path = d: {2673 const dir_path = d: {
2666 var buffer: [std.fs.MAX_PATH_BYTES]u8 = undefined;2674 var buffer: [std.fs.MAX_PATH_BYTES]u8 = undefined;
2667 const dir_path = dif.pkg.root_src_directory.path orelse ".";2675 const dir_path = try dif.mod.root.joinString(arena, dif.mod.root.sub_path);
2668 const abs_dir_path = if (std.fs.path.isAbsolute(dir_path))2676 const abs_dir_path = if (std.fs.path.isAbsolute(dir_path))
2669 dir_path2677 dir_path
2670 else2678 else
src/link/Elf.zig+3-3
...@@ -929,15 +929,15 @@ pub fn populateMissingMetadata(self: *Elf) !void {...@@ -929,15 +929,15 @@ pub fn populateMissingMetadata(self: *Elf) !void {
929929
930 if (self.base.options.module) |module| {930 if (self.base.options.module) |module| {
931 if (self.zig_module_index == null and !self.base.options.use_llvm) {931 if (self.zig_module_index == null and !self.base.options.use_llvm) {
932 const index = @as(File.Index, @intCast(try self.files.addOne(gpa)));932 const index: File.Index = @intCast(try self.files.addOne(gpa));
933 self.files.set(index, .{ .zig_module = .{933 self.files.set(index, .{ .zig_module = .{
934 .index = index,934 .index = index,
935 .path = module.main_pkg.root_src_path,935 .path = module.main_mod.root_src_path,
936 } });936 } });
937 self.zig_module_index = index;937 self.zig_module_index = index;
938 const zig_module = self.file(index).?.zig_module;938 const zig_module = self.file(index).?.zig_module;
939939
940 const name_off = try self.strtab.insert(gpa, std.fs.path.stem(module.main_pkg.root_src_path));940 const name_off = try self.strtab.insert(gpa, std.fs.path.stem(module.main_mod.root_src_path));
941 const symbol_index = try self.addSymbol();941 const symbol_index = try self.addSymbol();
942 try zig_module.local_symbols.append(gpa, symbol_index);942 try zig_module.local_symbols.append(gpa, symbol_index);
943 const symbol_ptr = self.symbol(symbol_index);943 const symbol_ptr = self.symbol(symbol_index);
src/link/Plan9.zig+6-3
...@@ -352,9 +352,12 @@ fn putFn(self: *Plan9, decl_index: Module.Decl.Index, out: FnDeclOutput) !void {...@@ -352,9 +352,12 @@ fn putFn(self: *Plan9, decl_index: Module.Decl.Index, out: FnDeclOutput) !void {
352352
353 // getting the full file path353 // getting the full file path
354 var buf: [std.fs.MAX_PATH_BYTES]u8 = undefined;354 var buf: [std.fs.MAX_PATH_BYTES]u8 = undefined;
355 const dir = file.pkg.root_src_directory.path orelse try std.os.getcwd(&buf);355 const full_path = try std.fs.path.join(arena, &.{
356 const sub_path = try std.fs.path.join(arena, &.{ dir, file.sub_file_path });356 file.mod.root.root_dir.path orelse try std.os.getcwd(&buf),
357 try self.addPathComponents(sub_path, &a);357 file.mod.root.sub_path,
358 file.sub_file_path,
359 });
360 try self.addPathComponents(full_path, &a);
358361
359 // null terminate362 // null terminate
360 try a.append(0);363 try a.append(0);
src/main.zig+58-55
...@@ -897,7 +897,7 @@ fn buildOutputType(...@@ -897,7 +897,7 @@ fn buildOutputType(
897 var override_local_cache_dir: ?[]const u8 = try optionalStringEnvVar(arena, "ZIG_LOCAL_CACHE_DIR");897 var override_local_cache_dir: ?[]const u8 = try optionalStringEnvVar(arena, "ZIG_LOCAL_CACHE_DIR");
898 var override_global_cache_dir: ?[]const u8 = try optionalStringEnvVar(arena, "ZIG_GLOBAL_CACHE_DIR");898 var override_global_cache_dir: ?[]const u8 = try optionalStringEnvVar(arena, "ZIG_GLOBAL_CACHE_DIR");
899 var override_lib_dir: ?[]const u8 = try optionalStringEnvVar(arena, "ZIG_LIB_DIR");899 var override_lib_dir: ?[]const u8 = try optionalStringEnvVar(arena, "ZIG_LIB_DIR");
900 var main_pkg_path: ?[]const u8 = null;900 var main_mod_path: ?[]const u8 = null;
901 var clang_preprocessor_mode: Compilation.ClangPreprocessorMode = .no;901 var clang_preprocessor_mode: Compilation.ClangPreprocessorMode = .no;
902 var subsystem: ?std.Target.SubSystem = null;902 var subsystem: ?std.Target.SubSystem = null;
903 var major_subsystem_version: ?u32 = null;903 var major_subsystem_version: ?u32 = null;
...@@ -1047,7 +1047,7 @@ fn buildOutputType(...@@ -1047,7 +1047,7 @@ fn buildOutputType(
1047 }1047 }
1048 root_deps_str = args_iter.nextOrFatal();1048 root_deps_str = args_iter.nextOrFatal();
1049 } else if (mem.eql(u8, arg, "--main-mod-path")) {1049 } else if (mem.eql(u8, arg, "--main-mod-path")) {
1050 main_pkg_path = args_iter.nextOrFatal();1050 main_mod_path = args_iter.nextOrFatal();
1051 } else if (mem.eql(u8, arg, "-cflags")) {1051 } else if (mem.eql(u8, arg, "-cflags")) {
1052 extra_cflags.shrinkRetainingCapacity(0);1052 extra_cflags.shrinkRetainingCapacity(0);
1053 while (true) {1053 while (true) {
...@@ -3236,8 +3236,8 @@ fn buildOutputType(...@@ -3236,8 +3236,8 @@ fn buildOutputType(
32363236
3237 const main_mod: ?*Package.Module = if (root_src_file) |unresolved_src_path| blk: {3237 const main_mod: ?*Package.Module = if (root_src_file) |unresolved_src_path| blk: {
3238 const src_path = try introspect.resolvePath(arena, unresolved_src_path);3238 const src_path = try introspect.resolvePath(arena, unresolved_src_path);
3239 if (main_pkg_path) |unresolved_main_pkg_path| {3239 if (main_mod_path) |unresolved_main_mod_path| {
3240 const p = try introspect.resolvePath(arena, unresolved_main_pkg_path);3240 const p = try introspect.resolvePath(arena, unresolved_main_mod_path);
3241 break :blk try Package.Module.create(arena, .{3241 break :blk try Package.Module.create(arena, .{
3242 .root = .{3242 .root = .{
3243 .root_dir = Cache.Directory.cwd(),3243 .root_dir = Cache.Directory.cwd(),
...@@ -3386,8 +3386,6 @@ fn buildOutputType(...@@ -3386,8 +3386,6 @@ fn buildOutputType(
33863386
3387 gimmeMoreOfThoseSweetSweetFileDescriptors();3387 gimmeMoreOfThoseSweetSweetFileDescriptors();
33883388
3389 if (true) @panic("TODO restore Compilation logic");
3390
3391 const comp = Compilation.create(gpa, .{3389 const comp = Compilation.create(gpa, .{
3392 .zig_lib_directory = zig_lib_directory,3390 .zig_lib_directory = zig_lib_directory,
3393 .local_cache_directory = local_cache_directory,3391 .local_cache_directory = local_cache_directory,
...@@ -4628,6 +4626,8 @@ pub const usage_build =...@@ -4628,6 +4626,8 @@ pub const usage_build =
4628;4626;
46294627
4630pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {4628pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
4629 const work_around_btrfs_bug = builtin.os.tag == .linux and
4630 std.process.hasEnvVarConstant("ZIG_BTRFS_WORKAROUND");
4631 var color: Color = .auto;4631 var color: Color = .auto;
46324632
4633 // We want to release all the locks before executing the child process, so we make a nice4633 // We want to release all the locks before executing the child process, so we make a nice
...@@ -4725,7 +4725,7 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi...@@ -4725,7 +4725,7 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi
47254725
4726 const cwd_path = try process.getCwdAlloc(arena);4726 const cwd_path = try process.getCwdAlloc(arena);
4727 const build_zig_basename = if (build_file) |bf| fs.path.basename(bf) else Package.build_zig_basename;4727 const build_zig_basename = if (build_file) |bf| fs.path.basename(bf) else Package.build_zig_basename;
4728 const build_directory: Compilation.Directory = blk: {4728 const build_root: Compilation.Directory = blk: {
4729 if (build_file) |bf| {4729 if (build_file) |bf| {
4730 if (fs.path.dirname(bf)) |dirname| {4730 if (fs.path.dirname(bf)) |dirname| {
4731 const dir = fs.cwd().openDir(dirname, .{}) catch |err| {4731 const dir = fs.cwd().openDir(dirname, .{}) catch |err| {
...@@ -4761,7 +4761,7 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi...@@ -4761,7 +4761,7 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi
4761 }4761 }
4762 }4762 }
4763 };4763 };
4764 child_argv.items[argv_index_build_file] = build_directory.path orelse cwd_path;4764 child_argv.items[argv_index_build_file] = build_root.path orelse cwd_path;
47654765
4766 var global_cache_directory: Compilation.Directory = l: {4766 var global_cache_directory: Compilation.Directory = l: {
4767 const p = override_global_cache_dir orelse try introspect.resolveGlobalCacheDir(arena);4767 const p = override_global_cache_dir orelse try introspect.resolveGlobalCacheDir(arena);
...@@ -4781,9 +4781,9 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi...@@ -4781,9 +4781,9 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi
4781 .path = local_cache_dir_path,4781 .path = local_cache_dir_path,
4782 };4782 };
4783 }4783 }
4784 const cache_dir_path = try build_directory.join(arena, &[_][]const u8{"zig-cache"});4784 const cache_dir_path = try build_root.join(arena, &[_][]const u8{"zig-cache"});
4785 break :l .{4785 break :l .{
4786 .handle = try build_directory.handle.makeOpenPath("zig-cache", .{}),4786 .handle = try build_root.handle.makeOpenPath("zig-cache", .{}),
4787 .path = cache_dir_path,4787 .path = cache_dir_path,
4788 };4788 };
4789 };4789 };
...@@ -4824,74 +4824,77 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi...@@ -4824,74 +4824,77 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi
4824 };4824 };
48254825
4826 var build_mod: Package.Module = .{4826 var build_mod: Package.Module = .{
4827 .root = .{ .root_dir = build_directory },4827 .root = .{ .root_dir = build_root },
4828 .root_src_path = build_zig_basename,4828 .root_src_path = build_zig_basename,
4829 };4829 };
4830 if (build_options.only_core_functionality) {4830 if (build_options.only_core_functionality) {
4831 const deps_pkg = try Package.createFilePkg(gpa, local_cache_directory, "dependencies.zig",4831 const deps_mod = try Package.createFilePkg(gpa, local_cache_directory, "dependencies.zig",
4832 \\pub const packages = struct {};4832 \\pub const packages = struct {};
4833 \\pub const root_deps: []const struct { []const u8, []const u8 } = &.{};4833 \\pub const root_deps: []const struct { []const u8, []const u8 } = &.{};
4834 \\4834 \\
4835 );4835 );
4836 try main_mod.deps.put(arena, "@dependencies", deps_pkg);4836 try main_mod.deps.put(arena, "@dependencies", deps_mod);
4837 } else {4837 } else {
4838 var http_client: std.http.Client = .{ .allocator = gpa };4838 var http_client: std.http.Client = .{ .allocator = gpa };
4839 defer http_client.deinit();4839 defer http_client.deinit();
48404840
4841 if (true) @panic("TODO restore package fetching logic");
4842
4843 // Here we provide an import to the build runner that allows using reflection to find
4844 // all of the dependencies. Without this, there would be no way to use `@import` to
4845 // access dependencies by name, since `@import` requires string literals.
4846 var dependencies_source = std.ArrayList(u8).init(gpa);
4847 defer dependencies_source.deinit();
4848
4849 var all_modules: Package.AllModules = .{};
4850 defer all_modules.deinit(gpa);
4851
4852 var wip_errors: std.zig.ErrorBundle.Wip = undefined;
4853 try wip_errors.init(gpa);
4854 defer wip_errors.deinit();
4855
4856 var progress: std.Progress = .{ .dont_print_on_dumb = true };4841 var progress: std.Progress = .{ .dont_print_on_dumb = true };
4857 const root_prog_node = progress.start("Fetch Packages", 0);4842 const root_prog_node = progress.start("Fetch Packages", 0);
4858 defer root_prog_node.end();4843 defer root_prog_node.end();
48594844
4860 // Here we borrow main package's table and will replace it with a fresh4845 var job_queue: Package.Fetch.JobQueue = .{
4861 // one after this process completes.4846 .http_client = &http_client,
4862 const fetch_result = build_mod.fetchAndAddDependencies(4847 .thread_pool = &thread_pool,
4863 &main_mod,4848 .global_cache = global_cache_directory,
4864 arena,4849 .recursive = true,
4865 &thread_pool,4850 .work_around_btrfs_bug = work_around_btrfs_bug,
4866 &http_client,4851 };
4867 build_directory,4852 defer job_queue.deinit();
4868 global_cache_directory,4853
4869 local_cache_directory,4854 try job_queue.all_fetches.ensureUnusedCapacity(gpa, 1);
4870 &dependencies_source,4855
4871 &wip_errors,4856 var fetch: Package.Fetch = .{
4872 &all_modules,4857 .arena = std.heap.ArenaAllocator.init(gpa),
4873 root_prog_node,4858 .location = .{ .relative_path = "" },
4874 null,4859 .location_tok = 0,
4875 );4860 .hash_tok = 0,
4876 if (wip_errors.root_list.items.len > 0) {4861 .parent_package_root = build_mod.root,
4877 var errors = try wip_errors.toOwnedBundle("");4862 .parent_manifest_ast = null,
4878 defer errors.deinit(gpa);4863 .prog_node = root_prog_node,
4864 .job_queue = &job_queue,
4865 .omit_missing_hash_error = true,
4866 .allow_missing_paths_field = true,
4867
4868 .package_root = undefined,
4869 .error_bundle = undefined,
4870 .manifest = null,
4871 .manifest_ast = undefined,
4872 .actual_hash = undefined,
4873 .has_build_zig = false,
4874 .oom_flag = false,
4875 };
4876 job_queue.all_fetches.appendAssumeCapacity(&fetch);
4877
4878 job_queue.wait_group.start();
4879 try job_queue.thread_pool.spawn(Package.Fetch.workerRun, .{&fetch});
4880 job_queue.wait_group.wait();
4881
4882 try job_queue.consolidateErrors();
4883
4884 if (fetch.error_bundle.root_list.items.len > 0) {
4885 var errors = try fetch.error_bundle.toOwnedBundle("");
4879 errors.renderToStdErr(renderOptions(color));4886 errors.renderToStdErr(renderOptions(color));
4880 process.exit(1);4887 process.exit(1);
4881 }4888 }
4882 try fetch_result;
48834889
4884 const deps_pkg = try Package.createFilePkg(4890 const deps_mod = try job_queue.createDependenciesModule(
4885 gpa,4891 arena,
4886 local_cache_directory,4892 local_cache_directory,
4887 "dependencies.zig",4893 "dependencies.zig",
4888 dependencies_source.items,
4889 );4894 );
48904895 try main_mod.deps.put(arena, "@dependencies", deps_mod);
4891 mem.swap(Package.Table, &main_mod.table, &deps_pkg.table);
4892 try main_mod.add(gpa, "@dependencies", deps_pkg);
4893 }4896 }
4894 try main_mod.add(gpa, "@build", &build_mod);4897 try main_mod.deps.put(arena, "@build", &build_mod);
48954898
4896 const comp = Compilation.create(gpa, .{4899 const comp = Compilation.create(gpa, .{
4897 .zig_lib_directory = zig_lib_directory,4900 .zig_lib_directory = zig_lib_directory,
src/musl.zig+1-1
...@@ -206,7 +206,7 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile, prog_node: *std.Progr...@@ -206,7 +206,7 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile, prog_node: *std.Progr
206 .zig_lib_directory = comp.zig_lib_directory,206 .zig_lib_directory = comp.zig_lib_directory,
207 .target = target,207 .target = target,
208 .root_name = "c",208 .root_name = "c",
209 .main_pkg = null,209 .main_mod = null,
210 .output_mode = .Lib,210 .output_mode = .Lib,
211 .link_mode = .Dynamic,211 .link_mode = .Dynamic,
212 .thread_pool = comp.thread_pool,212 .thread_pool = comp.thread_pool,