authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-12-01 17:32:27-08:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2021-12-01 17:32:27-08:00
log14b532ec8596d7f84b76127f1b858271f010126f
tree1e05b622760b5288bba853c575daa2d048a10403
parent1912ec0323af9a9077a8706157beb8207f6e3eb9
parent42db5156656ebd1649f7ec3707697b08657b9cab
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #10122 from ziglang/x86_64-as-native-rosetta

Treat x86_64 tests as native under the Rosetta 2 on M1 Macs

6 files changed, 43 insertions(+), 2 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+6
......@@ -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
......@@ -2530,6 +2533,9 @@ pub const LibExeObjStep = struct {
25302533 }
25312534 } else switch (self.target.getExternalExecutor()) {
25322535 .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)
lib/std/zig/CrossTarget.zig+9
......@@ -612,6 +612,7 @@ pub fn vcpkgTriplet(self: CrossTarget, allocator: mem.Allocator, linkage: VcpkgL
612612
613613pub const Executor = union(enum) {
614614 native,
615 rosetta,
615616 qemu: []const u8,
616617 wine: []const u8,
617618 wasmtime: []const u8,
......@@ -642,6 +643,14 @@ pub fn getExternalExecutor(self: CrossTarget) Executor {
642643 return .native;
643644 }
644645 }
646 // If the OS match and OS is macOS and CPU is arm64, we can use Rosetta 2
647 // to emulate the foreign architecture.
648 if (os_match and os_tag == .macos and builtin.cpu.arch == .aarch64) {
649 return switch (cpu_arch) {
650 .x86_64 => .rosetta,
651 else => .unavailable,
652 };
653 }
645654
646655 // If the OS matches, we can use QEMU to emulate a foreign architecture.
647656 if (os_match) {
src/test.zig+7
......@@ -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,6 +1133,12 @@ pub const TestContext = struct {
11321133 .native => try argv.append(exe_path),
11331134 .unavailable => return, // Pass test.
11341135
1136 .rosetta => if (enable_rosetta) {
1137 try argv.append(exe_path);
1138 } else {
1139 return; // Rosetta not available, pass test.
1140 },
1141
11351142 .qemu => |qemu_bin_name| if (enable_qemu) {
11361143 // TODO Ability for test cases to specify whether to link libc.
11371144 const need_cross_glibc = false; // target.isGnuLibC() and self.is_linking_libc;
test/behavior/muladd.zig+5
......@@ -1,3 +1,4 @@
1const builtin = @import("builtin");
12const expect = @import("std").testing.expect;
23
34test "@mulAdd" {
......@@ -24,6 +25,10 @@ fn testMulAdd() !void {
2425 var c: f64 = 6.25;
2526 try expect(@mulAdd(f64, a, b, c) == 20);
2627 }
28 if (builtin.os.tag == .macos and builtin.cpu.arch == .aarch64) {
29 // https://github.com/ziglang/zig/issues/9900
30 return error.SkipZigTest;
31 }
2732 {
2833 var a: f16 = 5.5;
2934 var b: f128 = 2.5;
test/tests.zig+10-2
......@@ -270,8 +270,14 @@ const test_targets = blk: {
270270 .os_tag = .macos,
271271 .abi = .gnu,
272272 },
273 // https://github.com/ziglang/zig/issues/3295
274 .disable_native = true,
273 },
274
275 TestTarget{
276 .target = .{
277 .cpu_arch = .aarch64,
278 .os_tag = .macos,
279 .abi = .gnu,
280 },
275281 },
276282
277283 TestTarget{
......@@ -511,6 +517,7 @@ pub fn addPkgTests(
511517 is_qemu_enabled: bool,
512518 is_wasmtime_enabled: bool,
513519 is_darling_enabled: bool,
520 is_rosetta_enabled: bool,
514521 glibc_dir: ?[]const u8,
515522) *build.Step {
516523 const step = b.step(b.fmt("test-{s}", .{name}), desc);
......@@ -572,6 +579,7 @@ pub fn addPkgTests(
572579 these_tests.enable_qemu = is_qemu_enabled;
573580 these_tests.enable_wasmtime = is_wasmtime_enabled;
574581 these_tests.enable_darling = is_darling_enabled;
582 these_tests.enable_rosetta = is_rosetta_enabled;
575583 these_tests.glibc_multi_install_dir = glibc_dir;
576584 these_tests.addIncludeDir("test");
577585