Commit 852841fd1f

Jakub Konka <kubkon@jakubkonka.com>
2021-12-01 23:33:28
Make Rosetta availability declarative by the user
Like QEMU or Wine, you need to declare you want Rosetta enabled for running tests/build artifacts via `-Denable-rosetta` flag.
1 parent 3e2f823
Changed files (4)
lib/std/build.zig
@@ -1516,6 +1516,9 @@ pub const LibExeObjStep = struct {
     /// Experimental. Uses system Darling installation to run cross compiled macOS build artifacts.
     enable_darling: bool = false,
 
+    /// Darwin. Uses Rosetta to run x86_64 macOS build artifacts on arm64 macOS.
+    enable_rosetta: bool = false,
+
     /// After following the steps in https://github.com/ziglang/zig/wiki/Updating-libc#glibc,
     /// this will be the directory $glibc-build-dir/install/glibcs
     /// Given the example of the aarch64 target, this is the directory
@@ -2529,7 +2532,10 @@ pub const LibExeObjStep = struct {
                 }
             }
         } else switch (self.target.getExternalExecutor()) {
-            .native, .rosetta, .unavailable => {},
+            .native, .unavailable => {},
+            .rosetta => if (self.enable_rosetta) {
+                try zig_args.append("--test-cmd-bin");
+            },
             .qemu => |bin_name| if (self.enable_qemu) qemu: {
                 const need_cross_glibc = self.target.isGnuLibC() and self.is_linking_libc;
                 const glibc_dir_arg = if (need_cross_glibc)
src/test.zig
@@ -10,6 +10,7 @@ const enable_qemu: bool = build_options.enable_qemu;
 const enable_wine: bool = build_options.enable_wine;
 const enable_wasmtime: bool = build_options.enable_wasmtime;
 const enable_darling: bool = build_options.enable_darling;
+const enable_rosetta: bool = build_options.enable_rosetta;
 const glibc_multi_install_dir: ?[]const u8 = build_options.glibc_multi_install_dir;
 const skip_compile_errors = build_options.skip_compile_errors;
 const ThreadPool = @import("ThreadPool.zig");
@@ -1132,24 +1133,7 @@ pub const TestContext = struct {
                             .native => try argv.append(exe_path),
                             .unavailable => return, // Pass test.
 
-                            .rosetta => if (builtin.os.tag == .macos) {
-                                // Check based on official Apple docs.
-                                // If sysctlbyname returns errno.ENOENT, then we are running a native process.
-                                // Otherwise, if an error occurs then we are not native and there is no Rosetta available.
-                                // Finally if OK, we are running a translated process via Rosetta.
-                                // https://developer.apple.com/documentation/apple-silicon/about-the-rosetta-translation-environment
-                                var ret: c_int = 0;
-                                var size: usize = @sizeOf(c_int);
-                                std.os.sysctlbynameZ(
-                                    "sysctl.proc_translated",
-                                    &ret,
-                                    &size,
-                                    null,
-                                    0,
-                                ) catch |err| switch (err) {
-                                    error.UnknownName => unreachable, // Native process, we should never trigger it as .rosetta.
-                                    else => return, // No Rosetta available, pass test.
-                                };
+                            .rosetta => if (enable_rosetta) {
                                 try argv.append(exe_path);
                             } else {
                                 return; // Rosetta not available, pass test.
test/tests.zig
@@ -511,6 +511,7 @@ pub fn addPkgTests(
     is_qemu_enabled: bool,
     is_wasmtime_enabled: bool,
     is_darling_enabled: bool,
+    is_rosetta_enabled: bool,
     glibc_dir: ?[]const u8,
 ) *build.Step {
     const step = b.step(b.fmt("test-{s}", .{name}), desc);
@@ -572,6 +573,7 @@ pub fn addPkgTests(
         these_tests.enable_qemu = is_qemu_enabled;
         these_tests.enable_wasmtime = is_wasmtime_enabled;
         these_tests.enable_darling = is_darling_enabled;
+        these_tests.enable_rosetta = is_rosetta_enabled;
         these_tests.glibc_multi_install_dir = glibc_dir;
         these_tests.addIncludeDir("test");
 
build.zig
@@ -300,6 +300,7 @@ pub fn build(b: *Builder) !void {
     const is_qemu_enabled = b.option(bool, "enable-qemu", "Use QEMU to run cross compiled foreign architecture tests") orelse false;
     const is_wasmtime_enabled = b.option(bool, "enable-wasmtime", "Use Wasmtime to enable and run WASI libstd tests") orelse false;
     const is_darling_enabled = b.option(bool, "enable-darling", "[Experimental] Use Darling to run cross compiled macOS tests") orelse false;
+    const is_rosetta_enabled = b.option(bool, "enable-rosetta", "(Darwin) Use Rosetta to run x86_64 macOS tests on arm64 macOS") orelse false;
     const glibc_multi_dir = b.option([]const u8, "enable-foreign-glibc", "Provide directory with glibc installations to run cross compiled tests that link glibc");
 
     const test_stage2_options = b.addOptions();
@@ -319,6 +320,7 @@ pub fn build(b: *Builder) !void {
     test_stage2_options.addOption(bool, "enable_qemu", is_qemu_enabled);
     test_stage2_options.addOption(bool, "enable_wine", is_wine_enabled);
     test_stage2_options.addOption(bool, "enable_wasmtime", is_wasmtime_enabled);
+    test_stage2_options.addOption(bool, "enable_rosetta", is_rosetta_enabled);
     test_stage2_options.addOption(u32, "mem_leak_frames", mem_leak_frames * 2);
     test_stage2_options.addOption(bool, "enable_darling", is_darling_enabled);
     test_stage2_options.addOption(?[]const u8, "glibc_multi_install_dir", glibc_multi_dir);
@@ -370,6 +372,7 @@ pub fn build(b: *Builder) !void {
         is_qemu_enabled,
         is_wasmtime_enabled,
         is_darling_enabled,
+        is_rosetta_enabled,
         glibc_multi_dir,
     ));
 
@@ -387,6 +390,7 @@ pub fn build(b: *Builder) !void {
         is_qemu_enabled,
         is_wasmtime_enabled,
         is_darling_enabled,
+        is_rosetta_enabled,
         glibc_multi_dir,
     ));
 
@@ -404,6 +408,7 @@ pub fn build(b: *Builder) !void {
         is_qemu_enabled,
         is_wasmtime_enabled,
         is_darling_enabled,
+        is_rosetta_enabled,
         glibc_multi_dir,
     ));
 
@@ -434,6 +439,7 @@ pub fn build(b: *Builder) !void {
         is_qemu_enabled,
         is_wasmtime_enabled,
         is_darling_enabled,
+        is_rosetta_enabled,
         glibc_multi_dir,
     );