authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-09-28 22:19:00-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-09-28 22:19:00-07:00
logef9582a1ec452aab816b67cd2d0d35ef7356ddae
treeaf7aefb6f7ec33d9921681b99309ab49cdd6cf39
parent29fd13009391060a2d6783fb0b91cb075c2e6cce

`zig test` and `zig run` do not try to run foreign binaries


3 files changed, 35 insertions(+), 2 deletions(-)

BRANCH_TODO+1
...@@ -1,3 +1,4 @@...@@ -1,3 +1,4 @@
1 * docs are failing to build
1 * MachO LLD linking2 * MachO LLD linking
2 * WASM LLD linking3 * WASM LLD linking
3 * audit the CLI options for stage24 * audit the CLI options for stage2
lib/std/target.zig+21
...@@ -1483,6 +1483,27 @@ pub const Target = struct {...@@ -1483,6 +1483,27 @@ pub const Target = struct {
1483 => return result,1483 => return result,
1484 }1484 }
1485 }1485 }
1486
1487 /// Return whether or not the given host target is capable of executing natively executables
1488 /// of the other target.
1489 pub fn canExecBinariesOf(host_target: std.Target, binary_target: std.Target) bool {
1490 if (host_target.os.tag != binary_target.os.tag)
1491 return false;
1492
1493 if (host_target.cpu.arch == binary_target.cpu.arch)
1494 return true;
1495
1496 if (host_target.cpu.arch == .x86_64 and binary_target.cpu.arch == .i386)
1497 return true;
1498
1499 if (host_target.cpu.arch == .aarch64 and binary_target.cpu.arch == .arm)
1500 return true;
1501
1502 if (host_target.cpu.arch == .aarch64_be and binary_target.cpu.arch == .armeb)
1503 return true;
1504
1505 return false;
1506 }
1486};1507};
14871508
1488test "" {1509test "" {
src/main.zig+13-2
...@@ -1618,6 +1618,17 @@ fn buildOutputType(...@@ -1618,6 +1618,17 @@ fn buildOutputType(
1618 defer argv.deinit();1618 defer argv.deinit();
16191619
1620 if (test_exec_args.items.len == 0) {1620 if (test_exec_args.items.len == 0) {
1621 if (!std.Target.current.canExecBinariesOf(target_info.target)) {
1622 switch (arg_mode) {
1623 .zig_test => {
1624 warn("created {s} but skipping execution because it is non-native", .{exe_path});
1625 if (!watch) return cleanExit();
1626 break :run;
1627 },
1628 .run => fatal("unable to execute {s}: non-native", .{exe_path}),
1629 else => unreachable,
1630 }
1631 }
1621 try argv.append(exe_path);1632 try argv.append(exe_path);
1622 } else {1633 } else {
1623 for (test_exec_args.items) |arg| {1634 for (test_exec_args.items) |arg| {
...@@ -2128,8 +2139,8 @@ pub fn cmdBuild(gpa: *Allocator, arena: *Allocator, args: []const []const u8) !v...@@ -2128,8 +2139,8 @@ pub fn cmdBuild(gpa: *Allocator, arena: *Allocator, args: []const []const u8) !v
2128 error.FileNotFound => {2139 error.FileNotFound => {
2129 dirname = fs.path.dirname(dirname) orelse {2140 dirname = fs.path.dirname(dirname) orelse {
2130 std.log.info("{}", .{2141 std.log.info("{}", .{
2131 \\Initialize a 'build.zig' template file with `zig init-lib` or `zig init-exe`,2142 \\Initialize a 'build.zig' template file with `zig init-lib` or `zig init-exe`,
2132 \\or see `zig --help` for more options.2143 \\or see `zig --help` for more options.
2133 });2144 });
2134 fatal("No 'build.zig' file found, in the current directory or any parent directories.", .{});2145 fatal("No 'build.zig' file found, in the current directory or any parent directories.", .{});
2135 };2146 };