authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-12-30 16:42:32-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-01-02 13:16:17-07:00
loge36718165cdc29b777392a3a343d92ccd1c6acf3
treec174fc245cbc9170730a0cbe57a5f24f3565f7b6
parent67e31807df73ad2da992293fcdf1f6ea7ca67d2a

stage2: add `@import` and `@embedFile` to CacheHash

when using `CacheMode.whole`. Also, I verified that `addDepFilePost` is in fact including the original C source file in addition to the files it depends on.

2 files changed, 77 insertions(+), 3 deletions(-)

src/Cache.zig+54-3
......@@ -47,10 +47,16 @@ pub const hasher_init: Hasher = Hasher.init(&[_]u8{0} ** Hasher.key_length);
4747pub const File = struct {
4848 path: ?[]const u8,
4949 max_file_size: ?usize,
50 stat: fs.File.Stat,
50 stat: Stat,
5151 bin_digest: BinDigest,
5252 contents: ?[]const u8,
5353
54 pub const Stat = struct {
55 inode: fs.File.INode,
56 size: u64,
57 mtime: i128,
58 };
59
5460 pub fn deinit(self: *File, allocator: Allocator) void {
5561 if (self.path) |owned_slice| {
5662 allocator.free(owned_slice);
......@@ -424,7 +430,11 @@ pub const Manifest = struct {
424430 if (!size_match or !mtime_match or !inode_match) {
425431 self.manifest_dirty = true;
426432
427 cache_hash_file.stat = actual_stat;
433 cache_hash_file.stat = .{
434 .size = actual_stat.size,
435 .mtime = actual_stat.mtime,
436 .inode = actual_stat.inode,
437 };
428438
429439 if (self.isProblematicTimestamp(cache_hash_file.stat.mtime)) {
430440 // The actual file has an unreliable timestamp, force it to be hashed
......@@ -530,7 +540,12 @@ pub const Manifest = struct {
530540 const file = try fs.cwd().openFile(ch_file.path.?, .{});
531541 defer file.close();
532542
533 ch_file.stat = try file.stat();
543 const actual_stat = try file.stat();
544 ch_file.stat = .{
545 .size = actual_stat.size,
546 .mtime = actual_stat.mtime,
547 .inode = actual_stat.inode,
548 };
534549
535550 if (self.isProblematicTimestamp(ch_file.stat.mtime)) {
536551 // The actual file has an unreliable timestamp, force it to be hashed
......@@ -615,6 +630,42 @@ pub const Manifest = struct {
615630 try self.populateFileHash(new_ch_file);
616631 }
617632
633 /// Like `addFilePost` but when the file contents have already been loaded from disk.
634 /// On success, cache takes ownership of `resolved_path`.
635 pub fn addFilePostContents(
636 self: *Manifest,
637 resolved_path: []const u8,
638 bytes: []const u8,
639 stat: File.Stat,
640 ) error{OutOfMemory}!void {
641 assert(self.manifest_file != null);
642
643 const ch_file = try self.files.addOne(self.cache.gpa);
644 errdefer self.files.shrinkRetainingCapacity(self.files.items.len - 1);
645
646 ch_file.* = .{
647 .path = resolved_path,
648 .max_file_size = null,
649 .stat = stat,
650 .bin_digest = undefined,
651 .contents = null,
652 };
653
654 if (self.isProblematicTimestamp(ch_file.stat.mtime)) {
655 // The actual file has an unreliable timestamp, force it to be hashed
656 ch_file.stat.mtime = 0;
657 ch_file.stat.inode = 0;
658 }
659
660 {
661 var hasher = hasher_init;
662 hasher.update(bytes);
663 hasher.final(&ch_file.bin_digest);
664 }
665
666 self.hash.hasher.update(&ch_file.bin_digest);
667 }
668
618669 pub fn addDepFilePost(self: *Manifest, dir: fs.Dir, dep_file_basename: []const u8) !void {
619670 assert(self.manifest_file != null);
620671
src/Module.zig+23
......@@ -3380,6 +3380,19 @@ pub fn semaFile(mod: *Module, file: *File) SemaError!void {
33803380 error.OutOfMemory => return error.OutOfMemory,
33813381 error.AnalysisFail => {},
33823382 }
3383
3384 if (mod.comp.whole_cache_manifest) |man| {
3385 assert(file.source_loaded);
3386 const resolved_path = try file.pkg.root_src_directory.join(gpa, &.{
3387 file.sub_file_path,
3388 });
3389 errdefer gpa.free(resolved_path);
3390 try man.addFilePostContents(resolved_path, file.source, .{
3391 .size = file.stat_size,
3392 .inode = file.stat_inode,
3393 .mtime = file.stat_mtime,
3394 });
3395 }
33833396 } else {
33843397 new_decl.analysis = .file_failure;
33853398 }
......@@ -3836,6 +3849,16 @@ pub fn embedFile(mod: *Module, cur_file: *File, rel_file_path: []const u8) !*Emb
38363849 resolved_root_path, resolved_path, sub_file_path, rel_file_path,
38373850 });
38383851
3852 if (mod.comp.whole_cache_manifest) |man| {
3853 const copied_resolved_path = try gpa.dupe(u8, resolved_path);
3854 errdefer gpa.free(copied_resolved_path);
3855 try man.addFilePostContents(copied_resolved_path, bytes, .{
3856 .size = stat.size,
3857 .inode = stat.inode,
3858 .mtime = stat.mtime,
3859 });
3860 }
3861
38393862 keep_resolved_path = true; // It's now owned by embed_table.
38403863 gop.value_ptr.* = new_file;
38413864 new_file.* = .{