authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-10-08 11:23:20-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-10-08 16:54:31-07:00
log29156de12022732cb90b5bc5f5a0e33bf25d894b
tree5ccc13a3c5e84f140430771a7dfb458e48bc7558
parenta605bd9887a8a4220e177e42259809e0680d4221

CLI: fix only_core_functionality logic


2 files changed, 67 insertions(+), 42 deletions(-)

src/Package/Fetch.zig+5-5
...@@ -105,13 +105,13 @@ pub const JobQueue = struct {...@@ -105,13 +105,13 @@ pub const JobQueue = struct {
105 }105 }
106 }106 }
107107
108 /// Creates the dependencies.zig file and corresponding `Module` for the108 /// Creates the dependencies.zig source code for the build runner to obtain
109 /// build runner to obtain via `@import("@dependencies")`.109 /// via `@import("@dependencies")`.
110 pub fn createDependenciesModule(jq: *JobQueue, buf: *std.ArrayList(u8)) Allocator.Error!void {110 pub fn createDependenciesSource(jq: *JobQueue, buf: *std.ArrayList(u8)) Allocator.Error!void {
111 const keys = jq.table.keys();111 const keys = jq.table.keys();
112112
113 if (keys.len == 0)113 if (keys.len == 0)
114 return createEmptyDependenciesModule(buf);114 return createEmptyDependenciesSource(buf);
115115
116 try buf.appendSlice("pub const packages = struct {\n");116 try buf.appendSlice("pub const packages = struct {\n");
117117
...@@ -188,7 +188,7 @@ pub const JobQueue = struct {...@@ -188,7 +188,7 @@ pub const JobQueue = struct {
188 try buf.appendSlice("};\n");188 try buf.appendSlice("};\n");
189 }189 }
190190
191 pub fn createEmptyDependenciesModule(buf: *std.ArrayList(u8)) Allocator.Error!void {191 pub fn createEmptyDependenciesSource(buf: *std.ArrayList(u8)) Allocator.Error!void {
192 try buf.appendSlice(192 try buf.appendSlice(
193 \\pub const packages = struct {};193 \\pub const packages = struct {};
194 \\pub const root_deps: []const struct { []const u8, []const u8 } = &.{};194 \\pub const root_deps: []const struct { []const u8, []const u8 } = &.{};
src/main.zig+62-37
...@@ -4831,10 +4831,8 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi...@@ -4831,10 +4831,8 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi
4831 .root = .{ .root_dir = build_root },4831 .root = .{ .root_dir = build_root },
4832 .root_src_path = build_zig_basename,4832 .root_src_path = build_zig_basename,
4833 };4833 };
4834 var dependencies_zig_src = std.ArrayList(u8).init(gpa);
4835 defer dependencies_zig_src.deinit();
4836 if (build_options.only_core_functionality) {4834 if (build_options.only_core_functionality) {
4837 try Package.Fetch.createEmptyDependenciesModule(&dependencies_zig_src);4835 try createEmptyDependenciesModule(arena, &main_mod, local_cache_directory);
4838 } else {4836 } else {
4839 var http_client: std.http.Client = .{ .allocator = gpa };4837 var http_client: std.http.Client = .{ .allocator = gpa };
4840 defer http_client.deinit();4838 defer http_client.deinit();
...@@ -4890,40 +4888,16 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi...@@ -4890,40 +4888,16 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi
48904888
4891 if (fetch_only) return cleanExit();4889 if (fetch_only) return cleanExit();
48924890
4893 try job_queue.createDependenciesModule(&dependencies_zig_src);4891 var source_buf = std.ArrayList(u8).init(gpa);
48944892 defer source_buf.deinit();
4895 const deps_mod = m: {4893 try job_queue.createDependenciesSource(&source_buf);
4896 // Atomically create the file in a directory named after the hash of its contents.4894 const deps_mod = try createDependenciesModule(
4897 const basename = "dependencies.zig";4895 arena,
4898 const rand_int = std.crypto.random.int(u64);4896 source_buf.items,
4899 const tmp_dir_sub_path = "tmp" ++ fs.path.sep_str ++4897 &main_mod,
4900 Package.Manifest.hex64(rand_int);4898 local_cache_directory,
4901 {4899 );
4902 var tmp_dir = try local_cache_directory.handle.makeOpenPath(tmp_dir_sub_path, .{});
4903 defer tmp_dir.close();
4904 try tmp_dir.writeFile(basename, dependencies_zig_src.items);
4905 }
49064900
4907 var hh: Cache.HashHelper = .{};
4908 hh.addBytes(build_options.version);
4909 hh.addBytes(dependencies_zig_src.items);
4910 const hex_digest = hh.final();
4911
4912 const o_dir_sub_path = try arena.dupe(u8, "o" ++ fs.path.sep_str ++ hex_digest);
4913 try Package.Fetch.renameTmpIntoCache(
4914 local_cache_directory.handle,
4915 tmp_dir_sub_path,
4916 o_dir_sub_path,
4917 );
4918
4919 break :m try Package.Module.create(arena, .{
4920 .root = .{
4921 .root_dir = local_cache_directory,
4922 .sub_path = o_dir_sub_path,
4923 },
4924 .root_src_path = basename,
4925 });
4926 };
4927 {4901 {
4928 // We need a Module for each package's build.zig.4902 // We need a Module for each package's build.zig.
4929 const hashes = job_queue.table.keys();4903 const hashes = job_queue.table.keys();
...@@ -4944,7 +4918,6 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi...@@ -4944,7 +4918,6 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi
4944 deps_mod.deps.putAssumeCapacityNoClobber(hash_cloned, m);4918 deps_mod.deps.putAssumeCapacityNoClobber(hash_cloned, m);
4945 }4919 }
4946 }4920 }
4947 try main_mod.deps.put(arena, "@dependencies", deps_mod);
4948 }4921 }
49494922
4950 try main_mod.deps.put(arena, "@build", &build_mod);4923 try main_mod.deps.put(arena, "@build", &build_mod);
...@@ -6795,3 +6768,55 @@ fn cmdFetch(...@@ -6795,3 +6768,55 @@ fn cmdFetch(
67956768
6796 return cleanExit();6769 return cleanExit();
6797}6770}
6771
6772fn createEmptyDependenciesModule(
6773 arena: Allocator,
6774 main_mod: *Package.Module,
6775 local_cache_directory: Cache.Directory,
6776) !void {
6777 var source = std.ArrayList(u8).init(arena);
6778 try Package.Fetch.JobQueue.createEmptyDependenciesSource(&source);
6779 _ = try createDependenciesModule(arena, source.items, main_mod, local_cache_directory);
6780}
6781
6782/// Creates the dependencies.zig file and corresponding `Package.Module` for the
6783/// build runner to obtain via `@import("@dependencies")`.
6784fn createDependenciesModule(
6785 arena: Allocator,
6786 source: []const u8,
6787 main_mod: *Package.Module,
6788 local_cache_directory: Cache.Directory,
6789) !*Package.Module {
6790 // Atomically create the file in a directory named after the hash of its contents.
6791 const basename = "dependencies.zig";
6792 const rand_int = std.crypto.random.int(u64);
6793 const tmp_dir_sub_path = "tmp" ++ fs.path.sep_str ++
6794 Package.Manifest.hex64(rand_int);
6795 {
6796 var tmp_dir = try local_cache_directory.handle.makeOpenPath(tmp_dir_sub_path, .{});
6797 defer tmp_dir.close();
6798 try tmp_dir.writeFile(basename, source);
6799 }
6800
6801 var hh: Cache.HashHelper = .{};
6802 hh.addBytes(build_options.version);
6803 hh.addBytes(source);
6804 const hex_digest = hh.final();
6805
6806 const o_dir_sub_path = try arena.dupe(u8, "o" ++ fs.path.sep_str ++ hex_digest);
6807 try Package.Fetch.renameTmpIntoCache(
6808 local_cache_directory.handle,
6809 tmp_dir_sub_path,
6810 o_dir_sub_path,
6811 );
6812
6813 const deps_mod = try Package.Module.create(arena, .{
6814 .root = .{
6815 .root_dir = local_cache_directory,
6816 .sub_path = o_dir_sub_path,
6817 },
6818 .root_src_path = basename,
6819 });
6820 try main_mod.deps.put(arena, "@dependencies", deps_mod);
6821 return deps_mod;
6822}