authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2023-04-22 18:15:19+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-05-18 00:27:21-07:00
logf65e8c78621c90c9b9932903a9ed99c973dbcf63
tree027dc7bdf661ae06bc3f75fb14fd71d5c394a511
parent378264d404e71da878f9e6934c045ad64193877f

Deduplicate uses of the same package across dependencies


2 files changed, 26 insertions(+), 7 deletions(-)

lib/std/Build.zig+16
...@@ -124,6 +124,9 @@ host: NativeTargetInfo,...@@ -124,6 +124,9 @@ host: NativeTargetInfo,
124dep_prefix: []const u8 = "",124dep_prefix: []const u8 = "",
125125
126modules: std.StringArrayHashMap(*Module),126modules: std.StringArrayHashMap(*Module),
127/// A map from build root dirs to the corresponding `*Dependency`. This is shared with all child
128/// `Build`s.
129initialized_deps: *std.StringHashMap(*Dependency),
127130
128pub const ExecError = error{131pub const ExecError = error{
129 ReadFailure,132 ReadFailure,
...@@ -209,6 +212,9 @@ pub fn create(...@@ -209,6 +212,9 @@ pub fn create(
209 const env_map = try allocator.create(EnvMap);212 const env_map = try allocator.create(EnvMap);
210 env_map.* = try process.getEnvMap(allocator);213 env_map.* = try process.getEnvMap(allocator);
211214
215 const initialized_deps = try allocator.create(std.StringHashMap(*Dependency));
216 initialized_deps.* = std.StringHashMap(*Dependency).init(allocator);
217
212 const self = try allocator.create(Build);218 const self = try allocator.create(Build);
213 self.* = .{219 self.* = .{
214 .zig_exe = zig_exe,220 .zig_exe = zig_exe,
...@@ -261,6 +267,7 @@ pub fn create(...@@ -261,6 +267,7 @@ pub fn create(
261 .args = null,267 .args = null,
262 .host = host,268 .host = host,
263 .modules = std.StringArrayHashMap(*Module).init(allocator),269 .modules = std.StringArrayHashMap(*Module).init(allocator),
270 .initialized_deps = initialized_deps,
264 };271 };
265 try self.top_level_steps.put(allocator, self.install_tls.step.name, &self.install_tls);272 try self.top_level_steps.put(allocator, self.install_tls.step.name, &self.install_tls);
266 try self.top_level_steps.put(allocator, self.uninstall_tls.step.name, &self.uninstall_tls);273 try self.top_level_steps.put(allocator, self.uninstall_tls.step.name, &self.uninstall_tls);
...@@ -345,6 +352,7 @@ fn createChildOnly(parent: *Build, dep_name: []const u8, build_root: Cache.Direc...@@ -345,6 +352,7 @@ fn createChildOnly(parent: *Build, dep_name: []const u8, build_root: Cache.Direc
345 .host = parent.host,352 .host = parent.host,
346 .dep_prefix = parent.fmt("{s}{s}.", .{ parent.dep_prefix, dep_name }),353 .dep_prefix = parent.fmt("{s}{s}.", .{ parent.dep_prefix, dep_name }),
347 .modules = std.StringArrayHashMap(*Module).init(allocator),354 .modules = std.StringArrayHashMap(*Module).init(allocator),
355 .initialized_deps = parent.initialized_deps,
348 };356 };
349 try child.top_level_steps.put(allocator, child.install_tls.step.name, &child.install_tls);357 try child.top_level_steps.put(allocator, child.install_tls.step.name, &child.install_tls);
350 try child.top_level_steps.put(allocator, child.uninstall_tls.step.name, &child.uninstall_tls);358 try child.top_level_steps.put(allocator, child.uninstall_tls.step.name, &child.uninstall_tls);
...@@ -1560,6 +1568,11 @@ pub fn dependencyInner(...@@ -1560,6 +1568,11 @@ pub fn dependencyInner(
1560 comptime build_zig: type,1568 comptime build_zig: type,
1561 args: anytype,1569 args: anytype,
1562) *Dependency {1570) *Dependency {
1571 if (b.initialized_deps.get(build_root_string)) |dep| {
1572 // TODO: check args are the same
1573 return dep;
1574 }
1575
1563 const build_root: std.Build.Cache.Directory = .{1576 const build_root: std.Build.Cache.Directory = .{
1564 .path = build_root_string,1577 .path = build_root_string,
1565 .handle = std.fs.cwd().openDir(build_root_string, .{}) catch |err| {1578 .handle = std.fs.cwd().openDir(build_root_string, .{}) catch |err| {
...@@ -1578,6 +1591,9 @@ pub fn dependencyInner(...@@ -1578,6 +1591,9 @@ pub fn dependencyInner(
15781591
1579 const dep = b.allocator.create(Dependency) catch @panic("OOM");1592 const dep = b.allocator.create(Dependency) catch @panic("OOM");
1580 dep.* = .{ .builder = sub_builder };1593 dep.* = .{ .builder = sub_builder };
1594
1595 b.initialized_deps.put(build_root_string, dep) catch @panic("OOM");
1596
1581 return dep;1597 return dep;
1582}1598}
15831599
src/Package.zig+10-7
...@@ -216,7 +216,7 @@ pub const build_zig_basename = "build.zig";...@@ -216,7 +216,7 @@ pub const build_zig_basename = "build.zig";
216216
217pub fn fetchAndAddDependencies(217pub fn fetchAndAddDependencies(
218 pkg: *Package,218 pkg: *Package,
219 root_pkg: *Package,219 deps_pkg: *Package,
220 arena: Allocator,220 arena: Allocator,
221 thread_pool: *ThreadPool,221 thread_pool: *ThreadPool,
222 http_client: *std.http.Client,222 http_client: *std.http.Client,
...@@ -272,7 +272,6 @@ pub fn fetchAndAddDependencies(...@@ -272,7 +272,6 @@ pub fn fetchAndAddDependencies(
272 .error_bundle = error_bundle,272 .error_bundle = error_bundle,
273 };273 };
274274
275 var any_error = false;
276 const deps_list = manifest.dependencies.values();275 const deps_list = manifest.dependencies.values();
277 for (manifest.dependencies.keys(), 0..) |name, i| {276 for (manifest.dependencies.keys(), 0..) |name, i| {
278 const dep = deps_list[i];277 const dep = deps_list[i];
...@@ -292,7 +291,7 @@ pub fn fetchAndAddDependencies(...@@ -292,7 +291,7 @@ pub fn fetchAndAddDependencies(
292 );291 );
293292
294 try sub_pkg.fetchAndAddDependencies(293 try sub_pkg.fetchAndAddDependencies(
295 root_pkg,294 deps_pkg,
296 arena,295 arena,
297 thread_pool,296 thread_pool,
298 http_client,297 http_client,
...@@ -307,14 +306,18 @@ pub fn fetchAndAddDependencies(...@@ -307,14 +306,18 @@ pub fn fetchAndAddDependencies(
307 );306 );
308307
309 try pkg.add(gpa, name, sub_pkg);308 try pkg.add(gpa, name, sub_pkg);
310 try root_pkg.add(gpa, fqn, sub_pkg);309 if (deps_pkg.table.get(dep.hash.?)) |other_sub| {
310 // This should be the same package (and hence module) since it's the same hash
311 // TODO: dedup multiple versions of the same package
312 assert(other_sub == sub_pkg);
313 } else {
314 try deps_pkg.add(gpa, dep.hash.?, sub_pkg);
315 }
311316
312 try dependencies_source.writer().print(" pub const {s} = @import(\"{}\");\n", .{317 try dependencies_source.writer().print(" pub const {s} = @import(\"{}\");\n", .{
313 std.zig.fmtId(fqn), std.zig.fmtEscapes(fqn),318 std.zig.fmtId(fqn), std.zig.fmtEscapes(dep.hash.?),
314 });319 });
315 }320 }
316
317 if (any_error) return error.InvalidBuildManifestFile;
318}321}
319322
320pub fn createFilePkg(323pub fn createFilePkg(