authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-03-17 23:03:45-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-03-17 23:03:45-04:00
logdbde5df568597c63e28bb1244d695156afbad5d0
tree4046729881ce284aaf4369e03d30bce7fa435911
parent7251eb1681d269ef5672193a608b580e371981fb
signaturelock-open Commit is signed but in an unrecognized format.

clean up some self-hosted bitrot + don't assume libstdc++

closes #4682 The self-hosted compiler is still bit rotted and still not compiling successfully yet. I have a more serious rework of the code in a different branch.

9 files changed, 158 insertions(+), 139 deletions(-)

build.zig+9-5
......@@ -298,10 +298,14 @@ fn configureStage2(b: *Builder, exe: var, ctx: Context) !void {
298298 dependOnLib(b, exe, ctx.llvm);
299299
300300 if (exe.target.getOsTag() == .linux) {
301 try addCxxKnownPath(b, ctx, exe, "libstdc++.a",
302 \\Unable to determine path to libstdc++.a
303 \\On Fedora, install libstdc++-static and try again.
304 );
301 // First we try to static link against gcc libstdc++. If that doesn't work,
302 // we fall back to -lc++ and cross our fingers.
303 addCxxKnownPath(b, ctx, exe, "libstdc++.a", "") catch |err| switch (err) {
304 error.RequiredLibraryNotFound => {
305 exe.linkSystemLibrary("c++");
306 },
307 else => |e| return e,
308 };
305309
306310 exe.linkSystemLibrary("pthread");
307311 } else if (exe.target.isFreeBSD()) {
......@@ -320,7 +324,7 @@ fn configureStage2(b: *Builder, exe: var, ctx: Context) !void {
320324 // System compiler, not gcc.
321325 exe.linkSystemLibrary("c++");
322326 },
323 else => return err,
327 else => |e| return e,
324328 }
325329 }
326330
lib/std/math/big/int.zig+2-2
......@@ -520,13 +520,13 @@ pub const Int = struct {
520520 comptime fmt: []const u8,
521521 options: std.fmt.FormatOptions,
522522 out_stream: var,
523 ) FmtError!void {
523 ) !void {
524524 self.assertWritable();
525525 // TODO look at fmt and support other bases
526526 // TODO support read-only fixed integers
527527 const str = self.toString(self.allocator.?, 10) catch @panic("TODO make this non allocating");
528528 defer self.allocator.?.free(str);
529 return out_stream.print(str);
529 return out_stream.writeAll(str);
530530 }
531531
532532 /// Returns -1, 0, 1 if |a| < |b|, |a| == |b| or |a| > |b| respectively.
src-self-hosted/c_int.zig+4-4
......@@ -69,9 +69,9 @@ pub const CInt = struct {
6969 };
7070
7171 pub fn sizeInBits(cint: CInt, self: Target) u32 {
72 const arch = self.getArch();
72 const arch = self.cpu.arch;
7373 switch (self.os.tag) {
74 .freestanding, .other => switch (self.getArch()) {
74 .freestanding, .other => switch (self.cpu.arch) {
7575 .msp430 => switch (cint.id) {
7676 .Short,
7777 .UShort,
......@@ -94,7 +94,7 @@ pub const CInt = struct {
9494 => return 32,
9595 .Long,
9696 .ULong,
97 => return self.getArchPtrBitWidth(),
97 => return self.cpu.arch.ptrBitWidth(),
9898 .LongLong,
9999 .ULongLong,
100100 => return 64,
......@@ -114,7 +114,7 @@ pub const CInt = struct {
114114 => return 32,
115115 .Long,
116116 .ULong,
117 => return self.getArchPtrBitWidth(),
117 => return self.cpu.arch.ptrBitWidth(),
118118 .LongLong,
119119 .ULongLong,
120120 => return 64,
src-self-hosted/compilation.zig+16-14
......@@ -95,7 +95,7 @@ pub const ZigCompiler = struct {
9595
9696 pub fn getNativeLibC(self: *ZigCompiler) !*LibCInstallation {
9797 if (self.native_libc.start()) |ptr| return ptr;
98 try self.native_libc.data.findNative(self.allocator);
98 self.native_libc.data = try LibCInstallation.findNative(.{ .allocator = self.allocator });
9999 self.native_libc.resolve();
100100 return &self.native_libc.data;
101101 }
......@@ -126,7 +126,7 @@ pub const Compilation = struct {
126126 name: Buffer,
127127 llvm_triple: Buffer,
128128 root_src_path: ?[]const u8,
129 target: Target,
129 target: std.Target,
130130 llvm_target: *llvm.Target,
131131 build_mode: builtin.Mode,
132132 zig_lib_dir: []const u8,
......@@ -338,7 +338,7 @@ pub const Compilation = struct {
338338 zig_compiler: *ZigCompiler,
339339 name: []const u8,
340340 root_src_path: ?[]const u8,
341 target: Target,
341 target: std.zig.CrossTarget,
342342 kind: Kind,
343343 build_mode: builtin.Mode,
344344 is_static: bool,
......@@ -370,13 +370,18 @@ pub const Compilation = struct {
370370 zig_compiler: *ZigCompiler,
371371 name: []const u8,
372372 root_src_path: ?[]const u8,
373 target: Target,
373 cross_target: std.zig.CrossTarget,
374374 kind: Kind,
375375 build_mode: builtin.Mode,
376376 is_static: bool,
377377 zig_lib_dir: []const u8,
378378 ) !void {
379379 const allocator = zig_compiler.allocator;
380
381 // TODO merge this line with stage2.zig crossTargetToTarget
382 const target_info = try std.zig.system.NativeTargetInfo.detect(std.heap.c_allocator, cross_target);
383 const target = target_info.target;
384
380385 var comp = Compilation{
381386 .arena_allocator = std.heap.ArenaAllocator.init(allocator),
382387 .zig_compiler = zig_compiler,
......@@ -419,7 +424,7 @@ pub const Compilation = struct {
419424 .target_machine = undefined,
420425 .target_data_ref = undefined,
421426 .target_layout_str = undefined,
422 .target_ptr_bits = target.getArchPtrBitWidth(),
427 .target_ptr_bits = target.cpu.arch.ptrBitWidth(),
423428
424429 .root_package = undefined,
425430 .std_package = undefined,
......@@ -440,7 +445,7 @@ pub const Compilation = struct {
440445 }
441446
442447 comp.name = try Buffer.init(comp.arena(), name);
443 comp.llvm_triple = try util.getTriple(comp.arena(), target);
448 comp.llvm_triple = try util.getLLVMTriple(comp.arena(), target);
444449 comp.llvm_target = try util.llvmTargetFromTriple(comp.llvm_triple);
445450 comp.zig_std_dir = try fs.path.join(comp.arena(), &[_][]const u8{ zig_lib_dir, "std" });
446451
......@@ -451,17 +456,12 @@ pub const Compilation = struct {
451456
452457 const reloc_mode = if (is_static) llvm.RelocStatic else llvm.RelocPIC;
453458
454 // LLVM creates invalid binaries on Windows sometimes.
455 // See https://github.com/ziglang/zig/issues/508
456 // As a workaround we do not use target native features on Windows.
457459 var target_specific_cpu_args: ?[*:0]u8 = null;
458460 var target_specific_cpu_features: ?[*:0]u8 = null;
459461 defer llvm.DisposeMessage(target_specific_cpu_args);
460462 defer llvm.DisposeMessage(target_specific_cpu_features);
461 if (target == Target.Native and !target.isWindows()) {
462 target_specific_cpu_args = llvm.GetHostCPUName() orelse return error.OutOfMemory;
463 target_specific_cpu_features = llvm.GetNativeFeatures() orelse return error.OutOfMemory;
464 }
463
464 // TODO detect native CPU & features here
465465
466466 comp.target_machine = llvm.CreateTargetMachine(
467467 comp.llvm_target,
......@@ -1125,7 +1125,9 @@ pub const Compilation = struct {
11251125 self.libc_link_lib = link_lib;
11261126
11271127 // get a head start on looking for the native libc
1128 if (self.target == Target.Native and self.override_libc == null) {
1128 // TODO this is missing a bunch of logic related to whether the target is native
1129 // and whether we can build libc
1130 if (self.override_libc == null) {
11291131 try self.deinit_group.call(startFindingNativeLibC, .{self});
11301132 }
11311133 }
src-self-hosted/errmsg.zig+4-7
......@@ -164,8 +164,7 @@ pub const Msg = struct {
164164 const realpath_copy = try mem.dupe(comp.gpa(), u8, tree_scope.root().realpath);
165165 errdefer comp.gpa().free(realpath_copy);
166166
167 var out_stream = &std.io.BufferOutStream.init(&text_buf).stream;
168 try parse_error.render(&tree_scope.tree.tokens, out_stream);
167 try parse_error.render(&tree_scope.tree.tokens, text_buf.outStream());
169168
170169 const msg = try comp.gpa().create(Msg);
171170 msg.* = Msg{
......@@ -204,8 +203,7 @@ pub const Msg = struct {
204203 const realpath_copy = try mem.dupe(allocator, u8, realpath);
205204 errdefer allocator.free(realpath_copy);
206205
207 var out_stream = &std.io.BufferOutStream.init(&text_buf).stream;
208 try parse_error.render(&tree.tokens, out_stream);
206 try parse_error.render(&tree.tokens, text_buf.outStream());
209207
210208 const msg = try allocator.create(Msg);
211209 msg.* = Msg{
......@@ -272,7 +270,7 @@ pub const Msg = struct {
272270 });
273271 try stream.writeByteNTimes(' ', start_loc.column);
274272 try stream.writeByteNTimes('~', last_token.end - first_token.start);
275 try stream.write("\n");
273 try stream.writeAll("\n");
276274 }
277275
278276 pub fn printToFile(msg: *const Msg, file: fs.File, color: Color) !void {
......@@ -281,7 +279,6 @@ pub const Msg = struct {
281279 .On => true,
282280 .Off => false,
283281 };
284 var stream = &file.outStream().stream;
285 return msg.printToStream(stream, color_on);
282 return msg.printToStream(file.outStream(), color_on);
286283 }
287284};
src-self-hosted/ir.zig+1-1
......@@ -1099,7 +1099,6 @@ pub const Builder = struct {
10991099 .Await => return error.Unimplemented,
11001100 .BitNot => return error.Unimplemented,
11011101 .BoolNot => return error.Unimplemented,
1102 .Cancel => return error.Unimplemented,
11031102 .OptionalType => return error.Unimplemented,
11041103 .Negation => return error.Unimplemented,
11051104 .NegationWrap => return error.Unimplemented,
......@@ -1188,6 +1187,7 @@ pub const Builder = struct {
11881187 .ParamDecl => return error.Unimplemented,
11891188 .FieldInitializer => return error.Unimplemented,
11901189 .EnumLiteral => return error.Unimplemented,
1190 .Noasync => return error.Unimplemented,
11911191 }
11921192 }
11931193
src-self-hosted/link.zig+54-58
......@@ -56,12 +56,13 @@ pub fn link(comp: *Compilation) !void {
5656 if (comp.haveLibC()) {
5757 // TODO https://github.com/ziglang/zig/issues/3190
5858 var libc = ctx.comp.override_libc orelse blk: {
59 switch (comp.target) {
60 Target.Native => {
61 break :blk comp.zig_compiler.getNativeLibC() catch return error.LibCRequiredButNotProvidedOrFound;
62 },
63 else => return error.LibCRequiredButNotProvidedOrFound,
64 }
59 @panic("this code has bitrotted");
60 //switch (comp.target) {
61 // Target.Native => {
62 // break :blk comp.zig_compiler.getNativeLibC() catch return error.LibCRequiredButNotProvidedOrFound;
63 // },
64 // else => return error.LibCRequiredButNotProvidedOrFound,
65 //}
6566 };
6667 ctx.libc = libc;
6768 }
......@@ -155,11 +156,11 @@ fn constructLinkerArgsElf(ctx: *Context) !void {
155156 //bool shared = !g->is_static && is_lib;
156157 //Buf *soname = nullptr;
157158 if (ctx.comp.is_static) {
158 if (util.isArmOrThumb(ctx.comp.target)) {
159 try ctx.args.append("-Bstatic");
160 } else {
161 try ctx.args.append("-static");
162 }
159 //if (util.isArmOrThumb(ctx.comp.target)) {
160 // try ctx.args.append("-Bstatic");
161 //} else {
162 // try ctx.args.append("-static");
163 //}
163164 }
164165 //} else if (shared) {
165166 // lj->args.append("-shared");
......@@ -176,29 +177,24 @@ fn constructLinkerArgsElf(ctx: *Context) !void {
176177
177178 if (ctx.link_in_crt) {
178179 const crt1o = if (ctx.comp.is_static) "crt1.o" else "Scrt1.o";
179 const crtbegino = if (ctx.comp.is_static) "crtbeginT.o" else "crtbegin.o";
180 try addPathJoin(ctx, ctx.libc.lib_dir.?, crt1o);
181 try addPathJoin(ctx, ctx.libc.lib_dir.?, "crti.o");
182 try addPathJoin(ctx, ctx.libc.static_lib_dir.?, crtbegino);
180 try addPathJoin(ctx, ctx.libc.crt_dir.?, crt1o);
181 try addPathJoin(ctx, ctx.libc.crt_dir.?, "crti.o");
183182 }
184183
185184 if (ctx.comp.haveLibC()) {
186185 try ctx.args.append("-L");
187186 // TODO addNullByte should probably return [:0]u8
188 try ctx.args.append(@ptrCast([*:0]const u8, (try std.cstr.addNullByte(&ctx.arena.allocator, ctx.libc.lib_dir.?)).ptr));
187 try ctx.args.append(@ptrCast([*:0]const u8, (try std.cstr.addNullByte(&ctx.arena.allocator, ctx.libc.crt_dir.?)).ptr));
189188
190 try ctx.args.append("-L");
191 try ctx.args.append(@ptrCast([*:0]const u8, (try std.cstr.addNullByte(&ctx.arena.allocator, ctx.libc.static_lib_dir.?)).ptr));
192
193 if (!ctx.comp.is_static) {
194 const dl = blk: {
195 if (ctx.libc.dynamic_linker_path) |dl| break :blk dl;
196 if (util.getDynamicLinkerPath(ctx.comp.target)) |dl| break :blk dl;
197 return error.LibCMissingDynamicLinker;
198 };
199 try ctx.args.append("-dynamic-linker");
200 try ctx.args.append(@ptrCast([*:0]const u8, (try std.cstr.addNullByte(&ctx.arena.allocator, dl)).ptr));
201 }
189 //if (!ctx.comp.is_static) {
190 // const dl = blk: {
191 // //if (ctx.libc.dynamic_linker_path) |dl| break :blk dl;
192 // //if (util.getDynamicLinkerPath(ctx.comp.target)) |dl| break :blk dl;
193 // return error.LibCMissingDynamicLinker;
194 // };
195 // try ctx.args.append("-dynamic-linker");
196 // try ctx.args.append(@ptrCast([*:0]const u8, (try std.cstr.addNullByte(&ctx.arena.allocator, dl)).ptr));
197 //}
202198 }
203199
204200 //if (shared) {
......@@ -265,13 +261,12 @@ fn constructLinkerArgsElf(ctx: *Context) !void {
265261
266262 // crt end
267263 if (ctx.link_in_crt) {
268 try addPathJoin(ctx, ctx.libc.static_lib_dir.?, "crtend.o");
269 try addPathJoin(ctx, ctx.libc.lib_dir.?, "crtn.o");
264 try addPathJoin(ctx, ctx.libc.crt_dir.?, "crtn.o");
270265 }
271266
272 if (ctx.comp.target != Target.Native) {
273 try ctx.args.append("--allow-shlib-undefined");
274 }
267 //if (ctx.comp.target != Target.Native) {
268 // try ctx.args.append("--allow-shlib-undefined");
269 //}
275270}
276271
277272fn addPathJoin(ctx: *Context, dirname: []const u8, basename: []const u8) !void {
......@@ -287,7 +282,7 @@ fn constructLinkerArgsCoff(ctx: *Context) !void {
287282 try ctx.args.append("-DEBUG");
288283 }
289284
290 switch (ctx.comp.target.getArch()) {
285 switch (ctx.comp.target.cpu.arch) {
291286 .i386 => try ctx.args.append("-MACHINE:X86"),
292287 .x86_64 => try ctx.args.append("-MACHINE:X64"),
293288 .aarch64 => try ctx.args.append("-MACHINE:ARM"),
......@@ -302,7 +297,7 @@ fn constructLinkerArgsCoff(ctx: *Context) !void {
302297 if (ctx.comp.haveLibC()) {
303298 try ctx.args.append(@ptrCast([*:0]const u8, (try std.fmt.allocPrint(&ctx.arena.allocator, "-LIBPATH:{}\x00", .{ctx.libc.msvc_lib_dir.?})).ptr));
304299 try ctx.args.append(@ptrCast([*:0]const u8, (try std.fmt.allocPrint(&ctx.arena.allocator, "-LIBPATH:{}\x00", .{ctx.libc.kernel32_lib_dir.?})).ptr));
305 try ctx.args.append(@ptrCast([*:0]const u8, (try std.fmt.allocPrint(&ctx.arena.allocator, "-LIBPATH:{}\x00", .{ctx.libc.lib_dir.?})).ptr));
300 try ctx.args.append(@ptrCast([*:0]const u8, (try std.fmt.allocPrint(&ctx.arena.allocator, "-LIBPATH:{}\x00", .{ctx.libc.crt_dir.?})).ptr));
306301 }
307302
308303 if (ctx.link_in_crt) {
......@@ -417,7 +412,7 @@ fn constructLinkerArgsMachO(ctx: *Context) !void {
417412 }
418413 },
419414 .IPhoneOS => {
420 if (ctx.comp.target.getArch() == .aarch64) {
415 if (ctx.comp.target.cpu.arch == .aarch64) {
421416 // iOS does not need any crt1 files for arm64
422417 } else if (platform.versionLessThan(3, 1)) {
423418 try ctx.args.append("-lcrt1.o");
......@@ -435,28 +430,29 @@ fn constructLinkerArgsMachO(ctx: *Context) !void {
435430 }
436431 try addFnObjects(ctx);
437432
438 if (ctx.comp.target == Target.Native) {
439 for (ctx.comp.link_libs_list.toSliceConst()) |lib| {
440 if (mem.eql(u8, lib.name, "c")) {
441 // on Darwin, libSystem has libc in it, but also you have to use it
442 // to make syscalls because the syscall numbers are not documented
443 // and change between versions.
444 // so we always link against libSystem
445 try ctx.args.append("-lSystem");
446 } else {
447 if (mem.indexOfScalar(u8, lib.name, '/') == null) {
448 const arg = try std.fmt.allocPrint(&ctx.arena.allocator, "-l{}\x00", .{lib.name});
449 try ctx.args.append(@ptrCast([*:0]const u8, arg.ptr));
450 } else {
451 const arg = try std.cstr.addNullByte(&ctx.arena.allocator, lib.name);
452 try ctx.args.append(@ptrCast([*:0]const u8, arg.ptr));
453 }
454 }
455 }
456 } else {
457 try ctx.args.append("-undefined");
458 try ctx.args.append("dynamic_lookup");
459 }
433 // TODO
434 //if (ctx.comp.target == Target.Native) {
435 // for (ctx.comp.link_libs_list.toSliceConst()) |lib| {
436 // if (mem.eql(u8, lib.name, "c")) {
437 // // on Darwin, libSystem has libc in it, but also you have to use it
438 // // to make syscalls because the syscall numbers are not documented
439 // // and change between versions.
440 // // so we always link against libSystem
441 // try ctx.args.append("-lSystem");
442 // } else {
443 // if (mem.indexOfScalar(u8, lib.name, '/') == null) {
444 // const arg = try std.fmt.allocPrint(&ctx.arena.allocator, "-l{}\x00", .{lib.name});
445 // try ctx.args.append(@ptrCast([*:0]const u8, arg.ptr));
446 // } else {
447 // const arg = try std.cstr.addNullByte(&ctx.arena.allocator, lib.name);
448 // try ctx.args.append(@ptrCast([*:0]const u8, arg.ptr));
449 // }
450 // }
451 // }
452 //} else {
453 // try ctx.args.append("-undefined");
454 // try ctx.args.append("dynamic_lookup");
455 //}
460456
461457 if (platform.kind == .MacOS) {
462458 if (platform.versionLessThan(10, 5)) {
src-self-hosted/main.zig+55-46
......@@ -18,10 +18,6 @@ const Target = std.Target;
1818const errmsg = @import("errmsg.zig");
1919const LibCInstallation = @import("libc_installation.zig").LibCInstallation;
2020
21var stderr_file: fs.File = undefined;
22var stderr: *io.OutStream(fs.File.WriteError) = undefined;
23var stdout: *io.OutStream(fs.File.WriteError) = undefined;
24
2521pub const io_mode = .evented;
2622
2723pub const max_src_size = 2 * 1024 * 1024 * 1024; // 2 GiB
......@@ -51,17 +47,14 @@ const Command = struct {
5147pub fn main() !void {
5248 const allocator = std.heap.c_allocator;
5349
54 stdout = &std.io.getStdOut().outStream().stream;
55
56 stderr_file = std.io.getStdErr();
57 stderr = &stderr_file.outStream().stream;
50 const stderr = io.getStdErr().outStream();
5851
5952 const args = try process.argsAlloc(allocator);
6053 defer process.argsFree(allocator, args);
6154
6255 if (args.len <= 1) {
63 try stderr.write("expected command argument\n\n");
64 try stderr.write(usage);
56 try stderr.writeAll("expected command argument\n\n");
57 try stderr.writeAll(usage);
6558 process.exit(1);
6659 }
6760
......@@ -78,8 +71,8 @@ pub fn main() !void {
7871 } else if (mem.eql(u8, cmd, "libc")) {
7972 return cmdLibC(allocator, cmd_args);
8073 } else if (mem.eql(u8, cmd, "targets")) {
81 const info = try std.zig.system.NativeTargetInfo.detect(allocator);
82 defer info.deinit(allocator);
74 const info = try std.zig.system.NativeTargetInfo.detect(allocator, .{});
75 const stdout = io.getStdOut().outStream();
8376 return @import("print_targets.zig").cmdTargets(allocator, cmd_args, stdout, info.target);
8477 } else if (mem.eql(u8, cmd, "version")) {
8578 return cmdVersion(allocator, cmd_args);
......@@ -91,7 +84,7 @@ pub fn main() !void {
9184 return cmdInternal(allocator, cmd_args);
9285 } else {
9386 try stderr.print("unknown command: {}\n\n", .{args[1]});
94 try stderr.write(usage);
87 try stderr.writeAll(usage);
9588 process.exit(1);
9689 }
9790}
......@@ -156,6 +149,8 @@ const usage_build_generic =
156149;
157150
158151fn buildOutputType(allocator: *Allocator, args: []const []const u8, out_type: Compilation.Kind) !void {
152 const stderr = io.getStdErr().outStream();
153
159154 var color: errmsg.Color = .Auto;
160155 var build_mode: std.builtin.Mode = .Debug;
161156 var emit_bin = true;
......@@ -208,11 +203,11 @@ fn buildOutputType(allocator: *Allocator, args: []const []const u8, out_type: Co
208203 const arg = args[i];
209204 if (mem.startsWith(u8, arg, "-")) {
210205 if (mem.eql(u8, arg, "--help")) {
211 try stdout.write(usage_build_generic);
206 try io.getStdOut().writeAll(usage_build_generic);
212207 process.exit(0);
213208 } else if (mem.eql(u8, arg, "--color")) {
214209 if (i + 1 >= args.len) {
215 try stderr.write("expected [auto|on|off] after --color\n");
210 try stderr.writeAll("expected [auto|on|off] after --color\n");
216211 process.exit(1);
217212 }
218213 i += 1;
......@@ -229,7 +224,7 @@ fn buildOutputType(allocator: *Allocator, args: []const []const u8, out_type: Co
229224 }
230225 } else if (mem.eql(u8, arg, "--mode")) {
231226 if (i + 1 >= args.len) {
232 try stderr.write("expected [Debug|ReleaseSafe|ReleaseFast|ReleaseSmall] after --mode\n");
227 try stderr.writeAll("expected [Debug|ReleaseSafe|ReleaseFast|ReleaseSmall] after --mode\n");
233228 process.exit(1);
234229 }
235230 i += 1;
......@@ -248,49 +243,49 @@ fn buildOutputType(allocator: *Allocator, args: []const []const u8, out_type: Co
248243 }
249244 } else if (mem.eql(u8, arg, "--name")) {
250245 if (i + 1 >= args.len) {
251 try stderr.write("expected parameter after --name\n");
246 try stderr.writeAll("expected parameter after --name\n");
252247 process.exit(1);
253248 }
254249 i += 1;
255250 provided_name = args[i];
256251 } else if (mem.eql(u8, arg, "--ver-major")) {
257252 if (i + 1 >= args.len) {
258 try stderr.write("expected parameter after --ver-major\n");
253 try stderr.writeAll("expected parameter after --ver-major\n");
259254 process.exit(1);
260255 }
261256 i += 1;
262257 version.major = try std.fmt.parseInt(u32, args[i], 10);
263258 } else if (mem.eql(u8, arg, "--ver-minor")) {
264259 if (i + 1 >= args.len) {
265 try stderr.write("expected parameter after --ver-minor\n");
260 try stderr.writeAll("expected parameter after --ver-minor\n");
266261 process.exit(1);
267262 }
268263 i += 1;
269264 version.minor = try std.fmt.parseInt(u32, args[i], 10);
270265 } else if (mem.eql(u8, arg, "--ver-patch")) {
271266 if (i + 1 >= args.len) {
272 try stderr.write("expected parameter after --ver-patch\n");
267 try stderr.writeAll("expected parameter after --ver-patch\n");
273268 process.exit(1);
274269 }
275270 i += 1;
276271 version.patch = try std.fmt.parseInt(u32, args[i], 10);
277272 } else if (mem.eql(u8, arg, "--linker-script")) {
278273 if (i + 1 >= args.len) {
279 try stderr.write("expected parameter after --linker-script\n");
274 try stderr.writeAll("expected parameter after --linker-script\n");
280275 process.exit(1);
281276 }
282277 i += 1;
283278 linker_script = args[i];
284279 } else if (mem.eql(u8, arg, "--libc")) {
285280 if (i + 1 >= args.len) {
286 try stderr.write("expected parameter after --libc\n");
281 try stderr.writeAll("expected parameter after --libc\n");
287282 process.exit(1);
288283 }
289284 i += 1;
290285 libc_arg = args[i];
291286 } else if (mem.eql(u8, arg, "-mllvm")) {
292287 if (i + 1 >= args.len) {
293 try stderr.write("expected parameter after -mllvm\n");
288 try stderr.writeAll("expected parameter after -mllvm\n");
294289 process.exit(1);
295290 }
296291 i += 1;
......@@ -300,14 +295,14 @@ fn buildOutputType(allocator: *Allocator, args: []const []const u8, out_type: Co
300295 try mllvm_flags.append(args[i]);
301296 } else if (mem.eql(u8, arg, "-mmacosx-version-min")) {
302297 if (i + 1 >= args.len) {
303 try stderr.write("expected parameter after -mmacosx-version-min\n");
298 try stderr.writeAll("expected parameter after -mmacosx-version-min\n");
304299 process.exit(1);
305300 }
306301 i += 1;
307302 macosx_version_min = args[i];
308303 } else if (mem.eql(u8, arg, "-mios-version-min")) {
309304 if (i + 1 >= args.len) {
310 try stderr.write("expected parameter after -mios-version-min\n");
305 try stderr.writeAll("expected parameter after -mios-version-min\n");
311306 process.exit(1);
312307 }
313308 i += 1;
......@@ -348,7 +343,7 @@ fn buildOutputType(allocator: *Allocator, args: []const []const u8, out_type: Co
348343 linker_rdynamic = true;
349344 } else if (mem.eql(u8, arg, "--pkg-begin")) {
350345 if (i + 2 >= args.len) {
351 try stderr.write("expected [name] [path] after --pkg-begin\n");
346 try stderr.writeAll("expected [name] [path] after --pkg-begin\n");
352347 process.exit(1);
353348 }
354349 i += 1;
......@@ -363,7 +358,7 @@ fn buildOutputType(allocator: *Allocator, args: []const []const u8, out_type: Co
363358 if (cur_pkg.parent) |parent| {
364359 cur_pkg = parent;
365360 } else {
366 try stderr.write("encountered --pkg-end with no matching --pkg-begin\n");
361 try stderr.writeAll("encountered --pkg-end with no matching --pkg-begin\n");
367362 process.exit(1);
368363 }
369364 } else if (mem.startsWith(u8, arg, "-l")) {
......@@ -411,18 +406,18 @@ fn buildOutputType(allocator: *Allocator, args: []const []const u8, out_type: Co
411406 var it = mem.separate(basename, ".");
412407 break :blk it.next() orelse basename;
413408 } else {
414 try stderr.write("--name [name] not provided and unable to infer\n");
409 try stderr.writeAll("--name [name] not provided and unable to infer\n");
415410 process.exit(1);
416411 }
417412 };
418413
419414 if (root_src_file == null and link_objects.len == 0 and assembly_files.len == 0) {
420 try stderr.write("Expected source file argument or at least one --object or --assembly argument\n");
415 try stderr.writeAll("Expected source file argument or at least one --object or --assembly argument\n");
421416 process.exit(1);
422417 }
423418
424419 if (out_type == Compilation.Kind.Obj and link_objects.len != 0) {
425 try stderr.write("When building an object file, --object arguments are invalid\n");
420 try stderr.writeAll("When building an object file, --object arguments are invalid\n");
426421 process.exit(1);
427422 }
428423
......@@ -440,7 +435,7 @@ fn buildOutputType(allocator: *Allocator, args: []const []const u8, out_type: Co
440435 &zig_compiler,
441436 root_name,
442437 root_src_file,
443 Target.Native,
438 .{},
444439 out_type,
445440 build_mode,
446441 !is_dynamic,
......@@ -478,7 +473,7 @@ fn buildOutputType(allocator: *Allocator, args: []const []const u8, out_type: Co
478473 comp.linker_rdynamic = linker_rdynamic;
479474
480475 if (macosx_version_min != null and ios_version_min != null) {
481 try stderr.write("-mmacosx-version-min and -mios-version-min options not allowed together\n");
476 try stderr.writeAll("-mmacosx-version-min and -mios-version-min options not allowed together\n");
482477 process.exit(1);
483478 }
484479
......@@ -501,6 +496,8 @@ fn buildOutputType(allocator: *Allocator, args: []const []const u8, out_type: Co
501496}
502497
503498fn processBuildEvents(comp: *Compilation, color: errmsg.Color) void {
499 const stderr_file = io.getStdErr();
500 const stderr = stderr_file.outStream();
504501 var count: usize = 0;
505502 while (!comp.cancelled) {
506503 const build_event = comp.events.get();
......@@ -551,7 +548,8 @@ const Fmt = struct {
551548};
552549
553550fn parseLibcPaths(allocator: *Allocator, libc: *LibCInstallation, libc_paths_file: []const u8) void {
554 libc.parse(allocator, libc_paths_file, stderr) catch |err| {
551 const stderr = io.getStdErr().outStream();
552 libc.* = LibCInstallation.parse(allocator, libc_paths_file, stderr) catch |err| {
555553 stderr.print("Unable to parse libc path file '{}': {}.\n" ++
556554 "Try running `zig libc` to see an example for the native target.\n", .{
557555 libc_paths_file,
......@@ -562,6 +560,7 @@ fn parseLibcPaths(allocator: *Allocator, libc: *LibCInstallation, libc_paths_fil
562560}
563561
564562fn cmdLibC(allocator: *Allocator, args: []const []const u8) !void {
563 const stderr = io.getStdErr().outStream();
565564 switch (args.len) {
566565 0 => {},
567566 1 => {
......@@ -582,10 +581,12 @@ fn cmdLibC(allocator: *Allocator, args: []const []const u8) !void {
582581 stderr.print("unable to find libc: {}\n", .{@errorName(err)}) catch {};
583582 process.exit(1);
584583 };
585 libc.render(stdout) catch process.exit(1);
584 libc.render(io.getStdOut().outStream()) catch process.exit(1);
586585}
587586
588587fn cmdFmt(allocator: *Allocator, args: []const []const u8) !void {
588 const stderr_file = io.getStdErr();
589 const stderr = stderr_file.outStream();
589590 var color: errmsg.Color = .Auto;
590591 var stdin_flag: bool = false;
591592 var check_flag: bool = false;
......@@ -597,11 +598,12 @@ fn cmdFmt(allocator: *Allocator, args: []const []const u8) !void {
597598 const arg = args[i];
598599 if (mem.startsWith(u8, arg, "-")) {
599600 if (mem.eql(u8, arg, "--help")) {
600 try stdout.write(usage_fmt);
601 const stdout = io.getStdOut().outStream();
602 try stdout.writeAll(usage_fmt);
601603 process.exit(0);
602604 } else if (mem.eql(u8, arg, "--color")) {
603605 if (i + 1 >= args.len) {
604 try stderr.write("expected [auto|on|off] after --color\n");
606 try stderr.writeAll("expected [auto|on|off] after --color\n");
605607 process.exit(1);
606608 }
607609 i += 1;
......@@ -632,14 +634,13 @@ fn cmdFmt(allocator: *Allocator, args: []const []const u8) !void {
632634
633635 if (stdin_flag) {
634636 if (input_files.len != 0) {
635 try stderr.write("cannot use --stdin with positional arguments\n");
637 try stderr.writeAll("cannot use --stdin with positional arguments\n");
636638 process.exit(1);
637639 }
638640
639 var stdin_file = io.getStdIn();
640 var stdin = stdin_file.inStream();
641 const stdin = io.getStdIn().inStream();
641642
642 const source_code = try stdin.stream.readAllAlloc(allocator, max_src_size);
643 const source_code = try stdin.readAllAlloc(allocator, max_src_size);
643644 defer allocator.free(source_code);
644645
645646 const tree = std.zig.parse(allocator, source_code) catch |err| {
......@@ -653,7 +654,7 @@ fn cmdFmt(allocator: *Allocator, args: []const []const u8) !void {
653654 const msg = try errmsg.Msg.createFromParseError(allocator, parse_error, tree, "<stdin>");
654655 defer msg.destroy();
655656
656 try msg.printToFile(stderr_file, color);
657 try msg.printToFile(io.getStdErr(), color);
657658 }
658659 if (tree.errors.len != 0) {
659660 process.exit(1);
......@@ -664,12 +665,13 @@ fn cmdFmt(allocator: *Allocator, args: []const []const u8) !void {
664665 process.exit(code);
665666 }
666667
668 const stdout = io.getStdOut().outStream();
667669 _ = try std.zig.render(allocator, stdout, tree);
668670 return;
669671 }
670672
671673 if (input_files.len == 0) {
672 try stderr.write("expected at least one source file argument\n");
674 try stderr.writeAll("expected at least one source file argument\n");
673675 process.exit(1);
674676 }
675677
......@@ -713,6 +715,9 @@ const FmtError = error{
713715} || fs.File.OpenError;
714716
715717async fn fmtPath(fmt: *Fmt, file_path_ref: []const u8, check_mode: bool) FmtError!void {
718 const stderr_file = io.getStdErr();
719 const stderr = stderr_file.outStream();
720
716721 const file_path = try std.mem.dupe(fmt.allocator, u8, file_path_ref);
717722 defer fmt.allocator.free(file_path);
718723
......@@ -791,11 +796,13 @@ async fn fmtPath(fmt: *Fmt, file_path_ref: []const u8, check_mode: bool) FmtErro
791796}
792797
793798fn cmdVersion(allocator: *Allocator, args: []const []const u8) !void {
799 const stdout = io.getStdOut().outStream();
794800 try stdout.print("{}\n", .{c.ZIG_VERSION_STRING});
795801}
796802
797803fn cmdHelp(allocator: *Allocator, args: []const []const u8) !void {
798 try stdout.write(usage);
804 const stdout = io.getStdOut();
805 try stdout.writeAll(usage);
799806}
800807
801808pub const info_zen =
......@@ -816,7 +823,7 @@ pub const info_zen =
816823;
817824
818825fn cmdZen(allocator: *Allocator, args: []const []const u8) !void {
819 try stdout.write(info_zen);
826 try io.getStdOut().writeAll(info_zen);
820827}
821828
822829const usage_internal =
......@@ -829,8 +836,9 @@ const usage_internal =
829836;
830837
831838fn cmdInternal(allocator: *Allocator, args: []const []const u8) !void {
839 const stderr = io.getStdErr().outStream();
832840 if (args.len == 0) {
833 try stderr.write(usage_internal);
841 try stderr.writeAll(usage_internal);
834842 process.exit(1);
835843 }
836844
......@@ -849,10 +857,11 @@ fn cmdInternal(allocator: *Allocator, args: []const []const u8) !void {
849857 }
850858
851859 try stderr.print("unknown sub command: {}\n\n", .{args[0]});
852 try stderr.write(usage_internal);
860 try stderr.writeAll(usage_internal);
853861}
854862
855863fn cmdInternalBuildInfo(allocator: *Allocator, args: []const []const u8) !void {
864 const stdout = io.getStdOut().outStream();
856865 try stdout.print(
857866 \\ZIG_CMAKE_BINARY_DIR {}
858867 \\ZIG_CXX_COMPILER {}
src-self-hosted/util.zig+13-2
......@@ -3,8 +3,7 @@ const Target = std.Target;
33const llvm = @import("llvm.zig");
44
55pub fn getDarwinArchString(self: Target) [:0]const u8 {
6 const arch = self.getArch();
7 switch (arch) {
6 switch (self.cpu.arch) {
87 .aarch64 => return "arm64",
98 .thumb,
109 .arm,
......@@ -34,3 +33,15 @@ pub fn initializeAllTargets() void {
3433 llvm.InitializeAllAsmPrinters();
3534 llvm.InitializeAllAsmParsers();
3635}
36
37pub fn getLLVMTriple(allocator: *std.mem.Allocator, target: std.Target) !std.Buffer {
38 var result = try std.Buffer.initSize(allocator, 0);
39 errdefer result.deinit();
40
41 try result.outStream().print(
42 "{}-unknown-{}-{}",
43 .{ @tagName(target.cpu.arch), @tagName(target.os.tag), @tagName(target.abi) },
44 );
45
46 return result;
47}