From aa13f339d4d91f8e39f005821c172290e1a0227f Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Fri, 28 Feb 2020 18:09:33 -0500 Subject: [PATCH] fix handling of CrossTarget.cpu_model --- lib/std/zig/cross_target.zig | 16 ++++++++++++++-- lib/std/zig/system.zig | 19 ++++++------------- src-self-hosted/stage2.zig | 1 + 3 files changed, 21 insertions(+), 15 deletions(-) diff --git a/lib/std/zig/cross_target.zig b/lib/std/zig/cross_target.zig index 7ce35a26dcf40b6effecbd893e7b659ebb16b4af..a2d265707bb2a242c94cae1b891ed4d180f7a31d 100644 --- a/lib/std/zig/cross_target.zig +++ b/lib/std/zig/cross_target.zig @@ -10,8 +10,7 @@ pub const CrossTarget = struct { /// `null` means native. If this is `null` then `cpu_model` must be `null`. cpu_arch: ?Target.Cpu.Arch = null, - /// `null` means native. - /// If this is non-null, `cpu_arch` must be specified. + /// `null` means native. If this is non-null, `cpu_arch` must be specified. cpu_model: ?*const Target.Cpu.Model = null, /// Sparse set of CPU features to add to the set from `cpu_model`. @@ -301,6 +300,10 @@ pub const CrossTarget = struct { return error.UnknownCpuFeature; } } + } else if (arch_is_native) { + result.cpu_model = null; + } else { + result.cpu_model = Target.Cpu.Model.baseline(arch); } return result; @@ -725,6 +728,15 @@ pub const CrossTarget = struct { }; test "CrossTarget.parse" { + { + const cross_target = try CrossTarget.parse(.{ + .arch_os_abi = "aarch64-linux", + .cpu_features = "native", + }); + + std.testing.expect(cross_target.cpu_arch.? == .aarch64); + std.testing.expect(cross_target.cpu_model == null); + } { const cross_target = try CrossTarget.parse(.{ .arch_os_abi = "native" }); diff --git a/lib/std/zig/system.zig b/lib/std/zig/system.zig index f31044b81108a4ddd254d87967a7f1d90cbfad2d..b712c7c0627b73b32589832d2694627535398345 100644 --- a/lib/std/zig/system.zig +++ b/lib/std/zig/system.zig @@ -192,21 +192,14 @@ pub const NativeTargetInfo = struct { /// TODO Remove the Allocator requirement from this function. pub fn detect(allocator: *Allocator, cross_target: CrossTarget) DetectError!NativeTargetInfo { const cpu = blk: { - if (cross_target.cpu_arch) |arch| { - if (cross_target.cpu_model) |model| { - var adjusted_model = model.toCpu(arch); - cross_target.updateCpuFeatures(&adjusted_model.features); - break :blk adjusted_model; - } else { - // TODO Detect native CPU model. Until that is implemented we use baseline. - var adjusted_baseline = Target.Cpu.baseline(arch); - cross_target.updateCpuFeatures(&adjusted_baseline.features); - break :blk adjusted_baseline; - } + const arch = cross_target.getCpuArch(); + if (cross_target.cpu_model) |model| { + var adjusted_model = model.toCpu(arch); + cross_target.updateCpuFeatures(&adjusted_model.features); + break :blk adjusted_model; } else { - assert(cross_target.cpu_model == null); // TODO Detect native CPU model & features. Until that is implemented we use baseline. - var adjusted_baseline = Target.Cpu.baseline(Target.current.cpu.arch); + var adjusted_baseline = Target.Cpu.baseline(arch); cross_target.updateCpuFeatures(&adjusted_baseline.features); break :blk adjusted_baseline; } diff --git a/src-self-hosted/stage2.zig b/src-self-hosted/stage2.zig index bef13a08e9470a84ef961e83c09198e2aa32e024..696d7ea76027076e3038bcb104bd10898f85909f 100644 --- a/src-self-hosted/stage2.zig +++ b/src-self-hosted/stage2.zig @@ -1163,6 +1163,7 @@ fn crossTargetToTarget(cross_target: CrossTarget, dynamic_linker_ptr: *?[*:0]u8) const arch = std.Target.current.cpu.arch; info.target.cpu = try detectNativeCpuWithLLVM(arch, llvm_cpu_name, llvm_cpu_features); cross_target.updateCpuFeatures(&info.target.cpu.features); + info.target.cpu.arch = cross_target.getCpuArch(); } if (info.dynamic_linker.get()) |dl| { dynamic_linker_ptr.* = try mem.dupeZ(std.heap.c_allocator, u8, dl); -- 2.54.0