authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-12-01 23:33:28+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-12-02 00:22:09+01:00
log852841fd1f82bb5ba5ecdf9a85a1f15ef70e6dd0
tree68268845bdc0cf075b0755e4213697e6309a9a25
parent3e2f8233a86bf4a2787eb6d37a54b9c8eae8a985

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.

4 files changed, 17 insertions(+), 19 deletions(-)

build.zig+6
......@@ -300,6 +300,7 @@ pub fn build(b: *Builder) !void {
300300 const is_qemu_enabled = b.option(bool, "enable-qemu", "Use QEMU to run cross compiled foreign architecture tests") orelse false;
301301 const is_wasmtime_enabled = b.option(bool, "enable-wasmtime", "Use Wasmtime to enable and run WASI libstd tests") orelse false;
302302 const is_darling_enabled = b.option(bool, "enable-darling", "[Experimental] Use Darling to run cross compiled macOS tests") orelse false;
303 const is_rosetta_enabled = b.option(bool, "enable-rosetta", "(Darwin) Use Rosetta to run x86_64 macOS tests on arm64 macOS") orelse false;
303304 const glibc_multi_dir = b.option([]const u8, "enable-foreign-glibc", "Provide directory with glibc installations to run cross compiled tests that link glibc");
304305
305306 const test_stage2_options = b.addOptions();
......@@ -319,6 +320,7 @@ pub fn build(b: *Builder) !void {
319320 test_stage2_options.addOption(bool, "enable_qemu", is_qemu_enabled);
320321 test_stage2_options.addOption(bool, "enable_wine", is_wine_enabled);
321322 test_stage2_options.addOption(bool, "enable_wasmtime", is_wasmtime_enabled);
323 test_stage2_options.addOption(bool, "enable_rosetta", is_rosetta_enabled);
322324 test_stage2_options.addOption(u32, "mem_leak_frames", mem_leak_frames * 2);
323325 test_stage2_options.addOption(bool, "enable_darling", is_darling_enabled);
324326 test_stage2_options.addOption(?[]const u8, "glibc_multi_install_dir", glibc_multi_dir);
......@@ -370,6 +372,7 @@ pub fn build(b: *Builder) !void {
370372 is_qemu_enabled,
371373 is_wasmtime_enabled,
372374 is_darling_enabled,
375 is_rosetta_enabled,
373376 glibc_multi_dir,
374377 ));
375378
......@@ -387,6 +390,7 @@ pub fn build(b: *Builder) !void {
387390 is_qemu_enabled,
388391 is_wasmtime_enabled,
389392 is_darling_enabled,
393 is_rosetta_enabled,
390394 glibc_multi_dir,
391395 ));
392396
......@@ -404,6 +408,7 @@ pub fn build(b: *Builder) !void {
404408 is_qemu_enabled,
405409 is_wasmtime_enabled,
406410 is_darling_enabled,
411 is_rosetta_enabled,
407412 glibc_multi_dir,
408413 ));
409414
......@@ -434,6 +439,7 @@ pub fn build(b: *Builder) !void {
434439 is_qemu_enabled,
435440 is_wasmtime_enabled,
436441 is_darling_enabled,
442 is_rosetta_enabled,
437443 glibc_multi_dir,
438444 );
439445
lib/std/build.zig+7-1
......@@ -1516,6 +1516,9 @@ pub const LibExeObjStep = struct {
15161516 /// Experimental. Uses system Darling installation to run cross compiled macOS build artifacts.
15171517 enable_darling: bool = false,
15181518
1519 /// Darwin. Uses Rosetta to run x86_64 macOS build artifacts on arm64 macOS.
1520 enable_rosetta: bool = false,
1521
15191522 /// After following the steps in https://github.com/ziglang/zig/wiki/Updating-libc#glibc,
15201523 /// this will be the directory $glibc-build-dir/install/glibcs
15211524 /// Given the example of the aarch64 target, this is the directory
......@@ -2529,7 +2532,10 @@ pub const LibExeObjStep = struct {
25292532 }
25302533 }
25312534 } else switch (self.target.getExternalExecutor()) {
2532 .native, .rosetta, .unavailable => {},
2535 .native, .unavailable => {},
2536 .rosetta => if (self.enable_rosetta) {
2537 try zig_args.append("--test-cmd-bin");
2538 },
25332539 .qemu => |bin_name| if (self.enable_qemu) qemu: {
25342540 const need_cross_glibc = self.target.isGnuLibC() and self.is_linking_libc;
25352541 const glibc_dir_arg = if (need_cross_glibc)
src/test.zig+2-18
......@@ -10,6 +10,7 @@ const enable_qemu: bool = build_options.enable_qemu;
1010const enable_wine: bool = build_options.enable_wine;
1111const enable_wasmtime: bool = build_options.enable_wasmtime;
1212const enable_darling: bool = build_options.enable_darling;
13const enable_rosetta: bool = build_options.enable_rosetta;
1314const glibc_multi_install_dir: ?[]const u8 = build_options.glibc_multi_install_dir;
1415const skip_compile_errors = build_options.skip_compile_errors;
1516const ThreadPool = @import("ThreadPool.zig");
......@@ -1132,24 +1133,7 @@ pub const TestContext = struct {
11321133 .native => try argv.append(exe_path),
11331134 .unavailable => return, // Pass test.
11341135
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 };
1136 .rosetta => if (enable_rosetta) {
11531137 try argv.append(exe_path);
11541138 } else {
11551139 return; // Rosetta not available, pass test.
test/tests.zig+2
......@@ -511,6 +511,7 @@ pub fn addPkgTests(
511511 is_qemu_enabled: bool,
512512 is_wasmtime_enabled: bool,
513513 is_darling_enabled: bool,
514 is_rosetta_enabled: bool,
514515 glibc_dir: ?[]const u8,
515516) *build.Step {
516517 const step = b.step(b.fmt("test-{s}", .{name}), desc);
......@@ -572,6 +573,7 @@ pub fn addPkgTests(
572573 these_tests.enable_qemu = is_qemu_enabled;
573574 these_tests.enable_wasmtime = is_wasmtime_enabled;
574575 these_tests.enable_darling = is_darling_enabled;
576 these_tests.enable_rosetta = is_rosetta_enabled;
575577 these_tests.glibc_multi_install_dir = glibc_dir;
576578 these_tests.addIncludeDir("test");
577579