| author | |
| committer | |
| log | e5c2a7dbca103b0c61bebaff95c5d5a5b777bd59 |
| tree | 2f41df7e7c01286ef102dc7a814aae6c7cdf5f84 |
| parent | 35d81c99c0f737f071e1ad0083342328dbb32acb |
* add Module instances for each package's build.zig and attach it to the
dependencies.zig module with the hash digest hex string as the name.
* fix incorrectly skipping the wrong packages for creating
dependencies.zig
* a couple more renaming of "package" to "module"6 files changed, 77 insertions(+), 39 deletions(-)
lib/std/Build/Cache.zig+7| ... | ... | @@ -9,6 +9,13 @@ pub const Directory = struct { |
| 9 | 9 | path: ?[]const u8, |
| 10 | 10 | handle: fs.Dir, |
| 11 | 11 | |
| 12 | pub fn clone(d: Directory, arena: Allocator) Allocator.Error!Directory { | |
| 13 | return .{ | |
| 14 | .path = if (d.path) |p| try arena.dupe(u8, p) else null, | |
| 15 | .handle = d.handle, | |
| 16 | }; | |
| 17 | } | |
| 18 | ||
| 12 | 19 | pub fn cwd() Directory { |
| 13 | 20 | return .{ |
| 14 | 21 | .path = null, |
src/Module.zig+3-3| ... | ... | @@ -4074,7 +4074,7 @@ pub fn importFile( |
| 4074 | 4074 | return mod.importPkg(pkg); |
| 4075 | 4075 | } |
| 4076 | 4076 | if (!mem.endsWith(u8, import_string, ".zig")) { |
| 4077 | return error.PackageNotFound; | |
| 4077 | return error.ModuleNotFound; | |
| 4078 | 4078 | } |
| 4079 | 4079 | const gpa = mod.gpa; |
| 4080 | 4080 | |
| ... | ... | @@ -4120,7 +4120,7 @@ pub fn importFile( |
| 4120 | 4120 | { |
| 4121 | 4121 | break :p try gpa.dupe(u8, resolved_path); |
| 4122 | 4122 | } |
| 4123 | return error.ImportOutsidePkgPath; | |
| 4123 | return error.ImportOutsideModulePath; | |
| 4124 | 4124 | }; |
| 4125 | 4125 | errdefer gpa.free(sub_file_path); |
| 4126 | 4126 | |
| ... | ... | @@ -4206,7 +4206,7 @@ pub fn embedFile(mod: *Module, cur_file: *File, import_string: []const u8) !*Emb |
| 4206 | 4206 | { |
| 4207 | 4207 | break :p try gpa.dupe(u8, resolved_path); |
| 4208 | 4208 | } |
| 4209 | return error.ImportOutsidePkgPath; | |
| 4209 | return error.ImportOutsideModulePath; | |
| 4210 | 4210 | }; |
| 4211 | 4211 | errdefer gpa.free(sub_file_path); |
| 4212 | 4212 |
src/Package.zig+7| ... | ... | @@ -9,6 +9,13 @@ pub const Path = struct { |
| 9 | 9 | /// Empty string means the root_dir is the path. |
| 10 | 10 | sub_path: []const u8 = "", |
| 11 | 11 | |
| 12 | pub fn clone(p: Path, arena: Allocator) Allocator.Error!Path { | |
| 13 | return .{ | |
| 14 | .root_dir = try p.root_dir.clone(arena), | |
| 15 | .sub_path = try arena.dupe(u8, p.sub_path), | |
| 16 | }; | |
| 17 | } | |
| 18 | ||
| 12 | 19 | pub fn cwd() Path { |
| 13 | 20 | return .{ .root_dir = Cache.Directory.cwd() }; |
| 14 | 21 | } |
src/Package/Fetch.zig+5-2| ... | ... | @@ -109,7 +109,6 @@ pub const JobQueue = struct { |
| 109 | 109 | /// build runner to obtain via `@import("@dependencies")`. |
| 110 | 110 | pub fn createDependenciesModule(jq: *JobQueue, buf: *std.ArrayList(u8)) Allocator.Error!void { |
| 111 | 111 | const keys = jq.table.keys(); |
| 112 | const values = jq.table.values(); | |
| 113 | 112 | |
| 114 | 113 | if (keys.len == 0) |
| 115 | 114 | return createEmptyDependenciesModule(buf); |
| ... | ... | @@ -124,7 +123,11 @@ pub const JobQueue = struct { |
| 124 | 123 | } |
| 125 | 124 | }, .{ .keys = keys })); |
| 126 | 125 | |
| 127 | for (keys[1..], values[1..]) |hash, fetch| { | |
| 126 | for (keys, jq.table.values()) |hash, fetch| { | |
| 127 | if (fetch == jq.all_fetches.items[0]) { | |
| 128 | // The first one is a dummy package for the current project. | |
| 129 | continue; | |
| 130 | } | |
| 128 | 131 | try buf.writer().print( |
| 129 | 132 | \\ pub const {} = struct {{ |
| 130 | 133 | \\ pub const build_root = "{q}"; |
src/Sema.zig+5-5| ... | ... | @@ -13075,13 +13075,13 @@ fn zirImport(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air. |
| 13075 | 13075 | const operand = inst_data.get(sema.code); |
| 13076 | 13076 | |
| 13077 | 13077 | const result = mod.importFile(block.getFileScope(mod), operand) catch |err| switch (err) { |
| 13078 | error.ImportOutsidePkgPath => { | |
| 13079 | return sema.fail(block, operand_src, "import of file outside package path: '{s}'", .{operand}); | |
| 13078 | error.ImportOutsideModulePath => { | |
| 13079 | return sema.fail(block, operand_src, "import of file outside module path: '{s}'", .{operand}); | |
| 13080 | 13080 | }, |
| 13081 | error.PackageNotFound => { | |
| 13081 | error.ModuleNotFound => { | |
| 13082 | 13082 | //const name = try block.getFileScope(mod).mod.getName(sema.gpa, mod.*); |
| 13083 | 13083 | //defer sema.gpa.free(name); |
| 13084 | return sema.fail(block, operand_src, "no package named '{s}' available within package '{}'", .{ | |
| 13084 | return sema.fail(block, operand_src, "no module named '{s}' available within module '{}'", .{ | |
| 13085 | 13085 | operand, block.getFileScope(mod).mod.root, |
| 13086 | 13086 | }); |
| 13087 | 13087 | }, |
| ... | ... | @@ -13112,7 +13112,7 @@ fn zirEmbedFile(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A |
| 13112 | 13112 | } |
| 13113 | 13113 | |
| 13114 | 13114 | const embed_file = mod.embedFile(block.getFileScope(mod), name) catch |err| switch (err) { |
| 13115 | error.ImportOutsidePkgPath => { | |
| 13115 | error.ImportOutsideModulePath => { | |
| 13116 | 13116 | return sema.fail(block, operand_src, "embed of file outside package path: '{s}'", .{name}); |
| 13117 | 13117 | }, |
| 13118 | 13118 | else => { |
src/main.zig+50-29| ... | ... | @@ -4885,39 +4885,60 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi |
| 4885 | 4885 | } |
| 4886 | 4886 | |
| 4887 | 4887 | try job_queue.createDependenciesModule(&dependencies_zig_src); |
| 4888 | } | |
| 4889 | { | |
| 4890 | // Atomically create the file in a directory named after the hash of its contents. | |
| 4891 | const basename = "dependencies.zig"; | |
| 4892 | const rand_int = std.crypto.random.int(u64); | |
| 4893 | const tmp_dir_sub_path = "tmp" ++ fs.path.sep_str ++ Package.Manifest.hex64(rand_int); | |
| 4894 | { | |
| 4895 | var tmp_dir = try local_cache_directory.handle.makeOpenPath(tmp_dir_sub_path, .{}); | |
| 4896 | defer tmp_dir.close(); | |
| 4897 | try tmp_dir.writeFile(basename, dependencies_zig_src.items); | |
| 4898 | } | |
| 4899 | ||
| 4900 | var hh: Cache.HashHelper = .{}; | |
| 4901 | hh.addBytes(build_options.version); | |
| 4902 | hh.addBytes(dependencies_zig_src.items); | |
| 4903 | const hex_digest = hh.final(); | |
| 4904 | 4888 | |
| 4905 | const o_dir_sub_path = try arena.dupe(u8, "o" ++ fs.path.sep_str ++ hex_digest); | |
| 4906 | try Package.Fetch.renameTmpIntoCache( | |
| 4907 | local_cache_directory.handle, | |
| 4908 | tmp_dir_sub_path, | |
| 4909 | o_dir_sub_path, | |
| 4910 | ); | |
| 4889 | const deps_mod = m: { | |
| 4890 | // Atomically create the file in a directory named after the hash of its contents. | |
| 4891 | const basename = "dependencies.zig"; | |
| 4892 | const rand_int = std.crypto.random.int(u64); | |
| 4893 | const tmp_dir_sub_path = "tmp" ++ fs.path.sep_str ++ | |
| 4894 | Package.Manifest.hex64(rand_int); | |
| 4895 | { | |
| 4896 | var tmp_dir = try local_cache_directory.handle.makeOpenPath(tmp_dir_sub_path, .{}); | |
| 4897 | defer tmp_dir.close(); | |
| 4898 | try tmp_dir.writeFile(basename, dependencies_zig_src.items); | |
| 4899 | } | |
| 4911 | 4900 | |
| 4912 | const deps_mod = try Package.Module.create(arena, .{ | |
| 4913 | .root = .{ | |
| 4914 | .root_dir = local_cache_directory, | |
| 4915 | .sub_path = o_dir_sub_path, | |
| 4916 | }, | |
| 4917 | .root_src_path = basename, | |
| 4918 | }); | |
| 4901 | var hh: Cache.HashHelper = .{}; | |
| 4902 | hh.addBytes(build_options.version); | |
| 4903 | hh.addBytes(dependencies_zig_src.items); | |
| 4904 | const hex_digest = hh.final(); | |
| 4905 | ||
| 4906 | const o_dir_sub_path = try arena.dupe(u8, "o" ++ fs.path.sep_str ++ hex_digest); | |
| 4907 | try Package.Fetch.renameTmpIntoCache( | |
| 4908 | local_cache_directory.handle, | |
| 4909 | tmp_dir_sub_path, | |
| 4910 | o_dir_sub_path, | |
| 4911 | ); | |
| 4912 | ||
| 4913 | break :m try Package.Module.create(arena, .{ | |
| 4914 | .root = .{ | |
| 4915 | .root_dir = local_cache_directory, | |
| 4916 | .sub_path = o_dir_sub_path, | |
| 4917 | }, | |
| 4918 | .root_src_path = basename, | |
| 4919 | }); | |
| 4920 | }; | |
| 4921 | { | |
| 4922 | // We need a Module for each package's build.zig. | |
| 4923 | const hashes = job_queue.table.keys(); | |
| 4924 | const fetches = job_queue.table.values(); | |
| 4925 | try deps_mod.deps.ensureUnusedCapacity(arena, @intCast(hashes.len)); | |
| 4926 | for (hashes, fetches) |hash, f| { | |
| 4927 | if (f == &fetch) { | |
| 4928 | // The first one is a dummy package for the current project. | |
| 4929 | continue; | |
| 4930 | } | |
| 4931 | const m = try Package.Module.create(arena, .{ | |
| 4932 | .root = try f.package_root.clone(arena), | |
| 4933 | .root_src_path = if (f.has_build_zig) Package.build_zig_basename else "", | |
| 4934 | }); | |
| 4935 | const hash_cloned = try arena.dupe(u8, &hash); | |
| 4936 | deps_mod.deps.putAssumeCapacityNoClobber(hash_cloned, m); | |
| 4937 | } | |
| 4938 | } | |
| 4919 | 4939 | try main_mod.deps.put(arena, "@dependencies", deps_mod); |
| 4920 | 4940 | } |
| 4941 | ||
| 4921 | 4942 | try main_mod.deps.put(arena, "@build", &build_mod); |
| 4922 | 4943 | |
| 4923 | 4944 | const comp = Compilation.create(gpa, .{ |