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 {
4646 }
4747}
4848
49test "getCwdAlloc" {
49test getCwdAlloc {
5050 if (builtin.os.tag == .wasi) return error.SkipZigTest;
5151
5252 const cwd = try getCwdAlloc(testing.allocator);
src/Autodoc.zig+17-18
......@@ -6,7 +6,7 @@ const Autodoc = @This();
66const Compilation = @import("Compilation.zig");
77const CompilationModule = @import("Module.zig");
88const File = CompilationModule.File;
9const Module = @import("Package.zig");
9const Module = @import("Package.zig").Module;
1010const Tokenizer = std.zig.Tokenizer;
1111const InternPool = @import("InternPool.zig");
1212const Zir = @import("Zir.zig");
......@@ -98,9 +98,8 @@ pub fn generate(cm: *CompilationModule, output_dir: std.fs.Dir) !void {
9898}
9999
100100fn generateZirData(self: *Autodoc, output_dir: std.fs.Dir) !void {
101 const root_src_dir = self.comp_module.main_pkg.root_src_directory;
102 const root_src_path = self.comp_module.main_pkg.root_src_path;
103 const joined_src_path = try root_src_dir.join(self.arena, &.{root_src_path});
101 const root_src_path = self.comp_module.main_mod.root_src_path;
102 const joined_src_path = try self.comp_module.main_mod.root.joinString(self.arena, root_src_path);
104103 defer self.arena.free(joined_src_path);
105104
106105 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 {
295294 }
296295
297296 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);
299298 break :blk rootName[0 .. rootName.len - 4];
300299 };
301300
302301 const main_type_index = self.types.items.len;
303302 {
304 try self.modules.put(self.arena, self.comp_module.main_pkg, .{
303 try self.modules.put(self.arena, self.comp_module.main_mod, .{
305304 .name = rootName,
306305 .main = main_type_index,
307306 .table = .{},
308307 });
309308 try self.modules.entries.items(.value)[0].table.put(
310309 self.arena,
311 self.comp_module.main_pkg,
310 self.comp_module.main_mod,
312311 .{
313312 .name = rootName,
314313 .value = 0,
......@@ -412,7 +411,7 @@ fn generateZirData(self: *Autodoc, output_dir: std.fs.Dir) !void {
412411
413412 while (files_iterator.next()) |entry| {
414413 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;
416415 const module_name = (self.modules.get(file_module) orelse continue).name;
417416
418417 const file_path = std.fs.path.dirname(sub_file_path) orelse "";
......@@ -986,12 +985,12 @@ fn walkInstruction(
986985
987986 // importFile cannot error out since all files
988987 // are already loaded at this point
989 if (file.pkg.table.get(path)) |other_module| {
988 if (file.mod.deps.get(path)) |other_module| {
990989 const result = try self.modules.getOrPut(self.arena, other_module);
991990
992991 // Immediately add this module to the import table of our
993992 // 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| {
995994 // TODO: apparently, in the stdlib a file gets analyzed before
996995 // its module gets added. I guess we're importing a file
997996 // that belongs to another module through its file path?
......@@ -1025,12 +1024,12 @@ fn walkInstruction(
10251024 // TODO: Add this module as a dependency to the current module
10261025 // TODO: this seems something that could be done in bulk
10271026 // at the beginning or the end, or something.
1028 const root_src_dir = other_module.root_src_directory;
1029 const root_src_path = other_module.root_src_path;
1030 const joined_src_path = try root_src_dir.join(self.arena, &.{root_src_path});
1031 defer self.arena.free(joined_src_path);
1032
1033 const abs_root_src_path = try std.fs.path.resolve(self.arena, &.{ ".", joined_src_path });
1027 const abs_root_src_path = try std.fs.path.resolve(self.arena, &.{
1028 ".",
1029 other_module.root.root_dir.path orelse ".",
1030 other_module.root.sub_path,
1031 other_module.root_src_path,
1032 });
10341033 defer self.arena.free(abs_root_src_path);
10351034
10361035 const new_file = self.comp_module.import_table.get(abs_root_src_path).?;
......@@ -5683,7 +5682,7 @@ fn writeFileTableToJson(
56835682 while (it.next()) |entry| {
56845683 try jsw.beginArray();
56855684 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);
56875686 try jsw.endArray();
56885687 }
56895688 try jsw.endArray();
......@@ -5840,7 +5839,7 @@ fn addGuide(self: *Autodoc, file: *File, guide_path: []const u8, section: *Secti
58405839 file.sub_file_path, "..", guide_path,
58415840 });
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, .{});
58445843 defer guide_file.close();
58455844
58465845 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(
772772 switch (hash_type) {
773773 .path_bytes => {
774774 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);
776777 },
777778 .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,
779781 mod.value.root_src_path,
780 });
782 );
781783 _ = try man.addFile(pkg_zig_file, null);
782784 },
783785 }
......@@ -1310,53 +1312,50 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
13101312 const std_mod = if (main_mod_is_std)
13111313 main_mod
13121314 else
1313 try Package.createWithDir(
1314 gpa,
1315 options.zig_lib_directory,
1316 "std",
1317 "std.zig",
1318 );
1319
1320 errdefer if (!main_mod_is_std) std_mod.destroy(gpa);
1315 try Package.Module.create(arena, .{
1316 .root = .{
1317 .root_dir = options.zig_lib_directory,
1318 .sub_path = "std",
1319 },
1320 .root_src_path = "std.zig",
1321 });
13211322
13221323 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_dir = std.fs.path.dirname(test_runner);
1325 const basename = std.fs.path.basename(test_runner);
1326 const pkg = try Package.create(gpa, test_dir, basename);
1327
1328 // copy module table from main_mod to root_mod
1329 pkg.deps = try main_mod.deps.clone(gpa);
1330 break :test_pkg pkg;
1331 } else try Package.createWithDir(
1332 gpa,
1333 options.zig_lib_directory,
1334 null,
1335 "test_runner.zig",
1336 );
1337 errdefer test_pkg.destroy(gpa);
1324 const test_mod = if (options.test_runner_path) |test_runner| test_mod: {
1325 const pkg = try Package.Module.create(arena, .{
1326 .root = .{
1327 .root_dir = Directory.cwd(),
1328 .sub_path = std.fs.path.dirname(test_runner) orelse "",
1329 },
1330 .root_src_path = std.fs.path.basename(test_runner),
1331 });
1332
1333 pkg.deps = try main_mod.deps.clone(arena);
1334 break :test_mod pkg;
1335 } else try Package.Module.create(arena, .{
1336 .root = .{
1337 .root_dir = options.zig_lib_directory,
1338 },
1339 .root_src_path = "test_runner.zig",
1340 });
13381341
1339 break :root_mod test_pkg;
1342 break :root_mod test_mod;
13401343 } else main_mod;
1341 errdefer if (options.is_test) root_mod.destroy(gpa);
13421344
13431345 const compiler_rt_mod = if (include_compiler_rt and options.output_mode == .Obj) compiler_rt_mod: {
1344 break :compiler_rt_mod try Package.createWithDir(
1345 gpa,
1346 options.zig_lib_directory,
1347 null,
1348 "compiler_rt.zig",
1349 );
1346 break :compiler_rt_mod try Package.Module.create(arena, .{
1347 .root = .{
1348 .root_dir = options.zig_lib_directory,
1349 },
1350 .root_src_path = "compiler_rt.zig",
1351 });
13501352 } 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| {
1358 try main_mod.add(gpa, "compiler_rt", p);
1359 }
1354 try main_mod.deps.put(gpa, "builtin", builtin_mod);
1355 try main_mod.deps.put(gpa, "root", root_mod);
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
13611360 // Pre-open the directory handles for cached ZIR code so that it does not need
13621361 // to redundantly happen for each AstGen operation.
......@@ -2004,7 +2003,7 @@ fn restorePrevZigCacheArtifactDirectory(comp: *Compilation, directory: *Director
20042003 // on the handle of zig_cache_artifact_directory.
20052004 if (comp.bin_file.options.module) |module| {
20062005 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;
20082007 }
20092008}
20102009
......@@ -2418,9 +2417,7 @@ fn addNonIncrementalStuffToCacheManifest(comp: *Compilation, man: *Cache.Manifes
24182417 comptime assert(link_hash_implementation_version == 10);
24192418
24202419 if (comp.bin_file.options.module) |mod| {
2421 const main_zig_file = try mod.main_mod.root_src_directory.join(arena, &[_][]const u8{
2422 mod.main_mod.root_src_path,
2423 });
2420 const main_zig_file = try mod.main_mod.root.joinString(arena, mod.main_mod.root_src_path);
24242421 _ = try man.addFile(main_zig_file, null);
24252422 {
24262423 var seen_table = std.AutoHashMap(*Package.Module, void).init(arena);
......@@ -2614,23 +2611,23 @@ fn reportMultiModuleErrors(mod: *Module) !void {
26142611 errdefer for (notes[0..i]) |*n| n.deinit(mod.gpa);
26152612 note.* = switch (ref) {
26162613 .import => |loc| blk: {
2617 const name = try loc.file_scope.pkg.getName(mod.gpa, mod.*);
2618 defer mod.gpa.free(name);
2614 //const name = try loc.file_scope.mod.getName(mod.gpa, mod.*);
2615 //defer mod.gpa.free(name);
26192616 break :blk try Module.ErrorMsg.init(
26202617 mod.gpa,
26212618 loc,
2622 "imported from module {s}",
2623 .{name},
2619 "imported from module {}",
2620 .{loc.file_scope.mod.root},
26242621 );
26252622 },
26262623 .root => |pkg| blk: {
2627 const name = try pkg.getName(mod.gpa, mod.*);
2628 defer mod.gpa.free(name);
2624 //const name = try pkg.getName(mod.gpa, mod.*);
2625 //defer mod.gpa.free(name);
26292626 break :blk try Module.ErrorMsg.init(
26302627 mod.gpa,
26312628 .{ .file_scope = file, .parent_decl_node = 0, .lazy = .entire_file },
2632 "root of module {s}",
2633 .{name},
2629 "root of module {}",
2630 .{pkg.root},
26342631 );
26352632 },
26362633 };
......@@ -4212,17 +4209,9 @@ fn reportRetryableAstGenError(
42124209 },
42134210 };
42144211
4215 const err_msg = if (file.pkg.root_src_directory.path) |dir_path|
4216 try Module.ErrorMsg.create(
4217 gpa,
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 });
4212 const err_msg = try Module.ErrorMsg.create(gpa, src_loc, "unable to load '{}{s}': {s}", .{
4213 file.mod.root, file.sub_file_path, @errorName(err),
4214 });
42264215 errdefer err_msg.destroy(gpa);
42274216
42284217 {
......@@ -4242,17 +4231,10 @@ fn reportRetryableEmbedFileError(
42424231
42434232 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|
4246 try Module.ErrorMsg.create(
4247 gpa,
4248 src_loc,
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 });
4234 const err_msg = try Module.ErrorMsg.create(gpa, src_loc, "unable to load '{}{s}': {s}", .{
4235 embed_file.mod.root, embed_file.sub_file_path, @errorName(err),
4236 });
4237
42564238 errdefer err_msg.destroy(gpa);
42574239
42584240 {
......@@ -6375,13 +6357,12 @@ fn buildOutputFromZig(
63756357 const tracy_trace = trace(@src());
63766358 defer tracy_trace.end();
63776359
6378 std.debug.assert(output_mode != .Exe);
6360 assert(output_mode != .Exe);
63796361
6380 var main_mod: Package = .{
6381 .root_src_directory = comp.zig_lib_directory,
6362 var main_mod: Package.Module = .{
6363 .root = .{ .root_dir = comp.zig_lib_directory },
63826364 .root_src_path = src_basename,
63836365 };
6384 defer main_mod.deinitTable(comp.gpa);
63856366 const root_name = src_basename[0 .. src_basename.len - std.fs.path.extension(src_basename).len];
63866367 const target = comp.getTarget();
63876368 const bin_basename = try std.zig.binNameAlloc(comp.gpa, .{
src/Module.zig+75-89
......@@ -998,8 +998,8 @@ pub const File = struct {
998998 pub const Reference = union(enum) {
999999 /// The file is imported directly (i.e. not as a package) with @import.
10001000 import: SrcLoc,
1001 /// The file is the root of a package.
1002 root: *Package,
1001 /// The file is the root of a module.
1002 root: *Package.Module,
10031003 };
10041004
10051005 pub fn unload(file: *File, gpa: Allocator) void {
......@@ -1174,10 +1174,10 @@ pub const File = struct {
11741174 }
11751175
11761176 const pkg = switch (ref) {
1177 .import => |loc| loc.file_scope.pkg,
1177 .import => |loc| loc.file_scope.mod,
11781178 .root => |pkg| pkg,
11791179 };
1180 if (pkg != file.pkg) file.multi_pkg = true;
1180 if (pkg != file.mod) file.multi_pkg = true;
11811181 }
11821182
11831183 /// Mark this file and every file referenced by it as multi_pkg and report an
......@@ -1219,7 +1219,7 @@ pub const EmbedFile = struct {
12191219 bytes: [:0]const u8,
12201220 stat: Cache.File.Stat,
12211221 /// Package that this file is a part of, managed externally.
1222 pkg: *Package,
1222 mod: *Package.Module,
12231223 /// The Decl that was created from the `@embedFile` to own this resource.
12241224 /// This is how zig knows what other Decl objects to invalidate if the file
12251225 /// changes on disk.
......@@ -2535,28 +2535,6 @@ pub fn deinit(mod: *Module) void {
25352535 }
25362536
25372537 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
25602538 mod.compile_log_text.deinit(gpa);
25612539
25622540 mod.zig_cache_artifact_directory.handle.close();
......@@ -2703,18 +2681,19 @@ pub fn astGenFile(mod: *Module, file: *File) !void {
27032681 const gpa = mod.gpa;
27042682
27052683 // 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, .{});
27072685 defer source_file.close();
27082686
27092687 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;
27122690 const digest = hash: {
27132691 var path_hash: Cache.HashHelper = .{};
27142692 path_hash.addBytes(build_options.version);
27152693 path_hash.add(builtin.zig_backend);
27162694 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);
27182697 }
27192698 path_hash.addBytes(file.sub_file_path);
27202699 break :hash path_hash.final();
......@@ -2939,10 +2918,8 @@ pub fn astGenFile(mod: *Module, file: *File) !void {
29392918 },
29402919 };
29412920 cache_file.writevAll(&iovecs) catch |err| {
2942 const pkg_path = file.pkg.root_src_directory.path orelse ".";
2943 const cache_path = cache_directory.path orelse ".";
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),
2921 log.warn("unable to write cached ZIR code for {}{s} to {}{s}: {s}", .{
2922 file.mod.root, file.sub_file_path, cache_directory, &digest, @errorName(err),
29462923 });
29472924 };
29482925
......@@ -3147,34 +3124,24 @@ pub fn populateBuiltinFile(mod: *Module) !void {
31473124 defer tracy.end();
31483125
31493126 const comp = mod.comp;
3150 const pkg_and_file = blk: {
3127 const builtin_mod, const file = blk: {
31513128 comp.mutex.lock();
31523129 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").?;
31553132 const result = try mod.importPkg(builtin_mod);
3156 break :blk .{
3157 .file = result.file,
3158 .pkg = builtin_mod,
3159 };
3133 break :blk .{ builtin_mod, result.file };
31603134 };
3161 const file = pkg_and_file.file;
3162 const builtin_mod = pkg_and_file.pkg;
31633135 const gpa = mod.gpa;
31643136 file.source = try comp.generateBuiltinZigSource(gpa);
31653137 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| {
31683140 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
31743141 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}. " ++
31763143 "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 },
31783145 );
31793146
31803147 try writeBuiltinFile(file, builtin_mod);
......@@ -3206,7 +3173,7 @@ pub fn populateBuiltinFile(mod: *Module) !void {
32063173}
32073174
32083175fn 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, .{});
32103177 defer af.deinit();
32113178 try af.file.writeAll(file.source);
32123179 try af.finish();
......@@ -3602,7 +3569,8 @@ pub fn updateEmbedFile(mod: *Module, embed_file: *EmbedFile) SemaError!void {
36023569 }
36033570}
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 {
36063574 const file = (try mod.importPkg(pkg)).file;
36073575 return mod.semaFile(file);
36083576}
......@@ -3704,13 +3672,11 @@ pub fn semaFile(mod: *Module, file: *File) SemaError!void {
37043672 return error.AnalysisFail;
37053673 };
37063674
3707 const resolved_path = std.fs.path.resolve(
3708 gpa,
3709 if (file.pkg.root_src_directory.path) |pkg_path|
3710 &[_][]const u8{ pkg_path, file.sub_file_path }
3711 else
3712 &[_][]const u8{file.sub_file_path},
3713 ) catch |err| {
3675 const resolved_path = std.fs.path.resolve(gpa, &.{
3676 file.mod.root.root_dir.path orelse ".",
3677 file.mod.root.sub_path,
3678 file.sub_file_path,
3679 }) catch |err| {
37143680 try reportRetryableFileError(mod, file, "unable to resolve path: {s}", .{@errorName(err)});
37153681 return error.AnalysisFail;
37163682 };
......@@ -3741,8 +3707,8 @@ fn semaDecl(mod: *Module, decl_index: Decl.Index) !bool {
37413707
37423708 // TODO: figure out how this works under incremental changes to builtin.zig!
37433709 const builtin_type_target_index: InternPool.Index = blk: {
3744 const std_mod = mod.main_mod.table.get("std").?;
3745 if (decl.getFileScope(mod).pkg != std_mod) break :blk .none;
3710 const std_mod = mod.main_mod.deps.get("std").?;
3711 if (decl.getFileScope(mod).mod != std_mod) break :blk .none;
37463712 // We're in the std module.
37473713 const std_file = (try mod.importPkg(std_mod)).file;
37483714 const std_decl = mod.declPtr(std_file.root_decl.unwrap().?);
......@@ -4035,14 +4001,17 @@ pub const ImportFileResult = struct {
40354001 is_pkg: bool,
40364002};
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 {
40394006 const gpa = mod.gpa;
40404007
40414008 // The resolved path is used as the key in the import table, to detect if
40424009 // an import refers to the same as another, despite different relative paths
40434010 // or differently mapped package names.
4044 const resolved_path = try std.fs.path.resolve(gpa, &[_][]const u8{
4045 pkg.root_src_directory.path orelse ".", pkg.root_src_path,
4011 const resolved_path = try std.fs.path.resolve(gpa, &.{
4012 pkg.root.root_dir.path orelse ".",
4013 pkg.root.sub_path,
4014 pkg.root_src_path,
40464015 });
40474016 var keep_resolved_path = false;
40484017 defer if (!keep_resolved_path) gpa.free(resolved_path);
......@@ -4076,7 +4045,7 @@ pub fn importPkg(mod: *Module, pkg: *Package) !ImportFileResult {
40764045 .tree = undefined,
40774046 .zir = undefined,
40784047 .status = .never_loaded,
4079 .pkg = pkg,
4048 .mod = pkg,
40804049 .root_decl = .none,
40814050 };
40824051 try new_file.addReference(mod.*, .{ .root = pkg });
......@@ -4093,15 +4062,15 @@ pub fn importFile(
40934062 import_string: []const u8,
40944063) !ImportFileResult {
40954064 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").?);
40974066 }
40984067 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").?);
41004069 }
41014070 if (std.mem.eql(u8, import_string, "root")) {
41024071 return mod.importPkg(mod.root_mod);
41034072 }
4104 if (cur_file.pkg.table.get(import_string)) |pkg| {
4073 if (cur_file.mod.deps.get(import_string)) |pkg| {
41054074 return mod.importPkg(pkg);
41064075 }
41074076 if (!mem.endsWith(u8, import_string, ".zig")) {
......@@ -4112,10 +4081,14 @@ pub fn importFile(
41124081 // The resolved path is used as the key in the import table, to detect if
41134082 // an import refers to the same as another, despite different relative paths
41144083 // or differently mapped package names.
4115 const cur_pkg_dir_path = cur_file.pkg.root_src_directory.path orelse ".";
4116 const resolved_path = try std.fs.path.resolve(gpa, &[_][]const u8{
4117 cur_pkg_dir_path, cur_file.sub_file_path, "..", import_string,
4084 const resolved_path = try std.fs.path.resolve(gpa, &.{
4085 cur_file.mod.root.root_dir.path orelse ".",
4086 cur_file.mod.root.sub_path,
4087 cur_file.sub_file_path,
4088 "..",
4089 import_string,
41184090 });
4091
41194092 var keep_resolved_path = false;
41204093 defer if (!keep_resolved_path) gpa.free(resolved_path);
41214094
......@@ -4130,7 +4103,10 @@ pub fn importFile(
41304103 const new_file = try gpa.create(File);
41314104 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 });
41344110 defer gpa.free(resolved_root_path);
41354111
41364112 const sub_file_path = p: {
......@@ -4164,7 +4140,7 @@ pub fn importFile(
41644140 .tree = undefined,
41654141 .zir = undefined,
41664142 .status = .never_loaded,
4167 .pkg = cur_file.pkg,
4143 .mod = cur_file.mod,
41684144 .root_decl = .none,
41694145 };
41704146 return ImportFileResult{
......@@ -4177,9 +4153,11 @@ pub fn importFile(
41774153pub fn embedFile(mod: *Module, cur_file: *File, import_string: []const u8) !*EmbedFile {
41784154 const gpa = mod.gpa;
41794155
4180 if (cur_file.pkg.table.get(import_string)) |pkg| {
4181 const resolved_path = try std.fs.path.resolve(gpa, &[_][]const u8{
4182 pkg.root_src_directory.path orelse ".", pkg.root_src_path,
4156 if (cur_file.mod.deps.get(import_string)) |pkg| {
4157 const resolved_path = try std.fs.path.resolve(gpa, &.{
4158 pkg.root.root_dir.path orelse ".",
4159 pkg.root.sub_path,
4160 pkg.root_src_path,
41834161 });
41844162 var keep_resolved_path = false;
41854163 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
41964174
41974175 // The resolved path is used as the key in the table, to detect if a file
41984176 // refers to the same as another, despite different relative paths.
4199 const cur_pkg_dir_path = cur_file.pkg.root_src_directory.path orelse ".";
4200 const resolved_path = try std.fs.path.resolve(gpa, &[_][]const u8{
4201 cur_pkg_dir_path, cur_file.sub_file_path, "..", import_string,
4177 const resolved_path = try std.fs.path.resolve(gpa, &.{
4178 cur_file.mod.root.root_dir.path orelse ".",
4179 cur_file.mod.root.sub_path,
4180 cur_file.sub_file_path,
4181 "..",
4182 import_string,
42024183 });
4184
42034185 var keep_resolved_path = false;
42044186 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
42074189 errdefer assert(mod.embed_table.remove(resolved_path));
42084190 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 });
42114196 defer gpa.free(resolved_root_path);
42124197
42134198 const sub_file_path = p: {
......@@ -4225,12 +4210,13 @@ pub fn embedFile(mod: *Module, cur_file: *File, import_string: []const u8) !*Emb
42254210 };
42264211 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);
42294214}
42304215
4216/// https://github.com/ziglang/zig/issues/14307
42314217fn newEmbedFile(
42324218 mod: *Module,
4233 pkg: *Package,
4219 pkg: *Package.Module,
42344220 sub_file_path: []const u8,
42354221 resolved_path: []const u8,
42364222 keep_resolved_path: *bool,
......@@ -4241,7 +4227,7 @@ fn newEmbedFile(
42414227 const new_file = try gpa.create(EmbedFile);
42424228 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, .{});
42454231 defer file.close();
42464232
42474233 const actual_stat = try file.stat();
......@@ -4268,14 +4254,14 @@ fn newEmbedFile(
42684254 .sub_file_path = sub_file_path,
42694255 .bytes = bytes,
42704256 .stat = stat,
4271 .pkg = pkg,
4257 .mod = pkg,
42724258 .owner_decl = undefined, // Set by Sema immediately after this function returns.
42734259 };
42744260 return new_file;
42754261}
42764262
42774263pub 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, .{});
42794265 defer file.close();
42804266
42814267 const stat = try file.stat();
......@@ -4448,21 +4434,21 @@ fn scanDecl(iter: *ScanDeclIter, decl_sub_index: usize, flags: u4) Allocator.Err
44484434 gop.key_ptr.* = new_decl_index;
44494435 // Exported decls, comptime decls, usingnamespace decls, and
44504436 // test decls if in test mode, get analyzed.
4451 const decl_pkg = namespace.file_scope.pkg;
4437 const decl_mod = namespace.file_scope.mod;
44524438 const want_analysis = is_exported or switch (decl_name_index) {
44534439 0 => true, // comptime or usingnamespace decl
44544440 1 => blk: {
44554441 // test decl with no name. Skip the part where we check against
44564442 // the test name filter.
44574443 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;
44594445 try mod.test_functions.put(gpa, new_decl_index, {});
44604446 break :blk true;
44614447 },
44624448 else => blk: {
44634449 if (!is_named_test) break :blk false;
44644450 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;
44664452 if (comp.test_filter) |test_filter| {
44674453 if (mem.indexOf(u8, ip.stringToSlice(decl_name), test_filter) == null) {
44684454 break :blk false;
......@@ -5589,7 +5575,7 @@ pub fn populateTestFunctions(
55895575) !void {
55905576 const gpa = mod.gpa;
55915577 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").?;
55935579 const builtin_file = (mod.importPkg(builtin_mod) catch unreachable).file;
55945580 const root_decl = mod.declPtr(builtin_file.root_decl.unwrap().?);
55955581 const builtin_namespace = mod.namespacePtr(root_decl.src_namespace);
src/Package.zig+29-4
......@@ -13,12 +13,13 @@ pub const Path = struct {
1313 return .{ .root_dir = Cache.Directory.cwd() };
1414 }
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;
1718 const parts: []const []const u8 =
1819 if (p.sub_path.len == 0) &.{sub_path} else &.{ p.sub_path, sub_path };
1920 return .{
2021 .root_dir = p.root_dir,
21 .sub_path = try fs.path.join(allocator, parts),
22 .sub_path = try fs.path.join(arena, parts),
2223 };
2324 }
2425
......@@ -28,7 +29,7 @@ pub const Path = struct {
2829 return p.root_dir.join(allocator, parts);
2930 }
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 {
3233 const parts: []const []const u8 =
3334 if (p.sub_path.len == 0) &.{sub_path} else &.{ p.sub_path, sub_path };
3435 return p.root_dir.joinZ(allocator, parts);
......@@ -38,7 +39,7 @@ pub const Path = struct {
3839 p: Path,
3940 sub_path: []const u8,
4041 flags: fs.File.OpenFlags,
41 ) fs.File.OpenError!fs.File {
42 ) !fs.File {
4243 var buf: [fs.MAX_PATH_BYTES]u8 = undefined;
4344 const joined_path = if (p.sub_path.len == 0) sub_path else p: {
4445 break :p std.fmt.bufPrint(&buf, "{s}" ++ fs.path.sep_str ++ "{s}", .{
......@@ -58,6 +59,30 @@ pub const Path = struct {
5859 return p.root_dir.handle.makeOpenPath(joined_path, opts);
5960 }
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
6186 pub fn format(
6287 self: Path,
6388 comptime fmt_string: []const u8,
src/Package/Fetch.zig+28-2
......@@ -49,7 +49,7 @@ allow_missing_paths_field: bool,
4949/// This will either be relative to `global_cache`, or to the build root of
5050/// the root package.
5151package_root: Package.Path,
52error_bundle: std.zig.ErrorBundle.Wip,
52error_bundle: ErrorBundle.Wip,
5353manifest: ?Manifest,
5454manifest_ast: std.zig.Ast,
5555actual_hash: Manifest.Digest,
......@@ -89,6 +89,31 @@ pub const JobQueue = struct {
8989 jq.all_fetches.deinit(gpa);
9090 jq.* = undefined;
9191 }
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 }
92117};
93118
94119pub const Location = union(enum) {
......@@ -502,7 +527,7 @@ pub fn workerRun(f: *Fetch) void {
502527fn srcLoc(
503528 f: *Fetch,
504529 tok: std.zig.Ast.TokenIndex,
505) Allocator.Error!std.zig.ErrorBundle.SourceLocationIndex {
530) Allocator.Error!ErrorBundle.SourceLocationIndex {
506531 const ast = f.parent_manifest_ast orelse return .none;
507532 const eb = &f.error_bundle;
508533 const token_starts = ast.tokens.items(.start);
......@@ -1258,3 +1283,4 @@ const Fetch = @This();
12581283const main = @import("../main.zig");
12591284const git = @import("../git.zig");
12601285const Package = @import("../Package.zig");
1286const ErrorBundle = std.zig.ErrorBundle;
src/Package/Module.zig+1-1
......@@ -8,7 +8,7 @@ root: Package.Path,
88root_src_path: []const u8,
99/// The dependency table of this module. Shared dependencies such as 'std',
1010/// 'builtin', and 'root' are not specified in every dependency table, but
11/// instead only in the table of `main_pkg`. `Module.importFile` is
11/// instead only in the table of `main_mod`. `Module.importFile` is
1212/// responsible for detecting these names and using the correct package.
1313deps: Deps = .{},
1414
src/Sema.zig+7-5
......@@ -13075,9 +13075,11 @@ fn zirImport(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.
1307513075 return sema.fail(block, operand_src, "import of file outside package path: '{s}'", .{operand});
1307613076 },
1307713077 error.PackageNotFound => {
13078 const name = try block.getFileScope(mod).pkg.getName(sema.gpa, mod.*);
13079 defer sema.gpa.free(name);
13080 return sema.fail(block, operand_src, "no package named '{s}' available within package '{s}'", .{ operand, name });
13078 //const name = try block.getFileScope(mod).mod.getName(sema.gpa, mod.*);
13079 //defer sema.gpa.free(name);
13080 return sema.fail(block, operand_src, "no package named '{s}' available within package '{}'", .{
13081 operand, block.getFileScope(mod).mod.root,
13082 });
1308113083 },
1308213084 else => {
1308313085 // 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
3641536417
3641636418 const mod = sema.mod;
3641736419 const ip = &mod.intern_pool;
36418 const std_pkg = mod.main_pkg.table.get("std").?;
36419 const std_file = (mod.importPkg(std_pkg) catch unreachable).file;
36420 const std_mod = mod.main_mod.deps.get("std").?;
36421 const std_file = (mod.importPkg(std_mod) catch unreachable).file;
3642036422 const opt_builtin_inst = (try sema.namespaceLookupRef(
3642136423 block,
3642236424 src,
src/codegen/llvm.zig+16-13
......@@ -892,21 +892,24 @@ pub const Object = struct {
892892 build_options.semver.patch,
893893 });
894894
895 // We fully resolve all paths at this point to avoid lack of source line info in stack
896 // traces or lack of debugging information which, if relative paths were used, would
897 // be very location dependent.
895 // We fully resolve all paths at this point to avoid lack of
896 // source line info in stack traces or lack of debugging
897 // information which, if relative paths were used, would be
898 // very location dependent.
898899 // TODO: the only concern I have with this is WASI as either host or target, should
899900 // we leave the paths as relative then?
900901 var buf: [std.fs.MAX_PATH_BYTES]u8 = undefined;
901 const compile_unit_dir = blk: {
902 const path = d: {
903 const mod = options.module orelse break :d ".";
904 break :d mod.root_pkg.root_src_directory.path orelse ".";
905 };
906 if (std.fs.path.isAbsolute(path)) break :blk path;
907 break :blk std.os.realpath(path, &buf) catch path; // If realpath fails, fallback to whatever path was
902 const compile_unit_dir_z = blk: {
903 if (options.module) |mod| {
904 const d = try mod.root_mod.root.joinStringZ(builder.gpa, "");
905 if (std.fs.path.isAbsolute(d)) break :blk d;
906 const abs = std.fs.realpath(d, &buf) catch break :blk d;
907 builder.gpa.free(d);
908 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);
908912 };
909 const compile_unit_dir_z = try builder.gpa.dupeZ(u8, compile_unit_dir);
910913 defer builder.gpa.free(compile_unit_dir_z);
911914
912915 builder.llvm.di_compile_unit = builder.llvm.di_builder.?.createCompileUnit(
......@@ -2828,8 +2831,8 @@ pub const Object = struct {
28282831 fn getStackTraceType(o: *Object) Allocator.Error!Type {
28292832 const mod = o.module;
28302833
2831 const std_pkg = mod.main_pkg.table.get("std").?;
2832 const std_file = (mod.importPkg(std_pkg) catch unreachable).file;
2834 const std_mod = mod.main_mod.deps.get("std").?;
2835 const std_file = (mod.importPkg(std_mod) catch unreachable).file;
28332836
28342837 const builtin_str = try mod.intern_pool.getOrPutString(mod.gpa, "builtin");
28352838 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(
10741074 .cache_mode = .whole,
10751075 .target = comp.getTarget(),
10761076 .root_name = lib.name,
1077 .main_pkg = null,
1077 .main_mod = null,
10781078 .output_mode = .Lib,
10791079 .link_mode = .Dynamic,
10801080 .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 {
233233 .cache_mode = .whole,
234234 .target = target,
235235 .root_name = root_name,
236 .main_pkg = null,
236 .main_mod = null,
237237 .output_mode = output_mode,
238238 .thread_pool = comp.thread_pool,
239239 .libc_installation = comp.bin_file.options.libc_installation,
......@@ -396,7 +396,7 @@ pub fn buildLibCXXABI(comp: *Compilation, prog_node: *std.Progress.Node) !void {
396396 .cache_mode = .whole,
397397 .target = target,
398398 .root_name = root_name,
399 .main_pkg = null,
399 .main_mod = null,
400400 .output_mode = output_mode,
401401 .thread_pool = comp.thread_pool,
402402 .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 {
202202 .cache_mode = .whole,
203203 .target = target,
204204 .root_name = root_name,
205 .main_pkg = null,
205 .main_mod = null,
206206 .output_mode = output_mode,
207207 .thread_pool = comp.thread_pool,
208208 .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 {
8989 .cache_mode = .whole,
9090 .target = target,
9191 .root_name = root_name,
92 .main_pkg = null,
92 .main_mod = null,
9393 .output_mode = output_mode,
9494 .thread_pool = comp.thread_pool,
9595 .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
18801880 },
18811881 }
18821882 // 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);
18841884 var compile_unit_dir_buffer: [std.fs.MAX_PATH_BYTES]u8 = undefined;
18851885 const compile_unit_dir = resolveCompilationDir(module, &compile_unit_dir_buffer);
18861886 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) []
19401940 // be very location dependent.
19411941 // TODO: the only concern I have with this is WASI as either host or target, should
19421942 // we leave the paths as relative then?
1943 const comp_dir_path = module.root_pkg.root_src_directory.path orelse ".";
1944 if (std.fs.path.isAbsolute(comp_dir_path)) return comp_dir_path;
1945 return std.os.realpath(comp_dir_path, buffer) catch comp_dir_path; // If realpath fails, fallback to whatever comp_dir_path was
1943 const root_dir_path = module.root_mod.root.root_dir.path orelse ".";
1944 const sub_path = module.root_mod.root.sub_path;
1945 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];
19461954}
19471955
19481956fn writeAddrAssumeCapacity(self: *Dwarf, buf: *std.ArrayList(u8), addr: u64) void {
......@@ -2664,7 +2672,7 @@ fn genIncludeDirsAndFileNames(self: *Dwarf, arena: Allocator) !struct {
26642672 for (self.di_files.keys()) |dif| {
26652673 const dir_path = d: {
26662674 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);
26682676 const abs_dir_path = if (std.fs.path.isAbsolute(dir_path))
26692677 dir_path
26702678 else
src/link/Elf.zig+3-3
......@@ -929,15 +929,15 @@ pub fn populateMissingMetadata(self: *Elf) !void {
929929
930930 if (self.base.options.module) |module| {
931931 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));
933933 self.files.set(index, .{ .zig_module = .{
934934 .index = index,
935 .path = module.main_pkg.root_src_path,
935 .path = module.main_mod.root_src_path,
936936 } });
937937 self.zig_module_index = index;
938938 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));
941941 const symbol_index = try self.addSymbol();
942942 try zig_module.local_symbols.append(gpa, symbol_index);
943943 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 {
352352
353353 // getting the full file path
354354 var buf: [std.fs.MAX_PATH_BYTES]u8 = undefined;
355 const dir = file.pkg.root_src_directory.path orelse try std.os.getcwd(&buf);
356 const sub_path = try std.fs.path.join(arena, &.{ dir, file.sub_file_path });
357 try self.addPathComponents(sub_path, &a);
355 const full_path = try std.fs.path.join(arena, &.{
356 file.mod.root.root_dir.path orelse try std.os.getcwd(&buf),
357 file.mod.root.sub_path,
358 file.sub_file_path,
359 });
360 try self.addPathComponents(full_path, &a);
358361
359362 // null terminate
360363 try a.append(0);
src/main.zig+58-55
......@@ -897,7 +897,7 @@ fn buildOutputType(
897897 var override_local_cache_dir: ?[]const u8 = try optionalStringEnvVar(arena, "ZIG_LOCAL_CACHE_DIR");
898898 var override_global_cache_dir: ?[]const u8 = try optionalStringEnvVar(arena, "ZIG_GLOBAL_CACHE_DIR");
899899 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;
901901 var clang_preprocessor_mode: Compilation.ClangPreprocessorMode = .no;
902902 var subsystem: ?std.Target.SubSystem = null;
903903 var major_subsystem_version: ?u32 = null;
......@@ -1047,7 +1047,7 @@ fn buildOutputType(
10471047 }
10481048 root_deps_str = args_iter.nextOrFatal();
10491049 } else if (mem.eql(u8, arg, "--main-mod-path")) {
1050 main_pkg_path = args_iter.nextOrFatal();
1050 main_mod_path = args_iter.nextOrFatal();
10511051 } else if (mem.eql(u8, arg, "-cflags")) {
10521052 extra_cflags.shrinkRetainingCapacity(0);
10531053 while (true) {
......@@ -3236,8 +3236,8 @@ fn buildOutputType(
32363236
32373237 const main_mod: ?*Package.Module = if (root_src_file) |unresolved_src_path| blk: {
32383238 const src_path = try introspect.resolvePath(arena, unresolved_src_path);
3239 if (main_pkg_path) |unresolved_main_pkg_path| {
3240 const p = try introspect.resolvePath(arena, unresolved_main_pkg_path);
3239 if (main_mod_path) |unresolved_main_mod_path| {
3240 const p = try introspect.resolvePath(arena, unresolved_main_mod_path);
32413241 break :blk try Package.Module.create(arena, .{
32423242 .root = .{
32433243 .root_dir = Cache.Directory.cwd(),
......@@ -3386,8 +3386,6 @@ fn buildOutputType(
33863386
33873387 gimmeMoreOfThoseSweetSweetFileDescriptors();
33883388
3389 if (true) @panic("TODO restore Compilation logic");
3390
33913389 const comp = Compilation.create(gpa, .{
33923390 .zig_lib_directory = zig_lib_directory,
33933391 .local_cache_directory = local_cache_directory,
......@@ -4628,6 +4626,8 @@ pub const usage_build =
46284626;
46294627
46304628pub 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");
46314631 var color: Color = .auto;
46324632
46334633 // 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
47254725
47264726 const cwd_path = try process.getCwdAlloc(arena);
47274727 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: {
47294729 if (build_file) |bf| {
47304730 if (fs.path.dirname(bf)) |dirname| {
47314731 const dir = fs.cwd().openDir(dirname, .{}) catch |err| {
......@@ -4761,7 +4761,7 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi
47614761 }
47624762 }
47634763 };
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
47664766 var global_cache_directory: Compilation.Directory = l: {
47674767 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
47814781 .path = local_cache_dir_path,
47824782 };
47834783 }
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"});
47854785 break :l .{
4786 .handle = try build_directory.handle.makeOpenPath("zig-cache", .{}),
4786 .handle = try build_root.handle.makeOpenPath("zig-cache", .{}),
47874787 .path = cache_dir_path,
47884788 };
47894789 };
......@@ -4824,74 +4824,77 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi
48244824 };
48254825
48264826 var build_mod: Package.Module = .{
4827 .root = .{ .root_dir = build_directory },
4827 .root = .{ .root_dir = build_root },
48284828 .root_src_path = build_zig_basename,
48294829 };
48304830 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",
48324832 \\pub const packages = struct {};
48334833 \\pub const root_deps: []const struct { []const u8, []const u8 } = &.{};
48344834 \\
48354835 );
4836 try main_mod.deps.put(arena, "@dependencies", deps_pkg);
4836 try main_mod.deps.put(arena, "@dependencies", deps_mod);
48374837 } else {
48384838 var http_client: std.http.Client = .{ .allocator = gpa };
48394839 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
48564841 var progress: std.Progress = .{ .dont_print_on_dumb = true };
48574842 const root_prog_node = progress.start("Fetch Packages", 0);
48584843 defer root_prog_node.end();
48594844
4860 // Here we borrow main package's table and will replace it with a fresh
4861 // one after this process completes.
4862 const fetch_result = build_mod.fetchAndAddDependencies(
4863 &main_mod,
4864 arena,
4865 &thread_pool,
4866 &http_client,
4867 build_directory,
4868 global_cache_directory,
4869 local_cache_directory,
4870 &dependencies_source,
4871 &wip_errors,
4872 &all_modules,
4873 root_prog_node,
4874 null,
4875 );
4876 if (wip_errors.root_list.items.len > 0) {
4877 var errors = try wip_errors.toOwnedBundle("");
4878 defer errors.deinit(gpa);
4845 var job_queue: Package.Fetch.JobQueue = .{
4846 .http_client = &http_client,
4847 .thread_pool = &thread_pool,
4848 .global_cache = global_cache_directory,
4849 .recursive = true,
4850 .work_around_btrfs_bug = work_around_btrfs_bug,
4851 };
4852 defer job_queue.deinit();
4853
4854 try job_queue.all_fetches.ensureUnusedCapacity(gpa, 1);
4855
4856 var fetch: Package.Fetch = .{
4857 .arena = std.heap.ArenaAllocator.init(gpa),
4858 .location = .{ .relative_path = "" },
4859 .location_tok = 0,
4860 .hash_tok = 0,
4861 .parent_package_root = build_mod.root,
4862 .parent_manifest_ast = null,
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("");
48794886 errors.renderToStdErr(renderOptions(color));
48804887 process.exit(1);
48814888 }
4882 try fetch_result;
48834889
4884 const deps_pkg = try Package.createFilePkg(
4885 gpa,
4890 const deps_mod = try job_queue.createDependenciesModule(
4891 arena,
48864892 local_cache_directory,
48874893 "dependencies.zig",
4888 dependencies_source.items,
48894894 );
4890
4891 mem.swap(Package.Table, &main_mod.table, &deps_pkg.table);
4892 try main_mod.add(gpa, "@dependencies", deps_pkg);
4895 try main_mod.deps.put(arena, "@dependencies", deps_mod);
48934896 }
4894 try main_mod.add(gpa, "@build", &build_mod);
4897 try main_mod.deps.put(arena, "@build", &build_mod);
48954898
48964899 const comp = Compilation.create(gpa, .{
48974900 .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
206206 .zig_lib_directory = comp.zig_lib_directory,
207207 .target = target,
208208 .root_name = "c",
209 .main_pkg = null,
209 .main_mod = null,
210210 .output_mode = .Lib,
211211 .link_mode = .Dynamic,
212212 .thread_pool = comp.thread_pool,