From ef9582a1ec452aab816b67cd2d0d35ef7356ddae Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Mon, 28 Sep 2020 22:19:00 -0700 Subject: [PATCH] `zig test` and `zig run` do not try to run foreign binaries --- BRANCH_TODO | 1 + lib/std/target.zig | 21 +++++++++++++++++++++ src/main.zig | 15 +++++++++++++-- 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/BRANCH_TODO b/BRANCH_TODO index a579d4a5eb..9665ba3d0e 100644 --- a/BRANCH_TODO +++ b/BRANCH_TODO @@ -1,3 +1,4 @@ + * docs are failing to build * MachO LLD linking * WASM LLD linking * audit the CLI options for stage2 diff --git a/lib/std/target.zig b/lib/std/target.zig index 1b8e7c1519..65e9f75457 100644 --- a/lib/std/target.zig +++ b/lib/std/target.zig @@ -1483,6 +1483,27 @@ pub const Target = struct { => return result, } } + + /// Return whether or not the given host target is capable of executing natively executables + /// of the other target. + pub fn canExecBinariesOf(host_target: std.Target, binary_target: std.Target) bool { + if (host_target.os.tag != binary_target.os.tag) + return false; + + if (host_target.cpu.arch == binary_target.cpu.arch) + return true; + + if (host_target.cpu.arch == .x86_64 and binary_target.cpu.arch == .i386) + return true; + + if (host_target.cpu.arch == .aarch64 and binary_target.cpu.arch == .arm) + return true; + + if (host_target.cpu.arch == .aarch64_be and binary_target.cpu.arch == .armeb) + return true; + + return false; + } }; test "" { diff --git a/src/main.zig b/src/main.zig index d96cfaf526..61ef77cce7 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1618,6 +1618,17 @@ fn buildOutputType( defer argv.deinit(); if (test_exec_args.items.len == 0) { + if (!std.Target.current.canExecBinariesOf(target_info.target)) { + switch (arg_mode) { + .zig_test => { + warn("created {s} but skipping execution because it is non-native", .{exe_path}); + if (!watch) return cleanExit(); + break :run; + }, + .run => fatal("unable to execute {s}: non-native", .{exe_path}), + else => unreachable, + } + } try argv.append(exe_path); } else { for (test_exec_args.items) |arg| { @@ -2128,8 +2139,8 @@ pub fn cmdBuild(gpa: *Allocator, arena: *Allocator, args: []const []const u8) !v error.FileNotFound => { dirname = fs.path.dirname(dirname) orelse { std.log.info("{}", .{ - \\Initialize a 'build.zig' template file with `zig init-lib` or `zig init-exe`, - \\or see `zig --help` for more options. + \\Initialize a 'build.zig' template file with `zig init-lib` or `zig init-exe`, + \\or see `zig --help` for more options. }); fatal("No 'build.zig' file found, in the current directory or any parent directories.", .{}); };