authorgravatar for noam@pixelhero.devNoam Preil <noam@pixelhero.dev> 2020-07-07 21:54:34-04:00
committergravatar for noam@pixelhero.devNoam Preil <noam@pixelhero.dev> 2020-07-07 21:54:34-04:00
log5461c482d0643abd83ac834be0a95da066c53c69
treec8285fc7878655563c24d56c977203528ed67d6d
parent64bf1301822866904f52a70213dd71eefce4b99a
signaturelock-open Commit is signed but in an unrecognized format.

CBE: Integrate into stage2 via --c-standard


1 files changed, 63 insertions(+), 43 deletions(-)

src-self-hosted/main.zig+63-43
......@@ -71,7 +71,7 @@ pub fn main() !void {
7171 const args = try process.argsAlloc(arena);
7272
7373 if (args.len <= 1) {
74 std.debug.warn("expected command argument\n\n{}", .{usage});
74 std.debug.print("expected command argument\n\n{}", .{usage});
7575 process.exit(1);
7676 }
7777
......@@ -91,14 +91,14 @@ pub fn main() !void {
9191 return @import("print_targets.zig").cmdTargets(arena, cmd_args, stdout, info.target);
9292 } else if (mem.eql(u8, cmd, "version")) {
9393 // Need to set up the build script to give the version as a comptime value.
94 std.debug.warn("TODO version command not implemented yet\n", .{});
94 std.debug.print("TODO version command not implemented yet\n", .{});
9595 return error.Unimplemented;
9696 } else if (mem.eql(u8, cmd, "zen")) {
9797 try io.getStdOut().writeAll(info_zen);
9898 } else if (mem.eql(u8, cmd, "help")) {
9999 try io.getStdOut().writeAll(usage);
100100 } else {
101 std.debug.warn("unknown command: {}\n\n{}", .{ args[1], usage });
101 std.debug.print("unknown command: {}\n\n{}", .{ args[1], usage });
102102 process.exit(1);
103103 }
104104}
......@@ -191,6 +191,7 @@ fn buildOutputType(
191191 var emit_zir: Emit = .no;
192192 var target_arch_os_abi: []const u8 = "native";
193193 var target_mcpu: ?[]const u8 = null;
194 var target_c_standard: ?Module.CStandard = null;
194195 var target_dynamic_linker: ?[]const u8 = null;
195196
196197 var system_libs = std.ArrayList([]const u8).init(gpa);
......@@ -206,7 +207,7 @@ fn buildOutputType(
206207 process.exit(0);
207208 } else if (mem.eql(u8, arg, "--color")) {
208209 if (i + 1 >= args.len) {
209 std.debug.warn("expected [auto|on|off] after --color\n", .{});
210 std.debug.print("expected [auto|on|off] after --color\n", .{});
210211 process.exit(1);
211212 }
212213 i += 1;
......@@ -218,12 +219,12 @@ fn buildOutputType(
218219 } else if (mem.eql(u8, next_arg, "off")) {
219220 color = .Off;
220221 } else {
221 std.debug.warn("expected [auto|on|off] after --color, found '{}'\n", .{next_arg});
222 std.debug.print("expected [auto|on|off] after --color, found '{}'\n", .{next_arg});
222223 process.exit(1);
223224 }
224225 } else if (mem.eql(u8, arg, "--mode")) {
225226 if (i + 1 >= args.len) {
226 std.debug.warn("expected [Debug|ReleaseSafe|ReleaseFast|ReleaseSmall] after --mode\n", .{});
227 std.debug.print("expected [Debug|ReleaseSafe|ReleaseFast|ReleaseSmall] after --mode\n", .{});
227228 process.exit(1);
228229 }
229230 i += 1;
......@@ -237,52 +238,64 @@ fn buildOutputType(
237238 } else if (mem.eql(u8, next_arg, "ReleaseSmall")) {
238239 build_mode = .ReleaseSmall;
239240 } else {
240 std.debug.warn("expected [Debug|ReleaseSafe|ReleaseFast|ReleaseSmall] after --mode, found '{}'\n", .{next_arg});
241 std.debug.print("expected [Debug|ReleaseSafe|ReleaseFast|ReleaseSmall] after --mode, found '{}'\n", .{next_arg});
241242 process.exit(1);
242243 }
243244 } else if (mem.eql(u8, arg, "--name")) {
244245 if (i + 1 >= args.len) {
245 std.debug.warn("expected parameter after --name\n", .{});
246 std.debug.print("expected parameter after --name\n", .{});
246247 process.exit(1);
247248 }
248249 i += 1;
249250 provided_name = args[i];
250251 } else if (mem.eql(u8, arg, "--library")) {
251252 if (i + 1 >= args.len) {
252 std.debug.warn("expected parameter after --library\n", .{});
253 std.debug.print("expected parameter after --library\n", .{});
253254 process.exit(1);
254255 }
255256 i += 1;
256257 try system_libs.append(args[i]);
257258 } else if (mem.eql(u8, arg, "--version")) {
258259 if (i + 1 >= args.len) {
259 std.debug.warn("expected parameter after --version\n", .{});
260 std.debug.print("expected parameter after --version\n", .{});
260261 process.exit(1);
261262 }
262263 i += 1;
263264 version = std.builtin.Version.parse(args[i]) catch |err| {
264 std.debug.warn("unable to parse --version '{}': {}\n", .{ args[i], @errorName(err) });
265 std.debug.print("unable to parse --version '{}': {}\n", .{ args[i], @errorName(err) });
265266 process.exit(1);
266267 };
267268 } else if (mem.eql(u8, arg, "-target")) {
268269 if (i + 1 >= args.len) {
269 std.debug.warn("expected parameter after -target\n", .{});
270 std.debug.print("expected parameter after -target\n", .{});
270271 process.exit(1);
271272 }
272273 i += 1;
273274 target_arch_os_abi = args[i];
274275 } else if (mem.eql(u8, arg, "-mcpu")) {
275276 if (i + 1 >= args.len) {
276 std.debug.warn("expected parameter after -mcpu\n", .{});
277 std.debug.print("expected parameter after -mcpu\n", .{});
277278 process.exit(1);
278279 }
279280 i += 1;
280281 target_mcpu = args[i];
282 } else if (mem.eql(u8, arg, "--c-standard")) {
283 if (i + 1 >= args.len) {
284 std.debug.print("expected parameter after --c-standard\n", .{});
285 process.exit(1);
286 }
287 i += 1;
288 if (std.meta.stringToEnum(Module.CStandard, args[i])) |cstd| {
289 target_c_standard = cstd;
290 } else {
291 std.debug.print("Invalid C standard: {}\n", .{args[i]});
292 process.exit(1);
293 }
281294 } else if (mem.startsWith(u8, arg, "-mcpu=")) {
282295 target_mcpu = arg["-mcpu=".len..];
283296 } else if (mem.eql(u8, arg, "--dynamic-linker")) {
284297 if (i + 1 >= args.len) {
285 std.debug.warn("expected parameter after --dynamic-linker\n", .{});
298 std.debug.print("expected parameter after --dynamic-linker\n", .{});
286299 process.exit(1);
287300 }
288301 i += 1;
......@@ -324,56 +337,61 @@ fn buildOutputType(
324337 } else if (mem.startsWith(u8, arg, "-l")) {
325338 try system_libs.append(arg[2..]);
326339 } else {
327 std.debug.warn("unrecognized parameter: '{}'", .{arg});
340 std.debug.print("unrecognized parameter: '{}'", .{arg});
328341 process.exit(1);
329342 }
330343 } else if (mem.endsWith(u8, arg, ".s") or mem.endsWith(u8, arg, ".S")) {
331 std.debug.warn("assembly files not supported yet", .{});
344 std.debug.print("assembly files not supported yet", .{});
332345 process.exit(1);
333346 } else if (mem.endsWith(u8, arg, ".o") or
334347 mem.endsWith(u8, arg, ".obj") or
335348 mem.endsWith(u8, arg, ".a") or
336349 mem.endsWith(u8, arg, ".lib"))
337350 {
338 std.debug.warn("object files and static libraries not supported yet", .{});
351 std.debug.print("object files and static libraries not supported yet", .{});
339352 process.exit(1);
340353 } else if (mem.endsWith(u8, arg, ".c") or
341354 mem.endsWith(u8, arg, ".cpp"))
342355 {
343 std.debug.warn("compilation of C and C++ source code requires LLVM extensions which are not implemented yet", .{});
356 std.debug.print("compilation of C and C++ source code requires LLVM extensions which are not implemented yet", .{});
344357 process.exit(1);
345358 } else if (mem.endsWith(u8, arg, ".so") or
346359 mem.endsWith(u8, arg, ".dylib") or
347360 mem.endsWith(u8, arg, ".dll"))
348361 {
349 std.debug.warn("linking against dynamic libraries not yet supported", .{});
362 std.debug.print("linking against dynamic libraries not yet supported", .{});
350363 process.exit(1);
351364 } else if (mem.endsWith(u8, arg, ".zig") or mem.endsWith(u8, arg, ".zir")) {
352365 if (root_src_file) |other| {
353 std.debug.warn("found another zig file '{}' after root source file '{}'", .{ arg, other });
366 std.debug.print("found another zig file '{}' after root source file '{}'", .{ arg, other });
354367 process.exit(1);
355368 } else {
356369 root_src_file = arg;
357370 }
358371 } else {
359 std.debug.warn("unrecognized file extension of parameter '{}'", .{arg});
372 std.debug.print("unrecognized file extension of parameter '{}'", .{arg});
360373 }
361374 }
362375 }
363376
377 if (target_c_standard != null and output_mode != .Obj) {
378 std.debug.print("The C backend must be used with build-obj\n", .{});
379 process.exit(1);
380 }
381
364382 const root_name = if (provided_name) |n| n else blk: {
365383 if (root_src_file) |file| {
366384 const basename = fs.path.basename(file);
367385 var it = mem.split(basename, ".");
368386 break :blk it.next() orelse basename;
369387 } else {
370 std.debug.warn("--name [name] not provided and unable to infer\n", .{});
388 std.debug.print("--name [name] not provided and unable to infer\n", .{});
371389 process.exit(1);
372390 }
373391 };
374392
375393 if (system_libs.items.len != 0) {
376 std.debug.warn("linking against system libraries not yet supported", .{});
394 std.debug.print("linking against system libraries not yet supported", .{});
377395 process.exit(1);
378396 }
379397
......@@ -385,17 +403,17 @@ fn buildOutputType(
385403 .diagnostics = &diags,
386404 }) catch |err| switch (err) {
387405 error.UnknownCpuModel => {
388 std.debug.warn("Unknown CPU: '{}'\nAvailable CPUs for architecture '{}':\n", .{
406 std.debug.print("Unknown CPU: '{}'\nAvailable CPUs for architecture '{}':\n", .{
389407 diags.cpu_name.?,
390408 @tagName(diags.arch.?),
391409 });
392410 for (diags.arch.?.allCpuModels()) |cpu| {
393 std.debug.warn(" {}\n", .{cpu.name});
411 std.debug.print(" {}\n", .{cpu.name});
394412 }
395413 process.exit(1);
396414 },
397415 error.UnknownCpuFeature => {
398 std.debug.warn(
416 std.debug.print(
399417 \\Unknown CPU feature: '{}'
400418 \\Available CPU features for architecture '{}':
401419 \\
......@@ -404,7 +422,7 @@ fn buildOutputType(
404422 @tagName(diags.arch.?),
405423 });
406424 for (diags.arch.?.allFeaturesList()) |feature| {
407 std.debug.warn(" {}: {}\n", .{ feature.name, feature.description });
425 std.debug.print(" {}: {}\n", .{ feature.name, feature.description });
408426 }
409427 process.exit(1);
410428 },
......@@ -416,21 +434,22 @@ fn buildOutputType(
416434 if (target_info.cpu_detection_unimplemented) {
417435 // TODO We want to just use detected_info.target but implementing
418436 // CPU model & feature detection is todo so here we rely on LLVM.
419 std.debug.warn("CPU features detection is not yet available for this system without LLVM extensions\n", .{});
437 std.debug.print("CPU features detection is not yet available for this system without LLVM extensions\n", .{});
420438 process.exit(1);
421439 }
422440
423441 const src_path = root_src_file orelse {
424 std.debug.warn("expected at least one file argument", .{});
442 std.debug.print("expected at least one file argument", .{});
425443 process.exit(1);
426444 };
427445
428446 const bin_path = switch (emit_bin) {
429447 .no => {
430 std.debug.warn("-fno-emit-bin not supported yet", .{});
448 std.debug.print("-fno-emit-bin not supported yet", .{});
431449 process.exit(1);
432450 },
433 .yes_default_path => try std.zig.binNameAlloc(arena, root_name, target_info.target, output_mode, link_mode),
451 .yes_default_path => try std.fmt.allocPrint(arena, "{}.c", .{root_name}),
452
434453 .yes => |p| p,
435454 };
436455
......@@ -460,6 +479,7 @@ fn buildOutputType(
460479 .object_format = object_format,
461480 .optimize_mode = build_mode,
462481 .keep_source_files_loaded = zir_out_path != null,
482 .c_standard = target_c_standard,
463483 });
464484 defer module.deinit();
465485
......@@ -506,7 +526,7 @@ fn updateModule(gpa: *Allocator, module: *Module, zir_out_path: ?[]const u8) !vo
506526
507527 if (errors.list.len != 0) {
508528 for (errors.list) |full_err_msg| {
509 std.debug.warn("{}:{}:{}: error: {}\n", .{
529 std.debug.print("{}:{}:{}: error: {}\n", .{
510530 full_err_msg.src_path,
511531 full_err_msg.line + 1,
512532 full_err_msg.column + 1,
......@@ -583,7 +603,7 @@ pub fn cmdFmt(gpa: *Allocator, args: []const []const u8) !void {
583603 process.exit(0);
584604 } else if (mem.eql(u8, arg, "--color")) {
585605 if (i + 1 >= args.len) {
586 std.debug.warn("expected [auto|on|off] after --color\n", .{});
606 std.debug.print("expected [auto|on|off] after --color\n", .{});
587607 process.exit(1);
588608 }
589609 i += 1;
......@@ -595,7 +615,7 @@ pub fn cmdFmt(gpa: *Allocator, args: []const []const u8) !void {
595615 } else if (mem.eql(u8, next_arg, "off")) {
596616 color = .Off;
597617 } else {
598 std.debug.warn("expected [auto|on|off] after --color, found '{}'\n", .{next_arg});
618 std.debug.print("expected [auto|on|off] after --color, found '{}'\n", .{next_arg});
599619 process.exit(1);
600620 }
601621 } else if (mem.eql(u8, arg, "--stdin")) {
......@@ -603,7 +623,7 @@ pub fn cmdFmt(gpa: *Allocator, args: []const []const u8) !void {
603623 } else if (mem.eql(u8, arg, "--check")) {
604624 check_flag = true;
605625 } else {
606 std.debug.warn("unrecognized parameter: '{}'", .{arg});
626 std.debug.print("unrecognized parameter: '{}'", .{arg});
607627 process.exit(1);
608628 }
609629 } else {
......@@ -614,7 +634,7 @@ pub fn cmdFmt(gpa: *Allocator, args: []const []const u8) !void {
614634
615635 if (stdin_flag) {
616636 if (input_files.items.len != 0) {
617 std.debug.warn("cannot use --stdin with positional arguments\n", .{});
637 std.debug.print("cannot use --stdin with positional arguments\n", .{});
618638 process.exit(1);
619639 }
620640
......@@ -624,7 +644,7 @@ pub fn cmdFmt(gpa: *Allocator, args: []const []const u8) !void {
624644 defer gpa.free(source_code);
625645
626646 const tree = std.zig.parse(gpa, source_code) catch |err| {
627 std.debug.warn("error parsing stdin: {}\n", .{err});
647 std.debug.print("error parsing stdin: {}\n", .{err});
628648 process.exit(1);
629649 };
630650 defer tree.deinit();
......@@ -647,7 +667,7 @@ pub fn cmdFmt(gpa: *Allocator, args: []const []const u8) !void {
647667 }
648668
649669 if (input_files.items.len == 0) {
650 std.debug.warn("expected at least one source file argument\n", .{});
670 std.debug.print("expected at least one source file argument\n", .{});
651671 process.exit(1);
652672 }
653673
......@@ -664,7 +684,7 @@ pub fn cmdFmt(gpa: *Allocator, args: []const []const u8) !void {
664684 for (input_files.span()) |file_path| {
665685 // Get the real path here to avoid Windows failing on relative file paths with . or .. in them.
666686 const real_path = fs.realpathAlloc(gpa, file_path) catch |err| {
667 std.debug.warn("unable to open '{}': {}\n", .{ file_path, err });
687 std.debug.print("unable to open '{}': {}\n", .{ file_path, err });
668688 process.exit(1);
669689 };
670690 defer gpa.free(real_path);
......@@ -702,7 +722,7 @@ fn fmtPath(fmt: *Fmt, file_path: []const u8, check_mode: bool, dir: fs.Dir, sub_
702722 fmtPathFile(fmt, file_path, check_mode, dir, sub_path) catch |err| switch (err) {
703723 error.IsDir, error.AccessDenied => return fmtPathDir(fmt, file_path, check_mode, dir, sub_path),
704724 else => {
705 std.debug.warn("unable to format '{}': {}\n", .{ file_path, err });
725 std.debug.print("unable to format '{}': {}\n", .{ file_path, err });
706726 fmt.any_error = true;
707727 return;
708728 },
......@@ -733,7 +753,7 @@ fn fmtPathDir(
733753 try fmtPathDir(fmt, full_path, check_mode, dir, entry.name);
734754 } else {
735755 fmtPathFile(fmt, full_path, check_mode, dir, entry.name) catch |err| {
736 std.debug.warn("unable to format '{}': {}\n", .{ full_path, err });
756 std.debug.print("unable to format '{}': {}\n", .{ full_path, err });
737757 fmt.any_error = true;
738758 return;
739759 };
......@@ -784,7 +804,7 @@ fn fmtPathFile(
784804 if (check_mode) {
785805 const anything_changed = try std.zig.render(fmt.gpa, io.null_out_stream, tree);
786806 if (anything_changed) {
787 std.debug.warn("{}\n", .{file_path});
807 std.debug.print("{}\n", .{file_path});
788808 fmt.any_error = true;
789809 }
790810 } else {
......@@ -800,7 +820,7 @@ fn fmtPathFile(
800820
801821 try af.file.writeAll(fmt.out_buffer.items);
802822 try af.finish();
803 std.debug.warn("{}\n", .{file_path});
823 std.debug.print("{}\n", .{file_path});
804824 }
805825}
806826