diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2024-07-20 13:04:49 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-07-20 13:04:49 -0700 |
| commit | b5f3d121644031cf644271296e1ee5dbf363abeb (patch) | |
| tree | 1a6aa7a7ca974b2d5e24bddf6e2f3b9f7c0040c6 /lib/std | |
| parent | ef3a746da1a85a8b4a653cb78e0464c71d35b64e (diff) | |
| parent | 645ad1ef72a09785fe5d7b33f1f9f2394bf52f57 (diff) | |
| download | zig-b5f3d121644031cf644271296e1ee5dbf363abeb.tar.gz zig-b5f3d121644031cf644271296e1ee5dbf363abeb.zip | |
Merge pull request #20688 from ziglang/incr-test
introduce a new tool for testing incremental compilation
Diffstat (limited to 'lib/std')
| -rw-r--r-- | lib/std/Build.zig | 15 | ||||
| -rw-r--r-- | lib/std/Build/Step/Options.zig | 2 | ||||
| -rw-r--r-- | lib/std/Build/Step/Run.zig | 2 | ||||
| -rw-r--r-- | lib/std/fmt.zig | 29 | ||||
| -rw-r--r-- | lib/std/process.zig | 6 | ||||
| -rw-r--r-- | lib/std/zig.zig | 6 | ||||
| -rw-r--r-- | lib/std/zig/Server.zig | 5 |
7 files changed, 47 insertions, 18 deletions
diff --git a/lib/std/Build.zig b/lib/std/Build.zig index 06de7aa6f4..c68170b2c1 100644 --- a/lib/std/Build.zig +++ b/lib/std/Build.zig @@ -2522,7 +2522,7 @@ pub const InstallDir = union(enum) { /// function. pub fn makeTempPath(b: *Build) []const u8 { const rand_int = std.crypto.random.int(u64); - const tmp_dir_sub_path = "tmp" ++ fs.path.sep_str ++ hex64(rand_int); + const tmp_dir_sub_path = "tmp" ++ fs.path.sep_str ++ std.fmt.hex(rand_int); const result_path = b.cache_root.join(b.allocator, &.{tmp_dir_sub_path}) catch @panic("OOM"); b.cache_root.handle.makePath(tmp_dir_sub_path) catch |err| { std.debug.print("unable to make tmp path '{s}': {s}\n", .{ @@ -2532,18 +2532,9 @@ pub fn makeTempPath(b: *Build) []const u8 { return result_path; } -/// There are a few copies of this function in miscellaneous places. Would be nice to find -/// a home for them. +/// Deprecated; use `std.fmt.hex` instead. pub fn hex64(x: u64) [16]u8 { - const hex_charset = "0123456789abcdef"; - var result: [16]u8 = undefined; - var i: usize = 0; - while (i < 8) : (i += 1) { - const byte: u8 = @truncate(x >> @as(u6, @intCast(8 * i))); - result[i * 2 + 0] = hex_charset[byte >> 4]; - result[i * 2 + 1] = hex_charset[byte & 15]; - } - return result; + return std.fmt.hex(x); } /// A pair of target query and fully resolved target. diff --git a/lib/std/Build/Step/Options.zig b/lib/std/Build/Step/Options.zig index 7c872db170..6131912b1b 100644 --- a/lib/std/Build/Step/Options.zig +++ b/lib/std/Build/Step/Options.zig @@ -457,7 +457,7 @@ fn make(step: *Step, make_options: Step.MakeOptions) !void { const rand_int = std.crypto.random.int(u64); const tmp_sub_path = "tmp" ++ fs.path.sep_str ++ - std.Build.hex64(rand_int) ++ fs.path.sep_str ++ + std.fmt.hex(rand_int) ++ fs.path.sep_str ++ basename; const tmp_sub_path_dirname = fs.path.dirname(tmp_sub_path).?; diff --git a/lib/std/Build/Step/Run.zig b/lib/std/Build/Step/Run.zig index d79d4647b1..0712ce8083 100644 --- a/lib/std/Build/Step/Run.zig +++ b/lib/std/Build/Step/Run.zig @@ -743,7 +743,7 @@ fn make(step: *Step, options: Step.MakeOptions) !void { // We do not know the final output paths yet, use temp paths to run the command. const rand_int = std.crypto.random.int(u64); - const tmp_dir_path = "tmp" ++ fs.path.sep_str ++ std.Build.hex64(rand_int); + const tmp_dir_path = "tmp" ++ fs.path.sep_str ++ std.fmt.hex(rand_int); for (output_placeholders.items) |placeholder| { const output_components = .{ tmp_dir_path, placeholder.output.basename }; diff --git a/lib/std/fmt.zig b/lib/std/fmt.zig index a4407a8cd2..a7136d99bb 100644 --- a/lib/std/fmt.zig +++ b/lib/std/fmt.zig @@ -2718,3 +2718,32 @@ test "recursive format function" { var r = R{ .Leaf = 1 }; try expectFmt("Leaf(1)\n", "{}\n", .{&r}); } + +pub const hex_charset = "0123456789abcdef"; + +/// Converts an unsigned integer of any multiple of u8 to an array of lowercase +/// hex bytes, little endian. +pub fn hex(x: anytype) [@sizeOf(@TypeOf(x)) * 2]u8 { + comptime assert(@typeInfo(@TypeOf(x)).Int.signedness == .unsigned); + var result: [@sizeOf(@TypeOf(x)) * 2]u8 = undefined; + var i: usize = 0; + while (i < result.len / 2) : (i += 1) { + const byte: u8 = @truncate(x >> @intCast(8 * i)); + result[i * 2 + 0] = hex_charset[byte >> 4]; + result[i * 2 + 1] = hex_charset[byte & 15]; + } + return result; +} + +test hex { + { + const x = hex(@as(u32, 0xdeadbeef)); + try std.testing.expect(x.len == 8); + try std.testing.expectEqualStrings("efbeadde", &x); + } + { + const s = "[" ++ hex(@as(u64, 0x12345678_abcdef00)) ++ "]"; + try std.testing.expect(s.len == 18); + try std.testing.expectEqualStrings("[00efcdab78563412]", s); + } +} diff --git a/lib/std/process.zig b/lib/std/process.zig index 86654e4b5a..eca3a26c29 100644 --- a/lib/std/process.zig +++ b/lib/std/process.zig @@ -2032,3 +2032,9 @@ pub fn createWindowsEnvBlock(allocator: mem.Allocator, env_map: *const EnvMap) ! i += 1; return try allocator.realloc(result, i); } + +/// Logs an error and then terminates the process with exit code 1. +pub fn fatal(comptime format: []const u8, format_arguments: anytype) noreturn { + std.log.err(format, format_arguments); + exit(1); +} diff --git a/lib/std/zig.zig b/lib/std/zig.zig index 230dc45ddc..01d3aafa75 100644 --- a/lib/std/zig.zig +++ b/lib/std/zig.zig @@ -667,10 +667,8 @@ pub fn parseTargetQueryOrReportFatalError( }; } -pub fn fatal(comptime format: []const u8, args: anytype) noreturn { - std.log.err(format, args); - std.process.exit(1); -} +/// Deprecated; see `std.process.fatal`. +pub const fatal = std.process.fatal; /// Collects all the environment variables that Zig could possibly inspect, so /// that we can do reflection on this and print them with `zig env`. diff --git a/lib/std/zig/Server.zig b/lib/std/zig/Server.zig index 2d66ee628d..4046fe4014 100644 --- a/lib/std/zig/Server.zig +++ b/lib/std/zig/Server.zig @@ -109,6 +109,7 @@ pub fn deinit(s: *Server) void { pub fn receiveMessage(s: *Server) !InMessage.Header { const Header = InMessage.Header; const fifo = &s.receive_fifo; + var last_amt_zero = false; while (true) { const buf = fifo.readableSlice(0); @@ -136,6 +137,10 @@ pub fn receiveMessage(s: *Server) !InMessage.Header { const write_buffer = try fifo.writableWithSize(256); const amt = try s.in.read(write_buffer); fifo.update(amt); + if (amt == 0) { + if (last_amt_zero) return error.BrokenPipe; + last_amt_zero = true; + } } } |
