Commit 0e9bdb44a6

Andrew Kelley <superjoe30@gmail.com>
2017-08-30 20:55:26
test suite cross-compile builds tests for other targets
1 parent 052b4ae
Changed files (5)
src/link.cpp
@@ -573,6 +573,9 @@ static void get_darwin_platform(LinkJob *lj, DarwinPlatform *platform) {
         platform->kind = MacOS;
     } else if (g->mios_version_min) {
         platform->kind = IPhoneOS;
+    } else if (g->zig_target.os == ZigLLVM_MacOSX) {
+        platform->kind = MacOS;
+        g->mmacosx_version_min = buf_create_from_str("10.10");
     } else {
         zig_panic("unable to infer -mmacosx-version-min or -mios-version-min");
     }
src/main.cpp
@@ -676,6 +676,16 @@ int main(int argc, char **argv) {
             } else if (cmd == CmdTest) {
                 codegen_build(g);
                 codegen_link(g, "./test");
+
+                ZigTarget native;
+                get_native_target(&native);
+                bool is_native_target = target == nullptr || (target->os == native.os &&
+                        target->arch.arch == native.arch.arch && target->arch.sub_arch == native.arch.sub_arch);
+                if (!is_native_target) {
+                    fprintf(stderr, "Skipping execution of non-native test binary.\n");
+                    return 0;
+                }
+
                 ZigList<const char *> args = {0};
                 Termination term;
                 os_spawn_process("./test", args, &term);
src/target.cpp
@@ -482,6 +482,7 @@ uint32_t target_c_type_size_in_bits(const ZigTarget *target, CIntType id) {
             }
         case ZigLLVM_Linux:
         case ZigLLVM_Darwin:
+        case ZigLLVM_MacOSX:
             switch (id) {
                 case CIntTypeShort:
                 case CIntTypeUShort:
@@ -521,7 +522,6 @@ uint32_t target_c_type_size_in_bits(const ZigTarget *target, CIntType id) {
         case ZigLLVM_IOS:
         case ZigLLVM_KFreeBSD:
         case ZigLLVM_Lv2:
-        case ZigLLVM_MacOSX:
         case ZigLLVM_NetBSD:
         case ZigLLVM_OpenBSD:
         case ZigLLVM_Solaris:
std/build.zig
@@ -1027,6 +1027,7 @@ pub const TestStep = struct {
     link_libs: BufSet,
     name_prefix: []const u8,
     filter: ?[]const u8,
+    target: Target,
 
     pub fn init(builder: &Builder, root_src: []const u8) -> TestStep {
         const step_name = builder.fmt("test {}", root_src);
@@ -1039,6 +1040,7 @@ pub const TestStep = struct {
             .name_prefix = "",
             .filter = null,
             .link_libs = BufSet.init(builder.allocator),
+            .target = Target.Native,
         }
     }
 
@@ -1062,6 +1064,18 @@ pub const TestStep = struct {
         self.filter = text;
     }
 
+    pub fn setTarget(self: &TestStep, target_arch: builtin.Arch, target_os: builtin.Os,
+        target_environ: builtin.Environ)
+    {
+        self.target = Target.Cross {
+            CrossTarget {
+                .arch = target_arch,
+                .os = target_os,
+                .environ = target_environ,
+            }
+        };
+    }
+
     fn make(step: &Step) -> %void {
         const self = @fieldParentPtr(TestStep, "step", step);
         const builder = self.builder;
@@ -1082,6 +1096,20 @@ pub const TestStep = struct {
             builtin.Mode.ReleaseFast => %%zig_args.append("--release-fast"),
         }
 
+        switch (self.target) {
+            Target.Native => {},
+            Target.Cross => |cross_target| {
+                %%zig_args.append("--target-arch");
+                %%zig_args.append(@enumTagName(cross_target.arch));
+
+                %%zig_args.append("--target-os");
+                %%zig_args.append(@enumTagName(cross_target.os));
+
+                %%zig_args.append("--target-environ");
+                %%zig_args.append(@enumTagName(cross_target.environ));
+            },
+        }
+
         if (self.filter) |filter| {
             %%zig_args.append("--test-filter");
             %%zig_args.append(filter);
test/tests.zig
@@ -9,7 +9,8 @@ const io = std.io;
 const mem = std.mem;
 const fmt = std.fmt;
 const ArrayList = std.ArrayList;
-const Mode = @import("builtin").Mode;
+const builtin = @import("builtin");
+const Mode = builtin.Mode;
 
 const compare_output = @import("compare_output.zig");
 const build_examples = @import("build_examples.zig");
@@ -18,6 +19,25 @@ const assemble_and_link = @import("assemble_and_link.zig");
 const debug_safety = @import("debug_safety.zig");
 const parseh = @import("parseh.zig");
 
+const TestTarget = struct {
+    os: builtin.Os,
+    arch: builtin.Arch,
+    environ: builtin.Environ,
+};
+
+const test_targets = []TestTarget {
+    TestTarget {
+        .os = builtin.Os.linux,
+        .arch = builtin.Arch.x86_64,
+        .environ = builtin.Environ.gnu,
+    },
+    TestTarget {
+        .os = builtin.Os.macosx,
+        .arch = builtin.Arch.x86_64,
+        .environ = builtin.Environ.unknown,
+    },
+};
+
 error TestFailed;
 
 pub fn addCompareOutputTests(b: &build.Builder, test_filter: ?[]const u8) -> &build.Step {
@@ -121,17 +141,27 @@ pub fn addPkgTestsRaw(b: &build.Builder, test_filter: ?[]const u8, root_src: []c
 {
     const libc_bools = if (always_link_libc) []bool{true} else []bool{false, true};
     const step = b.step(b.fmt("test-{}", name), desc);
-    for ([]Mode{Mode.Debug, Mode.ReleaseFast}) |mode| {
-        for (libc_bools) |link_libc| {
-            const these_tests = b.addTest(root_src);
-            these_tests.setNamePrefix(b.fmt("{}-{}-{} ", name, @enumTagName(mode),
-                if (link_libc) "c" else "bare"));
-            these_tests.setFilter(test_filter);
-            these_tests.setBuildMode(mode);
-            if (link_libc) {
-                these_tests.linkSystemLibrary("c");
+    for (test_targets) |test_target| {
+        const is_native = (test_target.os == builtin.os and test_target.arch == builtin.arch);
+        for ([]Mode{Mode.Debug, Mode.ReleaseFast}) |mode| {
+            for (libc_bools) |link_libc| {
+                if (link_libc and !is_native) {
+                    // don't assume we have a cross-compiling libc set up
+                    continue;
+                }
+                const these_tests = b.addTest(root_src);
+                these_tests.setNamePrefix(b.fmt("{}-{}-{}-{}-{} ", name, @enumTagName(test_target.os),
+                    @enumTagName(test_target.arch), @enumTagName(mode), if (link_libc) "c" else "bare"));
+                these_tests.setFilter(test_filter);
+                these_tests.setBuildMode(mode);
+                if (!is_native) {
+                    these_tests.setTarget(test_target.arch, test_target.os, test_target.environ);
+                }
+                if (link_libc) {
+                    these_tests.linkSystemLibrary("c");
+                }
+                step.dependOn(&these_tests.step);
             }
-            step.dependOn(&these_tests.step);
         }
     }
     return step;