| author | |
| committer | |
| log | 6d71d79dc27ddd6f66913a34fd6cd40691a8c959 |
| tree | d6fd9f56beb9ba26545a12f5fa827d91aec81972 |
| parent | c0284e242f7d78955204dc8a627fecd45aa5e521 |
| signature |
By @Vexu's suggestion, since fetching the name from the parent package
is error-prone and complex, and optimising Package for size isn't really
a priority.6 files changed, 54 insertions(+), 46 deletions(-)
src/Compilation.zig+10-4| ... | @@ -1610,6 +1610,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation { | ... | @@ -1610,6 +1610,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation { |
| 1610 | 1610 | ||
| 1611 | const builtin_pkg = try Package.createWithDir( | 1611 | const builtin_pkg = try Package.createWithDir( |
| 1612 | gpa, | 1612 | gpa, |
| 1613 | "builtin", | ||
| 1613 | zig_cache_artifact_directory, | 1614 | zig_cache_artifact_directory, |
| 1614 | null, | 1615 | null, |
| 1615 | "builtin.zig", | 1616 | "builtin.zig", |
| ... | @@ -1618,6 +1619,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation { | ... | @@ -1618,6 +1619,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation { |
| 1618 | 1619 | ||
| 1619 | const std_pkg = try Package.createWithDir( | 1620 | const std_pkg = try Package.createWithDir( |
| 1620 | gpa, | 1621 | gpa, |
| 1622 | "std", | ||
| 1621 | options.zig_lib_directory, | 1623 | options.zig_lib_directory, |
| 1622 | "std", | 1624 | "std", |
| 1623 | "std.zig", | 1625 | "std.zig", |
| ... | @@ -1625,11 +1627,14 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation { | ... | @@ -1625,11 +1627,14 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation { |
| 1625 | errdefer std_pkg.destroy(gpa); | 1627 | errdefer std_pkg.destroy(gpa); |
| 1626 | 1628 | ||
| 1627 | const root_pkg = if (options.is_test) root_pkg: { | 1629 | const root_pkg = if (options.is_test) root_pkg: { |
| 1630 | // TODO: we currently have two packages named 'root' here, which is weird. This | ||
| 1631 | // should be changed as part of the resolution of #12201 | ||
| 1628 | const test_pkg = if (options.test_runner_path) |test_runner| | 1632 | const test_pkg = if (options.test_runner_path) |test_runner| |
| 1629 | try Package.create(gpa, null, test_runner) | 1633 | try Package.create(gpa, "root", null, test_runner) |
| 1630 | else | 1634 | else |
| 1631 | try Package.createWithDir( | 1635 | try Package.createWithDir( |
| 1632 | gpa, | 1636 | gpa, |
| 1637 | "root", | ||
| 1633 | options.zig_lib_directory, | 1638 | options.zig_lib_directory, |
| 1634 | null, | 1639 | null, |
| 1635 | "test_runner.zig", | 1640 | "test_runner.zig", |
| ... | @@ -1640,9 +1645,9 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation { | ... | @@ -1640,9 +1645,9 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation { |
| 1640 | } else main_pkg; | 1645 | } else main_pkg; |
| 1641 | errdefer if (options.is_test) root_pkg.destroy(gpa); | 1646 | errdefer if (options.is_test) root_pkg.destroy(gpa); |
| 1642 | 1647 | ||
| 1643 | try main_pkg.addAndAdopt(gpa, "builtin", builtin_pkg); | 1648 | try main_pkg.addAndAdopt(gpa, builtin_pkg); |
| 1644 | try main_pkg.add(gpa, "root", root_pkg); | 1649 | try main_pkg.add(gpa, root_pkg); |
| 1645 | try main_pkg.addAndAdopt(gpa, "std", std_pkg); | 1650 | try main_pkg.addAndAdopt(gpa, std_pkg); |
| 1646 | 1651 | ||
| 1647 | const main_pkg_is_std = m: { | 1652 | const main_pkg_is_std = m: { |
| 1648 | const std_path = try std.fs.path.resolve(arena, &[_][]const u8{ | 1653 | const std_path = try std.fs.path.resolve(arena, &[_][]const u8{ |
| ... | @@ -5320,6 +5325,7 @@ fn buildOutputFromZig( | ... | @@ -5320,6 +5325,7 @@ fn buildOutputFromZig( |
| 5320 | var main_pkg: Package = .{ | 5325 | var main_pkg: Package = .{ |
| 5321 | .root_src_directory = comp.zig_lib_directory, | 5326 | .root_src_directory = comp.zig_lib_directory, |
| 5322 | .root_src_path = src_basename, | 5327 | .root_src_path = src_basename, |
| 5328 | .name = "root", | ||
| 5323 | }; | 5329 | }; |
| 5324 | defer main_pkg.deinitTable(comp.gpa); | 5330 | defer main_pkg.deinitTable(comp.gpa); |
| 5325 | const root_name = src_basename[0 .. src_basename.len - std.fs.path.extension(src_basename).len]; | 5331 | const root_name = src_basename[0 .. src_basename.len - std.fs.path.extension(src_basename).len]; |
src/Module.zig-5| ... | @@ -3220,16 +3220,11 @@ pub fn deinit(mod: *Module) void { | ... | @@ -3220,16 +3220,11 @@ pub fn deinit(mod: *Module) void { |
| 3220 | // The callsite of `Compilation.create` owns the `main_pkg`, however | 3220 | // The callsite of `Compilation.create` owns the `main_pkg`, however |
| 3221 | // Module owns the builtin and std packages that it adds. | 3221 | // Module owns the builtin and std packages that it adds. |
| 3222 | if (mod.main_pkg.table.fetchRemove("builtin")) |kv| { | 3222 | if (mod.main_pkg.table.fetchRemove("builtin")) |kv| { |
| 3223 | gpa.free(kv.key); | ||
| 3224 | kv.value.destroy(gpa); | 3223 | kv.value.destroy(gpa); |
| 3225 | } | 3224 | } |
| 3226 | if (mod.main_pkg.table.fetchRemove("std")) |kv| { | 3225 | if (mod.main_pkg.table.fetchRemove("std")) |kv| { |
| 3227 | gpa.free(kv.key); | ||
| 3228 | kv.value.destroy(gpa); | 3226 | kv.value.destroy(gpa); |
| 3229 | } | 3227 | } |
| 3230 | if (mod.main_pkg.table.fetchRemove("root")) |kv| { | ||
| 3231 | gpa.free(kv.key); | ||
| 3232 | } | ||
| 3233 | if (mod.root_pkg != mod.main_pkg) { | 3228 | if (mod.root_pkg != mod.main_pkg) { |
| 3234 | mod.root_pkg.destroy(gpa); | 3229 | mod.root_pkg.destroy(gpa); |
| 3235 | } | 3230 | } |
src/Package.zig+26-13| ... | @@ -24,10 +24,13 @@ table: Table = .{}, | ... | @@ -24,10 +24,13 @@ table: Table = .{}, |
| 24 | parent: ?*Package = null, | 24 | parent: ?*Package = null, |
| 25 | /// Whether to free `root_src_directory` on `destroy`. | 25 | /// Whether to free `root_src_directory` on `destroy`. |
| 26 | root_src_directory_owned: bool = false, | 26 | root_src_directory_owned: bool = false, |
| 27 | /// This information can be recovered from 'table', but it's more convenient to store on the package. | ||
| 28 | name: []const u8, | ||
| 27 | 29 | ||
| 28 | /// Allocate a Package. No references to the slices passed are kept. | 30 | /// Allocate a Package. No references to the slices passed are kept. |
| 29 | pub fn create( | 31 | pub fn create( |
| 30 | gpa: Allocator, | 32 | gpa: Allocator, |
| 33 | name: []const u8, | ||
| 31 | /// Null indicates the current working directory | 34 | /// Null indicates the current working directory |
| 32 | root_src_dir_path: ?[]const u8, | 35 | root_src_dir_path: ?[]const u8, |
| 33 | /// Relative to root_src_dir_path | 36 | /// Relative to root_src_dir_path |
| ... | @@ -42,6 +45,9 @@ pub fn create( | ... | @@ -42,6 +45,9 @@ pub fn create( |
| 42 | const owned_src_path = try gpa.dupe(u8, root_src_path); | 45 | const owned_src_path = try gpa.dupe(u8, root_src_path); |
| 43 | errdefer gpa.free(owned_src_path); | 46 | errdefer gpa.free(owned_src_path); |
| 44 | 47 | ||
| 48 | const owned_name = try gpa.dupe(u8, name); | ||
| 49 | errdefer gpa.free(owned_name); | ||
| 50 | |||
| 45 | ptr.* = .{ | 51 | ptr.* = .{ |
| 46 | .root_src_directory = .{ | 52 | .root_src_directory = .{ |
| 47 | .path = owned_dir_path, | 53 | .path = owned_dir_path, |
| ... | @@ -49,6 +55,7 @@ pub fn create( | ... | @@ -49,6 +55,7 @@ pub fn create( |
| 49 | }, | 55 | }, |
| 50 | .root_src_path = owned_src_path, | 56 | .root_src_path = owned_src_path, |
| 51 | .root_src_directory_owned = true, | 57 | .root_src_directory_owned = true, |
| 58 | .name = owned_name, | ||
| 52 | }; | 59 | }; |
| 53 | 60 | ||
| 54 | return ptr; | 61 | return ptr; |
| ... | @@ -56,6 +63,7 @@ pub fn create( | ... | @@ -56,6 +63,7 @@ pub fn create( |
| 56 | 63 | ||
| 57 | pub fn createWithDir( | 64 | pub fn createWithDir( |
| 58 | gpa: Allocator, | 65 | gpa: Allocator, |
| 66 | name: []const u8, | ||
| 59 | directory: Compilation.Directory, | 67 | directory: Compilation.Directory, |
| 60 | /// Relative to `directory`. If null, means `directory` is the root src dir | 68 | /// Relative to `directory`. If null, means `directory` is the root src dir |
| 61 | /// and is owned externally. | 69 | /// and is owned externally. |
| ... | @@ -69,6 +77,9 @@ pub fn createWithDir( | ... | @@ -69,6 +77,9 @@ pub fn createWithDir( |
| 69 | const owned_src_path = try gpa.dupe(u8, root_src_path); | 77 | const owned_src_path = try gpa.dupe(u8, root_src_path); |
| 70 | errdefer gpa.free(owned_src_path); | 78 | errdefer gpa.free(owned_src_path); |
| 71 | 79 | ||
| 80 | const owned_name = try gpa.dupe(u8, name); | ||
| 81 | errdefer gpa.free(owned_name); | ||
| 82 | |||
| 72 | if (root_src_dir_path) |p| { | 83 | if (root_src_dir_path) |p| { |
| 73 | const owned_dir_path = try directory.join(gpa, &[1][]const u8{p}); | 84 | const owned_dir_path = try directory.join(gpa, &[1][]const u8{p}); |
| 74 | errdefer gpa.free(owned_dir_path); | 85 | errdefer gpa.free(owned_dir_path); |
| ... | @@ -80,12 +91,14 @@ pub fn createWithDir( | ... | @@ -80,12 +91,14 @@ pub fn createWithDir( |
| 80 | }, | 91 | }, |
| 81 | .root_src_directory_owned = true, | 92 | .root_src_directory_owned = true, |
| 82 | .root_src_path = owned_src_path, | 93 | .root_src_path = owned_src_path, |
| 94 | .name = owned_name, | ||
| 83 | }; | 95 | }; |
| 84 | } else { | 96 | } else { |
| 85 | ptr.* = .{ | 97 | ptr.* = .{ |
| 86 | .root_src_directory = directory, | 98 | .root_src_directory = directory, |
| 87 | .root_src_directory_owned = false, | 99 | .root_src_directory_owned = false, |
| 88 | .root_src_path = owned_src_path, | 100 | .root_src_path = owned_src_path, |
| 101 | .name = owned_name, | ||
| 89 | }; | 102 | }; |
| 90 | } | 103 | } |
| 91 | return ptr; | 104 | return ptr; |
| ... | @@ -95,6 +108,7 @@ pub fn createWithDir( | ... | @@ -95,6 +108,7 @@ pub fn createWithDir( |
| 95 | /// inside its table; the caller is responsible for calling destroy() on them. | 108 | /// inside its table; the caller is responsible for calling destroy() on them. |
| 96 | pub fn destroy(pkg: *Package, gpa: Allocator) void { | 109 | pub fn destroy(pkg: *Package, gpa: Allocator) void { |
| 97 | gpa.free(pkg.root_src_path); | 110 | gpa.free(pkg.root_src_path); |
| 111 | gpa.free(pkg.name); | ||
| 98 | 112 | ||
| 99 | if (pkg.root_src_directory_owned) { | 113 | if (pkg.root_src_directory_owned) { |
| 100 | // If root_src_directory.path is null then the handle is the cwd() | 114 | // If root_src_directory.path is null then the handle is the cwd() |
| ... | @@ -111,24 +125,18 @@ pub fn destroy(pkg: *Package, gpa: Allocator) void { | ... | @@ -111,24 +125,18 @@ pub fn destroy(pkg: *Package, gpa: Allocator) void { |
| 111 | 125 | ||
| 112 | /// Only frees memory associated with the table. | 126 | /// Only frees memory associated with the table. |
| 113 | pub fn deinitTable(pkg: *Package, gpa: Allocator) void { | 127 | pub fn deinitTable(pkg: *Package, gpa: Allocator) void { |
| 114 | var it = pkg.table.keyIterator(); | ||
| 115 | while (it.next()) |key| { | ||
| 116 | gpa.free(key.*); | ||
| 117 | } | ||
| 118 | |||
| 119 | pkg.table.deinit(gpa); | 128 | pkg.table.deinit(gpa); |
| 120 | } | 129 | } |
| 121 | 130 | ||
| 122 | pub fn add(pkg: *Package, gpa: Allocator, name: []const u8, package: *Package) !void { | 131 | pub fn add(pkg: *Package, gpa: Allocator, package: *Package) !void { |
| 123 | try pkg.table.ensureUnusedCapacity(gpa, 1); | 132 | try pkg.table.ensureUnusedCapacity(gpa, 1); |
| 124 | const name_dupe = try gpa.dupe(u8, name); | 133 | pkg.table.putAssumeCapacityNoClobber(package.name, package); |
| 125 | pkg.table.putAssumeCapacityNoClobber(name_dupe, package); | ||
| 126 | } | 134 | } |
| 127 | 135 | ||
| 128 | pub fn addAndAdopt(parent: *Package, gpa: Allocator, name: []const u8, child: *Package) !void { | 136 | pub fn addAndAdopt(parent: *Package, gpa: Allocator, child: *Package) !void { |
| 129 | assert(child.parent == null); // make up your mind, who is the parent?? | 137 | assert(child.parent == null); // make up your mind, who is the parent?? |
| 130 | child.parent = parent; | 138 | child.parent = parent; |
| 131 | return parent.add(gpa, name, child); | 139 | return parent.add(gpa, child); |
| 132 | } | 140 | } |
| 133 | 141 | ||
| 134 | pub const build_zig_basename = "build.zig"; | 142 | pub const build_zig_basename = "build.zig"; |
| ... | @@ -237,7 +245,7 @@ pub fn fetchAndAddDependencies( | ... | @@ -237,7 +245,7 @@ pub fn fetchAndAddDependencies( |
| 237 | sub_prefix, | 245 | sub_prefix, |
| 238 | ); | 246 | ); |
| 239 | 247 | ||
| 240 | try addAndAdopt(pkg, gpa, fqn, sub_pkg); | 248 | try addAndAdopt(pkg, gpa, sub_pkg); |
| 241 | 249 | ||
| 242 | try dependencies_source.writer().print(" pub const {s} = @import(\"{}\");\n", .{ | 250 | try dependencies_source.writer().print(" pub const {s} = @import(\"{}\");\n", .{ |
| 243 | std.zig.fmtId(fqn), std.zig.fmtEscapes(fqn), | 251 | std.zig.fmtId(fqn), std.zig.fmtEscapes(fqn), |
| ... | @@ -249,6 +257,7 @@ pub fn fetchAndAddDependencies( | ... | @@ -249,6 +257,7 @@ pub fn fetchAndAddDependencies( |
| 249 | 257 | ||
| 250 | pub fn createFilePkg( | 258 | pub fn createFilePkg( |
| 251 | gpa: Allocator, | 259 | gpa: Allocator, |
| 260 | name: []const u8, | ||
| 252 | cache_directory: Compilation.Directory, | 261 | cache_directory: Compilation.Directory, |
| 253 | basename: []const u8, | 262 | basename: []const u8, |
| 254 | contents: []const u8, | 263 | contents: []const u8, |
| ... | @@ -269,7 +278,7 @@ pub fn createFilePkg( | ... | @@ -269,7 +278,7 @@ pub fn createFilePkg( |
| 269 | const o_dir_sub_path = "o" ++ fs.path.sep_str ++ hex_digest; | 278 | const o_dir_sub_path = "o" ++ fs.path.sep_str ++ hex_digest; |
| 270 | try renameTmpIntoCache(cache_directory.handle, tmp_dir_sub_path, o_dir_sub_path); | 279 | try renameTmpIntoCache(cache_directory.handle, tmp_dir_sub_path, o_dir_sub_path); |
| 271 | 280 | ||
| 272 | return createWithDir(gpa, cache_directory, o_dir_sub_path, basename); | 281 | return createWithDir(gpa, name, cache_directory, o_dir_sub_path, basename); |
| 273 | } | 282 | } |
| 274 | 283 | ||
| 275 | fn fetchAndUnpack( | 284 | fn fetchAndUnpack( |
| ... | @@ -312,6 +321,9 @@ fn fetchAndUnpack( | ... | @@ -312,6 +321,9 @@ fn fetchAndUnpack( |
| 312 | const owned_src_path = try gpa.dupe(u8, build_zig_basename); | 321 | const owned_src_path = try gpa.dupe(u8, build_zig_basename); |
| 313 | errdefer gpa.free(owned_src_path); | 322 | errdefer gpa.free(owned_src_path); |
| 314 | 323 | ||
| 324 | const owned_name = try gpa.dupe(u8, fqn); | ||
| 325 | errdefer gpa.free(owned_name); | ||
| 326 | |||
| 315 | const build_root = try global_cache_directory.join(gpa, &.{pkg_dir_sub_path}); | 327 | const build_root = try global_cache_directory.join(gpa, &.{pkg_dir_sub_path}); |
| 316 | errdefer gpa.free(build_root); | 328 | errdefer gpa.free(build_root); |
| 317 | 329 | ||
| ... | @@ -326,6 +338,7 @@ fn fetchAndUnpack( | ... | @@ -326,6 +338,7 @@ fn fetchAndUnpack( |
| 326 | }, | 338 | }, |
| 327 | .root_src_directory_owned = true, | 339 | .root_src_directory_owned = true, |
| 328 | .root_src_path = owned_src_path, | 340 | .root_src_path = owned_src_path, |
| 341 | .name = owned_name, | ||
| 329 | }; | 342 | }; |
| 330 | 343 | ||
| 331 | return ptr; | 344 | return ptr; |
| ... | @@ -414,7 +427,7 @@ fn fetchAndUnpack( | ... | @@ -414,7 +427,7 @@ fn fetchAndUnpack( |
| 414 | std.zig.fmtId(fqn), std.zig.fmtEscapes(build_root), | 427 | std.zig.fmtId(fqn), std.zig.fmtEscapes(build_root), |
| 415 | }); | 428 | }); |
| 416 | 429 | ||
| 417 | return createWithDir(gpa, global_cache_directory, pkg_dir_sub_path, build_zig_basename); | 430 | return createWithDir(gpa, fqn, global_cache_directory, pkg_dir_sub_path, build_zig_basename); |
| 418 | } | 431 | } |
| 419 | 432 | ||
| 420 | fn reportError( | 433 | fn reportError( |
src/Sema.zig+2-14| ... | @@ -5211,6 +5211,7 @@ fn zirCImport(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index) CompileEr | ... | @@ -5211,6 +5211,7 @@ fn zirCImport(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index) CompileEr |
| 5211 | } | 5211 | } |
| 5212 | const c_import_pkg = Package.create( | 5212 | const c_import_pkg = Package.create( |
| 5213 | sema.gpa, | 5213 | sema.gpa, |
| 5214 | "c_import", // TODO: should we make this unique? | ||
| 5214 | null, | 5215 | null, |
| 5215 | c_import_res.out_zig_path, | 5216 | c_import_res.out_zig_path, |
| 5216 | ) catch |err| switch (err) { | 5217 | ) catch |err| switch (err) { |
| ... | @@ -11663,20 +11664,7 @@ fn zirImport(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air. | ... | @@ -11663,20 +11664,7 @@ fn zirImport(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air. |
| 11663 | }, | 11664 | }, |
| 11664 | error.PackageNotFound => { | 11665 | error.PackageNotFound => { |
| 11665 | const cur_pkg = block.getFileScope().pkg; | 11666 | const cur_pkg = block.getFileScope().pkg; |
| 11666 | const parent = if (cur_pkg == sema.mod.main_pkg or cur_pkg == sema.mod.root_pkg) | 11667 | return sema.fail(block, operand_src, "no package named '{s}' available within package '{s}'", .{ operand, cur_pkg.name }); |
| 11667 | "root" | ||
| 11668 | else if (cur_pkg.parent) |parent| blk: { | ||
| 11669 | var it = parent.table.iterator(); | ||
| 11670 | while (it.next()) |pkg| { | ||
| 11671 | if (pkg.value_ptr.* == cur_pkg) { | ||
| 11672 | break :blk pkg.key_ptr.*; | ||
| 11673 | } | ||
| 11674 | } | ||
| 11675 | unreachable; | ||
| 11676 | } else { | ||
| 11677 | return sema.fail(block, operand_src, "no package named '{s}' available", .{operand}); | ||
| 11678 | }; | ||
| 11679 | return sema.fail(block, operand_src, "no package named '{s}' available within package '{s}'", .{ operand, parent }); | ||
| 11680 | }, | 11668 | }, |
| 11681 | else => { | 11669 | else => { |
| 11682 | // TODO: these errors are file system errors; make sure an update() will | 11670 | // TODO: these errors are file system errors; make sure an update() will |
src/main.zig+15-10| ... | @@ -857,6 +857,7 @@ fn buildOutputType( | ... | @@ -857,6 +857,7 @@ fn buildOutputType( |
| 857 | var pkg_tree_root: Package = .{ | 857 | var pkg_tree_root: Package = .{ |
| 858 | .root_src_directory = .{ .path = null, .handle = fs.cwd() }, | 858 | .root_src_directory = .{ .path = null, .handle = fs.cwd() }, |
| 859 | .root_src_path = &[0]u8{}, | 859 | .root_src_path = &[0]u8{}, |
| 860 | .name = &[0]u8{}, | ||
| 860 | }; | 861 | }; |
| 861 | defer freePkgTree(gpa, &pkg_tree_root, false); | 862 | defer freePkgTree(gpa, &pkg_tree_root, false); |
| 862 | var cur_pkg: *Package = &pkg_tree_root; | 863 | var cur_pkg: *Package = &pkg_tree_root; |
| ... | @@ -947,6 +948,7 @@ fn buildOutputType( | ... | @@ -947,6 +948,7 @@ fn buildOutputType( |
| 947 | 948 | ||
| 948 | const new_cur_pkg = Package.create( | 949 | const new_cur_pkg = Package.create( |
| 949 | gpa, | 950 | gpa, |
| 951 | pkg_name, | ||
| 950 | fs.path.dirname(pkg_path), | 952 | fs.path.dirname(pkg_path), |
| 951 | fs.path.basename(pkg_path), | 953 | fs.path.basename(pkg_path), |
| 952 | ) catch |err| { | 954 | ) catch |err| { |
| ... | @@ -958,7 +960,7 @@ fn buildOutputType( | ... | @@ -958,7 +960,7 @@ fn buildOutputType( |
| 958 | } else if (cur_pkg.table.get(pkg_name)) |prev| { | 960 | } else if (cur_pkg.table.get(pkg_name)) |prev| { |
| 959 | fatal("unable to add package '{s}' -> '{s}': already exists as '{s}", .{ pkg_name, pkg_path, prev.root_src_path }); | 961 | fatal("unable to add package '{s}' -> '{s}': already exists as '{s}", .{ pkg_name, pkg_path, prev.root_src_path }); |
| 960 | } | 962 | } |
| 961 | try cur_pkg.addAndAdopt(gpa, pkg_name, new_cur_pkg); | 963 | try cur_pkg.addAndAdopt(gpa, new_cur_pkg); |
| 962 | cur_pkg = new_cur_pkg; | 964 | cur_pkg = new_cur_pkg; |
| 963 | } else if (mem.eql(u8, arg, "--pkg-end")) { | 965 | } else if (mem.eql(u8, arg, "--pkg-end")) { |
| 964 | cur_pkg = cur_pkg.parent orelse | 966 | cur_pkg = cur_pkg.parent orelse |
| ... | @@ -2841,14 +2843,14 @@ fn buildOutputType( | ... | @@ -2841,14 +2843,14 @@ fn buildOutputType( |
| 2841 | if (main_pkg_path) |unresolved_main_pkg_path| { | 2843 | if (main_pkg_path) |unresolved_main_pkg_path| { |
| 2842 | const p = try introspect.resolvePath(arena, unresolved_main_pkg_path); | 2844 | const p = try introspect.resolvePath(arena, unresolved_main_pkg_path); |
| 2843 | if (p.len == 0) { | 2845 | if (p.len == 0) { |
| 2844 | break :blk try Package.create(gpa, null, src_path); | 2846 | break :blk try Package.create(gpa, "root", null, src_path); |
| 2845 | } else { | 2847 | } else { |
| 2846 | const rel_src_path = try fs.path.relative(arena, p, src_path); | 2848 | const rel_src_path = try fs.path.relative(arena, p, src_path); |
| 2847 | break :blk try Package.create(gpa, p, rel_src_path); | 2849 | break :blk try Package.create(gpa, "root", p, rel_src_path); |
| 2848 | } | 2850 | } |
| 2849 | } else { | 2851 | } else { |
| 2850 | const root_src_dir_path = fs.path.dirname(src_path); | 2852 | const root_src_dir_path = fs.path.dirname(src_path); |
| 2851 | break :blk Package.create(gpa, root_src_dir_path, fs.path.basename(src_path)) catch |err| { | 2853 | break :blk Package.create(gpa, "root", root_src_dir_path, fs.path.basename(src_path)) catch |err| { |
| 2852 | if (root_src_dir_path) |p| { | 2854 | if (root_src_dir_path) |p| { |
| 2853 | fatal("unable to open '{s}': {s}", .{ p, @errorName(err) }); | 2855 | fatal("unable to open '{s}': {s}", .{ p, @errorName(err) }); |
| 2854 | } else { | 2856 | } else { |
| ... | @@ -4093,6 +4095,7 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi | ... | @@ -4093,6 +4095,7 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi |
| 4093 | var main_pkg: Package = .{ | 4095 | var main_pkg: Package = .{ |
| 4094 | .root_src_directory = zig_lib_directory, | 4096 | .root_src_directory = zig_lib_directory, |
| 4095 | .root_src_path = "build_runner.zig", | 4097 | .root_src_path = "build_runner.zig", |
| 4098 | .name = "root", | ||
| 4096 | }; | 4099 | }; |
| 4097 | 4100 | ||
| 4098 | if (!build_options.omit_pkg_fetching_code) { | 4101 | if (!build_options.omit_pkg_fetching_code) { |
| ... | @@ -4133,20 +4136,22 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi | ... | @@ -4133,20 +4136,22 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi |
| 4133 | 4136 | ||
| 4134 | const deps_pkg = try Package.createFilePkg( | 4137 | const deps_pkg = try Package.createFilePkg( |
| 4135 | gpa, | 4138 | gpa, |
| 4139 | "@dependencies", | ||
| 4136 | local_cache_directory, | 4140 | local_cache_directory, |
| 4137 | "dependencies.zig", | 4141 | "dependencies.zig", |
| 4138 | dependencies_source.items, | 4142 | dependencies_source.items, |
| 4139 | ); | 4143 | ); |
| 4140 | 4144 | ||
| 4141 | mem.swap(Package.Table, &main_pkg.table, &deps_pkg.table); | 4145 | mem.swap(Package.Table, &main_pkg.table, &deps_pkg.table); |
| 4142 | try main_pkg.addAndAdopt(gpa, "@dependencies", deps_pkg); | 4146 | try main_pkg.addAndAdopt(gpa, deps_pkg); |
| 4143 | } | 4147 | } |
| 4144 | 4148 | ||
| 4145 | var build_pkg: Package = .{ | 4149 | var build_pkg: Package = .{ |
| 4146 | .root_src_directory = build_directory, | 4150 | .root_src_directory = build_directory, |
| 4147 | .root_src_path = build_zig_basename, | 4151 | .root_src_path = build_zig_basename, |
| 4152 | .name = "@build", | ||
| 4148 | }; | 4153 | }; |
| 4149 | try main_pkg.addAndAdopt(gpa, "@build", &build_pkg); | 4154 | try main_pkg.addAndAdopt(gpa, &build_pkg); |
| 4150 | 4155 | ||
| 4151 | const comp = Compilation.create(gpa, .{ | 4156 | const comp = Compilation.create(gpa, .{ |
| 4152 | .zig_lib_directory = zig_lib_directory, | 4157 | .zig_lib_directory = zig_lib_directory, |
| ... | @@ -4381,7 +4386,7 @@ pub fn cmdFmt(gpa: Allocator, arena: Allocator, args: []const []const u8) !void | ... | @@ -4381,7 +4386,7 @@ pub fn cmdFmt(gpa: Allocator, arena: Allocator, args: []const []const u8) !void |
| 4381 | .root_decl = .none, | 4386 | .root_decl = .none, |
| 4382 | }; | 4387 | }; |
| 4383 | 4388 | ||
| 4384 | file.pkg = try Package.create(gpa, null, file.sub_file_path); | 4389 | file.pkg = try Package.create(gpa, "root", null, file.sub_file_path); |
| 4385 | defer file.pkg.destroy(gpa); | 4390 | defer file.pkg.destroy(gpa); |
| 4386 | 4391 | ||
| 4387 | file.zir = try AstGen.generate(gpa, file.tree); | 4392 | file.zir = try AstGen.generate(gpa, file.tree); |
| ... | @@ -4591,7 +4596,7 @@ fn fmtPathFile( | ... | @@ -4591,7 +4596,7 @@ fn fmtPathFile( |
| 4591 | .root_decl = .none, | 4596 | .root_decl = .none, |
| 4592 | }; | 4597 | }; |
| 4593 | 4598 | ||
| 4594 | file.pkg = try Package.create(fmt.gpa, null, file.sub_file_path); | 4599 | file.pkg = try Package.create(fmt.gpa, "root", null, file.sub_file_path); |
| 4595 | defer file.pkg.destroy(fmt.gpa); | 4600 | defer file.pkg.destroy(fmt.gpa); |
| 4596 | 4601 | ||
| 4597 | if (stat.size > max_src_size) | 4602 | if (stat.size > max_src_size) |
| ... | @@ -5303,7 +5308,7 @@ pub fn cmdAstCheck( | ... | @@ -5303,7 +5308,7 @@ pub fn cmdAstCheck( |
| 5303 | file.stat.size = source.len; | 5308 | file.stat.size = source.len; |
| 5304 | } | 5309 | } |
| 5305 | 5310 | ||
| 5306 | file.pkg = try Package.create(gpa, null, file.sub_file_path); | 5311 | file.pkg = try Package.create(gpa, "root", null, file.sub_file_path); |
| 5307 | defer file.pkg.destroy(gpa); | 5312 | defer file.pkg.destroy(gpa); |
| 5308 | 5313 | ||
| 5309 | file.tree = try std.zig.parse(gpa, file.source); | 5314 | file.tree = try std.zig.parse(gpa, file.source); |
| ... | @@ -5422,7 +5427,7 @@ pub fn cmdChangelist( | ... | @@ -5422,7 +5427,7 @@ pub fn cmdChangelist( |
| 5422 | .root_decl = .none, | 5427 | .root_decl = .none, |
| 5423 | }; | 5428 | }; |
| 5424 | 5429 | ||
| 5425 | file.pkg = try Package.create(gpa, null, file.sub_file_path); | 5430 | file.pkg = try Package.create(gpa, "root", null, file.sub_file_path); |
| 5426 | defer file.pkg.destroy(gpa); | 5431 | defer file.pkg.destroy(gpa); |
| 5427 | 5432 | ||
| 5428 | const source = try arena.allocSentinel(u8, @intCast(usize, stat.size), 0); | 5433 | const source = try arena.allocSentinel(u8, @intCast(usize, stat.size), 0); |
src/test.zig+1| ... | @@ -1497,6 +1497,7 @@ pub const TestContext = struct { | ... | @@ -1497,6 +1497,7 @@ pub const TestContext = struct { |
| 1497 | var main_pkg: Package = .{ | 1497 | var main_pkg: Package = .{ |
| 1498 | .root_src_directory = .{ .path = tmp_dir_path, .handle = tmp.dir }, | 1498 | .root_src_directory = .{ .path = tmp_dir_path, .handle = tmp.dir }, |
| 1499 | .root_src_path = tmp_src_path, | 1499 | .root_src_path = tmp_src_path, |
| 1500 | .name = "root", | ||
| 1500 | }; | 1501 | }; |
| 1501 | defer main_pkg.table.deinit(allocator); | 1502 | defer main_pkg.table.deinit(allocator); |
| 1502 | 1503 |