aboutsummaryrefslogtreecommitdiff
path: root/lib/std/Build/Cache.zig
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2025-06-27 20:05:22 -0700
committerAndrew Kelley <andrew@ziglang.org>2025-07-07 22:43:51 -0700
commit0e37ff0d591dd75ceec9208196bec29efaec607a (patch)
treec126fa823a1f3864e9c363aac70e3a3db0219957 /lib/std/Build/Cache.zig
parent0b3f0124dc33403d329fb8ee63a93215d9af1f1e (diff)
downloadzig-0e37ff0d591dd75ceec9208196bec29efaec607a.tar.gz
zig-0e37ff0d591dd75ceec9208196bec29efaec607a.zip
std.fmt: breaking API changes
added adapter to AnyWriter and GenericWriter to help bridge the gap between old and new API make std.testing.expectFmt work at compile-time std.fmt no longer has a dependency on std.unicode. Formatted printing was never properly unicode-aware. Now it no longer pretends to be. Breakage/deprecations: * std.fs.File.reader -> std.fs.File.deprecatedReader * std.fs.File.writer -> std.fs.File.deprecatedWriter * std.io.GenericReader -> std.io.Reader * std.io.GenericWriter -> std.io.Writer * std.io.AnyReader -> std.io.Reader * std.io.AnyWriter -> std.io.Writer * std.fmt.format -> std.fmt.deprecatedFormat * std.fmt.fmtSliceEscapeLower -> std.ascii.hexEscape * std.fmt.fmtSliceEscapeUpper -> std.ascii.hexEscape * std.fmt.fmtSliceHexLower -> {x} * std.fmt.fmtSliceHexUpper -> {X} * std.fmt.fmtIntSizeDec -> {B} * std.fmt.fmtIntSizeBin -> {Bi} * std.fmt.fmtDuration -> {D} * std.fmt.fmtDurationSigned -> {D} * {} -> {f} when there is a format method * format method signature - anytype -> *std.io.Writer - inferred error set -> error{WriteFailed} - options -> (deleted) * std.fmt.Formatted - now takes context type explicitly - no fmt string
Diffstat (limited to 'lib/std/Build/Cache.zig')
-rw-r--r--lib/std/Build/Cache.zig68
1 files changed, 30 insertions, 38 deletions
diff --git a/lib/std/Build/Cache.zig b/lib/std/Build/Cache.zig
index bf63acdead..81a72a11c6 100644
--- a/lib/std/Build/Cache.zig
+++ b/lib/std/Build/Cache.zig
@@ -68,7 +68,7 @@ const PrefixedPath = struct {
fn findPrefix(cache: *const Cache, file_path: []const u8) !PrefixedPath {
const gpa = cache.gpa;
- const resolved_path = try fs.path.resolve(gpa, &[_][]const u8{file_path});
+ const resolved_path = try fs.path.resolve(gpa, &.{file_path});
errdefer gpa.free(resolved_path);
return findPrefixResolved(cache, resolved_path);
}
@@ -132,7 +132,7 @@ pub const Hasher = crypto.auth.siphash.SipHash128(1, 3);
/// Initial state with random bytes, that can be copied.
/// Refresh this with new random bytes when the manifest
/// format is modified in a non-backwards-compatible way.
-pub const hasher_init: Hasher = Hasher.init(&[_]u8{
+pub const hasher_init: Hasher = Hasher.init(&.{
0x33, 0x52, 0xa2, 0x84,
0xcf, 0x17, 0x56, 0x57,
0x01, 0xbb, 0xcd, 0xe4,
@@ -286,11 +286,8 @@ pub const HashHelper = struct {
pub fn binToHex(bin_digest: BinDigest) HexDigest {
var out_digest: HexDigest = undefined;
- _ = fmt.bufPrint(
- &out_digest,
- "{s}",
- .{fmt.fmtSliceHexLower(&bin_digest)},
- ) catch unreachable;
+ var w: std.io.Writer = .fixed(&out_digest);
+ w.printHex(&bin_digest, .lower) catch unreachable;
return out_digest;
}
@@ -337,7 +334,6 @@ pub const Manifest = struct {
manifest_create: fs.File.OpenError,
manifest_read: fs.File.ReadError,
manifest_lock: fs.File.LockError,
- manifest_seek: fs.File.SeekError,
file_open: FileOp,
file_stat: FileOp,
file_read: FileOp,
@@ -611,12 +607,6 @@ pub const Manifest = struct {
var file = self.files.pop().?;
file.key.deinit(self.cache.gpa);
}
- // Also, seek the file back to the start.
- self.manifest_file.?.seekTo(0) catch |err| {
- self.diagnostic = .{ .manifest_seek = err };
- return error.CacheCheckFailed;
- };
-
switch (try self.hitWithCurrentLock()) {
.hit => break :hit,
.miss => |m| break :digests m.file_digests_populated,
@@ -661,9 +651,8 @@ pub const Manifest = struct {
return true;
}
- /// Assumes that `self.hash.hasher` has been updated only with the original digest, that
- /// `self.files` contains only the original input files, and that `self.manifest_file.?` is
- /// seeked to the start of the file.
+ /// Assumes that `self.hash.hasher` has been updated only with the original digest and that
+ /// `self.files` contains only the original input files.
fn hitWithCurrentLock(self: *Manifest) HitError!union(enum) {
hit,
miss: struct {
@@ -672,12 +661,13 @@ pub const Manifest = struct {
} {
const gpa = self.cache.gpa;
const input_file_count = self.files.entries.len;
-
- const file_contents = self.manifest_file.?.reader().readAllAlloc(gpa, manifest_file_size_max) catch |err| switch (err) {
+ var manifest_reader = self.manifest_file.?.reader(&.{}); // Reads positionally from zero.
+ const limit: std.io.Limit = .limited(manifest_file_size_max);
+ const file_contents = manifest_reader.interface.allocRemaining(gpa, limit) catch |err| switch (err) {
error.OutOfMemory => return error.OutOfMemory,
error.StreamTooLong => return error.OutOfMemory,
- else => |e| {
- self.diagnostic = .{ .manifest_read = e };
+ error.ReadFailed => {
+ self.diagnostic = .{ .manifest_read = manifest_reader.err.? };
return error.CacheCheckFailed;
},
};
@@ -1063,14 +1053,17 @@ pub const Manifest = struct {
}
fn addDepFileMaybePost(self: *Manifest, dir: fs.Dir, dep_file_basename: []const u8) !void {
- const dep_file_contents = try dir.readFileAlloc(self.cache.gpa, dep_file_basename, manifest_file_size_max);
- defer self.cache.gpa.free(dep_file_contents);
+ const gpa = self.cache.gpa;
+ const dep_file_contents = try dir.readFileAlloc(gpa, dep_file_basename, manifest_file_size_max);
+ defer gpa.free(dep_file_contents);
- var error_buf = std.ArrayList(u8).init(self.cache.gpa);
- defer error_buf.deinit();
+ var error_buf: std.ArrayListUnmanaged(u8) = .empty;
+ defer error_buf.deinit(gpa);
- var it: DepTokenizer = .{ .bytes = dep_file_contents };
+ var resolve_buf: std.ArrayListUnmanaged(u8) = .empty;
+ defer resolve_buf.deinit(gpa);
+ var it: DepTokenizer = .{ .bytes = dep_file_contents };
while (it.next()) |token| {
switch (token) {
// We don't care about targets, we only want the prereqs
@@ -1080,16 +1073,14 @@ pub const Manifest = struct {
_ = try self.addFile(file_path, null);
} else try self.addFilePost(file_path),
.prereq_must_resolve => {
- var resolve_buf = std.ArrayList(u8).init(self.cache.gpa);
- defer resolve_buf.deinit();
-
- try token.resolve(resolve_buf.writer());
+ resolve_buf.clearRetainingCapacity();
+ try token.resolve(gpa, &resolve_buf);
if (self.manifest_file == null) {
_ = try self.addFile(resolve_buf.items, null);
} else try self.addFilePost(resolve_buf.items);
},
else => |err| {
- try err.printError(error_buf.writer());
+ try err.printError(gpa, &error_buf);
log.err("failed parsing {s}: {s}", .{ dep_file_basename, error_buf.items });
return error.InvalidDepFile;
},
@@ -1127,24 +1118,25 @@ pub const Manifest = struct {
if (self.manifest_dirty) {
self.manifest_dirty = false;
- var contents = std.ArrayList(u8).init(self.cache.gpa);
- defer contents.deinit();
+ const gpa = self.cache.gpa;
+ var contents: std.ArrayListUnmanaged(u8) = .empty;
+ defer contents.deinit(gpa);
- const writer = contents.writer();
- try writer.writeAll(manifest_header ++ "\n");
+ try contents.appendSlice(gpa, manifest_header ++ "\n");
for (self.files.keys()) |file| {
- try writer.print("{d} {d} {d} {} {d} {s}\n", .{
+ try contents.print(gpa, "{d} {d} {d} {x} {d} {s}\n", .{
file.stat.size,
file.stat.inode,
file.stat.mtime,
- fmt.fmtSliceHexLower(&file.bin_digest),
+ &file.bin_digest,
file.prefixed_path.prefix,
file.prefixed_path.sub_path,
});
}
try manifest_file.setEndPos(contents.items.len);
- try manifest_file.pwriteAll(contents.items, 0);
+ var pos: usize = 0;
+ while (pos < contents.items.len) pos += try manifest_file.pwrite(contents.items[pos..], pos);
}
if (self.want_shared_lock) {