authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-11-10 07:40:52-08:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-12-02 00:22:09+01:00
log3e2f8233a86bf4a2787eb6d37a54b9c8eae8a985
treec937506b8b1ada5367bb2856674ac3728af7178e
parent77c5208c77f8e91786192dfc4e5945d40d67312a

Make Rosetta a new variant in ExternalExecutor enum

When running, check if Rosetta is available, otherwise, pass the tests.

4 files changed, 37 insertions(+), 7 deletions(-)

lib/std/build.zig+1-1
...@@ -2529,7 +2529,7 @@ pub const LibExeObjStep = struct {...@@ -2529,7 +2529,7 @@ pub const LibExeObjStep = struct {
2529 }2529 }
2530 }2530 }
2531 } else switch (self.target.getExternalExecutor()) {2531 } else switch (self.target.getExternalExecutor()) {
2532 .native, .unavailable => {},2532 .native, .rosetta, .unavailable => {},
2533 .qemu => |bin_name| if (self.enable_qemu) qemu: {2533 .qemu => |bin_name| if (self.enable_qemu) qemu: {
2534 const need_cross_glibc = self.target.isGnuLibC() and self.is_linking_libc;2534 const need_cross_glibc = self.target.isGnuLibC() and self.is_linking_libc;
2535 const glibc_dir_arg = if (need_cross_glibc)2535 const glibc_dir_arg = if (need_cross_glibc)
lib/std/zig/CrossTarget.zig+6-3
...@@ -643,10 +643,13 @@ pub fn getExternalExecutor(self: CrossTarget) Executor {...@@ -643,10 +643,13 @@ pub fn getExternalExecutor(self: CrossTarget) Executor {
643 return .native;643 return .native;
644 }644 }
645 }645 }
646 // If the OS match and OS is macOS and CPU is arm64, treat always as native646 // If the OS match and OS is macOS and CPU is arm64, we can use Rosetta 2
647 // since we'll be running the foreign architecture tests using Rosetta2.647 // to emulate the foreign architecture.
648 if (os_match and os_tag == .macos and builtin.cpu.arch == .aarch64) {648 if (os_match and os_tag == .macos and builtin.cpu.arch == .aarch64) {
649 return .native;649 return switch (cpu_arch) {
650 .x86_64 => .rosetta,
651 else => .unavailable,
652 };
650 }653 }
651654
652 // If the OS matches, we can use QEMU to emulate a foreign architecture.655 // If the OS matches, we can use QEMU to emulate a foreign architecture.
lib/std/zig/cross_target.zig+7-3
...@@ -596,6 +596,7 @@ pub const CrossTarget = struct {...@@ -596,6 +596,7 @@ pub const CrossTarget = struct {
596596
597 pub const Executor = union(enum) {597 pub const Executor = union(enum) {
598 native,598 native,
599 rosetta,
599 qemu: []const u8,600 qemu: []const u8,
600 wine: []const u8,601 wine: []const u8,
601 wasmtime: []const u8,602 wasmtime: []const u8,
...@@ -626,10 +627,13 @@ pub const CrossTarget = struct {...@@ -626,10 +627,13 @@ pub const CrossTarget = struct {
626 return .native;627 return .native;
627 }628 }
628 }629 }
629 // If the OS match and OS is macOS and CPU is arm64, treat always as native630 // If the OS match and OS is macOS and CPU is arm64, we can use Rosetta 2
630 // since we'll be running the foreign architecture tests using Rosetta2.631 // to emulate the foreign architecture.
631 if (os_match and os_tag == .macos and builtin.cpu.arch == .aarch64) {632 if (os_match and os_tag == .macos and builtin.cpu.arch == .aarch64) {
632 return .native;633 return switch (cpu_arch) {
634 .x86_64 => .rosetta,
635 else => .unavailable,
636 };
633 }637 }
634638
635 // If the OS matches, we can use QEMU to emulate a foreign architecture.639 // If the OS matches, we can use QEMU to emulate a foreign architecture.
src/test.zig+23
...@@ -1132,6 +1132,29 @@ pub const TestContext = struct {...@@ -1132,6 +1132,29 @@ pub const TestContext = struct {
1132 .native => try argv.append(exe_path),1132 .native => try argv.append(exe_path),
1133 .unavailable => return, // Pass test.1133 .unavailable => return, // Pass test.
11341134
1135 .rosetta => if (builtin.os.tag == .macos) {
1136 // Check based on official Apple docs.
1137 // If sysctlbyname returns errno.ENOENT, then we are running a native process.
1138 // Otherwise, if an error occurs then we are not native and there is no Rosetta available.
1139 // Finally if OK, we are running a translated process via Rosetta.
1140 // https://developer.apple.com/documentation/apple-silicon/about-the-rosetta-translation-environment
1141 var ret: c_int = 0;
1142 var size: usize = @sizeOf(c_int);
1143 std.os.sysctlbynameZ(
1144 "sysctl.proc_translated",
1145 &ret,
1146 &size,
1147 null,
1148 0,
1149 ) catch |err| switch (err) {
1150 error.UnknownName => unreachable, // Native process, we should never trigger it as .rosetta.
1151 else => return, // No Rosetta available, pass test.
1152 };
1153 try argv.append(exe_path);
1154 } else {
1155 return; // Rosetta not available, pass test.
1156 },
1157
1135 .qemu => |qemu_bin_name| if (enable_qemu) {1158 .qemu => |qemu_bin_name| if (enable_qemu) {
1136 // TODO Ability for test cases to specify whether to link libc.1159 // TODO Ability for test cases to specify whether to link libc.
1137 const need_cross_glibc = false; // target.isGnuLibC() and self.is_linking_libc;1160 const need_cross_glibc = false; // target.isGnuLibC() and self.is_linking_libc;