authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-02-26 15:35:48-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-02-28 14:51:54-05:00
log0912484c4f1a8b278a4f04b3baa76cfa38daef6e
tree2e63ece8ffbee14d0c84b6a8b023efd330212c07
parentcebcacd872a05d8ee3edaafe3eff5cc6e73657b7
signature Commit is signed but in an unrecognized format.

improve the "external executor" detection logic


4 files changed, 22 insertions(+), 25 deletions(-)

lib/std/build.zig+1-2
...@@ -1141,7 +1141,7 @@ pub const LibExeObjStep = struct {...@@ -1141,7 +1141,7 @@ pub const LibExeObjStep = struct {
1141 out_pdb_filename: []const u8,1141 out_pdb_filename: []const u8,
1142 packages: ArrayList(Pkg),1142 packages: ArrayList(Pkg),
1143 build_options_contents: std.Buffer,1143 build_options_contents: std.Buffer,
1144 system_linker_hack: bool,1144 system_linker_hack: bool = false,
11451145
1146 object_src: []const u8,1146 object_src: []const u8,
11471147
...@@ -1273,7 +1273,6 @@ pub const LibExeObjStep = struct {...@@ -1273,7 +1273,6 @@ pub const LibExeObjStep = struct {
1273 .object_src = undefined,1273 .object_src = undefined,
1274 .build_options_contents = std.Buffer.initSize(builder.allocator, 0) catch unreachable,1274 .build_options_contents = std.Buffer.initSize(builder.allocator, 0) catch unreachable,
1275 .c_std = Builder.CStd.C99,1275 .c_std = Builder.CStd.C99,
1276 .system_linker_hack = false,
1277 .override_lib_dir = null,1276 .override_lib_dir = null,
1278 .main_pkg_path = null,1277 .main_pkg_path = null,
1279 .exec_cmd_args = null,1278 .exec_cmd_args = null,
lib/std/zig/cross_target.zig+17-19
...@@ -383,7 +383,7 @@ pub const CrossTarget = struct {...@@ -383,7 +383,7 @@ pub const CrossTarget = struct {
383 pub fn getAbi(self: CrossTarget) Target.Abi {383 pub fn getAbi(self: CrossTarget) Target.Abi {
384 if (self.abi) |abi| return abi;384 if (self.abi) |abi| return abi;
385385
386 if (self.isNativeOs()) {386 if (self.os_tag == null) {
387 // This works when doing `zig build` because Zig generates a build executable using387 // This works when doing `zig build` because Zig generates a build executable using
388 // native CPU model & features. However this will not be accurate otherwise, and388 // native CPU model & features. However this will not be accurate otherwise, and
389 // will need to be integrated with `std.zig.system.NativeTargetInfo.detect`.389 // will need to be integrated with `std.zig.system.NativeTargetInfo.detect`.
...@@ -441,21 +441,11 @@ pub const CrossTarget = struct {...@@ -441,21 +441,11 @@ pub const CrossTarget = struct {
441 return Target.libPrefix_cpu_arch_abi(self.getCpuArch(), self.getAbi());441 return Target.libPrefix_cpu_arch_abi(self.getCpuArch(), self.getAbi());
442 }442 }
443443
444 pub fn isNativeCpu(self: CrossTarget) bool {
445 return self.cpu_arch == null and self.cpu_model == null and
446 self.cpu_features_sub.isEmpty() and self.cpu_features_add.isEmpty();
447 }
448
449 pub fn isNativeOs(self: CrossTarget) bool {
450 return self.os_tag == null and self.os_version_min == null and self.os_version_max == null;
451 }
452
453 pub fn isNativeAbi(self: CrossTarget) bool {
454 return self.abi == null and self.glibc_version == null;
455 }
456
457 pub fn isNative(self: CrossTarget) bool {444 pub fn isNative(self: CrossTarget) bool {
458 return self.isNativeCpu() and self.isNativeOs() and self.isNativeAbi();445 return self.cpu_arch == null and self.cpu_model == null and
446 self.cpu_features_sub.isEmpty() and self.cpu_features_add.isEmpty() and
447 self.os_tag == null and self.os_version_min == null and self.os_version_max == null and
448 self.abi == null;
459 }449 }
460450
461 pub fn zigTriple(self: CrossTarget, allocator: *mem.Allocator) error{OutOfMemory}![:0]u8 {451 pub fn zigTriple(self: CrossTarget, allocator: *mem.Allocator) error{OutOfMemory}![:0]u8 {
...@@ -463,7 +453,7 @@ pub const CrossTarget = struct {...@@ -463,7 +453,7 @@ pub const CrossTarget = struct {
463 return mem.dupeZ(allocator, u8, "native");453 return mem.dupeZ(allocator, u8, "native");
464 }454 }
465455
466 const arch_name = if (self.isNativeCpu()) "native" else @tagName(self.getCpuArch());456 const arch_name = if (self.cpu_arch) |arch| @tagName(arch) else "native";
467 const os_name = if (self.os_tag) |os_tag| @tagName(os_tag) else "native";457 const os_name = if (self.os_tag) |os_tag| @tagName(os_tag) else "native";
468458
469 var result = try std.Buffer.allocPrint(allocator, "{}-{}", .{ arch_name, os_name });459 var result = try std.Buffer.allocPrint(allocator, "{}-{}", .{ arch_name, os_name });
...@@ -557,12 +547,20 @@ pub const CrossTarget = struct {...@@ -557,12 +547,20 @@ pub const CrossTarget = struct {
557 unavailable,547 unavailable,
558 };548 };
559549
550 /// Note that even a `CrossTarget` which returns `false` for `isNative` could still be natively executed.
551 /// For example `-target arm-native` running on an aarch64 host.
560 pub fn getExternalExecutor(self: CrossTarget) Executor {552 pub fn getExternalExecutor(self: CrossTarget) Executor {
561 const os_tag = self.getOsTag();
562 const cpu_arch = self.getCpuArch();553 const cpu_arch = self.getCpuArch();
554 const os_tag = self.getOsTag();
555 const os_match = os_tag == Target.current.os.tag;
556
557 // If the OS matches, and the CPU arch matches, the binary is considered native.
558 if (self.os_tag == null and cpu_arch == Target.current.cpu.arch) {
559 return .native;
560 }
563561
564 // If the target OS matches the host OS, we can use QEMU to emulate a foreign architecture.562 // If the OS matches, we can use QEMU to emulate a foreign architecture.
565 if (os_tag == Target.current.os.tag) {563 if (os_match) {
566 return switch (cpu_arch) {564 return switch (cpu_arch) {
567 .aarch64 => Executor{ .qemu = "qemu-aarch64" },565 .aarch64 => Executor{ .qemu = "qemu-aarch64" },
568 .aarch64_be => Executor{ .qemu = "qemu-aarch64_be" },566 .aarch64_be => Executor{ .qemu = "qemu-aarch64_be" },
src-self-hosted/stage2.zig+3-3
...@@ -1137,9 +1137,9 @@ fn enumInt(comptime Enum: type, int: c_int) Enum {...@@ -1137,9 +1137,9 @@ fn enumInt(comptime Enum: type, int: c_int) Enum {
1137/// TODO self-host this function1137/// TODO self-host this function
1138fn crossTargetToTarget(cross_target: CrossTarget, dynamic_linker_ptr: *?[*:0]u8) !Target {1138fn crossTargetToTarget(cross_target: CrossTarget, dynamic_linker_ptr: *?[*:0]u8) !Target {
1139 var adjusted_target = cross_target.toTarget();1139 var adjusted_target = cross_target.toTarget();
1140 if (cross_target.isNativeCpu() or cross_target.isNativeOs()) {1140 if (cross_target.cpu_arch == null or cross_target.os_tag == null) {
1141 const detected_info = try std.zig.system.NativeTargetInfo.detect(std.heap.c_allocator);1141 const detected_info = try std.zig.system.NativeTargetInfo.detect(std.heap.c_allocator);
1142 if (cross_target.isNativeCpu()) {1142 if (cross_target.cpu_arch == null) {
1143 adjusted_target.cpu = detected_info.target.cpu;1143 adjusted_target.cpu = detected_info.target.cpu;
11441144
1145 // TODO We want to just use detected_info.target but implementing1145 // TODO We want to just use detected_info.target but implementing
...@@ -1151,7 +1151,7 @@ fn crossTargetToTarget(cross_target: CrossTarget, dynamic_linker_ptr: *?[*:0]u8)...@@ -1151,7 +1151,7 @@ fn crossTargetToTarget(cross_target: CrossTarget, dynamic_linker_ptr: *?[*:0]u8)
1151 const arch = std.Target.current.cpu.arch;1151 const arch = std.Target.current.cpu.arch;
1152 adjusted_target.cpu = try detectNativeCpuWithLLVM(arch, llvm_cpu_name, llvm_cpu_features);1152 adjusted_target.cpu = try detectNativeCpuWithLLVM(arch, llvm_cpu_name, llvm_cpu_features);
1153 }1153 }
1154 if (cross_target.isNativeOs()) {1154 if (cross_target.os_tag == null) {
1155 adjusted_target.os = detected_info.target.os;1155 adjusted_target.os = detected_info.target.os;
11561156
1157 if (detected_info.dynamic_linker) |dl| {1157 if (detected_info.dynamic_linker) |dl| {
test/stack_traces.zig+1-1
...@@ -42,7 +42,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void {...@@ -42,7 +42,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
42 \\}42 \\}
43 ;43 ;
4444
45 switch (builtin.os.tag) {45 switch (std.Target.current.os.tag) {
46 .freebsd => {46 .freebsd => {
47 cases.addCase(47 cases.addCase(
48 "return",48 "return",