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 * docs are failing to build
12 * MachO LLD linking
23 * WASM LLD linking
34 * audit the CLI options for stage2
lib/std/target.zig+21
......@@ -1483,6 +1483,27 @@ pub const Target = struct {
14831483 => return result,
14841484 }
14851485 }
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 }
14861507};
14871508
14881509test "" {
src/main.zig+13-2
......@@ -1618,6 +1618,17 @@ fn buildOutputType(
16181618 defer argv.deinit();
16191619
16201620 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 }
16211632 try argv.append(exe_path);
16221633 } else {
16231634 for (test_exec_args.items) |arg| {
......@@ -2128,8 +2139,8 @@ pub fn cmdBuild(gpa: *Allocator, arena: *Allocator, args: []const []const u8) !v
21282139 error.FileNotFound => {
21292140 dirname = fs.path.dirname(dirname) orelse {
21302141 std.log.info("{}", .{
2131 \\Initialize a 'build.zig' template file with `zig init-lib` or `zig init-exe`,
2132 \\or see `zig --help` for more options.
2142 \\Initialize a 'build.zig' template file with `zig init-lib` or `zig init-exe`,
2143 \\or see `zig --help` for more options.
21332144 });
21342145 fatal("No 'build.zig' file found, in the current directory or any parent directories.", .{});
21352146 };