Commit ef9582a1ec

Andrew Kelley <andrew@ziglang.org>
2020-09-29 07:19:00
`zig test` and `zig run` do not try to run foreign binaries
1 parent 29fd130
Changed files (3)
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 "" {
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.", .{});
                         };
BRANCH_TODO
@@ -1,3 +1,4 @@
+ * docs are failing to build
  * MachO LLD linking
  * WASM LLD linking
  * audit the CLI options for stage2