From ed04acf90dad41b24d7e11168a0fdd3316fc6f82 Mon Sep 17 00:00:00 2001 From: Daniel Hooper Date: Tue, 5 Nov 2024 19:38:01 -0800 Subject: [PATCH] Provide a detailed message for invalid arch in target triple (#21921) --- lib/std/Target/Query.zig | 7 ++++++- lib/std/zig.zig | 11 +++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/std/Target/Query.zig b/lib/std/Target/Query.zig index e50759a347c3..f3e280ee32b9 100644 --- a/lib/std/Target/Query.zig +++ b/lib/std/Target/Query.zig @@ -193,6 +193,9 @@ pub const ParseOptions = struct { /// If error.UnknownCpuFeature is returned, this will be populated. unknown_feature_name: ?[]const u8 = null, + + /// If error.UnknownArchitecture is returned, this will be populated. + unknown_architecture_name: ?[]const u8 = null, }; }; @@ -208,8 +211,10 @@ pub fn parse(args: ParseOptions) !Query { const arch_name = it.first(); const arch_is_native = mem.eql(u8, arch_name, "native"); if (!arch_is_native) { - result.cpu_arch = std.meta.stringToEnum(Target.Cpu.Arch, arch_name) orelse + result.cpu_arch = std.meta.stringToEnum(Target.Cpu.Arch, arch_name) orelse { + diags.unknown_architecture_name = arch_name; return error.UnknownArchitecture; + }; } const arch = result.cpu_arch orelse builtin.cpu.arch; diags.arch = arch; diff --git a/lib/std/zig.zig b/lib/std/zig.zig index c3ce10663406..0ef03c24a49c 100644 --- a/lib/std/zig.zig +++ b/lib/std/zig.zig @@ -660,6 +660,17 @@ pub fn parseTargetQueryOrReportFatalError( } fatal("unknown object format: '{s}'", .{opts.object_format.?}); }, + error.UnknownArchitecture => { + help: { + var help_text = std.ArrayList(u8).init(allocator); + defer help_text.deinit(); + inline for (@typeInfo(std.Target.Cpu.Arch).@"enum".fields) |field| { + help_text.writer().print(" {s}\n", .{field.name}) catch break :help; + } + std.log.info("available architectures:\n{s} native\n", .{help_text.items}); + } + fatal("unknown architecture: '{s}'", .{diags.unknown_architecture_name.?}); + }, else => |e| fatal("unable to parse target query '{s}': {s}", .{ opts.arch_os_abi, @errorName(e), }),