From f2856403c6997ff1317c968abed0871df9586c7c Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Wed, 10 Jul 2024 15:08:23 -0700 Subject: [PATCH] introduce std.Build.Cache.Manifest.addFilePath and deprecate `addFile`. Part of an effort to move towards using `std.Build.Cache.Path` abstraction in more places, which makes it easier to avoid absolute paths and path resolution. --- lib/std/Build/Cache.zig | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/lib/std/Build/Cache.zig b/lib/std/Build/Cache.zig index 6d43361ae457..e78353fa455b 100644 --- a/lib/std/Build/Cache.zig +++ b/lib/std/Build/Cache.zig @@ -354,6 +354,19 @@ pub const Manifest = struct { /// ``` /// var file_contents = cache_hash.files.keys()[file_index].contents.?; /// ``` + pub fn addFilePath(m: *Manifest, file_path: Path, max_file_size: ?usize) !usize { + const gpa = m.cache.gpa; + try m.files.ensureUnusedCapacity(gpa, 1); + const resolved_path = try fs.path.resolve(gpa, &.{ + file_path.root_dir.path orelse ".", + file_path.subPathOrDot(), + }); + errdefer gpa.free(resolved_path); + const prefixed_path = try m.cache.findPrefixResolved(resolved_path); + return addFileInner(m, prefixed_path, max_file_size); + } + + /// Deprecated; use `addFilePath`. pub fn addFile(self: *Manifest, file_path: []const u8, max_file_size: ?usize) !usize { assert(self.manifest_file == null); @@ -362,6 +375,10 @@ pub const Manifest = struct { const prefixed_path = try self.cache.findPrefix(file_path); errdefer gpa.free(prefixed_path.sub_path); + return addFileInner(self, prefixed_path, max_file_size); + } + + fn addFileInner(self: *Manifest, prefixed_path: PrefixedPath, max_file_size: ?usize) !usize { const gop = self.files.getOrPutAssumeCapacityAdapted(prefixed_path, FilesAdapter{}); if (gop.found_existing) { gop.key_ptr.updateMaxSize(max_file_size);