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 {...@@ -298,10 +298,14 @@ fn configureStage2(b: *Builder, exe: var, ctx: Context) !void {
298 dependOnLib(b, exe, ctx.llvm);298 dependOnLib(b, exe, ctx.llvm);
299299
300 if (exe.target.getOsTag() == .linux) {300 if (exe.target.getOsTag() == .linux) {
301 try addCxxKnownPath(b, ctx, exe, "libstdc++.a",301 // First we try to static link against gcc libstdc++. If that doesn't work,
302 \\Unable to determine path to libstdc++.a302 // we fall back to -lc++ and cross our fingers.
303 \\On Fedora, install libstdc++-static and try again.303 addCxxKnownPath(b, ctx, exe, "libstdc++.a", "") catch |err| switch (err) {
304 );304 error.RequiredLibraryNotFound => {
305 exe.linkSystemLibrary("c++");
306 },
307 else => |e| return e,
308 };
305309
306 exe.linkSystemLibrary("pthread");310 exe.linkSystemLibrary("pthread");
307 } else if (exe.target.isFreeBSD()) {311 } else if (exe.target.isFreeBSD()) {
...@@ -320,7 +324,7 @@ fn configureStage2(b: *Builder, exe: var, ctx: Context) !void {...@@ -320,7 +324,7 @@ fn configureStage2(b: *Builder, exe: var, ctx: Context) !void {
320 // System compiler, not gcc.324 // System compiler, not gcc.
321 exe.linkSystemLibrary("c++");325 exe.linkSystemLibrary("c++");
322 },326 },
323 else => return err,327 else => |e| return e,
324 }328 }
325 }329 }
326330
lib/std/math/big/int.zig+2-2
...@@ -520,13 +520,13 @@ pub const Int = struct {...@@ -520,13 +520,13 @@ pub const Int = struct {
520 comptime fmt: []const u8,520 comptime fmt: []const u8,
521 options: std.fmt.FormatOptions,521 options: std.fmt.FormatOptions,
522 out_stream: var,522 out_stream: var,
523 ) FmtError!void {523 ) !void {
524 self.assertWritable();524 self.assertWritable();
525 // TODO look at fmt and support other bases525 // TODO look at fmt and support other bases
526 // TODO support read-only fixed integers526 // TODO support read-only fixed integers
527 const str = self.toString(self.allocator.?, 10) catch @panic("TODO make this non allocating");527 const str = self.toString(self.allocator.?, 10) catch @panic("TODO make this non allocating");
528 defer self.allocator.?.free(str);528 defer self.allocator.?.free(str);
529 return out_stream.print(str);529 return out_stream.writeAll(str);
530 }530 }
531531
532 /// Returns -1, 0, 1 if |a| < |b|, |a| == |b| or |a| > |b| respectively.532 /// 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 {...@@ -69,9 +69,9 @@ pub const CInt = struct {
69 };69 };
7070
71 pub fn sizeInBits(cint: CInt, self: Target) u32 {71 pub fn sizeInBits(cint: CInt, self: Target) u32 {
72 const arch = self.getArch();72 const arch = self.cpu.arch;
73 switch (self.os.tag) {73 switch (self.os.tag) {
74 .freestanding, .other => switch (self.getArch()) {74 .freestanding, .other => switch (self.cpu.arch) {
75 .msp430 => switch (cint.id) {75 .msp430 => switch (cint.id) {
76 .Short,76 .Short,
77 .UShort,77 .UShort,
...@@ -94,7 +94,7 @@ pub const CInt = struct {...@@ -94,7 +94,7 @@ pub const CInt = struct {
94 => return 32,94 => return 32,
95 .Long,95 .Long,
96 .ULong,96 .ULong,
97 => return self.getArchPtrBitWidth(),97 => return self.cpu.arch.ptrBitWidth(),
98 .LongLong,98 .LongLong,
99 .ULongLong,99 .ULongLong,
100 => return 64,100 => return 64,
...@@ -114,7 +114,7 @@ pub const CInt = struct {...@@ -114,7 +114,7 @@ pub const CInt = struct {
114 => return 32,114 => return 32,
115 .Long,115 .Long,
116 .ULong,116 .ULong,
117 => return self.getArchPtrBitWidth(),117 => return self.cpu.arch.ptrBitWidth(),
118 .LongLong,118 .LongLong,
119 .ULongLong,119 .ULongLong,
120 => return 64,120 => return 64,
src-self-hosted/compilation.zig+16-14
...@@ -95,7 +95,7 @@ pub const ZigCompiler = struct {...@@ -95,7 +95,7 @@ pub const ZigCompiler = struct {
9595
96 pub fn getNativeLibC(self: *ZigCompiler) !*LibCInstallation {96 pub fn getNativeLibC(self: *ZigCompiler) !*LibCInstallation {
97 if (self.native_libc.start()) |ptr| return ptr;97 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 });
99 self.native_libc.resolve();99 self.native_libc.resolve();
100 return &self.native_libc.data;100 return &self.native_libc.data;
101 }101 }
...@@ -126,7 +126,7 @@ pub const Compilation = struct {...@@ -126,7 +126,7 @@ pub const Compilation = struct {
126 name: Buffer,126 name: Buffer,
127 llvm_triple: Buffer,127 llvm_triple: Buffer,
128 root_src_path: ?[]const u8,128 root_src_path: ?[]const u8,
129 target: Target,129 target: std.Target,
130 llvm_target: *llvm.Target,130 llvm_target: *llvm.Target,
131 build_mode: builtin.Mode,131 build_mode: builtin.Mode,
132 zig_lib_dir: []const u8,132 zig_lib_dir: []const u8,
...@@ -338,7 +338,7 @@ pub const Compilation = struct {...@@ -338,7 +338,7 @@ pub const Compilation = struct {
338 zig_compiler: *ZigCompiler,338 zig_compiler: *ZigCompiler,
339 name: []const u8,339 name: []const u8,
340 root_src_path: ?[]const u8,340 root_src_path: ?[]const u8,
341 target: Target,341 target: std.zig.CrossTarget,
342 kind: Kind,342 kind: Kind,
343 build_mode: builtin.Mode,343 build_mode: builtin.Mode,
344 is_static: bool,344 is_static: bool,
...@@ -370,13 +370,18 @@ pub const Compilation = struct {...@@ -370,13 +370,18 @@ pub const Compilation = struct {
370 zig_compiler: *ZigCompiler,370 zig_compiler: *ZigCompiler,
371 name: []const u8,371 name: []const u8,
372 root_src_path: ?[]const u8,372 root_src_path: ?[]const u8,
373 target: Target,373 cross_target: std.zig.CrossTarget,
374 kind: Kind,374 kind: Kind,
375 build_mode: builtin.Mode,375 build_mode: builtin.Mode,
376 is_static: bool,376 is_static: bool,
377 zig_lib_dir: []const u8,377 zig_lib_dir: []const u8,
378 ) !void {378 ) !void {
379 const allocator = zig_compiler.allocator;379 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
380 var comp = Compilation{385 var comp = Compilation{
381 .arena_allocator = std.heap.ArenaAllocator.init(allocator),386 .arena_allocator = std.heap.ArenaAllocator.init(allocator),
382 .zig_compiler = zig_compiler,387 .zig_compiler = zig_compiler,
...@@ -419,7 +424,7 @@ pub const Compilation = struct {...@@ -419,7 +424,7 @@ pub const Compilation = struct {
419 .target_machine = undefined,424 .target_machine = undefined,
420 .target_data_ref = undefined,425 .target_data_ref = undefined,
421 .target_layout_str = undefined,426 .target_layout_str = undefined,
422 .target_ptr_bits = target.getArchPtrBitWidth(),427 .target_ptr_bits = target.cpu.arch.ptrBitWidth(),
423428
424 .root_package = undefined,429 .root_package = undefined,
425 .std_package = undefined,430 .std_package = undefined,
...@@ -440,7 +445,7 @@ pub const Compilation = struct {...@@ -440,7 +445,7 @@ pub const Compilation = struct {
440 }445 }
441446
442 comp.name = try Buffer.init(comp.arena(), name);447 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);
444 comp.llvm_target = try util.llvmTargetFromTriple(comp.llvm_triple);449 comp.llvm_target = try util.llvmTargetFromTriple(comp.llvm_triple);
445 comp.zig_std_dir = try fs.path.join(comp.arena(), &[_][]const u8{ zig_lib_dir, "std" });450 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 {...@@ -451,17 +456,12 @@ pub const Compilation = struct {
451456
452 const reloc_mode = if (is_static) llvm.RelocStatic else llvm.RelocPIC;457 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.
457 var target_specific_cpu_args: ?[*:0]u8 = null;459 var target_specific_cpu_args: ?[*:0]u8 = null;
458 var target_specific_cpu_features: ?[*:0]u8 = null;460 var target_specific_cpu_features: ?[*:0]u8 = null;
459 defer llvm.DisposeMessage(target_specific_cpu_args);461 defer llvm.DisposeMessage(target_specific_cpu_args);
460 defer llvm.DisposeMessage(target_specific_cpu_features);462 defer llvm.DisposeMessage(target_specific_cpu_features);
461 if (target == Target.Native and !target.isWindows()) {463
462 target_specific_cpu_args = llvm.GetHostCPUName() orelse return error.OutOfMemory;464 // TODO detect native CPU & features here
463 target_specific_cpu_features = llvm.GetNativeFeatures() orelse return error.OutOfMemory;
464 }
465465
466 comp.target_machine = llvm.CreateTargetMachine(466 comp.target_machine = llvm.CreateTargetMachine(
467 comp.llvm_target,467 comp.llvm_target,
...@@ -1125,7 +1125,9 @@ pub const Compilation = struct {...@@ -1125,7 +1125,9 @@ pub const Compilation = struct {
1125 self.libc_link_lib = link_lib;1125 self.libc_link_lib = link_lib;
11261126
1127 // get a head start on looking for the native libc1127 // 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) {
1129 try self.deinit_group.call(startFindingNativeLibC, .{self});1131 try self.deinit_group.call(startFindingNativeLibC, .{self});
1130 }1132 }
1131 }1133 }
src-self-hosted/errmsg.zig+4-7
...@@ -164,8 +164,7 @@ pub const Msg = struct {...@@ -164,8 +164,7 @@ pub const Msg = struct {
164 const realpath_copy = try mem.dupe(comp.gpa(), u8, tree_scope.root().realpath);164 const realpath_copy = try mem.dupe(comp.gpa(), u8, tree_scope.root().realpath);
165 errdefer comp.gpa().free(realpath_copy);165 errdefer comp.gpa().free(realpath_copy);
166166
167 var out_stream = &std.io.BufferOutStream.init(&text_buf).stream;167 try parse_error.render(&tree_scope.tree.tokens, text_buf.outStream());
168 try parse_error.render(&tree_scope.tree.tokens, out_stream);
169168
170 const msg = try comp.gpa().create(Msg);169 const msg = try comp.gpa().create(Msg);
171 msg.* = Msg{170 msg.* = Msg{
...@@ -204,8 +203,7 @@ pub const Msg = struct {...@@ -204,8 +203,7 @@ pub const Msg = struct {
204 const realpath_copy = try mem.dupe(allocator, u8, realpath);203 const realpath_copy = try mem.dupe(allocator, u8, realpath);
205 errdefer allocator.free(realpath_copy);204 errdefer allocator.free(realpath_copy);
206205
207 var out_stream = &std.io.BufferOutStream.init(&text_buf).stream;206 try parse_error.render(&tree.tokens, text_buf.outStream());
208 try parse_error.render(&tree.tokens, out_stream);
209207
210 const msg = try allocator.create(Msg);208 const msg = try allocator.create(Msg);
211 msg.* = Msg{209 msg.* = Msg{
...@@ -272,7 +270,7 @@ pub const Msg = struct {...@@ -272,7 +270,7 @@ pub const Msg = struct {
272 });270 });
273 try stream.writeByteNTimes(' ', start_loc.column);271 try stream.writeByteNTimes(' ', start_loc.column);
274 try stream.writeByteNTimes('~', last_token.end - first_token.start);272 try stream.writeByteNTimes('~', last_token.end - first_token.start);
275 try stream.write("\n");273 try stream.writeAll("\n");
276 }274 }
277275
278 pub fn printToFile(msg: *const Msg, file: fs.File, color: Color) !void {276 pub fn printToFile(msg: *const Msg, file: fs.File, color: Color) !void {
...@@ -281,7 +279,6 @@ pub const Msg = struct {...@@ -281,7 +279,6 @@ pub const Msg = struct {
281 .On => true,279 .On => true,
282 .Off => false,280 .Off => false,
283 };281 };
284 var stream = &file.outStream().stream;282 return msg.printToStream(file.outStream(), color_on);
285 return msg.printToStream(stream, color_on);
286 }283 }
287};284};
src-self-hosted/ir.zig+1-1
...@@ -1099,7 +1099,6 @@ pub const Builder = struct {...@@ -1099,7 +1099,6 @@ pub const Builder = struct {
1099 .Await => return error.Unimplemented,1099 .Await => return error.Unimplemented,
1100 .BitNot => return error.Unimplemented,1100 .BitNot => return error.Unimplemented,
1101 .BoolNot => return error.Unimplemented,1101 .BoolNot => return error.Unimplemented,
1102 .Cancel => return error.Unimplemented,
1103 .OptionalType => return error.Unimplemented,1102 .OptionalType => return error.Unimplemented,
1104 .Negation => return error.Unimplemented,1103 .Negation => return error.Unimplemented,
1105 .NegationWrap => return error.Unimplemented,1104 .NegationWrap => return error.Unimplemented,
...@@ -1188,6 +1187,7 @@ pub const Builder = struct {...@@ -1188,6 +1187,7 @@ pub const Builder = struct {
1188 .ParamDecl => return error.Unimplemented,1187 .ParamDecl => return error.Unimplemented,
1189 .FieldInitializer => return error.Unimplemented,1188 .FieldInitializer => return error.Unimplemented,
1190 .EnumLiteral => return error.Unimplemented,1189 .EnumLiteral => return error.Unimplemented,
1190 .Noasync => return error.Unimplemented,
1191 }1191 }
1192 }1192 }
11931193
src-self-hosted/link.zig+54-58
...@@ -56,12 +56,13 @@ pub fn link(comp: *Compilation) !void {...@@ -56,12 +56,13 @@ pub fn link(comp: *Compilation) !void {
56 if (comp.haveLibC()) {56 if (comp.haveLibC()) {
57 // TODO https://github.com/ziglang/zig/issues/319057 // TODO https://github.com/ziglang/zig/issues/3190
58 var libc = ctx.comp.override_libc orelse blk: {58 var libc = ctx.comp.override_libc orelse blk: {
59 switch (comp.target) {59 @panic("this code has bitrotted");
60 Target.Native => {60 //switch (comp.target) {
61 break :blk comp.zig_compiler.getNativeLibC() catch return error.LibCRequiredButNotProvidedOrFound;61 // Target.Native => {
62 },62 // break :blk comp.zig_compiler.getNativeLibC() catch return error.LibCRequiredButNotProvidedOrFound;
63 else => return error.LibCRequiredButNotProvidedOrFound,63 // },
64 }64 // else => return error.LibCRequiredButNotProvidedOrFound,
65 //}
65 };66 };
66 ctx.libc = libc;67 ctx.libc = libc;
67 }68 }
...@@ -155,11 +156,11 @@ fn constructLinkerArgsElf(ctx: *Context) !void {...@@ -155,11 +156,11 @@ fn constructLinkerArgsElf(ctx: *Context) !void {
155 //bool shared = !g->is_static && is_lib;156 //bool shared = !g->is_static && is_lib;
156 //Buf *soname = nullptr;157 //Buf *soname = nullptr;
157 if (ctx.comp.is_static) {158 if (ctx.comp.is_static) {
158 if (util.isArmOrThumb(ctx.comp.target)) {159 //if (util.isArmOrThumb(ctx.comp.target)) {
159 try ctx.args.append("-Bstatic");160 // try ctx.args.append("-Bstatic");
160 } else {161 //} else {
161 try ctx.args.append("-static");162 // try ctx.args.append("-static");
162 }163 //}
163 }164 }
164 //} else if (shared) {165 //} else if (shared) {
165 // lj->args.append("-shared");166 // lj->args.append("-shared");
...@@ -176,29 +177,24 @@ fn constructLinkerArgsElf(ctx: *Context) !void {...@@ -176,29 +177,24 @@ fn constructLinkerArgsElf(ctx: *Context) !void {
176177
177 if (ctx.link_in_crt) {178 if (ctx.link_in_crt) {
178 const crt1o = if (ctx.comp.is_static) "crt1.o" else "Scrt1.o";179 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.crt_dir.?, crt1o);
180 try addPathJoin(ctx, ctx.libc.lib_dir.?, crt1o);181 try addPathJoin(ctx, ctx.libc.crt_dir.?, "crti.o");
181 try addPathJoin(ctx, ctx.libc.lib_dir.?, "crti.o");
182 try addPathJoin(ctx, ctx.libc.static_lib_dir.?, crtbegino);
183 }182 }
184183
185 if (ctx.comp.haveLibC()) {184 if (ctx.comp.haveLibC()) {
186 try ctx.args.append("-L");185 try ctx.args.append("-L");
187 // TODO addNullByte should probably return [:0]u8186 // 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");189 //if (!ctx.comp.is_static) {
191 try ctx.args.append(@ptrCast([*:0]const u8, (try std.cstr.addNullByte(&ctx.arena.allocator, ctx.libc.static_lib_dir.?)).ptr));190 // const dl = blk: {
192191 // //if (ctx.libc.dynamic_linker_path) |dl| break :blk dl;
193 if (!ctx.comp.is_static) {192 // //if (util.getDynamicLinkerPath(ctx.comp.target)) |dl| break :blk dl;
194 const dl = blk: {193 // return error.LibCMissingDynamicLinker;
195 if (ctx.libc.dynamic_linker_path) |dl| break :blk dl;194 // };
196 if (util.getDynamicLinkerPath(ctx.comp.target)) |dl| break :blk dl;195 // try ctx.args.append("-dynamic-linker");
197 return error.LibCMissingDynamicLinker;196 // try ctx.args.append(@ptrCast([*:0]const u8, (try std.cstr.addNullByte(&ctx.arena.allocator, dl)).ptr));
198 };197 //}
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 }
202 }198 }
203199
204 //if (shared) {200 //if (shared) {
...@@ -265,13 +261,12 @@ fn constructLinkerArgsElf(ctx: *Context) !void {...@@ -265,13 +261,12 @@ fn constructLinkerArgsElf(ctx: *Context) !void {
265261
266 // crt end262 // crt end
267 if (ctx.link_in_crt) {263 if (ctx.link_in_crt) {
268 try addPathJoin(ctx, ctx.libc.static_lib_dir.?, "crtend.o");264 try addPathJoin(ctx, ctx.libc.crt_dir.?, "crtn.o");
269 try addPathJoin(ctx, ctx.libc.lib_dir.?, "crtn.o");
270 }265 }
271266
272 if (ctx.comp.target != Target.Native) {267 //if (ctx.comp.target != Target.Native) {
273 try ctx.args.append("--allow-shlib-undefined");268 // try ctx.args.append("--allow-shlib-undefined");
274 }269 //}
275}270}
276271
277fn addPathJoin(ctx: *Context, dirname: []const u8, basename: []const u8) !void {272fn addPathJoin(ctx: *Context, dirname: []const u8, basename: []const u8) !void {
...@@ -287,7 +282,7 @@ fn constructLinkerArgsCoff(ctx: *Context) !void {...@@ -287,7 +282,7 @@ fn constructLinkerArgsCoff(ctx: *Context) !void {
287 try ctx.args.append("-DEBUG");282 try ctx.args.append("-DEBUG");
288 }283 }
289284
290 switch (ctx.comp.target.getArch()) {285 switch (ctx.comp.target.cpu.arch) {
291 .i386 => try ctx.args.append("-MACHINE:X86"),286 .i386 => try ctx.args.append("-MACHINE:X86"),
292 .x86_64 => try ctx.args.append("-MACHINE:X64"),287 .x86_64 => try ctx.args.append("-MACHINE:X64"),
293 .aarch64 => try ctx.args.append("-MACHINE:ARM"),288 .aarch64 => try ctx.args.append("-MACHINE:ARM"),
...@@ -302,7 +297,7 @@ fn constructLinkerArgsCoff(ctx: *Context) !void {...@@ -302,7 +297,7 @@ fn constructLinkerArgsCoff(ctx: *Context) !void {
302 if (ctx.comp.haveLibC()) {297 if (ctx.comp.haveLibC()) {
303 try ctx.args.append(@ptrCast([*:0]const u8, (try std.fmt.allocPrint(&ctx.arena.allocator, "-LIBPATH:{}\x00", .{ctx.libc.msvc_lib_dir.?})).ptr));298 try ctx.args.append(@ptrCast([*:0]const u8, (try std.fmt.allocPrint(&ctx.arena.allocator, "-LIBPATH:{}\x00", .{ctx.libc.msvc_lib_dir.?})).ptr));
304 try ctx.args.append(@ptrCast([*:0]const u8, (try std.fmt.allocPrint(&ctx.arena.allocator, "-LIBPATH:{}\x00", .{ctx.libc.kernel32_lib_dir.?})).ptr));299 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));
306 }301 }
307302
308 if (ctx.link_in_crt) {303 if (ctx.link_in_crt) {
...@@ -417,7 +412,7 @@ fn constructLinkerArgsMachO(ctx: *Context) !void {...@@ -417,7 +412,7 @@ fn constructLinkerArgsMachO(ctx: *Context) !void {
417 }412 }
418 },413 },
419 .IPhoneOS => {414 .IPhoneOS => {
420 if (ctx.comp.target.getArch() == .aarch64) {415 if (ctx.comp.target.cpu.arch == .aarch64) {
421 // iOS does not need any crt1 files for arm64416 // iOS does not need any crt1 files for arm64
422 } else if (platform.versionLessThan(3, 1)) {417 } else if (platform.versionLessThan(3, 1)) {
423 try ctx.args.append("-lcrt1.o");418 try ctx.args.append("-lcrt1.o");
...@@ -435,28 +430,29 @@ fn constructLinkerArgsMachO(ctx: *Context) !void {...@@ -435,28 +430,29 @@ fn constructLinkerArgsMachO(ctx: *Context) !void {
435 }430 }
436 try addFnObjects(ctx);431 try addFnObjects(ctx);
437432
438 if (ctx.comp.target == Target.Native) {433 // TODO
439 for (ctx.comp.link_libs_list.toSliceConst()) |lib| {434 //if (ctx.comp.target == Target.Native) {
440 if (mem.eql(u8, lib.name, "c")) {435 // for (ctx.comp.link_libs_list.toSliceConst()) |lib| {
441 // on Darwin, libSystem has libc in it, but also you have to use it436 // if (mem.eql(u8, lib.name, "c")) {
442 // to make syscalls because the syscall numbers are not documented437 // // on Darwin, libSystem has libc in it, but also you have to use it
443 // and change between versions.438 // // to make syscalls because the syscall numbers are not documented
444 // so we always link against libSystem439 // // and change between versions.
445 try ctx.args.append("-lSystem");440 // // so we always link against libSystem
446 } else {441 // try ctx.args.append("-lSystem");
447 if (mem.indexOfScalar(u8, lib.name, '/') == null) {442 // } else {
448 const arg = try std.fmt.allocPrint(&ctx.arena.allocator, "-l{}\x00", .{lib.name});443 // if (mem.indexOfScalar(u8, lib.name, '/') == null) {
449 try ctx.args.append(@ptrCast([*:0]const u8, arg.ptr));444 // const arg = try std.fmt.allocPrint(&ctx.arena.allocator, "-l{}\x00", .{lib.name});
450 } else {445 // try ctx.args.append(@ptrCast([*:0]const u8, arg.ptr));
451 const arg = try std.cstr.addNullByte(&ctx.arena.allocator, lib.name);446 // } else {
452 try ctx.args.append(@ptrCast([*:0]const u8, arg.ptr));447 // const arg = try std.cstr.addNullByte(&ctx.arena.allocator, lib.name);
453 }448 // try ctx.args.append(@ptrCast([*:0]const u8, arg.ptr));
454 }449 // }
455 }450 // }
456 } else {451 // }
457 try ctx.args.append("-undefined");452 //} else {
458 try ctx.args.append("dynamic_lookup");453 // try ctx.args.append("-undefined");
459 }454 // try ctx.args.append("dynamic_lookup");
455 //}
460456
461 if (platform.kind == .MacOS) {457 if (platform.kind == .MacOS) {
462 if (platform.versionLessThan(10, 5)) {458 if (platform.versionLessThan(10, 5)) {
src-self-hosted/main.zig+55-46
...@@ -18,10 +18,6 @@ const Target = std.Target;...@@ -18,10 +18,6 @@ const Target = std.Target;
18const errmsg = @import("errmsg.zig");18const errmsg = @import("errmsg.zig");
19const LibCInstallation = @import("libc_installation.zig").LibCInstallation;19const 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
25pub const io_mode = .evented;21pub const io_mode = .evented;
2622
27pub const max_src_size = 2 * 1024 * 1024 * 1024; // 2 GiB23pub const max_src_size = 2 * 1024 * 1024 * 1024; // 2 GiB
...@@ -51,17 +47,14 @@ const Command = struct {...@@ -51,17 +47,14 @@ const Command = struct {
51pub fn main() !void {47pub fn main() !void {
52 const allocator = std.heap.c_allocator;48 const allocator = std.heap.c_allocator;
5349
54 stdout = &std.io.getStdOut().outStream().stream;50 const stderr = io.getStdErr().outStream();
55
56 stderr_file = std.io.getStdErr();
57 stderr = &stderr_file.outStream().stream;
5851
59 const args = try process.argsAlloc(allocator);52 const args = try process.argsAlloc(allocator);
60 defer process.argsFree(allocator, args);53 defer process.argsFree(allocator, args);
6154
62 if (args.len <= 1) {55 if (args.len <= 1) {
63 try stderr.write("expected command argument\n\n");56 try stderr.writeAll("expected command argument\n\n");
64 try stderr.write(usage);57 try stderr.writeAll(usage);
65 process.exit(1);58 process.exit(1);
66 }59 }
6760
...@@ -78,8 +71,8 @@ pub fn main() !void {...@@ -78,8 +71,8 @@ pub fn main() !void {
78 } else if (mem.eql(u8, cmd, "libc")) {71 } else if (mem.eql(u8, cmd, "libc")) {
79 return cmdLibC(allocator, cmd_args);72 return cmdLibC(allocator, cmd_args);
80 } else if (mem.eql(u8, cmd, "targets")) {73 } else if (mem.eql(u8, cmd, "targets")) {
81 const info = try std.zig.system.NativeTargetInfo.detect(allocator);74 const info = try std.zig.system.NativeTargetInfo.detect(allocator, .{});
82 defer info.deinit(allocator);75 const stdout = io.getStdOut().outStream();
83 return @import("print_targets.zig").cmdTargets(allocator, cmd_args, stdout, info.target);76 return @import("print_targets.zig").cmdTargets(allocator, cmd_args, stdout, info.target);
84 } else if (mem.eql(u8, cmd, "version")) {77 } else if (mem.eql(u8, cmd, "version")) {
85 return cmdVersion(allocator, cmd_args);78 return cmdVersion(allocator, cmd_args);
...@@ -91,7 +84,7 @@ pub fn main() !void {...@@ -91,7 +84,7 @@ pub fn main() !void {
91 return cmdInternal(allocator, cmd_args);84 return cmdInternal(allocator, cmd_args);
92 } else {85 } else {
93 try stderr.print("unknown command: {}\n\n", .{args[1]});86 try stderr.print("unknown command: {}\n\n", .{args[1]});
94 try stderr.write(usage);87 try stderr.writeAll(usage);
95 process.exit(1);88 process.exit(1);
96 }89 }
97}90}
...@@ -156,6 +149,8 @@ const usage_build_generic =...@@ -156,6 +149,8 @@ const usage_build_generic =
156;149;
157150
158fn buildOutputType(allocator: *Allocator, args: []const []const u8, out_type: Compilation.Kind) !void {151fn buildOutputType(allocator: *Allocator, args: []const []const u8, out_type: Compilation.Kind) !void {
152 const stderr = io.getStdErr().outStream();
153
159 var color: errmsg.Color = .Auto;154 var color: errmsg.Color = .Auto;
160 var build_mode: std.builtin.Mode = .Debug;155 var build_mode: std.builtin.Mode = .Debug;
161 var emit_bin = true;156 var emit_bin = true;
...@@ -208,11 +203,11 @@ fn buildOutputType(allocator: *Allocator, args: []const []const u8, out_type: Co...@@ -208,11 +203,11 @@ fn buildOutputType(allocator: *Allocator, args: []const []const u8, out_type: Co
208 const arg = args[i];203 const arg = args[i];
209 if (mem.startsWith(u8, arg, "-")) {204 if (mem.startsWith(u8, arg, "-")) {
210 if (mem.eql(u8, arg, "--help")) {205 if (mem.eql(u8, arg, "--help")) {
211 try stdout.write(usage_build_generic);206 try io.getStdOut().writeAll(usage_build_generic);
212 process.exit(0);207 process.exit(0);
213 } else if (mem.eql(u8, arg, "--color")) {208 } else if (mem.eql(u8, arg, "--color")) {
214 if (i + 1 >= args.len) {209 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");
216 process.exit(1);211 process.exit(1);
217 }212 }
218 i += 1;213 i += 1;
...@@ -229,7 +224,7 @@ fn buildOutputType(allocator: *Allocator, args: []const []const u8, out_type: Co...@@ -229,7 +224,7 @@ fn buildOutputType(allocator: *Allocator, args: []const []const u8, out_type: Co
229 }224 }
230 } else if (mem.eql(u8, arg, "--mode")) {225 } else if (mem.eql(u8, arg, "--mode")) {
231 if (i + 1 >= args.len) {226 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");
233 process.exit(1);228 process.exit(1);
234 }229 }
235 i += 1;230 i += 1;
...@@ -248,49 +243,49 @@ fn buildOutputType(allocator: *Allocator, args: []const []const u8, out_type: Co...@@ -248,49 +243,49 @@ fn buildOutputType(allocator: *Allocator, args: []const []const u8, out_type: Co
248 }243 }
249 } else if (mem.eql(u8, arg, "--name")) {244 } else if (mem.eql(u8, arg, "--name")) {
250 if (i + 1 >= args.len) {245 if (i + 1 >= args.len) {
251 try stderr.write("expected parameter after --name\n");246 try stderr.writeAll("expected parameter after --name\n");
252 process.exit(1);247 process.exit(1);
253 }248 }
254 i += 1;249 i += 1;
255 provided_name = args[i];250 provided_name = args[i];
256 } else if (mem.eql(u8, arg, "--ver-major")) {251 } else if (mem.eql(u8, arg, "--ver-major")) {
257 if (i + 1 >= args.len) {252 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");
259 process.exit(1);254 process.exit(1);
260 }255 }
261 i += 1;256 i += 1;
262 version.major = try std.fmt.parseInt(u32, args[i], 10);257 version.major = try std.fmt.parseInt(u32, args[i], 10);
263 } else if (mem.eql(u8, arg, "--ver-minor")) {258 } else if (mem.eql(u8, arg, "--ver-minor")) {
264 if (i + 1 >= args.len) {259 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");
266 process.exit(1);261 process.exit(1);
267 }262 }
268 i += 1;263 i += 1;
269 version.minor = try std.fmt.parseInt(u32, args[i], 10);264 version.minor = try std.fmt.parseInt(u32, args[i], 10);
270 } else if (mem.eql(u8, arg, "--ver-patch")) {265 } else if (mem.eql(u8, arg, "--ver-patch")) {
271 if (i + 1 >= args.len) {266 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");
273 process.exit(1);268 process.exit(1);
274 }269 }
275 i += 1;270 i += 1;
276 version.patch = try std.fmt.parseInt(u32, args[i], 10);271 version.patch = try std.fmt.parseInt(u32, args[i], 10);
277 } else if (mem.eql(u8, arg, "--linker-script")) {272 } else if (mem.eql(u8, arg, "--linker-script")) {
278 if (i + 1 >= args.len) {273 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");
280 process.exit(1);275 process.exit(1);
281 }276 }
282 i += 1;277 i += 1;
283 linker_script = args[i];278 linker_script = args[i];
284 } else if (mem.eql(u8, arg, "--libc")) {279 } else if (mem.eql(u8, arg, "--libc")) {
285 if (i + 1 >= args.len) {280 if (i + 1 >= args.len) {
286 try stderr.write("expected parameter after --libc\n");281 try stderr.writeAll("expected parameter after --libc\n");
287 process.exit(1);282 process.exit(1);
288 }283 }
289 i += 1;284 i += 1;
290 libc_arg = args[i];285 libc_arg = args[i];
291 } else if (mem.eql(u8, arg, "-mllvm")) {286 } else if (mem.eql(u8, arg, "-mllvm")) {
292 if (i + 1 >= args.len) {287 if (i + 1 >= args.len) {
293 try stderr.write("expected parameter after -mllvm\n");288 try stderr.writeAll("expected parameter after -mllvm\n");
294 process.exit(1);289 process.exit(1);
295 }290 }
296 i += 1;291 i += 1;
...@@ -300,14 +295,14 @@ fn buildOutputType(allocator: *Allocator, args: []const []const u8, out_type: Co...@@ -300,14 +295,14 @@ fn buildOutputType(allocator: *Allocator, args: []const []const u8, out_type: Co
300 try mllvm_flags.append(args[i]);295 try mllvm_flags.append(args[i]);
301 } else if (mem.eql(u8, arg, "-mmacosx-version-min")) {296 } else if (mem.eql(u8, arg, "-mmacosx-version-min")) {
302 if (i + 1 >= args.len) {297 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");
304 process.exit(1);299 process.exit(1);
305 }300 }
306 i += 1;301 i += 1;
307 macosx_version_min = args[i];302 macosx_version_min = args[i];
308 } else if (mem.eql(u8, arg, "-mios-version-min")) {303 } else if (mem.eql(u8, arg, "-mios-version-min")) {
309 if (i + 1 >= args.len) {304 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");
311 process.exit(1);306 process.exit(1);
312 }307 }
313 i += 1;308 i += 1;
...@@ -348,7 +343,7 @@ fn buildOutputType(allocator: *Allocator, args: []const []const u8, out_type: Co...@@ -348,7 +343,7 @@ fn buildOutputType(allocator: *Allocator, args: []const []const u8, out_type: Co
348 linker_rdynamic = true;343 linker_rdynamic = true;
349 } else if (mem.eql(u8, arg, "--pkg-begin")) {344 } else if (mem.eql(u8, arg, "--pkg-begin")) {
350 if (i + 2 >= args.len) {345 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");
352 process.exit(1);347 process.exit(1);
353 }348 }
354 i += 1;349 i += 1;
...@@ -363,7 +358,7 @@ fn buildOutputType(allocator: *Allocator, args: []const []const u8, out_type: Co...@@ -363,7 +358,7 @@ fn buildOutputType(allocator: *Allocator, args: []const []const u8, out_type: Co
363 if (cur_pkg.parent) |parent| {358 if (cur_pkg.parent) |parent| {
364 cur_pkg = parent;359 cur_pkg = parent;
365 } else {360 } 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");
367 process.exit(1);362 process.exit(1);
368 }363 }
369 } else if (mem.startsWith(u8, arg, "-l")) {364 } else if (mem.startsWith(u8, arg, "-l")) {
...@@ -411,18 +406,18 @@ fn buildOutputType(allocator: *Allocator, args: []const []const u8, out_type: Co...@@ -411,18 +406,18 @@ fn buildOutputType(allocator: *Allocator, args: []const []const u8, out_type: Co
411 var it = mem.separate(basename, ".");406 var it = mem.separate(basename, ".");
412 break :blk it.next() orelse basename;407 break :blk it.next() orelse basename;
413 } else {408 } 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");
415 process.exit(1);410 process.exit(1);
416 }411 }
417 };412 };
418413
419 if (root_src_file == null and link_objects.len == 0 and assembly_files.len == 0) {414 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");
421 process.exit(1);416 process.exit(1);
422 }417 }
423418
424 if (out_type == Compilation.Kind.Obj and link_objects.len != 0) {419 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");
426 process.exit(1);421 process.exit(1);
427 }422 }
428423
...@@ -440,7 +435,7 @@ fn buildOutputType(allocator: *Allocator, args: []const []const u8, out_type: Co...@@ -440,7 +435,7 @@ fn buildOutputType(allocator: *Allocator, args: []const []const u8, out_type: Co
440 &zig_compiler,435 &zig_compiler,
441 root_name,436 root_name,
442 root_src_file,437 root_src_file,
443 Target.Native,438 .{},
444 out_type,439 out_type,
445 build_mode,440 build_mode,
446 !is_dynamic,441 !is_dynamic,
...@@ -478,7 +473,7 @@ fn buildOutputType(allocator: *Allocator, args: []const []const u8, out_type: Co...@@ -478,7 +473,7 @@ fn buildOutputType(allocator: *Allocator, args: []const []const u8, out_type: Co
478 comp.linker_rdynamic = linker_rdynamic;473 comp.linker_rdynamic = linker_rdynamic;
479474
480 if (macosx_version_min != null and ios_version_min != null) {475 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");
482 process.exit(1);477 process.exit(1);
483 }478 }
484479
...@@ -501,6 +496,8 @@ fn buildOutputType(allocator: *Allocator, args: []const []const u8, out_type: Co...@@ -501,6 +496,8 @@ fn buildOutputType(allocator: *Allocator, args: []const []const u8, out_type: Co
501}496}
502497
503fn processBuildEvents(comp: *Compilation, color: errmsg.Color) void {498fn processBuildEvents(comp: *Compilation, color: errmsg.Color) void {
499 const stderr_file = io.getStdErr();
500 const stderr = stderr_file.outStream();
504 var count: usize = 0;501 var count: usize = 0;
505 while (!comp.cancelled) {502 while (!comp.cancelled) {
506 const build_event = comp.events.get();503 const build_event = comp.events.get();
...@@ -551,7 +548,8 @@ const Fmt = struct {...@@ -551,7 +548,8 @@ const Fmt = struct {
551};548};
552549
553fn parseLibcPaths(allocator: *Allocator, libc: *LibCInstallation, libc_paths_file: []const u8) void {550fn 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| {
555 stderr.print("Unable to parse libc path file '{}': {}.\n" ++553 stderr.print("Unable to parse libc path file '{}': {}.\n" ++
556 "Try running `zig libc` to see an example for the native target.\n", .{554 "Try running `zig libc` to see an example for the native target.\n", .{
557 libc_paths_file,555 libc_paths_file,
...@@ -562,6 +560,7 @@ fn parseLibcPaths(allocator: *Allocator, libc: *LibCInstallation, libc_paths_fil...@@ -562,6 +560,7 @@ fn parseLibcPaths(allocator: *Allocator, libc: *LibCInstallation, libc_paths_fil
562}560}
563561
564fn cmdLibC(allocator: *Allocator, args: []const []const u8) !void {562fn cmdLibC(allocator: *Allocator, args: []const []const u8) !void {
563 const stderr = io.getStdErr().outStream();
565 switch (args.len) {564 switch (args.len) {
566 0 => {},565 0 => {},
567 1 => {566 1 => {
...@@ -582,10 +581,12 @@ fn cmdLibC(allocator: *Allocator, args: []const []const u8) !void {...@@ -582,10 +581,12 @@ fn cmdLibC(allocator: *Allocator, args: []const []const u8) !void {
582 stderr.print("unable to find libc: {}\n", .{@errorName(err)}) catch {};581 stderr.print("unable to find libc: {}\n", .{@errorName(err)}) catch {};
583 process.exit(1);582 process.exit(1);
584 };583 };
585 libc.render(stdout) catch process.exit(1);584 libc.render(io.getStdOut().outStream()) catch process.exit(1);
586}585}
587586
588fn cmdFmt(allocator: *Allocator, args: []const []const u8) !void {587fn cmdFmt(allocator: *Allocator, args: []const []const u8) !void {
588 const stderr_file = io.getStdErr();
589 const stderr = stderr_file.outStream();
589 var color: errmsg.Color = .Auto;590 var color: errmsg.Color = .Auto;
590 var stdin_flag: bool = false;591 var stdin_flag: bool = false;
591 var check_flag: bool = false;592 var check_flag: bool = false;
...@@ -597,11 +598,12 @@ fn cmdFmt(allocator: *Allocator, args: []const []const u8) !void {...@@ -597,11 +598,12 @@ fn cmdFmt(allocator: *Allocator, args: []const []const u8) !void {
597 const arg = args[i];598 const arg = args[i];
598 if (mem.startsWith(u8, arg, "-")) {599 if (mem.startsWith(u8, arg, "-")) {
599 if (mem.eql(u8, arg, "--help")) {600 if (mem.eql(u8, arg, "--help")) {
600 try stdout.write(usage_fmt);601 const stdout = io.getStdOut().outStream();
602 try stdout.writeAll(usage_fmt);
601 process.exit(0);603 process.exit(0);
602 } else if (mem.eql(u8, arg, "--color")) {604 } else if (mem.eql(u8, arg, "--color")) {
603 if (i + 1 >= args.len) {605 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");
605 process.exit(1);607 process.exit(1);
606 }608 }
607 i += 1;609 i += 1;
...@@ -632,14 +634,13 @@ fn cmdFmt(allocator: *Allocator, args: []const []const u8) !void {...@@ -632,14 +634,13 @@ fn cmdFmt(allocator: *Allocator, args: []const []const u8) !void {
632634
633 if (stdin_flag) {635 if (stdin_flag) {
634 if (input_files.len != 0) {636 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");
636 process.exit(1);638 process.exit(1);
637 }639 }
638640
639 var stdin_file = io.getStdIn();641 const stdin = io.getStdIn().inStream();
640 var stdin = stdin_file.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);
643 defer allocator.free(source_code);644 defer allocator.free(source_code);
644645
645 const tree = std.zig.parse(allocator, source_code) catch |err| {646 const tree = std.zig.parse(allocator, source_code) catch |err| {
...@@ -653,7 +654,7 @@ fn cmdFmt(allocator: *Allocator, args: []const []const u8) !void {...@@ -653,7 +654,7 @@ fn cmdFmt(allocator: *Allocator, args: []const []const u8) !void {
653 const msg = try errmsg.Msg.createFromParseError(allocator, parse_error, tree, "<stdin>");654 const msg = try errmsg.Msg.createFromParseError(allocator, parse_error, tree, "<stdin>");
654 defer msg.destroy();655 defer msg.destroy();
655656
656 try msg.printToFile(stderr_file, color);657 try msg.printToFile(io.getStdErr(), color);
657 }658 }
658 if (tree.errors.len != 0) {659 if (tree.errors.len != 0) {
659 process.exit(1);660 process.exit(1);
...@@ -664,12 +665,13 @@ fn cmdFmt(allocator: *Allocator, args: []const []const u8) !void {...@@ -664,12 +665,13 @@ fn cmdFmt(allocator: *Allocator, args: []const []const u8) !void {
664 process.exit(code);665 process.exit(code);
665 }666 }
666667
668 const stdout = io.getStdOut().outStream();
667 _ = try std.zig.render(allocator, stdout, tree);669 _ = try std.zig.render(allocator, stdout, tree);
668 return;670 return;
669 }671 }
670672
671 if (input_files.len == 0) {673 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");
673 process.exit(1);675 process.exit(1);
674 }676 }
675677
...@@ -713,6 +715,9 @@ const FmtError = error{...@@ -713,6 +715,9 @@ const FmtError = error{
713} || fs.File.OpenError;715} || fs.File.OpenError;
714716
715async fn fmtPath(fmt: *Fmt, file_path_ref: []const u8, check_mode: bool) FmtError!void {717async 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
716 const file_path = try std.mem.dupe(fmt.allocator, u8, file_path_ref);721 const file_path = try std.mem.dupe(fmt.allocator, u8, file_path_ref);
717 defer fmt.allocator.free(file_path);722 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...@@ -791,11 +796,13 @@ async fn fmtPath(fmt: *Fmt, file_path_ref: []const u8, check_mode: bool) FmtErro
791}796}
792797
793fn cmdVersion(allocator: *Allocator, args: []const []const u8) !void {798fn cmdVersion(allocator: *Allocator, args: []const []const u8) !void {
799 const stdout = io.getStdOut().outStream();
794 try stdout.print("{}\n", .{c.ZIG_VERSION_STRING});800 try stdout.print("{}\n", .{c.ZIG_VERSION_STRING});
795}801}
796802
797fn cmdHelp(allocator: *Allocator, args: []const []const u8) !void {803fn cmdHelp(allocator: *Allocator, args: []const []const u8) !void {
798 try stdout.write(usage);804 const stdout = io.getStdOut();
805 try stdout.writeAll(usage);
799}806}
800807
801pub const info_zen =808pub const info_zen =
...@@ -816,7 +823,7 @@ pub const info_zen =...@@ -816,7 +823,7 @@ pub const info_zen =
816;823;
817824
818fn cmdZen(allocator: *Allocator, args: []const []const u8) !void {825fn cmdZen(allocator: *Allocator, args: []const []const u8) !void {
819 try stdout.write(info_zen);826 try io.getStdOut().writeAll(info_zen);
820}827}
821828
822const usage_internal =829const usage_internal =
...@@ -829,8 +836,9 @@ const usage_internal =...@@ -829,8 +836,9 @@ const usage_internal =
829;836;
830837
831fn cmdInternal(allocator: *Allocator, args: []const []const u8) !void {838fn cmdInternal(allocator: *Allocator, args: []const []const u8) !void {
839 const stderr = io.getStdErr().outStream();
832 if (args.len == 0) {840 if (args.len == 0) {
833 try stderr.write(usage_internal);841 try stderr.writeAll(usage_internal);
834 process.exit(1);842 process.exit(1);
835 }843 }
836844
...@@ -849,10 +857,11 @@ fn cmdInternal(allocator: *Allocator, args: []const []const u8) !void {...@@ -849,10 +857,11 @@ fn cmdInternal(allocator: *Allocator, args: []const []const u8) !void {
849 }857 }
850858
851 try stderr.print("unknown sub command: {}\n\n", .{args[0]});859 try stderr.print("unknown sub command: {}\n\n", .{args[0]});
852 try stderr.write(usage_internal);860 try stderr.writeAll(usage_internal);
853}861}
854862
855fn cmdInternalBuildInfo(allocator: *Allocator, args: []const []const u8) !void {863fn cmdInternalBuildInfo(allocator: *Allocator, args: []const []const u8) !void {
864 const stdout = io.getStdOut().outStream();
856 try stdout.print(865 try stdout.print(
857 \\ZIG_CMAKE_BINARY_DIR {}866 \\ZIG_CMAKE_BINARY_DIR {}
858 \\ZIG_CXX_COMPILER {}867 \\ZIG_CXX_COMPILER {}
src-self-hosted/util.zig+13-2
...@@ -3,8 +3,7 @@ const Target = std.Target;...@@ -3,8 +3,7 @@ const Target = std.Target;
3const llvm = @import("llvm.zig");3const llvm = @import("llvm.zig");
44
5pub fn getDarwinArchString(self: Target) [:0]const u8 {5pub fn getDarwinArchString(self: Target) [:0]const u8 {
6 const arch = self.getArch();6 switch (self.cpu.arch) {
7 switch (arch) {
8 .aarch64 => return "arm64",7 .aarch64 => return "arm64",
9 .thumb,8 .thumb,
10 .arm,9 .arm,
...@@ -34,3 +33,15 @@ pub fn initializeAllTargets() void {...@@ -34,3 +33,15 @@ pub fn initializeAllTargets() void {
34 llvm.InitializeAllAsmPrinters();33 llvm.InitializeAllAsmPrinters();
35 llvm.InitializeAllAsmParsers();34 llvm.InitializeAllAsmParsers();
36}35}
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}