fix target parsing

This commit is contained in:
Andrew Kelley 2020-02-26 14:33:31 -05:00
parent c8669a4cf8
commit cf233bad58
No known key found for this signature in database
GPG key ID: 7C5F548F728501A9
2 changed files with 16 additions and 1 deletions

View file

@ -177,6 +177,9 @@ pub const CrossTarget = struct {
/// If the architecture was determined, this will be populated. /// If the architecture was determined, this will be populated.
arch: ?Target.Cpu.Arch = null, arch: ?Target.Cpu.Arch = null,
/// If the OS name was determined, this will be populated.
os_name: ?[]const u8 = null,
/// If the OS tag was determined, this will be populated. /// If the OS tag was determined, this will be populated.
os_tag: ?Target.Os.Tag = null, os_tag: ?Target.Os.Tag = null,
@ -219,6 +222,7 @@ pub const CrossTarget = struct {
var abi_it = mem.separate(abi_text, "."); var abi_it = mem.separate(abi_text, ".");
const abi = std.meta.stringToEnum(Target.Abi, abi_it.next().?) orelse const abi = std.meta.stringToEnum(Target.Abi, abi_it.next().?) orelse
return error.UnknownApplicationBinaryInterface; return error.UnknownApplicationBinaryInterface;
result.abi = abi;
diags.abi = abi; diags.abi = abi;
const abi_ver_text = abi_it.rest(); const abi_ver_text = abi_it.rest();
@ -614,6 +618,7 @@ pub const CrossTarget = struct {
fn parseOs(result: *CrossTarget, diags: *ParseOptions.Diagnostics, text: []const u8) !void { fn parseOs(result: *CrossTarget, diags: *ParseOptions.Diagnostics, text: []const u8) !void {
var it = mem.separate(text, "."); var it = mem.separate(text, ".");
const os_name = it.next().?; const os_name = it.next().?;
diags.os_name = os_name;
const os_is_native = mem.eql(u8, os_name, "native"); const os_is_native = mem.eql(u8, os_name, "native");
if (!os_is_native) { if (!os_is_native) {
result.os_tag = std.meta.stringToEnum(Target.Os.Tag, os_name) orelse result.os_tag = std.meta.stringToEnum(Target.Os.Tag, os_name) orelse
@ -702,6 +707,16 @@ pub const CrossTarget = struct {
}; };
test "CrossTarget.parse" { test "CrossTarget.parse" {
{
const cross_target = try CrossTarget.parse(.{ .arch_os_abi = "native" });
std.testing.expect(cross_target.cpu_arch == null);
std.testing.expect(cross_target.isNative());
const text = try cross_target.zigTriple(std.testing.allocator);
defer std.testing.allocator.free(text);
std.testing.expectEqualSlices(u8, "native", text);
}
{ {
const cross_target = try CrossTarget.parse(.{ const cross_target = try CrossTarget.parse(.{
.arch_os_abi = "x86_64-linux-gnu", .arch_os_abi = "x86_64-linux-gnu",

View file

@ -679,7 +679,7 @@ fn stage2TargetParse(
) !void { ) !void {
const target: CrossTarget = if (zig_triple_oz) |zig_triple_z| blk: { const target: CrossTarget = if (zig_triple_oz) |zig_triple_z| blk: {
const zig_triple = mem.toSliceConst(u8, zig_triple_z); const zig_triple = mem.toSliceConst(u8, zig_triple_z);
const mcpu = if (mcpu_oz) |mcpu_z| mem.toSliceConst(u8, mcpu_z) else "baseline"; const mcpu = if (mcpu_oz) |mcpu_z| mem.toSliceConst(u8, mcpu_z) else null;
var diags: CrossTarget.ParseOptions.Diagnostics = .{}; var diags: CrossTarget.ParseOptions.Diagnostics = .{};
break :blk CrossTarget.parse(.{ break :blk CrossTarget.parse(.{
.arch_os_abi = zig_triple, .arch_os_abi = zig_triple,