authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-10-13 23:36:44-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-10-29 06:20:49-07:00
log2bcdde29850c4b7b769ac3e0ffc636825fd7b5e5
treee6c59080aaef7233aa67d605cc83843e0d3504f0
parentc2d1a339da65da0c93fdd66df90955500417b3c9

compiler: update for introduction of std.Io

only thing remaining is using libc dns resolution when linking libc

32 files changed, 267 insertions(+), 174 deletions(-)

BRANCH_TODO+1
...@@ -10,6 +10,7 @@...@@ -10,6 +10,7 @@
10* move max_iovecs_len to std.Io10* move max_iovecs_len to std.Io
11* address the cancelation race condition (signal received between checkCancel and syscall)11* address the cancelation race condition (signal received between checkCancel and syscall)
12* update signal values to be an enum12* update signal values to be an enum
13* delete the deprecated fs.File functions
13* move fs.File.Writer to Io14* move fs.File.Writer to Io
14* add non-blocking flag to net and fs operations, handle EAGAIN15* add non-blocking flag to net and fs operations, handle EAGAIN
15* finish moving std.fs to Io16* finish moving std.fs to Io
lib/compiler/aro/aro/Compilation.zig+29-26
...@@ -1,4 +1,5 @@...@@ -1,4 +1,5 @@
1const std = @import("std");1const std = @import("std");
2const Io = std.Io;
2const assert = std.debug.assert;3const assert = std.debug.assert;
3const EpochSeconds = std.time.epoch.EpochSeconds;4const EpochSeconds = std.time.epoch.EpochSeconds;
4const mem = std.mem;5const mem = std.mem;
...@@ -124,6 +125,7 @@ const Compilation = @This();...@@ -124,6 +125,7 @@ const Compilation = @This();
124gpa: Allocator,125gpa: Allocator,
125/// Allocations in this arena live all the way until `Compilation.deinit`.126/// Allocations in this arena live all the way until `Compilation.deinit`.
126arena: Allocator,127arena: Allocator,
128io: Io,
127diagnostics: *Diagnostics,129diagnostics: *Diagnostics,
128130
129code_gen_options: CodeGenOptions = .default,131code_gen_options: CodeGenOptions = .default,
...@@ -157,10 +159,11 @@ type_store: TypeStore = .{},...@@ -157,10 +159,11 @@ type_store: TypeStore = .{},
157ms_cwd_source_id: ?Source.Id = null,159ms_cwd_source_id: ?Source.Id = null,
158cwd: std.fs.Dir,160cwd: std.fs.Dir,
159161
160pub fn init(gpa: Allocator, arena: Allocator, diagnostics: *Diagnostics, cwd: std.fs.Dir) Compilation {162pub fn init(gpa: Allocator, arena: Allocator, io: Io, diagnostics: *Diagnostics, cwd: std.fs.Dir) Compilation {
161 return .{163 return .{
162 .gpa = gpa,164 .gpa = gpa,
163 .arena = arena,165 .arena = arena,
166 .io = io,
164 .diagnostics = diagnostics,167 .diagnostics = diagnostics,
165 .cwd = cwd,168 .cwd = cwd,
166 };169 };
...@@ -222,14 +225,14 @@ pub const SystemDefinesMode = enum {...@@ -222,14 +225,14 @@ pub const SystemDefinesMode = enum {
222 include_system_defines,225 include_system_defines,
223};226};
224227
225fn generateSystemDefines(comp: *Compilation, w: *std.Io.Writer) !void {228fn generateSystemDefines(comp: *Compilation, w: *Io.Writer) !void {
226 const define = struct {229 const define = struct {
227 fn define(_w: *std.Io.Writer, name: []const u8) !void {230 fn define(_w: *Io.Writer, name: []const u8) !void {
228 try _w.print("#define {s} 1\n", .{name});231 try _w.print("#define {s} 1\n", .{name});
229 }232 }
230 }.define;233 }.define;
231 const defineStd = struct {234 const defineStd = struct {
232 fn defineStd(_w: *std.Io.Writer, name: []const u8, is_gnu: bool) !void {235 fn defineStd(_w: *Io.Writer, name: []const u8, is_gnu: bool) !void {
233 if (is_gnu) {236 if (is_gnu) {
234 try _w.print("#define {s} 1\n", .{name});237 try _w.print("#define {s} 1\n", .{name});
235 }238 }
...@@ -957,7 +960,7 @@ fn generateSystemDefines(comp: *Compilation, w: *std.Io.Writer) !void {...@@ -957,7 +960,7 @@ fn generateSystemDefines(comp: *Compilation, w: *std.Io.Writer) !void {
957pub fn generateBuiltinMacros(comp: *Compilation, system_defines_mode: SystemDefinesMode) AddSourceError!Source {960pub fn generateBuiltinMacros(comp: *Compilation, system_defines_mode: SystemDefinesMode) AddSourceError!Source {
958 try comp.type_store.initNamedTypes(comp);961 try comp.type_store.initNamedTypes(comp);
959962
960 var allocating: std.Io.Writer.Allocating = try .initCapacity(comp.gpa, 2 << 13);963 var allocating: Io.Writer.Allocating = try .initCapacity(comp.gpa, 2 << 13);
961 defer allocating.deinit();964 defer allocating.deinit();
962965
963 comp.writeBuiltinMacros(system_defines_mode, &allocating.writer) catch |err| switch (err) {966 comp.writeBuiltinMacros(system_defines_mode, &allocating.writer) catch |err| switch (err) {
...@@ -971,7 +974,7 @@ pub fn generateBuiltinMacros(comp: *Compilation, system_defines_mode: SystemDefi...@@ -971,7 +974,7 @@ pub fn generateBuiltinMacros(comp: *Compilation, system_defines_mode: SystemDefi
971 return comp.addSourceFromOwnedBuffer("<builtin>", contents, .user);974 return comp.addSourceFromOwnedBuffer("<builtin>", contents, .user);
972}975}
973976
974fn writeBuiltinMacros(comp: *Compilation, system_defines_mode: SystemDefinesMode, w: *std.Io.Writer) !void {977fn writeBuiltinMacros(comp: *Compilation, system_defines_mode: SystemDefinesMode, w: *Io.Writer) !void {
975 if (system_defines_mode == .include_system_defines) {978 if (system_defines_mode == .include_system_defines) {
976 try w.writeAll(979 try w.writeAll(
977 \\#define __VERSION__ "Aro980 \\#define __VERSION__ "Aro
...@@ -1026,7 +1029,7 @@ fn writeBuiltinMacros(comp: *Compilation, system_defines_mode: SystemDefinesMode...@@ -1026,7 +1029,7 @@ fn writeBuiltinMacros(comp: *Compilation, system_defines_mode: SystemDefinesMode
1026 }1029 }
1027}1030}
10281031
1029fn generateFloatMacros(w: *std.Io.Writer, prefix: []const u8, semantics: target_util.FPSemantics, ext: []const u8) !void {1032fn generateFloatMacros(w: *Io.Writer, prefix: []const u8, semantics: target_util.FPSemantics, ext: []const u8) !void {
1030 const denormMin = semantics.chooseValue(1033 const denormMin = semantics.chooseValue(
1031 []const u8,1034 []const u8,
1032 .{1035 .{
...@@ -1101,7 +1104,7 @@ fn generateFloatMacros(w: *std.Io.Writer, prefix: []const u8, semantics: target_...@@ -1101,7 +1104,7 @@ fn generateFloatMacros(w: *std.Io.Writer, prefix: []const u8, semantics: target_
1101 try w.print("#define __{s}_MIN__ {s}{s}\n", .{ prefix, min, ext });1104 try w.print("#define __{s}_MIN__ {s}{s}\n", .{ prefix, min, ext });
1102}1105}
11031106
1104fn generateTypeMacro(comp: *const Compilation, w: *std.Io.Writer, name: []const u8, qt: QualType) !void {1107fn generateTypeMacro(comp: *const Compilation, w: *Io.Writer, name: []const u8, qt: QualType) !void {
1105 try w.print("#define {s} ", .{name});1108 try w.print("#define {s} ", .{name});
1106 try qt.print(comp, w);1109 try qt.print(comp, w);
1107 try w.writeByte('\n');1110 try w.writeByte('\n');
...@@ -1136,7 +1139,7 @@ fn generateFastOrLeastType(...@@ -1136,7 +1139,7 @@ fn generateFastOrLeastType(
1136 bits: usize,1139 bits: usize,
1137 kind: enum { least, fast },1140 kind: enum { least, fast },
1138 signedness: std.builtin.Signedness,1141 signedness: std.builtin.Signedness,
1139 w: *std.Io.Writer,1142 w: *Io.Writer,
1140) !void {1143) !void {
1141 const ty = comp.intLeastN(bits, signedness); // defining the fast types as the least types is permitted1144 const ty = comp.intLeastN(bits, signedness); // defining the fast types as the least types is permitted
11421145
...@@ -1166,7 +1169,7 @@ fn generateFastOrLeastType(...@@ -1166,7 +1169,7 @@ fn generateFastOrLeastType(
1166 try comp.generateFmt(prefix, w, ty);1169 try comp.generateFmt(prefix, w, ty);
1167}1170}
11681171
1169fn generateFastAndLeastWidthTypes(comp: *Compilation, w: *std.Io.Writer) !void {1172fn generateFastAndLeastWidthTypes(comp: *Compilation, w: *Io.Writer) !void {
1170 const sizes = [_]usize{ 8, 16, 32, 64 };1173 const sizes = [_]usize{ 8, 16, 32, 64 };
1171 for (sizes) |size| {1174 for (sizes) |size| {
1172 try comp.generateFastOrLeastType(size, .least, .signed, w);1175 try comp.generateFastOrLeastType(size, .least, .signed, w);
...@@ -1176,7 +1179,7 @@ fn generateFastAndLeastWidthTypes(comp: *Compilation, w: *std.Io.Writer) !void {...@@ -1176,7 +1179,7 @@ fn generateFastAndLeastWidthTypes(comp: *Compilation, w: *std.Io.Writer) !void {
1176 }1179 }
1177}1180}
11781181
1179fn generateExactWidthTypes(comp: *Compilation, w: *std.Io.Writer) !void {1182fn generateExactWidthTypes(comp: *Compilation, w: *Io.Writer) !void {
1180 try comp.generateExactWidthType(w, .schar);1183 try comp.generateExactWidthType(w, .schar);
11811184
1182 if (QualType.short.sizeof(comp) > QualType.char.sizeof(comp)) {1185 if (QualType.short.sizeof(comp) > QualType.char.sizeof(comp)) {
...@@ -1224,7 +1227,7 @@ fn generateExactWidthTypes(comp: *Compilation, w: *std.Io.Writer) !void {...@@ -1224,7 +1227,7 @@ fn generateExactWidthTypes(comp: *Compilation, w: *std.Io.Writer) !void {
1224 }1227 }
1225}1228}
12261229
1227fn generateFmt(comp: *const Compilation, prefix: []const u8, w: *std.Io.Writer, qt: QualType) !void {1230fn generateFmt(comp: *const Compilation, prefix: []const u8, w: *Io.Writer, qt: QualType) !void {
1228 const unsigned = qt.signedness(comp) == .unsigned;1231 const unsigned = qt.signedness(comp) == .unsigned;
1229 const modifier = qt.formatModifier(comp);1232 const modifier = qt.formatModifier(comp);
1230 const formats = if (unsigned) "ouxX" else "di";1233 const formats = if (unsigned) "ouxX" else "di";
...@@ -1233,7 +1236,7 @@ fn generateFmt(comp: *const Compilation, prefix: []const u8, w: *std.Io.Writer,...@@ -1233,7 +1236,7 @@ fn generateFmt(comp: *const Compilation, prefix: []const u8, w: *std.Io.Writer,
1233 }1236 }
1234}1237}
12351238
1236fn generateSuffixMacro(comp: *const Compilation, prefix: []const u8, w: *std.Io.Writer, qt: QualType) !void {1239fn generateSuffixMacro(comp: *const Compilation, prefix: []const u8, w: *Io.Writer, qt: QualType) !void {
1237 return w.print("#define {s}_C_SUFFIX__ {s}\n", .{ prefix, qt.intValueSuffix(comp) });1240 return w.print("#define {s}_C_SUFFIX__ {s}\n", .{ prefix, qt.intValueSuffix(comp) });
1238}1241}
12391242
...@@ -1241,7 +1244,7 @@ fn generateSuffixMacro(comp: *const Compilation, prefix: []const u8, w: *std.Io....@@ -1241,7 +1244,7 @@ fn generateSuffixMacro(comp: *const Compilation, prefix: []const u8, w: *std.Io.
1241/// Name macro (e.g. #define __UINT32_TYPE__ unsigned int)1244/// Name macro (e.g. #define __UINT32_TYPE__ unsigned int)
1242/// Format strings (e.g. #define __UINT32_FMTu__ "u")1245/// Format strings (e.g. #define __UINT32_FMTu__ "u")
1243/// Suffix macro (e.g. #define __UINT32_C_SUFFIX__ U)1246/// Suffix macro (e.g. #define __UINT32_C_SUFFIX__ U)
1244fn generateExactWidthType(comp: *Compilation, w: *std.Io.Writer, original_qt: QualType) !void {1247fn generateExactWidthType(comp: *Compilation, w: *Io.Writer, original_qt: QualType) !void {
1245 var qt = original_qt;1248 var qt = original_qt;
1246 const width = qt.sizeof(comp) * 8;1249 const width = qt.sizeof(comp) * 8;
1247 const unsigned = qt.signedness(comp) == .unsigned;1250 const unsigned = qt.signedness(comp) == .unsigned;
...@@ -1274,7 +1277,7 @@ pub fn hasHalfPrecisionFloatABI(comp: *const Compilation) bool {...@@ -1274,7 +1277,7 @@ pub fn hasHalfPrecisionFloatABI(comp: *const Compilation) bool {
1274 return comp.langopts.allow_half_args_and_returns or target_util.hasHalfPrecisionFloatABI(comp.target);1277 return comp.langopts.allow_half_args_and_returns or target_util.hasHalfPrecisionFloatABI(comp.target);
1275}1278}
12761279
1277fn generateIntMax(comp: *const Compilation, w: *std.Io.Writer, name: []const u8, qt: QualType) !void {1280fn generateIntMax(comp: *const Compilation, w: *Io.Writer, name: []const u8, qt: QualType) !void {
1278 const unsigned = qt.signedness(comp) == .unsigned;1281 const unsigned = qt.signedness(comp) == .unsigned;
1279 const max: u128 = switch (qt.bitSizeof(comp)) {1282 const max: u128 = switch (qt.bitSizeof(comp)) {
1280 8 => if (unsigned) std.math.maxInt(u8) else std.math.maxInt(i8),1283 8 => if (unsigned) std.math.maxInt(u8) else std.math.maxInt(i8),
...@@ -1298,7 +1301,7 @@ pub fn wcharMax(comp: *const Compilation) u32 {...@@ -1298,7 +1301,7 @@ pub fn wcharMax(comp: *const Compilation) u32 {
1298 };1301 };
1299}1302}
13001303
1301fn generateExactWidthIntMax(comp: *Compilation, w: *std.Io.Writer, original_qt: QualType) !void {1304fn generateExactWidthIntMax(comp: *Compilation, w: *Io.Writer, original_qt: QualType) !void {
1302 var qt = original_qt;1305 var qt = original_qt;
1303 const bit_count: u8 = @intCast(qt.sizeof(comp) * 8);1306 const bit_count: u8 = @intCast(qt.sizeof(comp) * 8);
1304 const unsigned = qt.signedness(comp) == .unsigned;1307 const unsigned = qt.signedness(comp) == .unsigned;
...@@ -1315,16 +1318,16 @@ fn generateExactWidthIntMax(comp: *Compilation, w: *std.Io.Writer, original_qt:...@@ -1315,16 +1318,16 @@ fn generateExactWidthIntMax(comp: *Compilation, w: *std.Io.Writer, original_qt:
1315 return comp.generateIntMax(w, name, qt);1318 return comp.generateIntMax(w, name, qt);
1316}1319}
13171320
1318fn generateIntWidth(comp: *Compilation, w: *std.Io.Writer, name: []const u8, qt: QualType) !void {1321fn generateIntWidth(comp: *Compilation, w: *Io.Writer, name: []const u8, qt: QualType) !void {
1319 try w.print("#define __{s}_WIDTH__ {d}\n", .{ name, qt.sizeof(comp) * 8 });1322 try w.print("#define __{s}_WIDTH__ {d}\n", .{ name, qt.sizeof(comp) * 8 });
1320}1323}
13211324
1322fn generateIntMaxAndWidth(comp: *Compilation, w: *std.Io.Writer, name: []const u8, qt: QualType) !void {1325fn generateIntMaxAndWidth(comp: *Compilation, w: *Io.Writer, name: []const u8, qt: QualType) !void {
1323 try comp.generateIntMax(w, name, qt);1326 try comp.generateIntMax(w, name, qt);
1324 try comp.generateIntWidth(w, name, qt);1327 try comp.generateIntWidth(w, name, qt);
1325}1328}
13261329
1327fn generateSizeofType(comp: *Compilation, w: *std.Io.Writer, name: []const u8, qt: QualType) !void {1330fn generateSizeofType(comp: *Compilation, w: *Io.Writer, name: []const u8, qt: QualType) !void {
1328 try w.print("#define {s} {d}\n", .{ name, qt.sizeof(comp) });1331 try w.print("#define {s} {d}\n", .{ name, qt.sizeof(comp) });
1329}1332}
13301333
...@@ -1805,7 +1808,7 @@ pub const IncludeType = enum {...@@ -1805,7 +1808,7 @@ pub const IncludeType = enum {
1805 angle_brackets,1808 angle_brackets,
1806};1809};
18071810
1808fn getPathContents(comp: *Compilation, path: []const u8, limit: std.Io.Limit) ![]u8 {1811fn getPathContents(comp: *Compilation, path: []const u8, limit: Io.Limit) ![]u8 {
1809 if (mem.indexOfScalar(u8, path, 0) != null) {1812 if (mem.indexOfScalar(u8, path, 0) != null) {
1810 return error.FileNotFound;1813 return error.FileNotFound;
1811 }1814 }
...@@ -1815,11 +1818,12 @@ fn getPathContents(comp: *Compilation, path: []const u8, limit: std.Io.Limit) ![...@@ -1815,11 +1818,12 @@ fn getPathContents(comp: *Compilation, path: []const u8, limit: std.Io.Limit) ![
1815 return comp.getFileContents(file, limit);1818 return comp.getFileContents(file, limit);
1816}1819}
18171820
1818fn getFileContents(comp: *Compilation, file: std.fs.File, limit: std.Io.Limit) ![]u8 {1821fn getFileContents(comp: *Compilation, file: std.fs.File, limit: Io.Limit) ![]u8 {
1822 const io = comp.io;
1819 var file_buf: [4096]u8 = undefined;1823 var file_buf: [4096]u8 = undefined;
1820 var file_reader = file.reader(&file_buf);1824 var file_reader = file.reader(io, &file_buf);
18211825
1822 var allocating: std.Io.Writer.Allocating = .init(comp.gpa);1826 var allocating: Io.Writer.Allocating = .init(comp.gpa);
1823 defer allocating.deinit();1827 defer allocating.deinit();
1824 if (file_reader.getSize()) |size| {1828 if (file_reader.getSize()) |size| {
1825 const limited_size = limit.minInt64(size);1829 const limited_size = limit.minInt64(size);
...@@ -1846,7 +1850,7 @@ pub fn findEmbed(...@@ -1846,7 +1850,7 @@ pub fn findEmbed(
1846 includer_token_source: Source.Id,1850 includer_token_source: Source.Id,
1847 /// angle bracket vs quotes1851 /// angle bracket vs quotes
1848 include_type: IncludeType,1852 include_type: IncludeType,
1849 limit: std.Io.Limit,1853 limit: Io.Limit,
1850 opt_dep_file: ?*DepFile,1854 opt_dep_file: ?*DepFile,
1851) !?[]u8 {1855) !?[]u8 {
1852 if (std.fs.path.isAbsolute(filename)) {1856 if (std.fs.path.isAbsolute(filename)) {
...@@ -2010,8 +2014,7 @@ pub fn locSlice(comp: *const Compilation, loc: Source.Location) []const u8 {...@@ -2010,8 +2014,7 @@ pub fn locSlice(comp: *const Compilation, loc: Source.Location) []const u8 {
2010pub fn getSourceMTimeUncached(comp: *const Compilation, source_id: Source.Id) ?u64 {2014pub fn getSourceMTimeUncached(comp: *const Compilation, source_id: Source.Id) ?u64 {
2011 const source = comp.getSource(source_id);2015 const source = comp.getSource(source_id);
2012 if (comp.cwd.statFile(source.path)) |stat| {2016 if (comp.cwd.statFile(source.path)) |stat| {
2013 const mtime = @divTrunc(stat.mtime, std.time.ns_per_s);2017 return std.math.cast(u64, stat.mtime.toSeconds());
2014 return std.math.cast(u64, mtime);
2015 } else |_| {2018 } else |_| {
2016 return null;2019 return null;
2017 }2020 }
lib/std/Io.zig+4
...@@ -878,6 +878,10 @@ pub const Timestamp = struct {...@@ -878,6 +878,10 @@ pub const Timestamp = struct {
878 return @intCast(@divTrunc(t.nanoseconds, std.time.ns_per_s));878 return @intCast(@divTrunc(t.nanoseconds, std.time.ns_per_s));
879 }879 }
880880
881 pub fn toNanoseconds(t: Timestamp) i96 {
882 return t.nanoseconds;
883 }
884
881 pub fn formatNumber(t: Timestamp, w: *std.Io.Writer, n: std.fmt.Number) std.Io.Writer.Error!void {885 pub fn formatNumber(t: Timestamp, w: *std.Io.Writer, n: std.fmt.Number) std.Io.Writer.Error!void {
882 return w.printInt(t.nanoseconds, n.mode.base() orelse 10, n.case, .{886 return w.printInt(t.nanoseconds, n.mode.base() orelse 10, n.case, .{
883 .precision = n.precision,887 .precision = n.precision,
lib/std/Io/Threaded.zig+4-3
...@@ -1142,7 +1142,7 @@ fn dirOpenFile(...@@ -1142,7 +1142,7 @@ fn dirOpenFile(
1142 }1142 }
1143 const fd: posix.fd_t = while (true) {1143 const fd: posix.fd_t = while (true) {
1144 try pool.checkCancel();1144 try pool.checkCancel();
1145 const rc = openat_sym(dir.handle, sub_path_posix, os_flags, 0);1145 const rc = openat_sym(dir.handle, sub_path_posix, os_flags, @as(posix.mode_t, 0));
1146 switch (posix.errno(rc)) {1146 switch (posix.errno(rc)) {
1147 .SUCCESS => break @intCast(rc),1147 .SUCCESS => break @intCast(rc),
1148 .INTR => continue,1148 .INTR => continue,
...@@ -2259,10 +2259,11 @@ fn netSendMany(...@@ -2259,10 +2259,11 @@ fn netSendMany(
2259 const rc = posix.system.sendmmsg(handle, clamped_msgs.ptr, @intCast(clamped_msgs.len), flags);2259 const rc = posix.system.sendmmsg(handle, clamped_msgs.ptr, @intCast(clamped_msgs.len), flags);
2260 switch (posix.errno(rc)) {2260 switch (posix.errno(rc)) {
2261 .SUCCESS => {2261 .SUCCESS => {
2262 for (clamped_messages[0..rc], clamped_msgs[0..rc]) |*message, *msg| {2262 const n: usize = @intCast(rc);
2263 for (clamped_messages[0..n], clamped_msgs[0..n]) |*message, *msg| {
2263 message.data_len = msg.len;2264 message.data_len = msg.len;
2264 }2265 }
2265 return rc;2266 return n;
2266 },2267 },
2267 .AGAIN => |err| return errnoBug(err),2268 .AGAIN => |err| return errnoBug(err),
2268 .ALREADY => return error.FastOpenAlreadyInProgress,2269 .ALREADY => return error.FastOpenAlreadyInProgress,
lib/std/Uri.zig+1-1
...@@ -39,7 +39,7 @@ pub const GetHostAllocError = GetHostError || error{OutOfMemory};...@@ -39,7 +39,7 @@ pub const GetHostAllocError = GetHostError || error{OutOfMemory};
39///39///
40/// See also:40/// See also:
41/// * `getHost`41/// * `getHost`
42pub fn getHostAlloc(uri: Uri, arena: Allocator) GetHostAllocError![]const u8 {42pub fn getHostAlloc(uri: Uri, arena: Allocator) GetHostAllocError!HostName {
43 const component = uri.host orelse return error.UriMissingHost;43 const component = uri.host orelse return error.UriMissingHost;
44 const bytes = try component.toRawMaybeAlloc(arena);44 const bytes = try component.toRawMaybeAlloc(arena);
45 return .{ .bytes = bytes };45 return .{ .bytes = bytes };
lib/std/c.zig+9
...@@ -4149,6 +4149,14 @@ const posix_msghdr_const = extern struct {...@@ -4149,6 +4149,14 @@ const posix_msghdr_const = extern struct {
4149 flags: u32,4149 flags: u32,
4150};4150};
41514151
4152pub const mmsghdr = switch (native_os) {
4153 .linux => linux.mmsghdr,
4154 else => extern struct {
4155 hdr: msghdr,
4156 len: u32,
4157 },
4158};
4159
4152pub const cmsghdr = switch (native_os) {4160pub const cmsghdr = switch (native_os) {
4153 .linux => if (@bitSizeOf(usize) > @bitSizeOf(i32) and builtin.abi.isMusl()) posix_cmsghdr else linux.cmsghdr,4161 .linux => if (@bitSizeOf(usize) > @bitSizeOf(i32) and builtin.abi.isMusl()) posix_cmsghdr else linux.cmsghdr,
4154 // https://github.com/emscripten-core/emscripten/blob/96371ed7888fc78c040179f4d4faa82a6a07a116/system/lib/libc/musl/include/sys/socket.h#L444162 // https://github.com/emscripten-core/emscripten/blob/96371ed7888fc78c040179f4d4faa82a6a07a116/system/lib/libc/musl/include/sys/socket.h#L44
...@@ -10665,6 +10673,7 @@ pub extern "c" fn sendto(...@@ -10665,6 +10673,7 @@ pub extern "c" fn sendto(
10665 addrlen: socklen_t,10673 addrlen: socklen_t,
10666) isize;10674) isize;
10667pub extern "c" fn sendmsg(sockfd: fd_t, msg: *const msghdr_const, flags: u32) isize;10675pub extern "c" fn sendmsg(sockfd: fd_t, msg: *const msghdr_const, flags: u32) isize;
10676pub extern "c" fn sendmmsg(sockfd: fd_t, msgvec: [*]mmsghdr, n: c_uint, flags: u32) c_int;
1066810677
10669pub extern "c" fn recv(10678pub extern "c" fn recv(
10670 sockfd: fd_t,10679 sockfd: fd_t,
lib/std/http/Client.zig+3-3
...@@ -377,17 +377,17 @@ pub const Connection = struct {...@@ -377,17 +377,17 @@ pub const Connection = struct {
377 }377 }
378 };378 };
379379
380 pub const ReadError = std.crypto.tls.Client.ReadError || Io.net.Stream.ReadError;380 pub const ReadError = std.crypto.tls.Client.ReadError || Io.net.Stream.Reader.Error;
381381
382 pub fn getReadError(c: *const Connection) ?ReadError {382 pub fn getReadError(c: *const Connection) ?ReadError {
383 return switch (c.protocol) {383 return switch (c.protocol) {
384 .tls => {384 .tls => {
385 if (disable_tls) unreachable;385 if (disable_tls) unreachable;
386 const tls: *const Tls = @alignCast(@fieldParentPtr("connection", c));386 const tls: *const Tls = @alignCast(@fieldParentPtr("connection", c));
387 return tls.client.read_err orelse c.stream_reader.getError();387 return tls.client.read_err orelse c.stream_reader.err.?;
388 },388 },
389 .plain => {389 .plain => {
390 return c.stream_reader.getError();390 return c.stream_reader.err.?;
391 },391 },
392 };392 };
393 }393 }
lib/std/posix.zig+2-1
...@@ -5532,6 +5532,8 @@ pub const RealPathError = error{...@@ -5532,6 +5532,8 @@ pub const RealPathError = error{
5532 /// On Windows, the volume does not contain a recognized file system. File5532 /// On Windows, the volume does not contain a recognized file system. File
5533 /// system drivers might not be loaded, or the volume may be corrupt.5533 /// system drivers might not be loaded, or the volume may be corrupt.
5534 UnrecognizedVolume,5534 UnrecognizedVolume,
5535
5536 Canceled,
5535} || UnexpectedError;5537} || UnexpectedError;
55365538
5537/// Return the canonicalized absolute pathname.5539/// Return the canonicalized absolute pathname.
...@@ -5596,7 +5598,6 @@ pub fn realpathZ(pathname: [*:0]const u8, out_buffer: *[max_path_bytes]u8) RealP...@@ -5596,7 +5598,6 @@ pub fn realpathZ(pathname: [*:0]const u8, out_buffer: *[max_path_bytes]u8) RealP
5596 error.FileLocksNotSupported => unreachable,5598 error.FileLocksNotSupported => unreachable,
5597 error.WouldBlock => unreachable,5599 error.WouldBlock => unreachable,
5598 error.FileBusy => unreachable, // not asking for write permissions5600 error.FileBusy => unreachable, // not asking for write permissions
5599 error.InvalidUtf8 => unreachable, // WASI-only
5600 else => |e| return e,5601 else => |e| return e,
5601 };5602 };
5602 defer close(fd);5603 defer close(fd);
lib/std/tar/Writer.zig+9
...@@ -39,6 +39,15 @@ pub fn writeDir(w: *Writer, sub_path: []const u8, options: Options) Error!void {...@@ -39,6 +39,15 @@ pub fn writeDir(w: *Writer, sub_path: []const u8, options: Options) Error!void {
3939
40pub const WriteFileError = Io.Writer.FileError || Error || Io.File.Reader.SizeError;40pub const WriteFileError = Io.Writer.FileError || Error || Io.File.Reader.SizeError;
4141
42pub fn writeFileTimestamp(
43 w: *Writer,
44 sub_path: []const u8,
45 file_reader: *Io.File.Reader,
46 mtime: Io.Timestamp,
47) WriteFileError!void {
48 return writeFile(w, sub_path, file_reader, @intCast(mtime.toSeconds()));
49}
50
42pub fn writeFile(51pub fn writeFile(
43 w: *Writer,52 w: *Writer,
44 sub_path: []const u8,53 sub_path: []const u8,
lib/std/zig/ErrorBundle.zig-1
...@@ -321,7 +321,6 @@ fn writeMsg(eb: ErrorBundle, err_msg: ErrorMessage, w: *Writer, indent: usize) !...@@ -321,7 +321,6 @@ fn writeMsg(eb: ErrorBundle, err_msg: ErrorMessage, w: *Writer, indent: usize) !
321321
322pub const Wip = struct {322pub const Wip = struct {
323 gpa: Allocator,323 gpa: Allocator,
324 io: Io,
325 string_bytes: std.ArrayListUnmanaged(u8),324 string_bytes: std.ArrayListUnmanaged(u8),
326 /// The first thing in this array is a ErrorMessageList.325 /// The first thing in this array is a ErrorMessageList.
327 extra: std.ArrayListUnmanaged(u32),326 extra: std.ArrayListUnmanaged(u32),
src/Builtin.zig+1-1
...@@ -360,7 +360,7 @@ pub fn updateFileOnDisk(file: *File, comp: *Compilation) !void {...@@ -360,7 +360,7 @@ pub fn updateFileOnDisk(file: *File, comp: *Compilation) !void {
360 file.stat = .{360 file.stat = .{
361 .size = file.source.?.len,361 .size = file.source.?.len,
362 .inode = 0, // dummy value362 .inode = 0, // dummy value
363 .mtime = 0, // dummy value363 .mtime = .zero, // dummy value
364 };364 };
365}365}
366366
src/Compilation.zig+35-24
...@@ -55,6 +55,7 @@ gpa: Allocator,...@@ -55,6 +55,7 @@ gpa: Allocator,
55/// Not thread-safe - lock `mutex` if potentially accessing from multiple55/// Not thread-safe - lock `mutex` if potentially accessing from multiple
56/// threads at once.56/// threads at once.
57arena: Allocator,57arena: Allocator,
58io: Io,
58/// Not every Compilation compiles .zig code! For example you could do `zig build-exe foo.o`.59/// Not every Compilation compiles .zig code! For example you could do `zig build-exe foo.o`.
59zcu: ?*Zcu,60zcu: ?*Zcu,
60/// Contains different state depending on the `CacheMode` used by this `Compilation`.61/// Contains different state depending on the `CacheMode` used by this `Compilation`.
...@@ -1077,26 +1078,26 @@ pub const CObject = struct {...@@ -1077,26 +1078,26 @@ pub const CObject = struct {
1077 diag.* = undefined;1078 diag.* = undefined;
1078 }1079 }
10791080
1080 pub fn count(diag: Diag) u32 {1081 pub fn count(diag: *const Diag) u32 {
1081 var total: u32 = 1;1082 var total: u32 = 1;
1082 for (diag.sub_diags) |sub_diag| total += sub_diag.count();1083 for (diag.sub_diags) |sub_diag| total += sub_diag.count();
1083 return total;1084 return total;
1084 }1085 }
10851086
1086 pub fn addToErrorBundle(diag: Diag, eb: *ErrorBundle.Wip, bundle: Bundle, note: *u32) !void {1087 pub fn addToErrorBundle(diag: *const Diag, io: Io, eb: *ErrorBundle.Wip, bundle: Bundle, note: *u32) !void {
1087 const err_msg = try eb.addErrorMessage(try diag.toErrorMessage(eb, bundle, 0));1088 const err_msg = try eb.addErrorMessage(try diag.toErrorMessage(io, eb, bundle, 0));
1088 eb.extra.items[note.*] = @intFromEnum(err_msg);1089 eb.extra.items[note.*] = @intFromEnum(err_msg);
1089 note.* += 1;1090 note.* += 1;
1090 for (diag.sub_diags) |sub_diag| try sub_diag.addToErrorBundle(eb, bundle, note);1091 for (diag.sub_diags) |sub_diag| try sub_diag.addToErrorBundle(io, eb, bundle, note);
1091 }1092 }
10921093
1093 pub fn toErrorMessage(1094 pub fn toErrorMessage(
1094 diag: Diag,1095 diag: *const Diag,
1096 io: Io,
1095 eb: *ErrorBundle.Wip,1097 eb: *ErrorBundle.Wip,
1096 bundle: Bundle,1098 bundle: Bundle,
1097 notes_len: u32,1099 notes_len: u32,
1098 ) !ErrorBundle.ErrorMessage {1100 ) !ErrorBundle.ErrorMessage {
1099 const io = eb.io;
1100 var start = diag.src_loc.offset;1101 var start = diag.src_loc.offset;
1101 var end = diag.src_loc.offset;1102 var end = diag.src_loc.offset;
1102 for (diag.src_ranges) |src_range| {1103 for (diag.src_ranges) |src_range| {
...@@ -1307,14 +1308,14 @@ pub const CObject = struct {...@@ -1307,14 +1308,14 @@ pub const CObject = struct {
1307 return bundle;1308 return bundle;
1308 }1309 }
13091310
1310 pub fn addToErrorBundle(bundle: Bundle, eb: *ErrorBundle.Wip) !void {1311 pub fn addToErrorBundle(bundle: Bundle, io: Io, eb: *ErrorBundle.Wip) !void {
1311 for (bundle.diags) |diag| {1312 for (bundle.diags) |diag| {
1312 const notes_len = diag.count() - 1;1313 const notes_len = diag.count() - 1;
1313 try eb.addRootErrorMessage(try diag.toErrorMessage(eb, bundle, notes_len));1314 try eb.addRootErrorMessage(try diag.toErrorMessage(io, eb, bundle, notes_len));
1314 if (notes_len > 0) {1315 if (notes_len > 0) {
1315 var note = try eb.reserveNotes(notes_len);1316 var note = try eb.reserveNotes(notes_len);
1316 for (diag.sub_diags) |sub_diag|1317 for (diag.sub_diags) |sub_diag|
1317 try sub_diag.addToErrorBundle(eb, bundle, &note);1318 try sub_diag.addToErrorBundle(io, eb, bundle, &note);
1318 }1319 }
1319 }1320 }
1320 }1321 }
...@@ -1906,7 +1907,7 @@ pub const CreateDiagnostic = union(enum) {...@@ -1906,7 +1907,7 @@ pub const CreateDiagnostic = union(enum) {
1906 return error.CreateFail;1907 return error.CreateFail;
1907 }1908 }
1908};1909};
1909pub fn create(gpa: Allocator, arena: Allocator, diag: *CreateDiagnostic, options: CreateOptions) error{1910pub fn create(gpa: Allocator, arena: Allocator, io: Io, diag: *CreateDiagnostic, options: CreateOptions) error{
1910 OutOfMemory,1911 OutOfMemory,
1911 Unexpected,1912 Unexpected,
1912 CurrentWorkingDirectoryUnlinked,1913 CurrentWorkingDirectoryUnlinked,
...@@ -2114,6 +2115,7 @@ pub fn create(gpa: Allocator, arena: Allocator, diag: *CreateDiagnostic, options...@@ -2114,6 +2115,7 @@ pub fn create(gpa: Allocator, arena: Allocator, diag: *CreateDiagnostic, options
2114 const cache = try arena.create(Cache);2115 const cache = try arena.create(Cache);
2115 cache.* = .{2116 cache.* = .{
2116 .gpa = gpa,2117 .gpa = gpa,
2118 .io = io,
2117 .manifest_dir = options.dirs.local_cache.handle.makeOpenPath("h", .{}) catch |err| {2119 .manifest_dir = options.dirs.local_cache.handle.makeOpenPath("h", .{}) catch |err| {
2118 return diag.fail(.{ .create_cache_path = .{ .which = .local, .sub = "h", .err = err } });2120 return diag.fail(.{ .create_cache_path = .{ .which = .local, .sub = "h", .err = err } });
2119 },2121 },
...@@ -2232,6 +2234,7 @@ pub fn create(gpa: Allocator, arena: Allocator, diag: *CreateDiagnostic, options...@@ -2232,6 +2234,7 @@ pub fn create(gpa: Allocator, arena: Allocator, diag: *CreateDiagnostic, options
2232 comp.* = .{2234 comp.* = .{
2233 .gpa = gpa,2235 .gpa = gpa,
2234 .arena = arena,2236 .arena = arena,
2237 .io = io,
2235 .zcu = opt_zcu,2238 .zcu = opt_zcu,
2236 .cache_use = undefined, // populated below2239 .cache_use = undefined, // populated below
2237 .bin_file = null, // populated below if necessary2240 .bin_file = null, // populated below if necessary
...@@ -3919,13 +3922,14 @@ fn addBuf(list: *std.array_list.Managed([]const u8), buf: []const u8) void {...@@ -3919,13 +3922,14 @@ fn addBuf(list: *std.array_list.Managed([]const u8), buf: []const u8) void {
3919/// This function is temporally single-threaded.3922/// This function is temporally single-threaded.
3920pub fn getAllErrorsAlloc(comp: *Compilation) error{OutOfMemory}!ErrorBundle {3923pub fn getAllErrorsAlloc(comp: *Compilation) error{OutOfMemory}!ErrorBundle {
3921 const gpa = comp.gpa;3924 const gpa = comp.gpa;
3925 const io = comp.io;
39223926
3923 var bundle: ErrorBundle.Wip = undefined;3927 var bundle: ErrorBundle.Wip = undefined;
3924 try bundle.init(gpa);3928 try bundle.init(gpa);
3925 defer bundle.deinit();3929 defer bundle.deinit();
39263930
3927 for (comp.failed_c_objects.values()) |diag_bundle| {3931 for (comp.failed_c_objects.values()) |diag_bundle| {
3928 try diag_bundle.addToErrorBundle(&bundle);3932 try diag_bundle.addToErrorBundle(io, &bundle);
3929 }3933 }
39303934
3931 for (comp.failed_win32_resources.values()) |error_bundle| {3935 for (comp.failed_win32_resources.values()) |error_bundle| {
...@@ -5310,6 +5314,7 @@ fn docsCopyModule(...@@ -5310,6 +5314,7 @@ fn docsCopyModule(
5310 name: []const u8,5314 name: []const u8,
5311 tar_file_writer: *fs.File.Writer,5315 tar_file_writer: *fs.File.Writer,
5312) !void {5316) !void {
5317 const io = comp.io;
5313 const root = module.root;5318 const root = module.root;
5314 var mod_dir = d: {5319 var mod_dir = d: {
5315 const root_dir, const sub_path = root.openInfo(comp.dirs);5320 const root_dir, const sub_path = root.openInfo(comp.dirs);
...@@ -5343,9 +5348,9 @@ fn docsCopyModule(...@@ -5343,9 +5348,9 @@ fn docsCopyModule(
5343 };5348 };
5344 defer file.close();5349 defer file.close();
5345 const stat = try file.stat();5350 const stat = try file.stat();
5346 var file_reader: fs.File.Reader = .initSize(file, &buffer, stat.size);5351 var file_reader: fs.File.Reader = .initSize(file.adaptToNewApi(), io, &buffer, stat.size);
53475352
5348 archiver.writeFile(entry.path, &file_reader, stat.mtime) catch |err| {5353 archiver.writeFileTimestamp(entry.path, &file_reader, stat.mtime) catch |err| {
5349 return comp.lockAndSetMiscFailure(.docs_copy, "unable to archive {f}{s}: {t}", .{5354 return comp.lockAndSetMiscFailure(.docs_copy, "unable to archive {f}{s}: {t}", .{
5350 root.fmt(comp), entry.path, err,5355 root.fmt(comp), entry.path, err,
5351 });5356 });
...@@ -5365,6 +5370,7 @@ fn workerDocsWasm(comp: *Compilation, parent_prog_node: std.Progress.Node) void...@@ -5365,6 +5370,7 @@ fn workerDocsWasm(comp: *Compilation, parent_prog_node: std.Progress.Node) void
53655370
5366fn workerDocsWasmFallible(comp: *Compilation, prog_node: std.Progress.Node) SubUpdateError!void {5371fn workerDocsWasmFallible(comp: *Compilation, prog_node: std.Progress.Node) SubUpdateError!void {
5367 const gpa = comp.gpa;5372 const gpa = comp.gpa;
5373 const io = comp.io;
53685374
5369 var arena_allocator = std.heap.ArenaAllocator.init(gpa);5375 var arena_allocator = std.heap.ArenaAllocator.init(gpa);
5370 defer arena_allocator.deinit();5376 defer arena_allocator.deinit();
...@@ -5373,7 +5379,7 @@ fn workerDocsWasmFallible(comp: *Compilation, prog_node: std.Progress.Node) SubU...@@ -5373,7 +5379,7 @@ fn workerDocsWasmFallible(comp: *Compilation, prog_node: std.Progress.Node) SubU
5373 const optimize_mode = std.builtin.OptimizeMode.ReleaseSmall;5379 const optimize_mode = std.builtin.OptimizeMode.ReleaseSmall;
5374 const output_mode = std.builtin.OutputMode.Exe;5380 const output_mode = std.builtin.OutputMode.Exe;
5375 const resolved_target: Package.Module.ResolvedTarget = .{5381 const resolved_target: Package.Module.ResolvedTarget = .{
5376 .result = std.zig.system.resolveTargetQuery(.{5382 .result = std.zig.system.resolveTargetQuery(io, .{
5377 .cpu_arch = .wasm32,5383 .cpu_arch = .wasm32,
5378 .os_tag = .freestanding,5384 .os_tag = .freestanding,
5379 .cpu_features_add = std.Target.wasm.featureSet(&.{5385 .cpu_features_add = std.Target.wasm.featureSet(&.{
...@@ -5449,7 +5455,7 @@ fn workerDocsWasmFallible(comp: *Compilation, prog_node: std.Progress.Node) SubU...@@ -5449,7 +5455,7 @@ fn workerDocsWasmFallible(comp: *Compilation, prog_node: std.Progress.Node) SubU
5449 try root_mod.deps.put(arena, "Walk", walk_mod);5455 try root_mod.deps.put(arena, "Walk", walk_mod);
54505456
5451 var sub_create_diag: CreateDiagnostic = undefined;5457 var sub_create_diag: CreateDiagnostic = undefined;
5452 const sub_compilation = Compilation.create(gpa, arena, &sub_create_diag, .{5458 const sub_compilation = Compilation.create(gpa, arena, io, &sub_create_diag, .{
5453 .dirs = dirs,5459 .dirs = dirs,
5454 .self_exe_path = comp.self_exe_path,5460 .self_exe_path = comp.self_exe_path,
5455 .config = config,5461 .config = config,
...@@ -5667,6 +5673,8 @@ pub fn translateC(...@@ -5667,6 +5673,8 @@ pub fn translateC(
5667) !CImportResult {5673) !CImportResult {
5668 dev.check(.translate_c_command);5674 dev.check(.translate_c_command);
56695675
5676 const gpa = comp.gpa;
5677 const io = comp.io;
5670 const tmp_basename = std.fmt.hex(std.crypto.random.int(u64));5678 const tmp_basename = std.fmt.hex(std.crypto.random.int(u64));
5671 const tmp_sub_path = "tmp" ++ fs.path.sep_str ++ tmp_basename;5679 const tmp_sub_path = "tmp" ++ fs.path.sep_str ++ tmp_basename;
5672 const cache_dir = comp.dirs.local_cache.handle;5680 const cache_dir = comp.dirs.local_cache.handle;
...@@ -5706,9 +5714,9 @@ pub fn translateC(...@@ -5706,9 +5714,9 @@ pub fn translateC(
57065714
5707 const mcpu = mcpu: {5715 const mcpu = mcpu: {
5708 var buf: std.ArrayListUnmanaged(u8) = .empty;5716 var buf: std.ArrayListUnmanaged(u8) = .empty;
5709 defer buf.deinit(comp.gpa);5717 defer buf.deinit(gpa);
57105718
5711 try buf.print(comp.gpa, "-mcpu={s}", .{target.cpu.model.name});5719 try buf.print(gpa, "-mcpu={s}", .{target.cpu.model.name});
57125720
5713 // TODO better serialization https://github.com/ziglang/zig/issues/45845721 // TODO better serialization https://github.com/ziglang/zig/issues/4584
5714 const all_features_list = target.cpu.arch.allFeaturesList();5722 const all_features_list = target.cpu.arch.allFeaturesList();
...@@ -5718,7 +5726,7 @@ pub fn translateC(...@@ -5718,7 +5726,7 @@ pub fn translateC(
5718 const is_enabled = target.cpu.features.isEnabled(index);5726 const is_enabled = target.cpu.features.isEnabled(index);
57195727
5720 const plus_or_minus = "-+"[@intFromBool(is_enabled)];5728 const plus_or_minus = "-+"[@intFromBool(is_enabled)];
5721 try buf.print(comp.gpa, "{c}{s}", .{ plus_or_minus, feature.name });5729 try buf.print(gpa, "{c}{s}", .{ plus_or_minus, feature.name });
5722 }5730 }
5723 break :mcpu try buf.toOwnedSlice(arena);5731 break :mcpu try buf.toOwnedSlice(arena);
5724 };5732 };
...@@ -5731,7 +5739,7 @@ pub fn translateC(...@@ -5731,7 +5739,7 @@ pub fn translateC(
5731 }5739 }
57325740
5733 var stdout: []u8 = undefined;5741 var stdout: []u8 = undefined;
5734 try @import("main.zig").translateC(comp.gpa, arena, argv.items, prog_node, &stdout);5742 try @import("main.zig").translateC(gpa, arena, io, argv.items, prog_node, &stdout);
57355743
5736 if (out_dep_path) |dep_file_path| add_deps: {5744 if (out_dep_path) |dep_file_path| add_deps: {
5737 if (comp.verbose_cimport) log.info("processing dep file at {s}", .{dep_file_path});5745 if (comp.verbose_cimport) log.info("processing dep file at {s}", .{dep_file_path});
...@@ -5767,7 +5775,7 @@ pub fn translateC(...@@ -5767,7 +5775,7 @@ pub fn translateC(
5767 fatal("unable to read {}-byte translate-c message body: {s}", .{ header.bytes_len, @errorName(err) });5775 fatal("unable to read {}-byte translate-c message body: {s}", .{ header.bytes_len, @errorName(err) });
5768 switch (header.tag) {5776 switch (header.tag) {
5769 .error_bundle => {5777 .error_bundle => {
5770 const error_bundle = try std.zig.Server.allocErrorBundle(comp.gpa, body);5778 const error_bundle = try std.zig.Server.allocErrorBundle(gpa, body);
5771 return .{5779 return .{
5772 .digest = undefined,5780 .digest = undefined,
5773 .cache_hit = false,5781 .cache_hit = false,
...@@ -6154,6 +6162,7 @@ fn updateCObject(comp: *Compilation, c_object: *CObject, c_obj_prog_node: std.Pr...@@ -6154,6 +6162,7 @@ fn updateCObject(comp: *Compilation, c_object: *CObject, c_obj_prog_node: std.Pr
6154 log.debug("updating C object: {s}", .{c_object.src.src_path});6162 log.debug("updating C object: {s}", .{c_object.src.src_path});
61556163
6156 const gpa = comp.gpa;6164 const gpa = comp.gpa;
6165 const io = comp.io;
61576166
6158 if (c_object.clearStatus(gpa)) {6167 if (c_object.clearStatus(gpa)) {
6159 // There was previous failure.6168 // There was previous failure.
...@@ -6353,7 +6362,7 @@ fn updateCObject(comp: *Compilation, c_object: *CObject, c_obj_prog_node: std.Pr...@@ -6353,7 +6362,7 @@ fn updateCObject(comp: *Compilation, c_object: *CObject, c_obj_prog_node: std.Pr
63536362
6354 try child.spawn();6363 try child.spawn();
63556364
6356 var stderr_reader = child.stderr.?.readerStreaming(&.{});6365 var stderr_reader = child.stderr.?.readerStreaming(io, &.{});
6357 const stderr = try stderr_reader.interface.allocRemaining(arena, .limited(std.math.maxInt(u32)));6366 const stderr = try stderr_reader.interface.allocRemaining(arena, .limited(std.math.maxInt(u32)));
63586367
6359 const term = child.wait() catch |err| {6368 const term = child.wait() catch |err| {
...@@ -6362,7 +6371,7 @@ fn updateCObject(comp: *Compilation, c_object: *CObject, c_obj_prog_node: std.Pr...@@ -6362,7 +6371,7 @@ fn updateCObject(comp: *Compilation, c_object: *CObject, c_obj_prog_node: std.Pr
63626371
6363 switch (term) {6372 switch (term) {
6364 .Exited => |code| if (code != 0) if (out_diag_path) |diag_file_path| {6373 .Exited => |code| if (code != 0) if (out_diag_path) |diag_file_path| {
6365 const bundle = CObject.Diag.Bundle.parse(gpa, diag_file_path) catch |err| {6374 const bundle = CObject.Diag.Bundle.parse(gpa, io, diag_file_path) catch |err| {
6366 log.err("{}: failed to parse clang diagnostics: {s}", .{ err, stderr });6375 log.err("{}: failed to parse clang diagnostics: {s}", .{ err, stderr });
6367 return comp.failCObj(c_object, "clang exited with code {d}", .{code});6376 return comp.failCObj(c_object, "clang exited with code {d}", .{code});
6368 };6377 };
...@@ -7807,6 +7816,7 @@ fn buildOutputFromZig(...@@ -7807,6 +7816,7 @@ fn buildOutputFromZig(
7807 defer tracy_trace.end();7816 defer tracy_trace.end();
78087817
7809 const gpa = comp.gpa;7818 const gpa = comp.gpa;
7819 const io = comp.io;
7810 var arena_allocator = std.heap.ArenaAllocator.init(gpa);7820 var arena_allocator = std.heap.ArenaAllocator.init(gpa);
7811 defer arena_allocator.deinit();7821 defer arena_allocator.deinit();
7812 const arena = arena_allocator.allocator();7822 const arena = arena_allocator.allocator();
...@@ -7880,7 +7890,7 @@ fn buildOutputFromZig(...@@ -7880,7 +7890,7 @@ fn buildOutputFromZig(
7880 };7890 };
78817891
7882 var sub_create_diag: CreateDiagnostic = undefined;7892 var sub_create_diag: CreateDiagnostic = undefined;
7883 const sub_compilation = Compilation.create(gpa, arena, &sub_create_diag, .{7893 const sub_compilation = Compilation.create(gpa, arena, io, &sub_create_diag, .{
7884 .dirs = comp.dirs.withoutLocalCache(),7894 .dirs = comp.dirs.withoutLocalCache(),
7885 .cache_mode = .whole,7895 .cache_mode = .whole,
7886 .parent_whole_cache = parent_whole_cache,7896 .parent_whole_cache = parent_whole_cache,
...@@ -7948,6 +7958,7 @@ pub fn build_crt_file(...@@ -7948,6 +7958,7 @@ pub fn build_crt_file(
7948 defer tracy_trace.end();7958 defer tracy_trace.end();
79497959
7950 const gpa = comp.gpa;7960 const gpa = comp.gpa;
7961 const io = comp.io;
7951 var arena_allocator = std.heap.ArenaAllocator.init(gpa);7962 var arena_allocator = std.heap.ArenaAllocator.init(gpa);
7952 defer arena_allocator.deinit();7963 defer arena_allocator.deinit();
7953 const arena = arena_allocator.allocator();7964 const arena = arena_allocator.allocator();
...@@ -8016,7 +8027,7 @@ pub fn build_crt_file(...@@ -8016,7 +8027,7 @@ pub fn build_crt_file(
8016 }8027 }
80178028
8018 var sub_create_diag: CreateDiagnostic = undefined;8029 var sub_create_diag: CreateDiagnostic = undefined;
8019 const sub_compilation = Compilation.create(gpa, arena, &sub_create_diag, .{8030 const sub_compilation = Compilation.create(gpa, arena, io, &sub_create_diag, .{
8020 .dirs = comp.dirs.withoutLocalCache(),8031 .dirs = comp.dirs.withoutLocalCache(),
8021 .self_exe_path = comp.self_exe_path,8032 .self_exe_path = comp.self_exe_path,
8022 .cache_mode = .whole,8033 .cache_mode = .whole,
src/IncrementalDebugServer.zig+13-10
...@@ -44,22 +44,24 @@ pub fn spawn(ids: *IncrementalDebugServer) void {...@@ -44,22 +44,24 @@ pub fn spawn(ids: *IncrementalDebugServer) void {
44}44}
45fn runThread(ids: *IncrementalDebugServer) void {45fn runThread(ids: *IncrementalDebugServer) void {
46 const gpa = ids.zcu.gpa;46 const gpa = ids.zcu.gpa;
47 const io = ids.zcu.comp.io;
4748
48 var cmd_buf: [1024]u8 = undefined;49 var cmd_buf: [1024]u8 = undefined;
49 var text_out: std.ArrayListUnmanaged(u8) = .empty;50 var text_out: std.ArrayListUnmanaged(u8) = .empty;
50 defer text_out.deinit(gpa);51 defer text_out.deinit(gpa);
5152
52 const addr = std.net.Address.parseIp6("::", port) catch unreachable;53 const addr: std.Io.net.IpAddress = .{ .ip6 = .loopback(port) };
53 var server = addr.listen(.{}) catch @panic("IncrementalDebugServer: failed to listen");54 var server = addr.listen(io, .{}) catch @panic("IncrementalDebugServer: failed to listen");
54 defer server.deinit();55 defer server.deinit(io);
55 const conn = server.accept() catch @panic("IncrementalDebugServer: failed to accept");56 var stream = server.accept(io) catch @panic("IncrementalDebugServer: failed to accept");
56 defer conn.stream.close();57 defer stream.close(io);
5758
58 var stream_reader = conn.stream.reader(&cmd_buf);59 var stream_reader = stream.reader(io, &cmd_buf);
60 var stream_writer = stream.writer(io, &.{});
5961
60 while (ids.running.load(.monotonic)) {62 while (ids.running.load(.monotonic)) {
61 conn.stream.writeAll("zig> ") catch @panic("IncrementalDebugServer: failed to write");63 stream_writer.interface.writeAll("zig> ") catch @panic("IncrementalDebugServer: failed to write");
62 const untrimmed = stream_reader.interface().takeSentinel('\n') catch |err| switch (err) {64 const untrimmed = stream_reader.interface.takeSentinel('\n') catch |err| switch (err) {
63 error.EndOfStream => break,65 error.EndOfStream => break,
64 else => @panic("IncrementalDebugServer: failed to read command"),66 else => @panic("IncrementalDebugServer: failed to read command"),
65 };67 };
...@@ -72,7 +74,7 @@ fn runThread(ids: *IncrementalDebugServer) void {...@@ -72,7 +74,7 @@ fn runThread(ids: *IncrementalDebugServer) void {
72 text_out.clearRetainingCapacity();74 text_out.clearRetainingCapacity();
73 {75 {
74 if (!ids.mutex.tryLock()) {76 if (!ids.mutex.tryLock()) {
75 conn.stream.writeAll("waiting for in-progress update to finish...\n") catch @panic("IncrementalDebugServer: failed to write");77 stream_writer.interface.writeAll("waiting for in-progress update to finish...\n") catch @panic("IncrementalDebugServer: failed to write");
76 ids.mutex.lock();78 ids.mutex.lock();
77 }79 }
78 defer ids.mutex.unlock();80 defer ids.mutex.unlock();
...@@ -81,7 +83,7 @@ fn runThread(ids: *IncrementalDebugServer) void {...@@ -81,7 +83,7 @@ fn runThread(ids: *IncrementalDebugServer) void {
81 handleCommand(ids.zcu, &allocating.writer, cmd, arg) catch @panic("IncrementalDebugServer: out of memory");83 handleCommand(ids.zcu, &allocating.writer, cmd, arg) catch @panic("IncrementalDebugServer: out of memory");
82 }84 }
83 text_out.append(gpa, '\n') catch @panic("IncrementalDebugServer: out of memory");85 text_out.append(gpa, '\n') catch @panic("IncrementalDebugServer: out of memory");
84 conn.stream.writeAll(text_out.items) catch @panic("IncrementalDebugServer: failed to write");86 stream_writer.interface.writeAll(text_out.items) catch @panic("IncrementalDebugServer: failed to write");
85 }87 }
86 std.debug.print("closing incremental debug server\n", .{});88 std.debug.print("closing incremental debug server\n", .{});
87}89}
...@@ -373,6 +375,7 @@ fn printType(ty: Type, zcu: *const Zcu, w: anytype) !void {...@@ -373,6 +375,7 @@ fn printType(ty: Type, zcu: *const Zcu, w: anytype) !void {
373}375}
374376
375const std = @import("std");377const std = @import("std");
378const Io = std.Io;
376const Allocator = std.mem.Allocator;379const Allocator = std.mem.Allocator;
377380
378const Compilation = @import("Compilation.zig");381const Compilation = @import("Compilation.zig");
src/Package/Fetch.zig+27-15
...@@ -26,9 +26,13 @@...@@ -26,9 +26,13 @@
26//!26//!
27//! All of this must be done with only referring to the state inside this struct27//! All of this must be done with only referring to the state inside this struct
28//! because this work will be done in a dedicated thread.28//! because this work will be done in a dedicated thread.
29const Fetch = @This();
2930
30const builtin = @import("builtin");31const builtin = @import("builtin");
32const native_os = builtin.os.tag;
33
31const std = @import("std");34const std = @import("std");
35const Io = std.Io;
32const fs = std.fs;36const fs = std.fs;
33const assert = std.debug.assert;37const assert = std.debug.assert;
34const ascii = std.ascii;38const ascii = std.ascii;
...@@ -36,14 +40,13 @@ const Allocator = std.mem.Allocator;...@@ -36,14 +40,13 @@ const Allocator = std.mem.Allocator;
36const Cache = std.Build.Cache;40const Cache = std.Build.Cache;
37const ThreadPool = std.Thread.Pool;41const ThreadPool = std.Thread.Pool;
38const WaitGroup = std.Thread.WaitGroup;42const WaitGroup = std.Thread.WaitGroup;
39const Fetch = @This();
40const git = @import("Fetch/git.zig");43const git = @import("Fetch/git.zig");
41const Package = @import("../Package.zig");44const Package = @import("../Package.zig");
42const Manifest = Package.Manifest;45const Manifest = Package.Manifest;
43const ErrorBundle = std.zig.ErrorBundle;46const ErrorBundle = std.zig.ErrorBundle;
44const native_os = builtin.os.tag;
4547
46arena: std.heap.ArenaAllocator,48arena: std.heap.ArenaAllocator,
49io: Io,
47location: Location,50location: Location,
48location_tok: std.zig.Ast.TokenIndex,51location_tok: std.zig.Ast.TokenIndex,
49hash_tok: std.zig.Ast.OptionalTokenIndex,52hash_tok: std.zig.Ast.OptionalTokenIndex,
...@@ -323,6 +326,7 @@ pub const RunError = error{...@@ -323,6 +326,7 @@ pub const RunError = error{
323};326};
324327
325pub fn run(f: *Fetch) RunError!void {328pub fn run(f: *Fetch) RunError!void {
329 const io = f.io;
326 const eb = &f.error_bundle;330 const eb = &f.error_bundle;
327 const arena = f.arena.allocator();331 const arena = f.arena.allocator();
328 const gpa = f.arena.child_allocator;332 const gpa = f.arena.child_allocator;
...@@ -389,7 +393,7 @@ pub fn run(f: *Fetch) RunError!void {...@@ -389,7 +393,7 @@ pub fn run(f: *Fetch) RunError!void {
389393
390 const file_err = if (dir_err == error.NotDir) e: {394 const file_err = if (dir_err == error.NotDir) e: {
391 if (fs.cwd().openFile(path_or_url, .{})) |file| {395 if (fs.cwd().openFile(path_or_url, .{})) |file| {
392 var resource: Resource = .{ .file = file.reader(&server_header_buffer) };396 var resource: Resource = .{ .file = file.reader(io, &server_header_buffer) };
393 return f.runResource(path_or_url, &resource, null);397 return f.runResource(path_or_url, &resource, null);
394 } else |err| break :e err;398 } else |err| break :e err;
395 } else dir_err;399 } else dir_err;
...@@ -484,7 +488,8 @@ fn runResource(...@@ -484,7 +488,8 @@ fn runResource(
484 resource: *Resource,488 resource: *Resource,
485 remote_hash: ?Package.Hash,489 remote_hash: ?Package.Hash,
486) RunError!void {490) RunError!void {
487 defer resource.deinit();491 const io = f.io;
492 defer resource.deinit(io);
488 const arena = f.arena.allocator();493 const arena = f.arena.allocator();
489 const eb = &f.error_bundle;494 const eb = &f.error_bundle;
490 const s = fs.path.sep_str;495 const s = fs.path.sep_str;
...@@ -697,6 +702,7 @@ fn loadManifest(f: *Fetch, pkg_root: Cache.Path) RunError!void {...@@ -697,6 +702,7 @@ fn loadManifest(f: *Fetch, pkg_root: Cache.Path) RunError!void {
697}702}
698703
699fn queueJobsForDeps(f: *Fetch) RunError!void {704fn queueJobsForDeps(f: *Fetch) RunError!void {
705 const io = f.io;
700 assert(f.job_queue.recursive);706 assert(f.job_queue.recursive);
701707
702 // If the package does not have a build.zig.zon file then there are no dependencies.708 // If the package does not have a build.zig.zon file then there are no dependencies.
...@@ -786,6 +792,7 @@ fn queueJobsForDeps(f: *Fetch) RunError!void {...@@ -786,6 +792,7 @@ fn queueJobsForDeps(f: *Fetch) RunError!void {
786 f.job_queue.all_fetches.appendAssumeCapacity(new_fetch);792 f.job_queue.all_fetches.appendAssumeCapacity(new_fetch);
787 }793 }
788 new_fetch.* = .{794 new_fetch.* = .{
795 .io = io,
789 .arena = std.heap.ArenaAllocator.init(gpa),796 .arena = std.heap.ArenaAllocator.init(gpa),
790 .location = location,797 .location = location,
791 .location_tok = dep.location_tok,798 .location_tok = dep.location_tok,
...@@ -897,9 +904,9 @@ const Resource = union(enum) {...@@ -897,9 +904,9 @@ const Resource = union(enum) {
897 decompress_buffer: []u8,904 decompress_buffer: []u8,
898 };905 };
899906
900 fn deinit(resource: *Resource) void {907 fn deinit(resource: *Resource, io: Io) void {
901 switch (resource.*) {908 switch (resource.*) {
902 .file => |*file_reader| file_reader.file.close(),909 .file => |*file_reader| file_reader.file.close(io),
903 .http_request => |*http_request| http_request.request.deinit(),910 .http_request => |*http_request| http_request.request.deinit(),
904 .git => |*git_resource| {911 .git => |*git_resource| {
905 git_resource.fetch_stream.deinit();912 git_resource.fetch_stream.deinit();
...@@ -909,7 +916,7 @@ const Resource = union(enum) {...@@ -909,7 +916,7 @@ const Resource = union(enum) {
909 resource.* = undefined;916 resource.* = undefined;
910 }917 }
911918
912 fn reader(resource: *Resource) *std.Io.Reader {919 fn reader(resource: *Resource) *Io.Reader {
913 return switch (resource.*) {920 return switch (resource.*) {
914 .file => |*file_reader| return &file_reader.interface,921 .file => |*file_reader| return &file_reader.interface,
915 .http_request => |*http_request| return http_request.response.readerDecompressing(922 .http_request => |*http_request| return http_request.response.readerDecompressing(
...@@ -985,6 +992,7 @@ const FileType = enum {...@@ -985,6 +992,7 @@ const FileType = enum {
985const init_resource_buffer_size = git.Packet.max_data_length;992const init_resource_buffer_size = git.Packet.max_data_length;
986993
987fn initResource(f: *Fetch, uri: std.Uri, resource: *Resource, reader_buffer: []u8) RunError!void {994fn initResource(f: *Fetch, uri: std.Uri, resource: *Resource, reader_buffer: []u8) RunError!void {
995 const io = f.io;
988 const arena = f.arena.allocator();996 const arena = f.arena.allocator();
989 const eb = &f.error_bundle;997 const eb = &f.error_bundle;
990998
...@@ -995,7 +1003,7 @@ fn initResource(f: *Fetch, uri: std.Uri, resource: *Resource, reader_buffer: []u...@@ -995,7 +1003,7 @@ fn initResource(f: *Fetch, uri: std.Uri, resource: *Resource, reader_buffer: []u
995 f.parent_package_root, path, err,1003 f.parent_package_root, path, err,
996 }));1004 }));
997 };1005 };
998 resource.* = .{ .file = file.reader(reader_buffer) };1006 resource.* = .{ .file = file.reader(io, reader_buffer) };
999 return;1007 return;
1000 }1008 }
10011009
...@@ -1242,7 +1250,7 @@ fn unpackResource(...@@ -1242,7 +1250,7 @@ fn unpackResource(
1242 }1250 }
1243}1251}
12441252
1245fn unpackTarball(f: *Fetch, out_dir: fs.Dir, reader: *std.Io.Reader) RunError!UnpackResult {1253fn unpackTarball(f: *Fetch, out_dir: fs.Dir, reader: *Io.Reader) RunError!UnpackResult {
1246 const eb = &f.error_bundle;1254 const eb = &f.error_bundle;
1247 const arena = f.arena.allocator();1255 const arena = f.arena.allocator();
12481256
...@@ -1273,11 +1281,12 @@ fn unpackTarball(f: *Fetch, out_dir: fs.Dir, reader: *std.Io.Reader) RunError!Un...@@ -1273,11 +1281,12 @@ fn unpackTarball(f: *Fetch, out_dir: fs.Dir, reader: *std.Io.Reader) RunError!Un
1273 return res;1281 return res;
1274}1282}
12751283
1276fn unzip(f: *Fetch, out_dir: fs.Dir, reader: *std.Io.Reader) error{ ReadFailed, OutOfMemory, FetchFailed }!UnpackResult {1284fn unzip(f: *Fetch, out_dir: fs.Dir, reader: *Io.Reader) error{ ReadFailed, OutOfMemory, FetchFailed }!UnpackResult {
1277 // We write the entire contents to a file first because zip files1285 // We write the entire contents to a file first because zip files
1278 // must be processed back to front and they could be too large to1286 // must be processed back to front and they could be too large to
1279 // load into memory.1287 // load into memory.
12801288
1289 const io = f.io;
1281 const cache_root = f.job_queue.global_cache;1290 const cache_root = f.job_queue.global_cache;
1282 const prefix = "tmp/";1291 const prefix = "tmp/";
1283 const suffix = ".zip";1292 const suffix = ".zip";
...@@ -1319,7 +1328,7 @@ fn unzip(f: *Fetch, out_dir: fs.Dir, reader: *std.Io.Reader) error{ ReadFailed,...@@ -1319,7 +1328,7 @@ fn unzip(f: *Fetch, out_dir: fs.Dir, reader: *std.Io.Reader) error{ ReadFailed,
1319 f.location_tok,1328 f.location_tok,
1320 try eb.printString("failed writing temporary zip file: {t}", .{err}),1329 try eb.printString("failed writing temporary zip file: {t}", .{err}),
1321 );1330 );
1322 break :b zip_file_writer.moveToReader();1331 break :b zip_file_writer.moveToReader(io);
1323 };1332 };
13241333
1325 var diagnostics: std.zip.Diagnostics = .{ .allocator = f.arena.allocator() };1334 var diagnostics: std.zip.Diagnostics = .{ .allocator = f.arena.allocator() };
...@@ -1339,7 +1348,10 @@ fn unzip(f: *Fetch, out_dir: fs.Dir, reader: *std.Io.Reader) error{ ReadFailed,...@@ -1339,7 +1348,10 @@ fn unzip(f: *Fetch, out_dir: fs.Dir, reader: *std.Io.Reader) error{ ReadFailed,
1339}1348}
13401349
1341fn unpackGitPack(f: *Fetch, out_dir: fs.Dir, resource: *Resource.Git) anyerror!UnpackResult {1350fn unpackGitPack(f: *Fetch, out_dir: fs.Dir, resource: *Resource.Git) anyerror!UnpackResult {
1351 const io = f.io;
1342 const arena = f.arena.allocator();1352 const arena = f.arena.allocator();
1353 // TODO don't try to get a gpa from an arena. expose this dependency higher up
1354 // because the backing of arena could be page allocator
1343 const gpa = f.arena.child_allocator;1355 const gpa = f.arena.child_allocator;
1344 const object_format: git.Oid.Format = resource.want_oid;1356 const object_format: git.Oid.Format = resource.want_oid;
13451357
...@@ -1358,7 +1370,7 @@ fn unpackGitPack(f: *Fetch, out_dir: fs.Dir, resource: *Resource.Git) anyerror!U...@@ -1358,7 +1370,7 @@ fn unpackGitPack(f: *Fetch, out_dir: fs.Dir, resource: *Resource.Git) anyerror!U
1358 const fetch_reader = &resource.fetch_stream.reader;1370 const fetch_reader = &resource.fetch_stream.reader;
1359 _ = try fetch_reader.streamRemaining(&pack_file_writer.interface);1371 _ = try fetch_reader.streamRemaining(&pack_file_writer.interface);
1360 try pack_file_writer.interface.flush();1372 try pack_file_writer.interface.flush();
1361 break :b pack_file_writer.moveToReader();1373 break :b pack_file_writer.moveToReader(io);
1362 };1374 };
13631375
1364 var index_file = try pack_dir.createFile("pkg.idx", .{ .read = true });1376 var index_file = try pack_dir.createFile("pkg.idx", .{ .read = true });
...@@ -1372,7 +1384,7 @@ fn unpackGitPack(f: *Fetch, out_dir: fs.Dir, resource: *Resource.Git) anyerror!U...@@ -1372,7 +1384,7 @@ fn unpackGitPack(f: *Fetch, out_dir: fs.Dir, resource: *Resource.Git) anyerror!U
1372 }1384 }
13731385
1374 {1386 {
1375 var index_file_reader = index_file.reader(&index_file_buffer);1387 var index_file_reader = index_file.reader(io, &index_file_buffer);
1376 const checkout_prog_node = f.prog_node.start("Checkout", 0);1388 const checkout_prog_node = f.prog_node.start("Checkout", 0);
1377 defer checkout_prog_node.end();1389 defer checkout_prog_node.end();
1378 var repository: git.Repository = undefined;1390 var repository: git.Repository = undefined;
...@@ -2029,7 +2041,7 @@ const UnpackResult = struct {...@@ -2029,7 +2041,7 @@ const UnpackResult = struct {
2029 // output errors to string2041 // output errors to string
2030 var errors = try fetch.error_bundle.toOwnedBundle("");2042 var errors = try fetch.error_bundle.toOwnedBundle("");
2031 defer errors.deinit(gpa);2043 defer errors.deinit(gpa);
2032 var aw: std.Io.Writer.Allocating = .init(gpa);2044 var aw: Io.Writer.Allocating = .init(gpa);
2033 defer aw.deinit();2045 defer aw.deinit();
2034 try errors.renderToWriter(.{ .ttyconf = .no_color }, &aw.writer);2046 try errors.renderToWriter(.{ .ttyconf = .no_color }, &aw.writer);
2035 try std.testing.expectEqualStrings(2047 try std.testing.expectEqualStrings(
...@@ -2338,7 +2350,7 @@ const TestFetchBuilder = struct {...@@ -2338,7 +2350,7 @@ const TestFetchBuilder = struct {
2338 if (notes_len > 0) {2350 if (notes_len > 0) {
2339 try std.testing.expectEqual(notes_len, em.notes_len);2351 try std.testing.expectEqual(notes_len, em.notes_len);
2340 }2352 }
2341 var aw: std.Io.Writer.Allocating = .init(std.testing.allocator);2353 var aw: Io.Writer.Allocating = .init(std.testing.allocator);
2342 defer aw.deinit();2354 defer aw.deinit();
2343 try errors.renderToWriter(.{ .ttyconf = .no_color }, &aw.writer);2355 try errors.renderToWriter(.{ .ttyconf = .no_color }, &aw.writer);
2344 try std.testing.expectEqualStrings(msg, aw.written());2356 try std.testing.expectEqualStrings(msg, aw.written());
src/Zcu.zig+19-13
...@@ -4,9 +4,12 @@...@@ -4,9 +4,12 @@
4//!4//!
5//! Each `Compilation` has exactly one or zero `Zcu`, depending on whether5//! Each `Compilation` has exactly one or zero `Zcu`, depending on whether
6//! there is or is not any zig source code, respectively.6//! there is or is not any zig source code, respectively.
7const Zcu = @This();
8const builtin = @import("builtin");
79
8const std = @import("std");10const std = @import("std");
9const builtin = @import("builtin");11const Io = std.Io;
12const Writer = std.Io.Writer;
10const mem = std.mem;13const mem = std.mem;
11const Allocator = std.mem.Allocator;14const Allocator = std.mem.Allocator;
12const assert = std.debug.assert;15const assert = std.debug.assert;
...@@ -15,9 +18,7 @@ const BigIntConst = std.math.big.int.Const;...@@ -15,9 +18,7 @@ const BigIntConst = std.math.big.int.Const;
15const BigIntMutable = std.math.big.int.Mutable;18const BigIntMutable = std.math.big.int.Mutable;
16const Target = std.Target;19const Target = std.Target;
17const Ast = std.zig.Ast;20const Ast = std.zig.Ast;
18const Writer = std.Io.Writer;
1921
20const Zcu = @This();
21const Compilation = @import("Compilation.zig");22const Compilation = @import("Compilation.zig");
22const Cache = std.Build.Cache;23const Cache = std.Build.Cache;
23pub const Value = @import("Value.zig");24pub const Value = @import("Value.zig");
...@@ -1037,10 +1038,15 @@ pub const File = struct {...@@ -1037,10 +1038,15 @@ pub const File = struct {
1037 stat: Cache.File.Stat,1038 stat: Cache.File.Stat,
1038 };1039 };
10391040
1040 pub const GetSourceError = error{ OutOfMemory, FileTooBig } || std.fs.File.OpenError || std.fs.File.ReadError;1041 pub const GetSourceError = error{
1042 OutOfMemory,
1043 FileTooBig,
1044 Streaming,
1045 } || std.fs.File.OpenError || std.fs.File.ReadError;
10411046
1042 pub fn getSource(file: *File, zcu: *const Zcu) GetSourceError!Source {1047 pub fn getSource(file: *File, zcu: *const Zcu) GetSourceError!Source {
1043 const gpa = zcu.gpa;1048 const gpa = zcu.gpa;
1049 const io = zcu.comp.io;
10441050
1045 if (file.source) |source| return .{1051 if (file.source) |source| return .{
1046 .bytes = source,1052 .bytes = source,
...@@ -1061,7 +1067,7 @@ pub const File = struct {...@@ -1061,7 +1067,7 @@ pub const File = struct {
1061 const source = try gpa.allocSentinel(u8, @intCast(stat.size), 0);1067 const source = try gpa.allocSentinel(u8, @intCast(stat.size), 0);
1062 errdefer gpa.free(source);1068 errdefer gpa.free(source);
10631069
1064 var file_reader = f.reader(&.{});1070 var file_reader = f.reader(io, &.{});
1065 file_reader.size = stat.size;1071 file_reader.size = stat.size;
1066 file_reader.interface.readSliceAll(source) catch return file_reader.err.?;1072 file_reader.interface.readSliceAll(source) catch return file_reader.err.?;
10671073
...@@ -2859,9 +2865,9 @@ comptime {...@@ -2859,9 +2865,9 @@ comptime {
2859 }2865 }
2860}2866}
28612867
2862pub fn loadZirCache(gpa: Allocator, cache_file: std.fs.File) !Zir {2868pub fn loadZirCache(gpa: Allocator, io: Io, cache_file: std.fs.File) !Zir {
2863 var buffer: [2000]u8 = undefined;2869 var buffer: [2000]u8 = undefined;
2864 var file_reader = cache_file.reader(&buffer);2870 var file_reader = cache_file.reader(io, &buffer);
2865 return result: {2871 return result: {
2866 const header = file_reader.interface.takeStructPointer(Zir.Header) catch |err| break :result err;2872 const header = file_reader.interface.takeStructPointer(Zir.Header) catch |err| break :result err;
2867 break :result loadZirCacheBody(gpa, header.*, &file_reader.interface);2873 break :result loadZirCacheBody(gpa, header.*, &file_reader.interface);
...@@ -2871,7 +2877,7 @@ pub fn loadZirCache(gpa: Allocator, cache_file: std.fs.File) !Zir {...@@ -2871,7 +2877,7 @@ pub fn loadZirCache(gpa: Allocator, cache_file: std.fs.File) !Zir {
2871 };2877 };
2872}2878}
28732879
2874pub fn loadZirCacheBody(gpa: Allocator, header: Zir.Header, cache_br: *std.Io.Reader) !Zir {2880pub fn loadZirCacheBody(gpa: Allocator, header: Zir.Header, cache_br: *Io.Reader) !Zir {
2875 var instructions: std.MultiArrayList(Zir.Inst) = .{};2881 var instructions: std.MultiArrayList(Zir.Inst) = .{};
2876 errdefer instructions.deinit(gpa);2882 errdefer instructions.deinit(gpa);
28772883
...@@ -2940,7 +2946,7 @@ pub fn saveZirCache(gpa: Allocator, cache_file: std.fs.File, stat: std.fs.File.S...@@ -2940,7 +2946,7 @@ pub fn saveZirCache(gpa: Allocator, cache_file: std.fs.File, stat: std.fs.File.S
29402946
2941 .stat_size = stat.size,2947 .stat_size = stat.size,
2942 .stat_inode = stat.inode,2948 .stat_inode = stat.inode,
2943 .stat_mtime = stat.mtime,2949 .stat_mtime = stat.mtime.toNanoseconds(),
2944 };2950 };
2945 var vecs = [_][]const u8{2951 var vecs = [_][]const u8{
2946 @ptrCast((&header)[0..1]),2952 @ptrCast((&header)[0..1]),
...@@ -2969,7 +2975,7 @@ pub fn saveZoirCache(cache_file: std.fs.File, stat: std.fs.File.Stat, zoir: Zoir...@@ -2969,7 +2975,7 @@ pub fn saveZoirCache(cache_file: std.fs.File, stat: std.fs.File.Stat, zoir: Zoir
29692975
2970 .stat_size = stat.size,2976 .stat_size = stat.size,
2971 .stat_inode = stat.inode,2977 .stat_inode = stat.inode,
2972 .stat_mtime = stat.mtime,2978 .stat_mtime = stat.mtime.toNanoseconds(),
2973 };2979 };
2974 var vecs = [_][]const u8{2980 var vecs = [_][]const u8{
2975 @ptrCast((&header)[0..1]),2981 @ptrCast((&header)[0..1]),
...@@ -2988,7 +2994,7 @@ pub fn saveZoirCache(cache_file: std.fs.File, stat: std.fs.File.Stat, zoir: Zoir...@@ -2988,7 +2994,7 @@ pub fn saveZoirCache(cache_file: std.fs.File, stat: std.fs.File.Stat, zoir: Zoir
2988 };2994 };
2989}2995}
29902996
2991pub fn loadZoirCacheBody(gpa: Allocator, header: Zoir.Header, cache_br: *std.Io.Reader) !Zoir {2997pub fn loadZoirCacheBody(gpa: Allocator, header: Zoir.Header, cache_br: *Io.Reader) !Zoir {
2992 var zoir: Zoir = .{2998 var zoir: Zoir = .{
2993 .nodes = .empty,2999 .nodes = .empty,
2994 .extra = &.{},3000 .extra = &.{},
...@@ -4283,7 +4289,7 @@ const FormatAnalUnit = struct {...@@ -4283,7 +4289,7 @@ const FormatAnalUnit = struct {
4283 zcu: *Zcu,4289 zcu: *Zcu,
4284};4290};
42854291
4286fn formatAnalUnit(data: FormatAnalUnit, writer: *std.Io.Writer) std.Io.Writer.Error!void {4292fn formatAnalUnit(data: FormatAnalUnit, writer: *Io.Writer) Io.Writer.Error!void {
4287 const zcu = data.zcu;4293 const zcu = data.zcu;
4288 const ip = &zcu.intern_pool;4294 const ip = &zcu.intern_pool;
4289 switch (data.unit.unwrap()) {4295 switch (data.unit.unwrap()) {
...@@ -4309,7 +4315,7 @@ fn formatAnalUnit(data: FormatAnalUnit, writer: *std.Io.Writer) std.Io.Writer.Er...@@ -4309,7 +4315,7 @@ fn formatAnalUnit(data: FormatAnalUnit, writer: *std.Io.Writer) std.Io.Writer.Er
43094315
4310const FormatDependee = struct { dependee: InternPool.Dependee, zcu: *Zcu };4316const FormatDependee = struct { dependee: InternPool.Dependee, zcu: *Zcu };
43114317
4312fn formatDependee(data: FormatDependee, writer: *std.Io.Writer) std.Io.Writer.Error!void {4318fn formatDependee(data: FormatDependee, writer: *Io.Writer) Io.Writer.Error!void {
4313 const zcu = data.zcu;4319 const zcu = data.zcu;
4314 const ip = &zcu.intern_pool;4320 const ip = &zcu.intern_pool;
4315 switch (data.dependee) {4321 switch (data.dependee) {
src/Zcu/PerThread.zig+9-8
...@@ -87,6 +87,7 @@ pub fn updateFile(...@@ -87,6 +87,7 @@ pub fn updateFile(
87 const zcu = pt.zcu;87 const zcu = pt.zcu;
88 const comp = zcu.comp;88 const comp = zcu.comp;
89 const gpa = zcu.gpa;89 const gpa = zcu.gpa;
90 const io = comp.io;
9091
91 // In any case we need to examine the stat of the file to determine the course of action.92 // In any case we need to examine the stat of the file to determine the course of action.
92 var source_file = f: {93 var source_file = f: {
...@@ -127,7 +128,7 @@ pub fn updateFile(...@@ -127,7 +128,7 @@ pub fn updateFile(
127 .astgen_failure, .success => lock: {128 .astgen_failure, .success => lock: {
128 const unchanged_metadata =129 const unchanged_metadata =
129 stat.size == file.stat.size and130 stat.size == file.stat.size and
130 stat.mtime == file.stat.mtime and131 stat.mtime.nanoseconds == file.stat.mtime.nanoseconds and
131 stat.inode == file.stat.inode;132 stat.inode == file.stat.inode;
132133
133 if (unchanged_metadata) {134 if (unchanged_metadata) {
...@@ -173,8 +174,6 @@ pub fn updateFile(...@@ -173,8 +174,6 @@ pub fn updateFile(
173 .lock = lock,174 .lock = lock,
174 }) catch |err| switch (err) {175 }) catch |err| switch (err) {
175 error.NotDir => unreachable, // no dir components176 error.NotDir => unreachable, // no dir components
176 error.InvalidUtf8 => unreachable, // it's a hex encoded name
177 error.InvalidWtf8 => unreachable, // it's a hex encoded name
178 error.BadPathName => unreachable, // it's a hex encoded name177 error.BadPathName => unreachable, // it's a hex encoded name
179 error.NameTooLong => unreachable, // it's a fixed size name178 error.NameTooLong => unreachable, // it's a fixed size name
180 error.PipeBusy => unreachable, // it's not a pipe179 error.PipeBusy => unreachable, // it's not a pipe
...@@ -255,7 +254,7 @@ pub fn updateFile(...@@ -255,7 +254,7 @@ pub fn updateFile(
255254
256 const source = try gpa.allocSentinel(u8, @intCast(stat.size), 0);255 const source = try gpa.allocSentinel(u8, @intCast(stat.size), 0);
257 defer if (file.source == null) gpa.free(source);256 defer if (file.source == null) gpa.free(source);
258 var source_fr = source_file.reader(&.{});257 var source_fr = source_file.reader(io, &.{});
259 source_fr.size = stat.size;258 source_fr.size = stat.size;
260 source_fr.interface.readSliceAll(source) catch |err| switch (err) {259 source_fr.interface.readSliceAll(source) catch |err| switch (err) {
261 error.ReadFailed => return source_fr.err.?,260 error.ReadFailed => return source_fr.err.?,
...@@ -353,6 +352,7 @@ fn loadZirZoirCache(...@@ -353,6 +352,7 @@ fn loadZirZoirCache(
353 assert(file.getMode() == mode);352 assert(file.getMode() == mode);
354353
355 const gpa = zcu.gpa;354 const gpa = zcu.gpa;
355 const io = zcu.comp.io;
356356
357 const Header = switch (mode) {357 const Header = switch (mode) {
358 .zig => Zir.Header,358 .zig => Zir.Header,
...@@ -360,7 +360,7 @@ fn loadZirZoirCache(...@@ -360,7 +360,7 @@ fn loadZirZoirCache(
360 };360 };
361361
362 var buffer: [2000]u8 = undefined;362 var buffer: [2000]u8 = undefined;
363 var cache_fr = cache_file.reader(&buffer);363 var cache_fr = cache_file.reader(io, &buffer);
364 cache_fr.size = stat.size;364 cache_fr.size = stat.size;
365 const cache_br = &cache_fr.interface;365 const cache_br = &cache_fr.interface;
366366
...@@ -375,7 +375,7 @@ fn loadZirZoirCache(...@@ -375,7 +375,7 @@ fn loadZirZoirCache(
375375
376 const unchanged_metadata =376 const unchanged_metadata =
377 stat.size == header.stat_size and377 stat.size == header.stat_size and
378 stat.mtime == header.stat_mtime and378 stat.mtime.nanoseconds == header.stat_mtime and
379 stat.inode == header.stat_inode;379 stat.inode == header.stat_inode;
380380
381 if (!unchanged_metadata) {381 if (!unchanged_metadata) {
...@@ -2436,6 +2436,7 @@ fn updateEmbedFileInner(...@@ -2436,6 +2436,7 @@ fn updateEmbedFileInner(
2436 const tid = pt.tid;2436 const tid = pt.tid;
2437 const zcu = pt.zcu;2437 const zcu = pt.zcu;
2438 const gpa = zcu.gpa;2438 const gpa = zcu.gpa;
2439 const io = zcu.comp.io;
2439 const ip = &zcu.intern_pool;2440 const ip = &zcu.intern_pool;
24402441
2441 var file = f: {2442 var file = f: {
...@@ -2450,7 +2451,7 @@ fn updateEmbedFileInner(...@@ -2450,7 +2451,7 @@ fn updateEmbedFileInner(
2450 const old_stat = ef.stat;2451 const old_stat = ef.stat;
2451 const unchanged_metadata =2452 const unchanged_metadata =
2452 stat.size == old_stat.size and2453 stat.size == old_stat.size and
2453 stat.mtime == old_stat.mtime and2454 stat.mtime.nanoseconds == old_stat.mtime.nanoseconds and
2454 stat.inode == old_stat.inode;2455 stat.inode == old_stat.inode;
2455 if (unchanged_metadata) return;2456 if (unchanged_metadata) return;
2456 }2457 }
...@@ -2464,7 +2465,7 @@ fn updateEmbedFileInner(...@@ -2464,7 +2465,7 @@ fn updateEmbedFileInner(
2464 const old_len = string_bytes.mutate.len;2465 const old_len = string_bytes.mutate.len;
2465 errdefer string_bytes.shrinkRetainingCapacity(old_len);2466 errdefer string_bytes.shrinkRetainingCapacity(old_len);
2466 const bytes = (try string_bytes.addManyAsSlice(size_plus_one))[0];2467 const bytes = (try string_bytes.addManyAsSlice(size_plus_one))[0];
2467 var fr = file.reader(&.{});2468 var fr = file.reader(io, &.{});
2468 fr.size = stat.size;2469 fr.size = stat.size;
2469 fr.interface.readSliceAll(bytes[0..size]) catch |err| switch (err) {2470 fr.interface.readSliceAll(bytes[0..size]) catch |err| switch (err) {
2470 error.ReadFailed => return fr.err.?,2471 error.ReadFailed => return fr.err.?,
src/codegen/llvm.zig+8-8
...@@ -794,10 +794,10 @@ pub const Object = struct {...@@ -794,10 +794,10 @@ pub const Object = struct {
794 pub const EmitOptions = struct {794 pub const EmitOptions = struct {
795 pre_ir_path: ?[]const u8,795 pre_ir_path: ?[]const u8,
796 pre_bc_path: ?[]const u8,796 pre_bc_path: ?[]const u8,
797 bin_path: ?[*:0]const u8,797 bin_path: ?[:0]const u8,
798 asm_path: ?[*:0]const u8,798 asm_path: ?[:0]const u8,
799 post_ir_path: ?[*:0]const u8,799 post_ir_path: ?[:0]const u8,
800 post_bc_path: ?[*:0]const u8,800 post_bc_path: ?[]const u8,
801801
802 is_debug: bool,802 is_debug: bool,
803 is_small: bool,803 is_small: bool,
...@@ -1001,7 +1001,7 @@ pub const Object = struct {...@@ -1001,7 +1001,7 @@ pub const Object = struct {
1001 options.post_ir_path == null and options.post_bc_path == null) return;1001 options.post_ir_path == null and options.post_bc_path == null) return;
10021002
1003 if (options.post_bc_path) |path| {1003 if (options.post_bc_path) |path| {
1004 var file = std.fs.cwd().createFileZ(path, .{}) catch |err|1004 var file = std.fs.cwd().createFile(path, .{}) catch |err|
1005 return diags.fail("failed to create '{s}': {s}", .{ path, @errorName(err) });1005 return diags.fail("failed to create '{s}': {s}", .{ path, @errorName(err) });
1006 defer file.close();1006 defer file.close();
10071007
...@@ -1110,8 +1110,8 @@ pub const Object = struct {...@@ -1110,8 +1110,8 @@ pub const Object = struct {
1110 // though it's clearly not ready and produces multiple miscompilations in our std tests.1110 // though it's clearly not ready and produces multiple miscompilations in our std tests.
1111 .allow_machine_outliner = !comp.root_mod.resolved_target.result.cpu.arch.isRISCV(),1111 .allow_machine_outliner = !comp.root_mod.resolved_target.result.cpu.arch.isRISCV(),
1112 .asm_filename = null,1112 .asm_filename = null,
1113 .bin_filename = options.bin_path,1113 .bin_filename = if (options.bin_path) |x| x.ptr else null,
1114 .llvm_ir_filename = options.post_ir_path,1114 .llvm_ir_filename = if (options.post_ir_path) |x| x.ptr else null,
1115 .bitcode_filename = null,1115 .bitcode_filename = null,
11161116
1117 // `.coverage` value is only used when `.sancov` is enabled.1117 // `.coverage` value is only used when `.sancov` is enabled.
...@@ -1158,7 +1158,7 @@ pub const Object = struct {...@@ -1158,7 +1158,7 @@ pub const Object = struct {
1158 lowered_options.time_report_out = &time_report_c_str;1158 lowered_options.time_report_out = &time_report_c_str;
1159 }1159 }
11601160
1161 lowered_options.asm_filename = options.asm_path;1161 lowered_options.asm_filename = if (options.asm_path) |x| x.ptr else null;
1162 if (target_machine.emitToFile(module, &error_message, &lowered_options)) {1162 if (target_machine.emitToFile(module, &error_message, &lowered_options)) {
1163 defer llvm.disposeMessage(error_message);1163 defer llvm.disposeMessage(error_message);
1164 return diags.fail("LLVM failed to emit asm={s} bin={s} ir={s} bc={s}: {s}", .{1164 return diags.fail("LLVM failed to emit asm={s} bin={s} ir={s} bc={s}: {s}", .{
src/fmt.zig+12-4
...@@ -1,4 +1,5 @@...@@ -1,4 +1,5 @@
1const std = @import("std");1const std = @import("std");
2const Io = std.Io;
2const mem = std.mem;3const mem = std.mem;
3const fs = std.fs;4const fs = std.fs;
4const process = std.process;5const process = std.process;
...@@ -34,13 +35,14 @@ const Fmt = struct {...@@ -34,13 +35,14 @@ const Fmt = struct {
34 color: Color,35 color: Color,
35 gpa: Allocator,36 gpa: Allocator,
36 arena: Allocator,37 arena: Allocator,
38 io: Io,
37 out_buffer: std.Io.Writer.Allocating,39 out_buffer: std.Io.Writer.Allocating,
38 stdout_writer: *fs.File.Writer,40 stdout_writer: *fs.File.Writer,
3941
40 const SeenMap = std.AutoHashMap(fs.File.INode, void);42 const SeenMap = std.AutoHashMap(fs.File.INode, void);
41};43};
4244
43pub fn run(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {45pub fn run(gpa: Allocator, arena: Allocator, io: Io, args: []const []const u8) !void {
44 var color: Color = .auto;46 var color: Color = .auto;
45 var stdin_flag = false;47 var stdin_flag = false;
46 var check_flag = false;48 var check_flag = false;
...@@ -99,7 +101,7 @@ pub fn run(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {...@@ -99,7 +101,7 @@ pub fn run(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
99101
100 const stdin: fs.File = .stdin();102 const stdin: fs.File = .stdin();
101 var stdio_buffer: [1024]u8 = undefined;103 var stdio_buffer: [1024]u8 = undefined;
102 var file_reader: fs.File.Reader = stdin.reader(&stdio_buffer);104 var file_reader: fs.File.Reader = stdin.reader(io, &stdio_buffer);
103 const source_code = std.zig.readSourceFileToEndAlloc(gpa, &file_reader) catch |err| {105 const source_code = std.zig.readSourceFileToEndAlloc(gpa, &file_reader) catch |err| {
104 fatal("unable to read stdin: {}", .{err});106 fatal("unable to read stdin: {}", .{err});
105 };107 };
...@@ -165,6 +167,7 @@ pub fn run(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {...@@ -165,6 +167,7 @@ pub fn run(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
165 var fmt: Fmt = .{167 var fmt: Fmt = .{
166 .gpa = gpa,168 .gpa = gpa,
167 .arena = arena,169 .arena = arena,
170 .io = io,
168 .seen = .init(gpa),171 .seen = .init(gpa),
169 .any_error = false,172 .any_error = false,
170 .check_ast = check_ast_flag,173 .check_ast = check_ast_flag,
...@@ -255,6 +258,8 @@ fn fmtPathFile(...@@ -255,6 +258,8 @@ fn fmtPathFile(
255 dir: fs.Dir,258 dir: fs.Dir,
256 sub_path: []const u8,259 sub_path: []const u8,
257) !void {260) !void {
261 const io = fmt.io;
262
258 const source_file = try dir.openFile(sub_path, .{});263 const source_file = try dir.openFile(sub_path, .{});
259 var file_closed = false;264 var file_closed = false;
260 errdefer if (!file_closed) source_file.close();265 errdefer if (!file_closed) source_file.close();
...@@ -265,7 +270,7 @@ fn fmtPathFile(...@@ -265,7 +270,7 @@ fn fmtPathFile(
265 return error.IsDir;270 return error.IsDir;
266271
267 var read_buffer: [1024]u8 = undefined;272 var read_buffer: [1024]u8 = undefined;
268 var file_reader: fs.File.Reader = source_file.reader(&read_buffer);273 var file_reader: fs.File.Reader = source_file.reader(io, &read_buffer);
269 file_reader.size = stat.size;274 file_reader.size = stat.size;
270275
271 const gpa = fmt.gpa;276 const gpa = fmt.gpa;
...@@ -363,5 +368,8 @@ pub fn main() !void {...@@ -363,5 +368,8 @@ pub fn main() !void {
363 var arena_instance = std.heap.ArenaAllocator.init(gpa);368 var arena_instance = std.heap.ArenaAllocator.init(gpa);
364 const arena = arena_instance.allocator();369 const arena = arena_instance.allocator();
365 const args = try process.argsAlloc(arena);370 const args = try process.argsAlloc(arena);
366 return run(gpa, arena, args[1..]);371 var threaded: std.Io.Threaded = .init(gpa);
372 defer threaded.deinit();
373 const io = threaded.io();
374 return run(gpa, arena, io, args[1..]);
367}375}
src/libs/freebsd.zig+4-1
...@@ -426,6 +426,7 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) anye...@@ -426,6 +426,7 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) anye
426 }426 }
427427
428 const gpa = comp.gpa;428 const gpa = comp.gpa;
429 const io = comp.io;
429430
430 var arena_allocator = std.heap.ArenaAllocator.init(gpa);431 var arena_allocator = std.heap.ArenaAllocator.init(gpa);
431 defer arena_allocator.deinit();432 defer arena_allocator.deinit();
...@@ -438,6 +439,7 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) anye...@@ -438,6 +439,7 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) anye
438 // Use the global cache directory.439 // Use the global cache directory.
439 var cache: Cache = .{440 var cache: Cache = .{
440 .gpa = gpa,441 .gpa = gpa,
442 .io = io,
441 .manifest_dir = try comp.dirs.global_cache.handle.makeOpenPath("h", .{}),443 .manifest_dir = try comp.dirs.global_cache.handle.makeOpenPath("h", .{}),
442 };444 };
443 cache.addPrefix(.{ .path = null, .handle = fs.cwd() });445 cache.addPrefix(.{ .path = null, .handle = fs.cwd() });
...@@ -1017,6 +1019,7 @@ fn buildSharedLib(...@@ -1017,6 +1019,7 @@ fn buildSharedLib(
1017 const tracy = trace(@src());1019 const tracy = trace(@src());
1018 defer tracy.end();1020 defer tracy.end();
10191021
1022 const io = comp.io;
1020 const basename = try std.fmt.allocPrint(arena, "lib{s}.so.{d}", .{ lib.name, lib.sover });1023 const basename = try std.fmt.allocPrint(arena, "lib{s}.so.{d}", .{ lib.name, lib.sover });
1021 const version: Version = .{ .major = lib.sover, .minor = 0, .patch = 0 };1024 const version: Version = .{ .major = lib.sover, .minor = 0, .patch = 0 };
1022 const ld_basename = path.basename(comp.getTarget().standardDynamicLinkerPath().get().?);1025 const ld_basename = path.basename(comp.getTarget().standardDynamicLinkerPath().get().?);
...@@ -1071,7 +1074,7 @@ fn buildSharedLib(...@@ -1071,7 +1074,7 @@ fn buildSharedLib(
1071 const misc_task: Compilation.MiscTask = .@"freebsd libc shared object";1074 const misc_task: Compilation.MiscTask = .@"freebsd libc shared object";
10721075
1073 var sub_create_diag: Compilation.CreateDiagnostic = undefined;1076 var sub_create_diag: Compilation.CreateDiagnostic = undefined;
1074 const sub_compilation = Compilation.create(comp.gpa, arena, &sub_create_diag, .{1077 const sub_compilation = Compilation.create(comp.gpa, arena, io, &sub_create_diag, .{
1075 .dirs = comp.dirs.withoutLocalCache(),1078 .dirs = comp.dirs.withoutLocalCache(),
1076 .thread_pool = comp.thread_pool,1079 .thread_pool = comp.thread_pool,
1077 .self_exe_path = comp.self_exe_path,1080 .self_exe_path = comp.self_exe_path,
src/libs/glibc.zig+4-1
...@@ -666,6 +666,7 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) anye...@@ -666,6 +666,7 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) anye
666 }666 }
667667
668 const gpa = comp.gpa;668 const gpa = comp.gpa;
669 const io = comp.io;
669670
670 var arena_allocator = std.heap.ArenaAllocator.init(gpa);671 var arena_allocator = std.heap.ArenaAllocator.init(gpa);
671 defer arena_allocator.deinit();672 defer arena_allocator.deinit();
...@@ -677,6 +678,7 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) anye...@@ -677,6 +678,7 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) anye
677 // Use the global cache directory.678 // Use the global cache directory.
678 var cache: Cache = .{679 var cache: Cache = .{
679 .gpa = gpa,680 .gpa = gpa,
681 .io = io,
680 .manifest_dir = try comp.dirs.global_cache.handle.makeOpenPath("h", .{}),682 .manifest_dir = try comp.dirs.global_cache.handle.makeOpenPath("h", .{}),
681 };683 };
682 cache.addPrefix(.{ .path = null, .handle = fs.cwd() });684 cache.addPrefix(.{ .path = null, .handle = fs.cwd() });
...@@ -1175,6 +1177,7 @@ fn buildSharedLib(...@@ -1175,6 +1177,7 @@ fn buildSharedLib(
1175 const tracy = trace(@src());1177 const tracy = trace(@src());
1176 defer tracy.end();1178 defer tracy.end();
11771179
1180 const io = comp.io;
1178 const basename = try std.fmt.allocPrint(arena, "lib{s}.so.{d}", .{ lib.name, lib.sover });1181 const basename = try std.fmt.allocPrint(arena, "lib{s}.so.{d}", .{ lib.name, lib.sover });
1179 const version: Version = .{ .major = lib.sover, .minor = 0, .patch = 0 };1182 const version: Version = .{ .major = lib.sover, .minor = 0, .patch = 0 };
1180 const ld_basename = path.basename(comp.getTarget().standardDynamicLinkerPath().get().?);1183 const ld_basename = path.basename(comp.getTarget().standardDynamicLinkerPath().get().?);
...@@ -1229,7 +1232,7 @@ fn buildSharedLib(...@@ -1229,7 +1232,7 @@ fn buildSharedLib(
1229 const misc_task: Compilation.MiscTask = .@"glibc shared object";1232 const misc_task: Compilation.MiscTask = .@"glibc shared object";
12301233
1231 var sub_create_diag: Compilation.CreateDiagnostic = undefined;1234 var sub_create_diag: Compilation.CreateDiagnostic = undefined;
1232 const sub_compilation = Compilation.create(comp.gpa, arena, &sub_create_diag, .{1235 const sub_compilation = Compilation.create(comp.gpa, arena, io, &sub_create_diag, .{
1233 .dirs = comp.dirs.withoutLocalCache(),1236 .dirs = comp.dirs.withoutLocalCache(),
1234 .thread_pool = comp.thread_pool,1237 .thread_pool = comp.thread_pool,
1235 .self_exe_path = comp.self_exe_path,1238 .self_exe_path = comp.self_exe_path,
src/libs/libcxx.zig+4-2
...@@ -123,6 +123,7 @@ pub fn buildLibCxx(comp: *Compilation, prog_node: std.Progress.Node) BuildError!...@@ -123,6 +123,7 @@ pub fn buildLibCxx(comp: *Compilation, prog_node: std.Progress.Node) BuildError!
123 defer arena_allocator.deinit();123 defer arena_allocator.deinit();
124 const arena = arena_allocator.allocator();124 const arena = arena_allocator.allocator();
125125
126 const io = comp.io;
126 const root_name = "c++";127 const root_name = "c++";
127 const output_mode = .Lib;128 const output_mode = .Lib;
128 const link_mode = .static;129 const link_mode = .static;
...@@ -263,7 +264,7 @@ pub fn buildLibCxx(comp: *Compilation, prog_node: std.Progress.Node) BuildError!...@@ -263,7 +264,7 @@ pub fn buildLibCxx(comp: *Compilation, prog_node: std.Progress.Node) BuildError!
263 const misc_task: Compilation.MiscTask = .libcxx;264 const misc_task: Compilation.MiscTask = .libcxx;
264265
265 var sub_create_diag: Compilation.CreateDiagnostic = undefined;266 var sub_create_diag: Compilation.CreateDiagnostic = undefined;
266 const sub_compilation = Compilation.create(comp.gpa, arena, &sub_create_diag, .{267 const sub_compilation = Compilation.create(comp.gpa, arena, io, &sub_create_diag, .{
267 .dirs = comp.dirs.withoutLocalCache(),268 .dirs = comp.dirs.withoutLocalCache(),
268 .self_exe_path = comp.self_exe_path,269 .self_exe_path = comp.self_exe_path,
269 .cache_mode = .whole,270 .cache_mode = .whole,
...@@ -318,6 +319,7 @@ pub fn buildLibCxxAbi(comp: *Compilation, prog_node: std.Progress.Node) BuildErr...@@ -318,6 +319,7 @@ pub fn buildLibCxxAbi(comp: *Compilation, prog_node: std.Progress.Node) BuildErr
318 defer arena_allocator.deinit();319 defer arena_allocator.deinit();
319 const arena = arena_allocator.allocator();320 const arena = arena_allocator.allocator();
320321
322 const io = comp.io;
321 const root_name = "c++abi";323 const root_name = "c++abi";
322 const output_mode = .Lib;324 const output_mode = .Lib;
323 const link_mode = .static;325 const link_mode = .static;
...@@ -455,7 +457,7 @@ pub fn buildLibCxxAbi(comp: *Compilation, prog_node: std.Progress.Node) BuildErr...@@ -455,7 +457,7 @@ pub fn buildLibCxxAbi(comp: *Compilation, prog_node: std.Progress.Node) BuildErr
455 const misc_task: Compilation.MiscTask = .libcxxabi;457 const misc_task: Compilation.MiscTask = .libcxxabi;
456458
457 var sub_create_diag: Compilation.CreateDiagnostic = undefined;459 var sub_create_diag: Compilation.CreateDiagnostic = undefined;
458 const sub_compilation = Compilation.create(comp.gpa, arena, &sub_create_diag, .{460 const sub_compilation = Compilation.create(comp.gpa, arena, io, &sub_create_diag, .{
459 .dirs = comp.dirs.withoutLocalCache(),461 .dirs = comp.dirs.withoutLocalCache(),
460 .self_exe_path = comp.self_exe_path,462 .self_exe_path = comp.self_exe_path,
461 .cache_mode = .whole,463 .cache_mode = .whole,
src/libs/libtsan.zig+2-1
...@@ -25,6 +25,7 @@ pub fn buildTsan(comp: *Compilation, prog_node: std.Progress.Node) BuildError!vo...@@ -25,6 +25,7 @@ pub fn buildTsan(comp: *Compilation, prog_node: std.Progress.Node) BuildError!vo
25 defer arena_allocator.deinit();25 defer arena_allocator.deinit();
26 const arena = arena_allocator.allocator();26 const arena = arena_allocator.allocator();
2727
28 const io = comp.io;
28 const target = comp.getTarget();29 const target = comp.getTarget();
29 const root_name = switch (target.os.tag) {30 const root_name = switch (target.os.tag) {
30 // On Apple platforms, we use the same name as LLVM because the31 // On Apple platforms, we use the same name as LLVM because the
...@@ -277,7 +278,7 @@ pub fn buildTsan(comp: *Compilation, prog_node: std.Progress.Node) BuildError!vo...@@ -277,7 +278,7 @@ pub fn buildTsan(comp: *Compilation, prog_node: std.Progress.Node) BuildError!vo
277 const misc_task: Compilation.MiscTask = .libtsan;278 const misc_task: Compilation.MiscTask = .libtsan;
278279
279 var sub_create_diag: Compilation.CreateDiagnostic = undefined;280 var sub_create_diag: Compilation.CreateDiagnostic = undefined;
280 const sub_compilation = Compilation.create(comp.gpa, arena, &sub_create_diag, .{281 const sub_compilation = Compilation.create(comp.gpa, arena, io, &sub_create_diag, .{
281 .dirs = comp.dirs.withoutLocalCache(),282 .dirs = comp.dirs.withoutLocalCache(),
282 .thread_pool = comp.thread_pool,283 .thread_pool = comp.thread_pool,
283 .self_exe_path = comp.self_exe_path,284 .self_exe_path = comp.self_exe_path,
src/libs/libunwind.zig+2-1
...@@ -26,6 +26,7 @@ pub fn buildStaticLib(comp: *Compilation, prog_node: std.Progress.Node) BuildErr...@@ -26,6 +26,7 @@ pub fn buildStaticLib(comp: *Compilation, prog_node: std.Progress.Node) BuildErr
26 defer arena_allocator.deinit();26 defer arena_allocator.deinit();
27 const arena = arena_allocator.allocator();27 const arena = arena_allocator.allocator();
2828
29 const io = comp.io;
29 const output_mode = .Lib;30 const output_mode = .Lib;
30 const target = &comp.root_mod.resolved_target.result;31 const target = &comp.root_mod.resolved_target.result;
31 const unwind_tables: std.builtin.UnwindTables =32 const unwind_tables: std.builtin.UnwindTables =
...@@ -143,7 +144,7 @@ pub fn buildStaticLib(comp: *Compilation, prog_node: std.Progress.Node) BuildErr...@@ -143,7 +144,7 @@ pub fn buildStaticLib(comp: *Compilation, prog_node: std.Progress.Node) BuildErr
143 const misc_task: Compilation.MiscTask = .libunwind;144 const misc_task: Compilation.MiscTask = .libunwind;
144145
145 var sub_create_diag: Compilation.CreateDiagnostic = undefined;146 var sub_create_diag: Compilation.CreateDiagnostic = undefined;
146 const sub_compilation = Compilation.create(comp.gpa, arena, &sub_create_diag, .{147 const sub_compilation = Compilation.create(comp.gpa, arena, io, &sub_create_diag, .{
147 .dirs = comp.dirs.withoutLocalCache(),148 .dirs = comp.dirs.withoutLocalCache(),
148 .self_exe_path = comp.self_exe_path,149 .self_exe_path = comp.self_exe_path,
149 .config = config,150 .config = config,
src/libs/mingw.zig+3-1
...@@ -235,6 +235,7 @@ pub fn buildImportLib(comp: *Compilation, lib_name: []const u8) !void {...@@ -235,6 +235,7 @@ pub fn buildImportLib(comp: *Compilation, lib_name: []const u8) !void {
235 dev.check(.build_import_lib);235 dev.check(.build_import_lib);
236236
237 const gpa = comp.gpa;237 const gpa = comp.gpa;
238 const io = comp.io;
238239
239 var arena_allocator = std.heap.ArenaAllocator.init(gpa);240 var arena_allocator = std.heap.ArenaAllocator.init(gpa);
240 defer arena_allocator.deinit();241 defer arena_allocator.deinit();
...@@ -255,6 +256,7 @@ pub fn buildImportLib(comp: *Compilation, lib_name: []const u8) !void {...@@ -255,6 +256,7 @@ pub fn buildImportLib(comp: *Compilation, lib_name: []const u8) !void {
255 // Use the global cache directory.256 // Use the global cache directory.
256 var cache: Cache = .{257 var cache: Cache = .{
257 .gpa = gpa,258 .gpa = gpa,
259 .io = io,
258 .manifest_dir = try comp.dirs.global_cache.handle.makeOpenPath("h", .{}),260 .manifest_dir = try comp.dirs.global_cache.handle.makeOpenPath("h", .{}),
259 };261 };
260 cache.addPrefix(.{ .path = null, .handle = std.fs.cwd() });262 cache.addPrefix(.{ .path = null, .handle = std.fs.cwd() });
...@@ -302,7 +304,7 @@ pub fn buildImportLib(comp: *Compilation, lib_name: []const u8) !void {...@@ -302,7 +304,7 @@ pub fn buildImportLib(comp: *Compilation, lib_name: []const u8) !void {
302 .output = .{ .to_list = .{ .arena = .init(gpa) } },304 .output = .{ .to_list = .{ .arena = .init(gpa) } },
303 };305 };
304 defer diagnostics.deinit();306 defer diagnostics.deinit();
305 var aro_comp = aro.Compilation.init(gpa, arena, &diagnostics, std.fs.cwd());307 var aro_comp = aro.Compilation.init(gpa, arena, io, &diagnostics, std.fs.cwd());
306 defer aro_comp.deinit();308 defer aro_comp.deinit();
307309
308 aro_comp.target = target.*;310 aro_comp.target = target.*;
src/libs/musl.zig+2-1
...@@ -26,6 +26,7 @@ pub fn buildCrtFile(comp: *Compilation, in_crt_file: CrtFile, prog_node: std.Pro...@@ -26,6 +26,7 @@ pub fn buildCrtFile(comp: *Compilation, in_crt_file: CrtFile, prog_node: std.Pro
26 var arena_allocator = std.heap.ArenaAllocator.init(gpa);26 var arena_allocator = std.heap.ArenaAllocator.init(gpa);
27 defer arena_allocator.deinit();27 defer arena_allocator.deinit();
28 const arena = arena_allocator.allocator();28 const arena = arena_allocator.allocator();
29 const io = comp.io;
2930
30 switch (in_crt_file) {31 switch (in_crt_file) {
31 .crt1_o => {32 .crt1_o => {
...@@ -246,7 +247,7 @@ pub fn buildCrtFile(comp: *Compilation, in_crt_file: CrtFile, prog_node: std.Pro...@@ -246,7 +247,7 @@ pub fn buildCrtFile(comp: *Compilation, in_crt_file: CrtFile, prog_node: std.Pro
246 const misc_task: Compilation.MiscTask = .@"musl libc.so";247 const misc_task: Compilation.MiscTask = .@"musl libc.so";
247248
248 var sub_create_diag: Compilation.CreateDiagnostic = undefined;249 var sub_create_diag: Compilation.CreateDiagnostic = undefined;
249 const sub_compilation = Compilation.create(comp.gpa, arena, &sub_create_diag, .{250 const sub_compilation = Compilation.create(comp.gpa, arena, io, &sub_create_diag, .{
250 .dirs = comp.dirs.withoutLocalCache(),251 .dirs = comp.dirs.withoutLocalCache(),
251 .self_exe_path = comp.self_exe_path,252 .self_exe_path = comp.self_exe_path,
252 .cache_mode = .whole,253 .cache_mode = .whole,
src/libs/netbsd.zig+4-1
...@@ -372,6 +372,7 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) anye...@@ -372,6 +372,7 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) anye
372 }372 }
373373
374 const gpa = comp.gpa;374 const gpa = comp.gpa;
375 const io = comp.io;
375376
376 var arena_allocator = std.heap.ArenaAllocator.init(gpa);377 var arena_allocator = std.heap.ArenaAllocator.init(gpa);
377 defer arena_allocator.deinit();378 defer arena_allocator.deinit();
...@@ -383,6 +384,7 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) anye...@@ -383,6 +384,7 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) anye
383 // Use the global cache directory.384 // Use the global cache directory.
384 var cache: Cache = .{385 var cache: Cache = .{
385 .gpa = gpa,386 .gpa = gpa,
387 .io = io,
386 .manifest_dir = try comp.dirs.global_cache.handle.makeOpenPath("h", .{}),388 .manifest_dir = try comp.dirs.global_cache.handle.makeOpenPath("h", .{}),
387 };389 };
388 cache.addPrefix(.{ .path = null, .handle = fs.cwd() });390 cache.addPrefix(.{ .path = null, .handle = fs.cwd() });
...@@ -680,6 +682,7 @@ fn buildSharedLib(...@@ -680,6 +682,7 @@ fn buildSharedLib(
680 const tracy = trace(@src());682 const tracy = trace(@src());
681 defer tracy.end();683 defer tracy.end();
682684
685 const io = comp.io;
683 const basename = try std.fmt.allocPrint(arena, "lib{s}.so.{d}", .{ lib.name, lib.sover });686 const basename = try std.fmt.allocPrint(arena, "lib{s}.so.{d}", .{ lib.name, lib.sover });
684 const version: Version = .{ .major = lib.sover, .minor = 0, .patch = 0 };687 const version: Version = .{ .major = lib.sover, .minor = 0, .patch = 0 };
685 const ld_basename = path.basename(comp.getTarget().standardDynamicLinkerPath().get().?);688 const ld_basename = path.basename(comp.getTarget().standardDynamicLinkerPath().get().?);
...@@ -733,7 +736,7 @@ fn buildSharedLib(...@@ -733,7 +736,7 @@ fn buildSharedLib(
733 const misc_task: Compilation.MiscTask = .@"netbsd libc shared object";736 const misc_task: Compilation.MiscTask = .@"netbsd libc shared object";
734737
735 var sub_create_diag: Compilation.CreateDiagnostic = undefined;738 var sub_create_diag: Compilation.CreateDiagnostic = undefined;
736 const sub_compilation = Compilation.create(comp.gpa, arena, &sub_create_diag, .{739 const sub_compilation = Compilation.create(comp.gpa, arena, io, &sub_create_diag, .{
737 .dirs = comp.dirs.withoutLocalCache(),740 .dirs = comp.dirs.withoutLocalCache(),
738 .thread_pool = comp.thread_pool,741 .thread_pool = comp.thread_pool,
739 .self_exe_path = comp.self_exe_path,742 .self_exe_path = comp.self_exe_path,
src/link/Lld.zig+6-8
...@@ -1614,11 +1614,9 @@ fn wasmLink(lld: *Lld, arena: Allocator) !void {...@@ -1614,11 +1614,9 @@ fn wasmLink(lld: *Lld, arena: Allocator) !void {
1614 }1614 }
1615}1615}
16161616
1617fn spawnLld(1617fn spawnLld(comp: *Compilation, arena: Allocator, argv: []const []const u8) !void {
1618 comp: *Compilation,1618 const io = comp.io;
1619 arena: Allocator,1619
1620 argv: []const []const u8,
1621) !void {
1622 if (comp.verbose_link) {1620 if (comp.verbose_link) {
1623 // Skip over our own name so that the LLD linker name is the first argv item.1621 // Skip over our own name so that the LLD linker name is the first argv item.
1624 Compilation.dump_argv(argv[1..]);1622 Compilation.dump_argv(argv[1..]);
...@@ -1650,7 +1648,7 @@ fn spawnLld(...@@ -1650,7 +1648,7 @@ fn spawnLld(
1650 child.stderr_behavior = .Pipe;1648 child.stderr_behavior = .Pipe;
16511649
1652 child.spawn() catch |err| break :term err;1650 child.spawn() catch |err| break :term err;
1653 var stderr_reader = child.stderr.?.readerStreaming(&.{});1651 var stderr_reader = child.stderr.?.readerStreaming(io, &.{});
1654 stderr = try stderr_reader.interface.allocRemaining(comp.gpa, .unlimited);1652 stderr = try stderr_reader.interface.allocRemaining(comp.gpa, .unlimited);
1655 break :term child.wait();1653 break :term child.wait();
1656 }) catch |first_err| term: {1654 }) catch |first_err| term: {
...@@ -1660,7 +1658,7 @@ fn spawnLld(...@@ -1660,7 +1658,7 @@ fn spawnLld(
1660 const rand_int = std.crypto.random.int(u64);1658 const rand_int = std.crypto.random.int(u64);
1661 const rsp_path = "tmp" ++ s ++ std.fmt.hex(rand_int) ++ ".rsp";1659 const rsp_path = "tmp" ++ s ++ std.fmt.hex(rand_int) ++ ".rsp";
16621660
1663 const rsp_file = try comp.dirs.local_cache.handle.createFileZ(rsp_path, .{});1661 const rsp_file = try comp.dirs.local_cache.handle.createFile(rsp_path, .{});
1664 defer comp.dirs.local_cache.handle.deleteFileZ(rsp_path) catch |err|1662 defer comp.dirs.local_cache.handle.deleteFileZ(rsp_path) catch |err|
1665 log.warn("failed to delete response file {s}: {s}", .{ rsp_path, @errorName(err) });1663 log.warn("failed to delete response file {s}: {s}", .{ rsp_path, @errorName(err) });
1666 {1664 {
...@@ -1700,7 +1698,7 @@ fn spawnLld(...@@ -1700,7 +1698,7 @@ fn spawnLld(
1700 rsp_child.stderr_behavior = .Pipe;1698 rsp_child.stderr_behavior = .Pipe;
17011699
1702 rsp_child.spawn() catch |err| break :err err;1700 rsp_child.spawn() catch |err| break :err err;
1703 var stderr_reader = rsp_child.stderr.?.readerStreaming(&.{});1701 var stderr_reader = rsp_child.stderr.?.readerStreaming(io, &.{});
1704 stderr = try stderr_reader.interface.allocRemaining(comp.gpa, .unlimited);1702 stderr = try stderr_reader.interface.allocRemaining(comp.gpa, .unlimited);
1705 break :term rsp_child.wait() catch |err| break :err err;1703 break :term rsp_child.wait() catch |err| break :err err;
1706 }1704 }
src/link/MachO.zig+7-9
...@@ -915,7 +915,7 @@ pub fn readArMagic(file: std.fs.File, offset: usize, buffer: *[Archive.SARMAG]u8...@@ -915,7 +915,7 @@ pub fn readArMagic(file: std.fs.File, offset: usize, buffer: *[Archive.SARMAG]u8
915 return buffer[0..Archive.SARMAG];915 return buffer[0..Archive.SARMAG];
916}916}
917917
918fn addObject(self: *MachO, path: Path, handle: File.HandleIndex, offset: u64) !void {918fn addObject(self: *MachO, path: Path, handle_index: File.HandleIndex, offset: u64) !void {
919 const tracy = trace(@src());919 const tracy = trace(@src());
920 defer tracy.end();920 defer tracy.end();
921921
...@@ -929,17 +929,15 @@ fn addObject(self: *MachO, path: Path, handle: File.HandleIndex, offset: u64) !v...@@ -929,17 +929,15 @@ fn addObject(self: *MachO, path: Path, handle: File.HandleIndex, offset: u64) !v
929 });929 });
930 errdefer gpa.free(abs_path);930 errdefer gpa.free(abs_path);
931931
932 const mtime: u64 = mtime: {932 const file = self.getFileHandle(handle_index);
933 const file = self.getFileHandle(handle);933 const stat = try file.stat();
934 const stat = file.stat() catch break :mtime 0;934 const mtime = stat.mtime.toSeconds();
935 break :mtime @as(u64, @intCast(@divFloor(stat.mtime, 1_000_000_000)));935 const index: File.Index = @intCast(try self.files.addOne(gpa));
936 };
937 const index = @as(File.Index, @intCast(try self.files.addOne(gpa)));
938 self.files.set(index, .{ .object = .{936 self.files.set(index, .{ .object = .{
939 .offset = offset,937 .offset = offset,
940 .path = abs_path,938 .path = abs_path,
941 .file_handle = handle,939 .file_handle = handle_index,
942 .mtime = mtime,940 .mtime = @intCast(mtime),
943 .index = index,941 .index = index,
944 } });942 } });
945 try self.objects.append(gpa, index);943 try self.objects.append(gpa, index);
src/link/MappedFile.zig+8-6
...@@ -16,11 +16,13 @@ writers: std.SinglyLinkedList,...@@ -16,11 +16,13 @@ writers: std.SinglyLinkedList,
1616
17pub const growth_factor = 4;17pub const growth_factor = 4;
1818
19pub const Error = std.posix.MMapError ||19pub const Error = std.posix.MMapError || std.posix.MRemapError || std.fs.File.SetEndPosError || error{
20 std.posix.MRemapError ||20 NotFile,
21 std.fs.File.SetEndPosError ||21 SystemResources,
22 std.fs.File.CopyRangeError ||22 IsDir,
23 error{NotFile};23 Unseekable,
24 NoSpaceLeft,
25};
2426
25pub fn init(file: std.fs.File, gpa: std.mem.Allocator) !MappedFile {27pub fn init(file: std.fs.File, gpa: std.mem.Allocator) !MappedFile {
26 var mf: MappedFile = .{28 var mf: MappedFile = .{
...@@ -402,7 +404,7 @@ pub const Node = extern struct {...@@ -402,7 +404,7 @@ pub const Node = extern struct {
402404
403 const w: *Writer = @fieldParentPtr("interface", interface);405 const w: *Writer = @fieldParentPtr("interface", interface);
404 const copy_size: usize = @intCast(w.mf.copyFileRange(406 const copy_size: usize = @intCast(w.mf.copyFileRange(
405 file_reader.file,407 .adaptFromNewApi(file_reader.file),
406 file_reader.pos,408 file_reader.pos,
407 w.ni.fileLocation(w.mf, true).offset + interface.end,409 w.ni.fileLocation(w.mf, true).offset + interface.end,
408 limit.minInt(interface.unusedCapacityLen()),410 limit.minInt(interface.unusedCapacityLen()),
src/link/Wasm.zig+14-6
...@@ -3029,18 +3029,22 @@ fn openParseObjectReportingFailure(wasm: *Wasm, path: Path) void {...@@ -3029,18 +3029,22 @@ fn openParseObjectReportingFailure(wasm: *Wasm, path: Path) void {
3029fn parseObject(wasm: *Wasm, obj: link.Input.Object) !void {3029fn parseObject(wasm: *Wasm, obj: link.Input.Object) !void {
3030 log.debug("parseObject {f}", .{obj.path});3030 log.debug("parseObject {f}", .{obj.path});
3031 const gpa = wasm.base.comp.gpa;3031 const gpa = wasm.base.comp.gpa;
3032 const io = wasm.base.comp.io;
3032 const gc_sections = wasm.base.gc_sections;3033 const gc_sections = wasm.base.gc_sections;
30333034
3034 defer obj.file.close();3035 defer obj.file.close();
30353036
3037 var file_reader = obj.file.reader(io, &.{});
3038
3036 try wasm.objects.ensureUnusedCapacity(gpa, 1);3039 try wasm.objects.ensureUnusedCapacity(gpa, 1);
3037 const stat = try obj.file.stat();3040 const size = std.math.cast(usize, try file_reader.getSize()) orelse return error.FileTooBig;
3038 const size = std.math.cast(usize, stat.size) orelse return error.FileTooBig;
30393041
3040 const file_contents = try gpa.alloc(u8, size);3042 const file_contents = try gpa.alloc(u8, size);
3041 defer gpa.free(file_contents);3043 defer gpa.free(file_contents);
30423044
3043 const n = try obj.file.preadAll(file_contents, 0);3045 const n = file_reader.interface.readSliceShort(file_contents) catch |err| switch (err) {
3046 error.ReadFailed => return file_reader.err.?,
3047 };
3044 if (n != file_contents.len) return error.UnexpectedEndOfFile;3048 if (n != file_contents.len) return error.UnexpectedEndOfFile;
30453049
3046 var ss: Object.ScratchSpace = .{};3050 var ss: Object.ScratchSpace = .{};
...@@ -3053,17 +3057,21 @@ fn parseObject(wasm: *Wasm, obj: link.Input.Object) !void {...@@ -3053,17 +3057,21 @@ fn parseObject(wasm: *Wasm, obj: link.Input.Object) !void {
3053fn parseArchive(wasm: *Wasm, obj: link.Input.Object) !void {3057fn parseArchive(wasm: *Wasm, obj: link.Input.Object) !void {
3054 log.debug("parseArchive {f}", .{obj.path});3058 log.debug("parseArchive {f}", .{obj.path});
3055 const gpa = wasm.base.comp.gpa;3059 const gpa = wasm.base.comp.gpa;
3060 const io = wasm.base.comp.io;
3056 const gc_sections = wasm.base.gc_sections;3061 const gc_sections = wasm.base.gc_sections;
30573062
3058 defer obj.file.close();3063 defer obj.file.close();
30593064
3060 const stat = try obj.file.stat();3065 var file_reader = obj.file.reader(io, &.{});
3061 const size = std.math.cast(usize, stat.size) orelse return error.FileTooBig;3066
3067 const size = std.math.cast(usize, try file_reader.getSize()) orelse return error.FileTooBig;
30623068
3063 const file_contents = try gpa.alloc(u8, size);3069 const file_contents = try gpa.alloc(u8, size);
3064 defer gpa.free(file_contents);3070 defer gpa.free(file_contents);
30653071
3066 const n = try obj.file.preadAll(file_contents, 0);3072 const n = file_reader.interface.readSliceShort(file_contents) catch |err| switch (err) {
3073 error.ReadFailed => return file_reader.err.?,
3074 };
3067 if (n != file_contents.len) return error.UnexpectedEndOfFile;3075 if (n != file_contents.len) return error.UnexpectedEndOfFile;
30683076
3069 var archive = try Archive.parse(gpa, file_contents);3077 var archive = try Archive.parse(gpa, file_contents);
src/link/Wasm/Flush.zig+8-3
...@@ -1064,9 +1064,14 @@ pub fn finish(f: *Flush, wasm: *Wasm) !void {...@@ -1064,9 +1064,14 @@ pub fn finish(f: *Flush, wasm: *Wasm) !void {
1064 }1064 }
10651065
1066 // Finally, write the entire binary into the file.1066 // Finally, write the entire binary into the file.
1067 const file = wasm.base.file.?;1067 var file_writer = wasm.base.file.?.writer(&.{});
1068 try file.pwriteAll(binary_bytes.items, 0);1068 file_writer.interface.writeAll(binary_bytes.items) catch |err| switch (err) {
1069 try file.setEndPos(binary_bytes.items.len);1069 error.WriteFailed => return file_writer.err.?,
1070 };
1071 file_writer.end() catch |err| switch (err) {
1072 error.WriteFailed => return file_writer.err.?,
1073 else => |e| return e,
1074 };
1070}1075}
10711076
1072const VirtualAddrs = struct {1077const VirtualAddrs = struct {
src/main.zig+13-15
...@@ -312,7 +312,7 @@ fn mainArgs(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {...@@ -312,7 +312,7 @@ fn mainArgs(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
312 });312 });
313 } else if (mem.eql(u8, cmd, "fmt")) {313 } else if (mem.eql(u8, cmd, "fmt")) {
314 dev.check(.fmt_command);314 dev.check(.fmt_command);
315 return @import("fmt.zig").run(gpa, arena, cmd_args);315 return @import("fmt.zig").run(gpa, arena, io, cmd_args);
316 } else if (mem.eql(u8, cmd, "objcopy")) {316 } else if (mem.eql(u8, cmd, "objcopy")) {
317 return jitCmd(gpa, arena, io, cmd_args, .{317 return jitCmd(gpa, arena, io, cmd_args, .{
318 .cmd_name = "objcopy",318 .cmd_name = "objcopy",
...@@ -376,7 +376,7 @@ fn mainArgs(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {...@@ -376,7 +376,7 @@ fn mainArgs(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
376 } else if (build_options.enable_debug_extensions and mem.eql(u8, cmd, "changelist")) {376 } else if (build_options.enable_debug_extensions and mem.eql(u8, cmd, "changelist")) {
377 return cmdChangelist(arena, io, cmd_args);377 return cmdChangelist(arena, io, cmd_args);
378 } else if (build_options.enable_debug_extensions and mem.eql(u8, cmd, "dump-zir")) {378 } else if (build_options.enable_debug_extensions and mem.eql(u8, cmd, "dump-zir")) {
379 return cmdDumpZir(arena, cmd_args);379 return cmdDumpZir(arena, io, cmd_args);
380 } else if (build_options.enable_debug_extensions and mem.eql(u8, cmd, "llvm-ints")) {380 } else if (build_options.enable_debug_extensions and mem.eql(u8, cmd, "llvm-ints")) {
381 return cmdDumpLlvmInts(gpa, arena, cmd_args);381 return cmdDumpLlvmInts(gpa, arena, cmd_args);
382 } else {382 } else {
...@@ -3376,7 +3376,7 @@ fn buildOutputType(...@@ -3376,7 +3376,7 @@ fn buildOutputType(
3376 try create_module.rpath_list.appendSlice(arena, rpath_dedup.keys());3376 try create_module.rpath_list.appendSlice(arena, rpath_dedup.keys());
33773377
3378 var create_diag: Compilation.CreateDiagnostic = undefined;3378 var create_diag: Compilation.CreateDiagnostic = undefined;
3379 const comp = Compilation.create(gpa, arena, &create_diag, .{3379 const comp = Compilation.create(gpa, arena, io, &create_diag, .{
3380 .dirs = dirs,3380 .dirs = dirs,
3381 .thread_pool = &thread_pool,3381 .thread_pool = &thread_pool,
3382 .self_exe_path = switch (native_os) {3382 .self_exe_path = switch (native_os) {
...@@ -3554,7 +3554,6 @@ fn buildOutputType(...@@ -3554,7 +3554,6 @@ fn buildOutputType(
3554 var stdin_reader = fs.File.stdin().reader(io, &stdin_buffer);3554 var stdin_reader = fs.File.stdin().reader(io, &stdin_buffer);
3555 var stdout_writer = fs.File.stdout().writer(&stdout_buffer);3555 var stdout_writer = fs.File.stdout().writer(&stdout_buffer);
3556 try serve(3556 try serve(
3557 io,
3558 comp,3557 comp,
3559 &stdin_reader.interface,3558 &stdin_reader.interface,
3560 &stdout_writer.interface,3559 &stdout_writer.interface,
...@@ -3581,7 +3580,6 @@ fn buildOutputType(...@@ -3581,7 +3580,6 @@ fn buildOutputType(
3581 var output = stream.writer(io, &stdout_buffer);3580 var output = stream.writer(io, &stdout_buffer);
35823581
3583 try serve(3582 try serve(
3584 io,
3585 comp,3583 comp,
3586 &input.interface,3584 &input.interface,
3587 &output.interface,3585 &output.interface,
...@@ -4051,7 +4049,6 @@ fn saveState(comp: *Compilation, incremental: bool) void {...@@ -4051,7 +4049,6 @@ fn saveState(comp: *Compilation, incremental: bool) void {
4051}4049}
40524050
4053fn serve(4051fn serve(
4054 io: Io,
4055 comp: *Compilation,4052 comp: *Compilation,
4056 in: *Io.Reader,4053 in: *Io.Reader,
4057 out: *Io.Writer,4054 out: *Io.Writer,
...@@ -4104,7 +4101,7 @@ fn serve(...@@ -4104,7 +4101,7 @@ fn serve(
4104 defer arena_instance.deinit();4101 defer arena_instance.deinit();
4105 const arena = arena_instance.allocator();4102 const arena = arena_instance.allocator();
4106 var output: Compilation.CImportResult = undefined;4103 var output: Compilation.CImportResult = undefined;
4107 try cmdTranslateC(io, comp, arena, &output, file_system_inputs, main_progress_node);4104 try cmdTranslateC(comp, arena, &output, file_system_inputs, main_progress_node);
4108 defer output.deinit(gpa);4105 defer output.deinit(gpa);
41094106
4110 if (file_system_inputs.items.len != 0) {4107 if (file_system_inputs.items.len != 0) {
...@@ -4537,6 +4534,8 @@ fn cmdTranslateC(...@@ -4537,6 +4534,8 @@ fn cmdTranslateC(
4537) !void {4534) !void {
4538 dev.check(.translate_c_command);4535 dev.check(.translate_c_command);
45394536
4537 const io = comp.io;
4538
4540 assert(comp.c_source_files.len == 1);4539 assert(comp.c_source_files.len == 1);
4541 const c_source_file = comp.c_source_files[0];4540 const c_source_file = comp.c_source_files[0];
45424541
...@@ -4600,7 +4599,7 @@ fn cmdTranslateC(...@@ -4600,7 +4599,7 @@ fn cmdTranslateC(
4600 };4599 };
4601 defer zig_file.close();4600 defer zig_file.close();
4602 var stdout_writer = fs.File.stdout().writer(&stdout_buffer);4601 var stdout_writer = fs.File.stdout().writer(&stdout_buffer);
4603 var file_reader = zig_file.reader(&.{});4602 var file_reader = zig_file.reader(io, &.{});
4604 _ = try stdout_writer.interface.sendFileAll(&file_reader, .unlimited);4603 _ = try stdout_writer.interface.sendFileAll(&file_reader, .unlimited);
4605 try stdout_writer.interface.flush();4604 try stdout_writer.interface.flush();
4606 return cleanExit();4605 return cleanExit();
...@@ -5156,6 +5155,7 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, io: Io, args: []const []const u8)...@@ -5156,6 +5155,7 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, io: Io, args: []const []const u8)
51565155
5157 var fetch: Package.Fetch = .{5156 var fetch: Package.Fetch = .{
5158 .arena = std.heap.ArenaAllocator.init(gpa),5157 .arena = std.heap.ArenaAllocator.init(gpa),
5158 .io = io,
5159 .location = .{ .relative_path = phantom_package_root },5159 .location = .{ .relative_path = phantom_package_root },
5160 .location_tok = 0,5160 .location_tok = 0,
5161 .hash_tok = .none,5161 .hash_tok = .none,
...@@ -5278,7 +5278,7 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, io: Io, args: []const []const u8)...@@ -5278,7 +5278,7 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, io: Io, args: []const []const u8)
5278 try root_mod.deps.put(arena, "@build", build_mod);5278 try root_mod.deps.put(arena, "@build", build_mod);
52795279
5280 var create_diag: Compilation.CreateDiagnostic = undefined;5280 var create_diag: Compilation.CreateDiagnostic = undefined;
5281 const comp = Compilation.create(gpa, arena, &create_diag, .{5281 const comp = Compilation.create(gpa, arena, io, &create_diag, .{
5282 .libc_installation = libc_installation,5282 .libc_installation = libc_installation,
5283 .dirs = dirs,5283 .dirs = dirs,
5284 .root_name = "build",5284 .root_name = "build",
...@@ -5522,7 +5522,7 @@ fn jitCmd(...@@ -5522,7 +5522,7 @@ fn jitCmd(
5522 }5522 }
55235523
5524 var create_diag: Compilation.CreateDiagnostic = undefined;5524 var create_diag: Compilation.CreateDiagnostic = undefined;
5525 const comp = Compilation.create(gpa, arena, &create_diag, .{5525 const comp = Compilation.create(gpa, arena, io, &create_diag, .{
5526 .dirs = dirs,5526 .dirs = dirs,
5527 .root_name = options.cmd_name,5527 .root_name = options.cmd_name,
5528 .config = config,5528 .config = config,
...@@ -6400,10 +6400,7 @@ fn cmdDumpLlvmInts(...@@ -6400,10 +6400,7 @@ fn cmdDumpLlvmInts(
6400}6400}
64016401
6402/// This is only enabled for debug builds.6402/// This is only enabled for debug builds.
6403fn cmdDumpZir(6403fn cmdDumpZir(arena: Allocator, io: Io, args: []const []const u8) !void {
6404 arena: Allocator,
6405 args: []const []const u8,
6406) !void {
6407 dev.check(.dump_zir_command);6404 dev.check(.dump_zir_command);
64086405
6409 const Zir = std.zig.Zir;6406 const Zir = std.zig.Zir;
...@@ -6415,7 +6412,7 @@ fn cmdDumpZir(...@@ -6415,7 +6412,7 @@ fn cmdDumpZir(
6415 };6412 };
6416 defer f.close();6413 defer f.close();
64176414
6418 const zir = try Zcu.loadZirCache(arena, f);6415 const zir = try Zcu.loadZirCache(arena, io, f);
6419 var stdout_writer = fs.File.stdout().writerStreaming(&stdout_buffer);6416 var stdout_writer = fs.File.stdout().writerStreaming(&stdout_buffer);
6420 const stdout_bw = &stdout_writer.interface;6417 const stdout_bw = &stdout_writer.interface;
6421 {6418 {
...@@ -6914,6 +6911,7 @@ fn cmdFetch(...@@ -6914,6 +6911,7 @@ fn cmdFetch(
69146911
6915 var fetch: Package.Fetch = .{6912 var fetch: Package.Fetch = .{
6916 .arena = std.heap.ArenaAllocator.init(gpa),6913 .arena = std.heap.ArenaAllocator.init(gpa),
6914 .io = io,
6917 .location = .{ .path_or_url = path_or_url },6915 .location = .{ .path_or_url = path_or_url },
6918 .location_tok = 0,6916 .location_tok = 0,
6919 .hash_tok = .none,6917 .hash_tok = .none,