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 {
25292529 }
25302530 }
25312531 } else switch (self.target.getExternalExecutor()) {
2532 .native, .unavailable => {},
2532 .native, .rosetta, .unavailable => {},
25332533 .qemu => |bin_name| if (self.enable_qemu) qemu: {
25342534 const need_cross_glibc = self.target.isGnuLibC() and self.is_linking_libc;
25352535 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 {
643643 return .native;
644644 }
645645 }
646 // If the OS match and OS is macOS and CPU is arm64, treat always as native
647 // since we'll be running the foreign architecture tests using Rosetta2.
646 // If the OS match and OS is macOS and CPU is arm64, we can use Rosetta 2
647 // to emulate the foreign architecture.
648648 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 };
650653 }
651654
652655 // 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 {
596596
597597 pub const Executor = union(enum) {
598598 native,
599 rosetta,
599600 qemu: []const u8,
600601 wine: []const u8,
601602 wasmtime: []const u8,
......@@ -626,10 +627,13 @@ pub const CrossTarget = struct {
626627 return .native;
627628 }
628629 }
629 // If the OS match and OS is macOS and CPU is arm64, treat always as native
630 // since we'll be running the foreign architecture tests using Rosetta2.
630 // If the OS match and OS is macOS and CPU is arm64, we can use Rosetta 2
631 // to emulate the foreign architecture.
631632 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 };
633637 }
634638
635639 // 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 {
11321132 .native => try argv.append(exe_path),
11331133 .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
11351158 .qemu => |qemu_bin_name| if (enable_qemu) {
11361159 // TODO Ability for test cases to specify whether to link libc.
11371160 const need_cross_glibc = false; // target.isGnuLibC() and self.is_linking_libc;