authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2023-02-17 01:44:08+00:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2023-02-21 01:59:37+00:00
log705d2a3c2cd94faf8e16c660b3b342d6fe900e55
tree4db97f8c31a2bbd4299418caf609aff219d543a6
parentdc1f50e505105cabe1ed53951ca612778d6019ee
signaturelock-open Commit is signed but in an unrecognized format.

Implement new module CLI


7 files changed, 325 insertions(+), 185 deletions(-)

src/Autodoc.zig+1-9
......@@ -860,17 +860,9 @@ fn walkInstruction(
860860 const str_tok = data[inst_index].str_tok;
861861 var path = str_tok.get(file.zir);
862862
863 const maybe_other_package: ?*Package = blk: {
864 if (self.module.main_pkg_is_std and std.mem.eql(u8, path, "std")) {
865 path = "std";
866 break :blk self.module.main_pkg;
867 } else {
868 break :blk file.pkg.table.get(path);
869 }
870 };
871863 // importFile cannot error out since all files
872864 // are already loaded at this point
873 if (maybe_other_package) |other_package| {
865 if (file.pkg.table.get(path)) |other_package| {
874866 const result = try self.packages.getOrPut(self.arena, other_package);
875867
876868 // Immediately add this package to the import table of our
src/Compilation.zig+54-46
......@@ -1596,36 +1596,53 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
15961596
15971597 const builtin_pkg = try Package.createWithDir(
15981598 gpa,
1599 "builtin",
16001599 zig_cache_artifact_directory,
16011600 null,
16021601 "builtin.zig",
16031602 );
16041603 errdefer builtin_pkg.destroy(gpa);
16051604
1606 const std_pkg = try Package.createWithDir(
1607 gpa,
1608 "std",
1609 options.zig_lib_directory,
1610 "std",
1611 "std.zig",
1612 );
1613 errdefer std_pkg.destroy(gpa);
1605 // When you're testing std, the main module is std. In that case, we'll just set the std
1606 // module to the main one, since avoiding the errors caused by duplicating it is more
1607 // effort than it's worth.
1608 const main_pkg_is_std = m: {
1609 const std_path = try std.fs.path.resolve(arena, &[_][]const u8{
1610 options.zig_lib_directory.path orelse ".",
1611 "std",
1612 "std.zig",
1613 });
1614 defer arena.free(std_path);
1615 const main_path = try std.fs.path.resolve(arena, &[_][]const u8{
1616 main_pkg.root_src_directory.path orelse ".",
1617 main_pkg.root_src_path,
1618 });
1619 defer arena.free(main_path);
1620 break :m mem.eql(u8, main_path, std_path);
1621 };
1622
1623 const std_pkg = if (main_pkg_is_std)
1624 main_pkg
1625 else
1626 try Package.createWithDir(
1627 gpa,
1628 options.zig_lib_directory,
1629 "std",
1630 "std.zig",
1631 );
1632
1633 errdefer if (!main_pkg_is_std) std_pkg.destroy(gpa);
16141634
16151635 const root_pkg = if (options.is_test) root_pkg: {
1616 // TODO: we currently have two packages named 'root' here, which is weird. This
1617 // should be changed as part of the resolution of #12201
16181636 const test_pkg = if (options.test_runner_path) |test_runner| test_pkg: {
16191637 const test_dir = std.fs.path.dirname(test_runner);
16201638 const basename = std.fs.path.basename(test_runner);
1621 const pkg = try Package.create(gpa, "root", test_dir, basename);
1639 const pkg = try Package.create(gpa, test_dir, basename);
16221640
16231641 // copy package table from main_pkg to root_pkg
16241642 pkg.table = try main_pkg.table.clone(gpa);
16251643 break :test_pkg pkg;
16261644 } else try Package.createWithDir(
16271645 gpa,
1628 "root",
16291646 options.zig_lib_directory,
16301647 null,
16311648 "test_runner.zig",
......@@ -1639,7 +1656,6 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
16391656 const compiler_rt_pkg = if (include_compiler_rt and options.output_mode == .Obj) compiler_rt_pkg: {
16401657 break :compiler_rt_pkg try Package.createWithDir(
16411658 gpa,
1642 "compiler_rt",
16431659 options.zig_lib_directory,
16441660 null,
16451661 "compiler_rt.zig",
......@@ -1647,28 +1663,14 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
16471663 } else null;
16481664 errdefer if (compiler_rt_pkg) |p| p.destroy(gpa);
16491665
1650 try main_pkg.addAndAdopt(gpa, builtin_pkg);
1651 try main_pkg.add(gpa, root_pkg);
1652 try main_pkg.addAndAdopt(gpa, std_pkg);
1666 try main_pkg.add(gpa, "builtin", builtin_pkg);
1667 try main_pkg.add(gpa, "root", root_pkg);
1668 try main_pkg.add(gpa, "std", std_pkg);
16531669
16541670 if (compiler_rt_pkg) |p| {
1655 try main_pkg.addAndAdopt(gpa, p);
1671 try main_pkg.add(gpa, "compiler_rt", p);
16561672 }
16571673
1658 const main_pkg_is_std = m: {
1659 const std_path = try std.fs.path.resolve(arena, &[_][]const u8{
1660 std_pkg.root_src_directory.path orelse ".",
1661 std_pkg.root_src_path,
1662 });
1663 defer arena.free(std_path);
1664 const main_path = try std.fs.path.resolve(arena, &[_][]const u8{
1665 main_pkg.root_src_directory.path orelse ".",
1666 main_pkg.root_src_path,
1667 });
1668 defer arena.free(main_path);
1669 break :m mem.eql(u8, main_path, std_path);
1670 };
1671
16721674 // Pre-open the directory handles for cached ZIR code so that it does not need
16731675 // to redundantly happen for each AstGen operation.
16741676 const zir_sub_dir = "z";
......@@ -1705,7 +1707,6 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
17051707 .gpa = gpa,
17061708 .comp = comp,
17071709 .main_pkg = main_pkg,
1708 .main_pkg_is_std = main_pkg_is_std,
17091710 .root_pkg = root_pkg,
17101711 .zig_cache_artifact_directory = zig_cache_artifact_directory,
17111712 .global_zir_cache = global_zir_cache,
......@@ -3107,18 +3108,26 @@ pub fn performAllTheWork(
31073108 for (notes, 0..) |*note, i| {
31083109 errdefer for (notes[0..i]) |*n| n.deinit(mod.gpa);
31093110 note.* = switch (file.references.items[i]) {
3110 .import => |loc| try Module.ErrorMsg.init(
3111 mod.gpa,
3112 loc,
3113 "imported from package {s}",
3114 .{loc.file_scope.pkg.name},
3115 ),
3116 .root => |pkg| try Module.ErrorMsg.init(
3117 mod.gpa,
3118 .{ .file_scope = file, .parent_decl_node = 0, .lazy = .entire_file },
3119 "root of package {s}",
3120 .{pkg.name},
3121 ),
3111 .import => |loc| blk: {
3112 const name = try loc.file_scope.pkg.getName(mod.gpa, mod.*);
3113 defer mod.gpa.free(name);
3114 break :blk try Module.ErrorMsg.init(
3115 mod.gpa,
3116 loc,
3117 "imported from package {s}",
3118 .{name},
3119 );
3120 },
3121 .root => |pkg| blk: {
3122 const name = try pkg.getName(mod.gpa, mod.*);
3123 defer mod.gpa.free(name);
3124 break :blk try Module.ErrorMsg.init(
3125 mod.gpa,
3126 .{ .file_scope = file, .parent_decl_node = 0, .lazy = .entire_file },
3127 "root of package {s}",
3128 .{name},
3129 );
3130 },
31223131 };
31233132 }
31243133 errdefer for (notes) |*n| n.deinit(mod.gpa);
......@@ -5408,7 +5417,6 @@ fn buildOutputFromZig(
54085417 var main_pkg: Package = .{
54095418 .root_src_directory = comp.zig_lib_directory,
54105419 .root_src_path = src_basename,
5411 .name = "root",
54125420 };
54135421 defer main_pkg.deinitTable(comp.gpa);
54145422 const root_name = src_basename[0 .. src_basename.len - std.fs.path.extension(src_basename).len];
src/Module.zig+41-21
......@@ -144,10 +144,6 @@ stage1_flags: packed struct {
144144} = .{},
145145
146146job_queued_update_builtin_zig: bool = true,
147/// This makes it so that we can run `zig test` on the standard library.
148/// Otherwise, the logic for scanning test decls skips all of them because
149/// `main_pkg != std_pkg`.
150main_pkg_is_std: bool,
151147
152148compile_log_text: ArrayListUnmanaged(u8) = .{},
153149
......@@ -2113,7 +2109,27 @@ pub const File = struct {
21132109
21142110 /// Add a reference to this file during AstGen.
21152111 pub fn addReference(file: *File, mod: Module, ref: Reference) !void {
2116 try file.references.append(mod.gpa, ref);
2112 // Don't add the same module root twice. Note that since we always add module roots at the
2113 // front of the references array (see below), this loop is actually O(1) on valid code.
2114 if (ref == .root) {
2115 for (file.references.items) |other| {
2116 switch (other) {
2117 .root => |r| if (ref.root == r) return,
2118 else => break, // reached the end of the "is-root" references
2119 }
2120 }
2121 }
2122
2123 switch (ref) {
2124 // We put root references at the front of the list both to make the above loop fast and
2125 // to make multi-module errors more helpful (since "root-of" notes are generally more
2126 // informative than "imported-from" notes). This path is hit very rarely, so the speed
2127 // of the insert operation doesn't matter too much.
2128 .root => try file.references.insert(mod.gpa, 0, ref),
2129
2130 // Other references we'll just put at the end.
2131 else => try file.references.append(mod.gpa, ref),
2132 }
21172133
21182134 const pkg = switch (ref) {
21192135 .import => |loc| loc.file_scope.pkg,
......@@ -3323,10 +3339,19 @@ pub fn deinit(mod: *Module) void {
33233339 // The callsite of `Compilation.create` owns the `main_pkg`, however
33243340 // Module owns the builtin and std packages that it adds.
33253341 if (mod.main_pkg.table.fetchRemove("builtin")) |kv| {
3342 gpa.free(kv.key);
33263343 kv.value.destroy(gpa);
33273344 }
33283345 if (mod.main_pkg.table.fetchRemove("std")) |kv| {
3329 kv.value.destroy(gpa);
3346 gpa.free(kv.key);
3347 // It's possible for main_pkg to be std when running 'zig test'! In this case, we must not
3348 // destroy it, since it would lead to a double-free.
3349 if (kv.value != mod.main_pkg) {
3350 kv.value.destroy(gpa);
3351 }
3352 }
3353 if (mod.main_pkg.table.fetchRemove("root")) |kv| {
3354 gpa.free(kv.key);
33303355 }
33313356 if (mod.root_pkg != mod.main_pkg) {
33323357 mod.root_pkg.destroy(gpa);
......@@ -4808,11 +4833,14 @@ pub fn importPkg(mod: *Module, pkg: *Package) !ImportFileResult {
48084833
48094834 const gop = try mod.import_table.getOrPut(gpa, resolved_path);
48104835 errdefer _ = mod.import_table.pop();
4811 if (gop.found_existing) return ImportFileResult{
4812 .file = gop.value_ptr.*,
4813 .is_new = false,
4814 .is_pkg = true,
4815 };
4836 if (gop.found_existing) {
4837 try gop.value_ptr.*.addReference(mod.*, .{ .root = pkg });
4838 return ImportFileResult{
4839 .file = gop.value_ptr.*,
4840 .is_new = false,
4841 .is_pkg = true,
4842 };
4843 }
48164844
48174845 const sub_file_path = try gpa.dupe(u8, pkg.root_src_path);
48184846 errdefer gpa.free(sub_file_path);
......@@ -5208,22 +5236,14 @@ fn scanDecl(iter: *ScanDeclIter, decl_sub_index: usize, flags: u4) Allocator.Err
52085236 // test decl with no name. Skip the part where we check against
52095237 // the test name filter.
52105238 if (!comp.bin_file.options.is_test) break :blk false;
5211 if (decl_pkg != mod.main_pkg) {
5212 if (!mod.main_pkg_is_std) break :blk false;
5213 const std_pkg = mod.main_pkg.table.get("std").?;
5214 if (std_pkg != decl_pkg) break :blk false;
5215 }
5239 if (decl_pkg != mod.main_pkg) break :blk false;
52165240 try mod.test_functions.put(gpa, new_decl_index, {});
52175241 break :blk true;
52185242 },
52195243 else => blk: {
52205244 if (!is_named_test) break :blk false;
52215245 if (!comp.bin_file.options.is_test) break :blk false;
5222 if (decl_pkg != mod.main_pkg) {
5223 if (!mod.main_pkg_is_std) break :blk false;
5224 const std_pkg = mod.main_pkg.table.get("std").?;
5225 if (std_pkg != decl_pkg) break :blk false;
5226 }
5246 if (decl_pkg != mod.main_pkg) break :blk false;
52275247 if (comp.test_filter) |test_filter| {
52285248 if (mem.indexOf(u8, decl_name, test_filter) == null) {
52295249 break :blk false;
src/Package.zig+94-29
......@@ -22,17 +22,16 @@ pub const Table = std.StringHashMapUnmanaged(*Package);
2222root_src_directory: Compilation.Directory,
2323/// Relative to `root_src_directory`. May contain path separators.
2424root_src_path: []const u8,
25/// The dependency table of this module. Shared dependencies such as 'std', 'builtin', and 'root'
26/// are not specified in every dependency table, but instead only in the table of `main_pkg`.
27/// `Module.importFile` is responsible for detecting these names and using the correct package.
2528table: Table = .{},
26parent: ?*Package = null,
2729/// Whether to free `root_src_directory` on `destroy`.
2830root_src_directory_owned: bool = false,
29/// This information can be recovered from 'table', but it's more convenient to store on the package.
30name: []const u8,
3131
3232/// Allocate a Package. No references to the slices passed are kept.
3333pub fn create(
3434 gpa: Allocator,
35 name: []const u8,
3635 /// Null indicates the current working directory
3736 root_src_dir_path: ?[]const u8,
3837 /// Relative to root_src_dir_path
......@@ -47,9 +46,6 @@ pub fn create(
4746 const owned_src_path = try gpa.dupe(u8, root_src_path);
4847 errdefer gpa.free(owned_src_path);
4948
50 const owned_name = try gpa.dupe(u8, name);
51 errdefer gpa.free(owned_name);
52
5349 ptr.* = .{
5450 .root_src_directory = .{
5551 .path = owned_dir_path,
......@@ -57,7 +53,6 @@ pub fn create(
5753 },
5854 .root_src_path = owned_src_path,
5955 .root_src_directory_owned = true,
60 .name = owned_name,
6156 };
6257
6358 return ptr;
......@@ -65,7 +60,6 @@ pub fn create(
6560
6661pub fn createWithDir(
6762 gpa: Allocator,
68 name: []const u8,
6963 directory: Compilation.Directory,
7064 /// Relative to `directory`. If null, means `directory` is the root src dir
7165 /// and is owned externally.
......@@ -79,9 +73,6 @@ pub fn createWithDir(
7973 const owned_src_path = try gpa.dupe(u8, root_src_path);
8074 errdefer gpa.free(owned_src_path);
8175
82 const owned_name = try gpa.dupe(u8, name);
83 errdefer gpa.free(owned_name);
84
8576 if (root_src_dir_path) |p| {
8677 const owned_dir_path = try directory.join(gpa, &[1][]const u8{p});
8778 errdefer gpa.free(owned_dir_path);
......@@ -93,14 +84,12 @@ pub fn createWithDir(
9384 },
9485 .root_src_directory_owned = true,
9586 .root_src_path = owned_src_path,
96 .name = owned_name,
9787 };
9888 } else {
9989 ptr.* = .{
10090 .root_src_directory = directory,
10191 .root_src_directory_owned = false,
10292 .root_src_path = owned_src_path,
103 .name = owned_name,
10493 };
10594 }
10695 return ptr;
......@@ -110,7 +99,6 @@ pub fn createWithDir(
11099/// inside its table; the caller is responsible for calling destroy() on them.
111100pub fn destroy(pkg: *Package, gpa: Allocator) void {
112101 gpa.free(pkg.root_src_path);
113 gpa.free(pkg.name);
114102
115103 if (pkg.root_src_directory_owned) {
116104 // If root_src_directory.path is null then the handle is the cwd()
......@@ -130,15 +118,97 @@ pub fn deinitTable(pkg: *Package, gpa: Allocator) void {
130118 pkg.table.deinit(gpa);
131119}
132120
133pub fn add(pkg: *Package, gpa: Allocator, package: *Package) !void {
121pub fn add(pkg: *Package, gpa: Allocator, name: []const u8, package: *Package) !void {
134122 try pkg.table.ensureUnusedCapacity(gpa, 1);
135 pkg.table.putAssumeCapacityNoClobber(package.name, package);
123 const name_dupe = try gpa.dupe(u8, name);
124 pkg.table.putAssumeCapacityNoClobber(name_dupe, package);
136125}
137126
138pub fn addAndAdopt(parent: *Package, gpa: Allocator, child: *Package) !void {
139 assert(child.parent == null); // make up your mind, who is the parent??
140 child.parent = parent;
141 return parent.add(gpa, child);
127/// Compute a readable name for the package. The returned name should be freed from gpa. This
128/// function is very slow, as it traverses the whole package hierarchy to find a path to this
129/// package. It should only be used for error output.
130pub fn getName(target: *const Package, gpa: Allocator, mod: Module) ![]const u8 {
131 // we'll do a breadth-first search from the root module to try and find a short name for this
132 // module, using a TailQueue of module/parent pairs. note that the "parent" there is just the
133 // first-found shortest path - a module may be children of arbitrarily many other modules.
134 // also, this path may vary between executions due to hashmap iteration order, but that doesn't
135 // matter too much.
136 var node_arena = std.heap.ArenaAllocator.init(gpa);
137 defer node_arena.deinit();
138 const Parented = struct {
139 parent: ?*const @This(),
140 mod: *const Package,
141 };
142 const Queue = std.TailQueue(Parented);
143 var to_check: Queue = .{};
144
145 {
146 const new = try node_arena.allocator().create(Queue.Node);
147 new.* = .{ .data = .{ .parent = null, .mod = mod.root_pkg } };
148 to_check.prepend(new);
149 }
150
151 if (mod.main_pkg != mod.root_pkg) {
152 const new = try node_arena.allocator().create(Queue.Node);
153 // TODO: once #12201 is resolved, we may want a way of indicating a different name for this
154 new.* = .{ .data = .{ .parent = null, .mod = mod.main_pkg } };
155 to_check.prepend(new);
156 }
157
158 // set of modules we've already checked to prevent loops
159 var checked = std.AutoHashMap(*const Package, void).init(gpa);
160 defer checked.deinit();
161
162 const linked = while (to_check.pop()) |node| {
163 const check = &node.data;
164
165 if (checked.contains(check.mod)) continue;
166 try checked.put(check.mod, {});
167
168 if (check.mod == target) break check;
169
170 var it = check.mod.table.iterator();
171 while (it.next()) |kv| {
172 var new = try node_arena.allocator().create(Queue.Node);
173 new.* = .{ .data = .{
174 .parent = check,
175 .mod = kv.value_ptr.*,
176 } };
177 to_check.prepend(new);
178 }
179 } else {
180 // this can happen for e.g. @cImport packages
181 return gpa.dupe(u8, "<unnamed>");
182 };
183
184 // we found a path to the module! unfortunately, we can only traverse *up* it, so we have to put
185 // all the names into a buffer so we can then print them in order.
186 var names = std.ArrayList([]const u8).init(gpa);
187 defer names.deinit();
188
189 var cur: *const Parented = linked;
190 while (cur.parent) |parent| : (cur = parent) {
191 // find cur's name in parent
192 var it = parent.mod.table.iterator();
193 const name = while (it.next()) |kv| {
194 if (kv.value_ptr.* == cur.mod) {
195 break kv.key_ptr.*;
196 }
197 } else unreachable;
198 try names.append(name);
199 }
200
201 // finally, print the names into a buffer!
202 var buf = std.ArrayList(u8).init(gpa);
203 defer buf.deinit();
204 try buf.writer().writeAll("root");
205 var i: usize = names.items.len;
206 while (i > 0) {
207 i -= 1;
208 try buf.writer().print(".{s}", .{names.items[i]});
209 }
210
211 return buf.toOwnedSlice();
142212}
143213
144214pub const build_zig_basename = "build.zig";
......@@ -236,7 +306,7 @@ pub fn fetchAndAddDependencies(
236306 color,
237307 );
238308
239 try addAndAdopt(pkg, gpa, sub_pkg);
309 try add(pkg, gpa, fqn, sub_pkg);
240310
241311 try dependencies_source.writer().print(" pub const {s} = @import(\"{}\");\n", .{
242312 std.zig.fmtId(fqn), std.zig.fmtEscapes(fqn),
......@@ -248,7 +318,6 @@ pub fn fetchAndAddDependencies(
248318
249319pub fn createFilePkg(
250320 gpa: Allocator,
251 name: []const u8,
252321 cache_directory: Compilation.Directory,
253322 basename: []const u8,
254323 contents: []const u8,
......@@ -269,7 +338,7 @@ pub fn createFilePkg(
269338 const o_dir_sub_path = "o" ++ fs.path.sep_str ++ hex_digest;
270339 try renameTmpIntoCache(cache_directory.handle, tmp_dir_sub_path, o_dir_sub_path);
271340
272 return createWithDir(gpa, name, cache_directory, o_dir_sub_path, basename);
341 return createWithDir(gpa, cache_directory, o_dir_sub_path, basename);
273342}
274343
275344const Report = struct {
......@@ -363,9 +432,6 @@ fn fetchAndUnpack(
363432 const owned_src_path = try gpa.dupe(u8, build_zig_basename);
364433 errdefer gpa.free(owned_src_path);
365434
366 const owned_name = try gpa.dupe(u8, fqn);
367 errdefer gpa.free(owned_name);
368
369435 const build_root = try global_cache_directory.join(gpa, &.{pkg_dir_sub_path});
370436 errdefer gpa.free(build_root);
371437
......@@ -380,7 +446,6 @@ fn fetchAndUnpack(
380446 },
381447 .root_src_directory_owned = true,
382448 .root_src_path = owned_src_path,
383 .name = owned_name,
384449 };
385450
386451 return ptr;
......@@ -455,7 +520,7 @@ fn fetchAndUnpack(
455520 std.zig.fmtId(fqn), std.zig.fmtEscapes(build_root),
456521 });
457522
458 return createWithDir(gpa, fqn, global_cache_directory, pkg_dir_sub_path, build_zig_basename);
523 return createWithDir(gpa, global_cache_directory, pkg_dir_sub_path, build_zig_basename);
459524}
460525
461526fn unpackTarball(
src/Sema.zig+3-3
......@@ -5311,7 +5311,6 @@ fn zirCImport(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index) CompileEr
53115311 }
53125312 const c_import_pkg = Package.create(
53135313 sema.gpa,
5314 "c_import", // TODO: should we make this unique?
53155314 null,
53165315 c_import_res.out_zig_path,
53175316 ) catch |err| switch (err) {
......@@ -11793,8 +11792,9 @@ fn zirImport(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.
1179311792 return sema.fail(block, operand_src, "import of file outside package path: '{s}'", .{operand});
1179411793 },
1179511794 error.PackageNotFound => {
11796 const cur_pkg = block.getFileScope().pkg;
11797 return sema.fail(block, operand_src, "no package named '{s}' available within package '{s}'", .{ operand, cur_pkg.name });
11795 const name = try block.getFileScope().pkg.getName(sema.gpa, mod.*);
11796 defer sema.gpa.free(name);
11797 return sema.fail(block, operand_src, "no package named '{s}' available within package '{s}'", .{ operand, name });
1179811798 },
1179911799 else => {
1180011800 // TODO: these errors are file system errors; make sure an update() will
src/main.zig+132-76
......@@ -403,8 +403,11 @@ const usage_build_generic =
403403 \\ ReleaseFast Optimizations on, safety off
404404 \\ ReleaseSafe Optimizations on, safety on
405405 \\ ReleaseSmall Optimize for small binary, safety off
406 \\ --pkg-begin [name] [path] Make pkg available to import and push current pkg
407 \\ --pkg-end Pop current pkg
406 \\ --mod [name]:[deps]:[src] Make a module available for dependency under the given name
407 \\ deps: [dep],[dep],...
408 \\ dep: [[import=]name]
409 \\ --deps [dep],[dep],... Set dependency names for the root package
410 \\ dep: [[import=]name]
408411 \\ --main-pkg-path Set the directory of the root package
409412 \\ -fPIC Force-enable Position Independent Code
410413 \\ -fno-PIC Force-disable Position Independent Code
......@@ -858,15 +861,21 @@ fn buildOutputType(
858861 var linker_export_symbol_names = std.ArrayList([]const u8).init(gpa);
859862 defer linker_export_symbol_names.deinit();
860863
861 // This package only exists to clean up the code parsing --pkg-begin and
862 // --pkg-end flags. Use dummy values that are safe for the destroy call.
863 var pkg_tree_root: Package = .{
864 .root_src_directory = .{ .path = null, .handle = fs.cwd() },
865 .root_src_path = &[0]u8{},
866 .name = &[0]u8{},
867 };
868 defer freePkgTree(gpa, &pkg_tree_root, false);
869 var cur_pkg: *Package = &pkg_tree_root;
864 // Contains every module specified via --mod. The dependencies are added
865 // after argument parsing is completed. We use a StringArrayHashMap to make
866 // error output consistent.
867 var modules = std.StringArrayHashMap(struct {
868 mod: *Package,
869 deps_str: []const u8, // still in CLI arg format
870 }).init(gpa);
871 defer {
872 var it = modules.iterator();
873 while (it.next()) |kv| kv.value_ptr.mod.destroy(gpa);
874 modules.deinit();
875 }
876
877 // The dependency string for the root package
878 var root_deps_str: ?[]const u8 = null;
870879
871880 // before arg parsing, check for the NO_COLOR environment variable
872881 // if it exists, default the color setting to .off
......@@ -943,34 +952,44 @@ fn buildOutputType(
943952 } else {
944953 fatal("unexpected end-of-parameter mark: --", .{});
945954 }
946 } else if (mem.eql(u8, arg, "--pkg-begin")) {
947 const opt_pkg_name = args_iter.next();
948 const opt_pkg_path = args_iter.next();
949 if (opt_pkg_name == null or opt_pkg_path == null)
950 fatal("Expected 2 arguments after {s}", .{arg});
951
952 const pkg_name = opt_pkg_name.?;
953 const pkg_path = try introspect.resolvePath(arena, opt_pkg_path.?);
954
955 const new_cur_pkg = Package.create(
956 gpa,
957 pkg_name,
958 fs.path.dirname(pkg_path),
959 fs.path.basename(pkg_path),
960 ) catch |err| {
961 fatal("Failed to add package at path {s}: {s}", .{ pkg_path, @errorName(err) });
962 };
955 } else if (mem.eql(u8, arg, "--mod")) {
956 const info = args_iter.nextOrFatal();
957 var info_it = mem.split(u8, info, ":");
958 const mod_name = info_it.next() orelse fatal("expected non-empty argument after {s}", .{arg});
959 const deps_str = info_it.next() orelse fatal("expected 'name:deps:path' after {s}", .{arg});
960 const root_src_orig = info_it.rest();
961 if (root_src_orig.len == 0) fatal("expected 'name:deps:path' after {s}", .{arg});
962 if (mod_name.len == 0) fatal("empty name for module at '{s}'", .{root_src_orig});
963
964 const root_src = try introspect.resolvePath(arena, root_src_orig);
965
966 for ([_][]const u8{ "std", "root", "builtin" }) |name| {
967 if (mem.eql(u8, mod_name, name)) {
968 fatal("unable to add module '{s}' -> '{s}': conflicts with builtin module", .{ mod_name, root_src });
969 }
970 }
963971
964 if (mem.eql(u8, pkg_name, "std") or mem.eql(u8, pkg_name, "root") or mem.eql(u8, pkg_name, "builtin")) {
965 fatal("unable to add package '{s}' -> '{s}': conflicts with builtin package", .{ pkg_name, pkg_path });
966 } else if (cur_pkg.table.get(pkg_name)) |prev| {
967 fatal("unable to add package '{s}' -> '{s}': already exists as '{s}", .{ pkg_name, pkg_path, prev.root_src_path });
972 var mod_it = modules.iterator();
973 while (mod_it.next()) |kv| {
974 if (std.mem.eql(u8, mod_name, kv.key_ptr.*)) {
975 fatal("unable to add module '{s}' -> '{s}': already exists as '{s}'", .{ mod_name, root_src, kv.value_ptr.mod.root_src_path });
976 }
977 }
978
979 try modules.ensureUnusedCapacity(1);
980 modules.put(mod_name, .{
981 .mod = try Package.create(
982 gpa,
983 fs.path.dirname(root_src),
984 fs.path.basename(root_src),
985 ),
986 .deps_str = deps_str,
987 }) catch unreachable;
988 } else if (mem.eql(u8, arg, "--deps")) {
989 if (root_deps_str != null) {
990 fatal("only one --deps argument is allowed", .{});
968991 }
969 try cur_pkg.addAndAdopt(gpa, new_cur_pkg);
970 cur_pkg = new_cur_pkg;
971 } else if (mem.eql(u8, arg, "--pkg-end")) {
972 cur_pkg = cur_pkg.parent orelse
973 fatal("encountered --pkg-end with no matching --pkg-begin", .{});
992 root_deps_str = args_iter.nextOrFatal();
974993 } else if (mem.eql(u8, arg, "--main-pkg-path")) {
975994 main_pkg_path = args_iter.nextOrFatal();
976995 } else if (mem.eql(u8, arg, "-cflags")) {
......@@ -2307,6 +2326,31 @@ fn buildOutputType(
23072326 },
23082327 }
23092328
2329 {
2330 // Resolve module dependencies
2331 var it = modules.iterator();
2332 while (it.next()) |kv| {
2333 const deps_str = kv.value_ptr.deps_str;
2334 var deps_it = ModuleDepIterator.init(deps_str);
2335 while (deps_it.next()) |dep| {
2336 if (dep.expose.len == 0) {
2337 fatal("module '{s}' depends on '{s}' with a blank name", .{ kv.key_ptr.*, dep.name });
2338 }
2339
2340 for ([_][]const u8{ "std", "root", "builtin" }) |name| {
2341 if (mem.eql(u8, dep.expose, name)) {
2342 fatal("unable to add module '{s}' under name '{s}': conflicts with builtin module", .{ dep.name, dep.expose });
2343 }
2344 }
2345
2346 const dep_mod = modules.get(dep.name) orelse
2347 fatal("module '{s}' depends on module '{s}' which does not exist", .{ kv.key_ptr.*, dep.name });
2348
2349 try kv.value_ptr.mod.add(gpa, dep.expose, dep_mod.mod);
2350 }
2351 }
2352 }
2353
23102354 if (arg_mode == .build and optimize_mode == .ReleaseSmall and strip == null)
23112355 strip = true;
23122356
......@@ -2886,14 +2930,14 @@ fn buildOutputType(
28862930 if (main_pkg_path) |unresolved_main_pkg_path| {
28872931 const p = try introspect.resolvePath(arena, unresolved_main_pkg_path);
28882932 if (p.len == 0) {
2889 break :blk try Package.create(gpa, "root", null, src_path);
2933 break :blk try Package.create(gpa, null, src_path);
28902934 } else {
28912935 const rel_src_path = try fs.path.relative(arena, p, src_path);
2892 break :blk try Package.create(gpa, "root", p, rel_src_path);
2936 break :blk try Package.create(gpa, p, rel_src_path);
28932937 }
28942938 } else {
28952939 const root_src_dir_path = fs.path.dirname(src_path);
2896 break :blk Package.create(gpa, "root", root_src_dir_path, fs.path.basename(src_path)) catch |err| {
2940 break :blk Package.create(gpa, root_src_dir_path, fs.path.basename(src_path)) catch |err| {
28972941 if (root_src_dir_path) |p| {
28982942 fatal("unable to open '{s}': {s}", .{ p, @errorName(err) });
28992943 } else {
......@@ -2904,23 +2948,24 @@ fn buildOutputType(
29042948 } else null;
29052949 defer if (main_pkg) |p| p.destroy(gpa);
29062950
2907 // Transfer packages added with --pkg-begin/--pkg-end to the root package
2908 if (main_pkg) |pkg| {
2909 var it = pkg_tree_root.table.valueIterator();
2910 while (it.next()) |p| {
2911 if (p.*.parent == &pkg_tree_root) {
2912 p.*.parent = pkg;
2951 // Transfer packages added with --deps to the root package
2952 if (main_pkg) |mod| {
2953 var it = ModuleDepIterator.init(root_deps_str orelse "");
2954 while (it.next()) |dep| {
2955 if (dep.expose.len == 0) {
2956 fatal("root module depends on '{s}' with a blank name", .{dep.name});
29132957 }
2914 }
2915 pkg.table = pkg_tree_root.table;
2916 pkg_tree_root.table = .{};
2917 } else {
2918 // Remove any dangling pointers just in case.
2919 var it = pkg_tree_root.table.valueIterator();
2920 while (it.next()) |p| {
2921 if (p.*.parent == &pkg_tree_root) {
2922 p.*.parent = null;
2958
2959 for ([_][]const u8{ "std", "root", "builtin" }) |name| {
2960 if (mem.eql(u8, dep.expose, name)) {
2961 fatal("unable to add module '{s}' under name '{s}': conflicts with builtin module", .{ dep.name, dep.expose });
2962 }
29232963 }
2964
2965 const dep_mod = modules.get(dep.name) orelse
2966 fatal("root module depends on module '{s}' which does not exist", .{dep.name});
2967
2968 try mod.add(gpa, dep.expose, dep_mod.mod);
29242969 }
29252970 }
29262971
......@@ -3400,6 +3445,32 @@ fn buildOutputType(
34003445 return cleanExit();
34013446}
34023447
3448const ModuleDepIterator = struct {
3449 split: mem.SplitIterator(u8),
3450
3451 fn init(deps_str: []const u8) ModuleDepIterator {
3452 return .{ .split = mem.split(u8, deps_str, ",") };
3453 }
3454
3455 const Dependency = struct {
3456 expose: []const u8,
3457 name: []const u8,
3458 };
3459
3460 fn next(it: *ModuleDepIterator) ?Dependency {
3461 if (it.split.buffer.len == 0) return null; // don't return "" for the first iteration on ""
3462 const str = it.split.next() orelse return null;
3463 if (mem.indexOfScalar(u8, str, '=')) |i| {
3464 return .{
3465 .expose = str[0..i],
3466 .name = str[i + 1 ..],
3467 };
3468 } else {
3469 return .{ .expose = str, .name = str };
3470 }
3471 }
3472};
3473
34033474fn parseCrossTargetOrReportFatalError(
34043475 allocator: Allocator,
34053476 opts: std.zig.CrossTarget.ParseOptions,
......@@ -3626,18 +3697,6 @@ fn updateModule(gpa: Allocator, comp: *Compilation, hook: AfterUpdateHook) !void
36263697 }
36273698}
36283699
3629fn freePkgTree(gpa: Allocator, pkg: *Package, free_parent: bool) void {
3630 {
3631 var it = pkg.table.valueIterator();
3632 while (it.next()) |value| {
3633 freePkgTree(gpa, value.*, true);
3634 }
3635 }
3636 if (free_parent) {
3637 pkg.destroy(gpa);
3638 }
3639}
3640
36413700fn cmdTranslateC(comp: *Compilation, arena: Allocator, enable_cache: bool) !void {
36423701 if (!build_options.have_llvm)
36433702 fatal("cannot translate-c: compiler built without LLVM extensions", .{});
......@@ -4141,7 +4200,6 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi
41414200 var main_pkg: Package = .{
41424201 .root_src_directory = zig_lib_directory,
41434202 .root_src_path = "build_runner.zig",
4144 .name = "root",
41454203 };
41464204
41474205 if (!build_options.omit_pkg_fetching_code) {
......@@ -4184,22 +4242,20 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi
41844242
41854243 const deps_pkg = try Package.createFilePkg(
41864244 gpa,
4187 "@dependencies",
41884245 local_cache_directory,
41894246 "dependencies.zig",
41904247 dependencies_source.items,
41914248 );
41924249
41934250 mem.swap(Package.Table, &main_pkg.table, &deps_pkg.table);
4194 try main_pkg.addAndAdopt(gpa, deps_pkg);
4251 try main_pkg.add(gpa, "@dependencies", deps_pkg);
41954252 }
41964253
41974254 var build_pkg: Package = .{
41984255 .root_src_directory = build_directory,
41994256 .root_src_path = build_zig_basename,
4200 .name = "@build",
42014257 };
4202 try main_pkg.addAndAdopt(gpa, &build_pkg);
4258 try main_pkg.add(gpa, "@build", &build_pkg);
42034259
42044260 const comp = Compilation.create(gpa, .{
42054261 .zig_lib_directory = zig_lib_directory,
......@@ -4434,7 +4490,7 @@ pub fn cmdFmt(gpa: Allocator, arena: Allocator, args: []const []const u8) !void
44344490 .root_decl = .none,
44354491 };
44364492
4437 file.pkg = try Package.create(gpa, "root", null, file.sub_file_path);
4493 file.pkg = try Package.create(gpa, null, file.sub_file_path);
44384494 defer file.pkg.destroy(gpa);
44394495
44404496 file.zir = try AstGen.generate(gpa, file.tree);
......@@ -4645,7 +4701,7 @@ fn fmtPathFile(
46454701 .root_decl = .none,
46464702 };
46474703
4648 file.pkg = try Package.create(fmt.gpa, "root", null, file.sub_file_path);
4704 file.pkg = try Package.create(fmt.gpa, null, file.sub_file_path);
46494705 defer file.pkg.destroy(fmt.gpa);
46504706
46514707 if (stat.size > max_src_size)
......@@ -5357,7 +5413,7 @@ pub fn cmdAstCheck(
53575413 file.stat.size = source.len;
53585414 }
53595415
5360 file.pkg = try Package.create(gpa, "root", null, file.sub_file_path);
5416 file.pkg = try Package.create(gpa, null, file.sub_file_path);
53615417 defer file.pkg.destroy(gpa);
53625418
53635419 file.tree = try Ast.parse(gpa, file.source, .zig);
......@@ -5476,7 +5532,7 @@ pub fn cmdChangelist(
54765532 .root_decl = .none,
54775533 };
54785534
5479 file.pkg = try Package.create(gpa, "root", null, file.sub_file_path);
5535 file.pkg = try Package.create(gpa, null, file.sub_file_path);
54805536 defer file.pkg.destroy(gpa);
54815537
54825538 const source = try arena.allocSentinel(u8, @intCast(usize, stat.size), 0);
src/test.zig-1
......@@ -1497,7 +1497,6 @@ pub const TestContext = struct {
14971497 var main_pkg: Package = .{
14981498 .root_src_directory = .{ .path = tmp_dir_path, .handle = tmp.dir },
14991499 .root_src_path = tmp_src_path,
1500 .name = "root",
15011500 };
15021501 defer main_pkg.table.deinit(allocator);
15031502