From 38edef35bfcba5789ea50adc7c76dec504079812 Mon Sep 17 00:00:00 2001 From: Jakub Konka Date: Sat, 28 May 2022 11:44:53 +0200 Subject: test: introduce link(er) tests - builds on standalone tests --- test/link.zig | 52 ++++++++++++++++++++++ test/link/bss/build.zig | 14 ++++++ test/link/bss/main.zig | 13 ++++++ test/link/common_symbols/a.c | 6 +++ test/link/common_symbols/b.c | 7 +++ test/link/common_symbols/build.zig | 16 +++++++ test/link/common_symbols/c.c | 5 +++ test/link/common_symbols/main.zig | 16 +++++++ test/link/common_symbols_alignment/a.c | 2 + test/link/common_symbols_alignment/build.zig | 16 +++++++ test/link/common_symbols_alignment/main.zig | 9 ++++ test/link/dylib/a.c | 7 +++ test/link/dylib/build.zig | 29 ++++++++++++ test/link/dylib/main.c | 9 ++++ test/link/frameworks/build.zig | 20 +++++++++ test/link/frameworks/main.c | 7 +++ test/link/interdependent_static_c_libs/a.c | 4 ++ test/link/interdependent_static_c_libs/a.h | 2 + test/link/interdependent_static_c_libs/b.c | 6 +++ test/link/interdependent_static_c_libs/b.h | 2 + test/link/interdependent_static_c_libs/build.zig | 24 ++++++++++ test/link/interdependent_static_c_libs/main.zig | 8 ++++ test/link/objc/Foo.h | 7 +++ test/link/objc/Foo.m | 11 +++++ test/link/objc/build.zig | 22 +++++++++ test/link/objc/test.m | 12 +++++ test/link/objcpp/Foo.h | 7 +++ test/link/objcpp/Foo.mm | 11 +++++ test/link/objcpp/build.zig | 24 ++++++++++ test/link/objcpp/test.mm | 14 ++++++ test/link/static_lib_as_system_lib/a.c | 4 ++ test/link/static_lib_as_system_lib/a.h | 2 + test/link/static_lib_as_system_lib/build.zig | 23 ++++++++++ test/link/static_lib_as_system_lib/main.zig | 8 ++++ test/link/tls/a.c | 5 +++ test/link/tls/build.zig | 18 ++++++++ test/link/tls/main.zig | 15 +++++++ test/standalone.zig | 37 ++------------- test/standalone/link_common_symbols/a.c | 6 --- test/standalone/link_common_symbols/b.c | 7 --- test/standalone/link_common_symbols/build.zig | 16 ------- test/standalone/link_common_symbols/c.c | 5 --- test/standalone/link_common_symbols/main.zig | 16 ------- test/standalone/link_common_symbols_alignment/a.c | 2 - .../link_common_symbols_alignment/build.zig | 16 ------- .../link_common_symbols_alignment/main.zig | 9 ---- test/standalone/link_frameworks/build.zig | 34 -------------- test/standalone/link_frameworks/main.c | 7 --- test/standalone/link_import_tls_dylib/a.c | 1 - test/standalone/link_import_tls_dylib/build.zig | 16 ------- test/standalone/link_import_tls_dylib/main.zig | 7 --- .../link_interdependent_static_c_libs/a.c | 4 -- .../link_interdependent_static_c_libs/a.h | 2 - .../link_interdependent_static_c_libs/b.c | 6 --- .../link_interdependent_static_c_libs/b.h | 2 - .../link_interdependent_static_c_libs/build.zig | 24 ---------- .../link_interdependent_static_c_libs/main.zig | 8 ---- test/standalone/link_static_lib_as_system_lib/a.c | 4 -- test/standalone/link_static_lib_as_system_lib/a.h | 2 - .../link_static_lib_as_system_lib/build.zig | 23 ---------- .../link_static_lib_as_system_lib/main.zig | 8 ---- test/standalone/objc/Foo.h | 7 --- test/standalone/objc/Foo.m | 11 ----- test/standalone/objc/build.zig | 36 --------------- test/standalone/objc/test.m | 12 ----- test/standalone/objcpp/Foo.h | 7 --- test/standalone/objcpp/Foo.mm | 11 ----- test/standalone/objcpp/build.zig | 36 --------------- test/standalone/objcpp/test.mm | 14 ------ test/tests.zig | 22 +++++++++ 70 files changed, 483 insertions(+), 392 deletions(-) create mode 100644 test/link.zig create mode 100644 test/link/bss/build.zig create mode 100644 test/link/bss/main.zig create mode 100644 test/link/common_symbols/a.c create mode 100644 test/link/common_symbols/b.c create mode 100644 test/link/common_symbols/build.zig create mode 100644 test/link/common_symbols/c.c create mode 100644 test/link/common_symbols/main.zig create mode 100644 test/link/common_symbols_alignment/a.c create mode 100644 test/link/common_symbols_alignment/build.zig create mode 100644 test/link/common_symbols_alignment/main.zig create mode 100644 test/link/dylib/a.c create mode 100644 test/link/dylib/build.zig create mode 100644 test/link/dylib/main.c create mode 100644 test/link/frameworks/build.zig create mode 100644 test/link/frameworks/main.c create mode 100644 test/link/interdependent_static_c_libs/a.c create mode 100644 test/link/interdependent_static_c_libs/a.h create mode 100644 test/link/interdependent_static_c_libs/b.c create mode 100644 test/link/interdependent_static_c_libs/b.h create mode 100644 test/link/interdependent_static_c_libs/build.zig create mode 100644 test/link/interdependent_static_c_libs/main.zig create mode 100644 test/link/objc/Foo.h create mode 100644 test/link/objc/Foo.m create mode 100644 test/link/objc/build.zig create mode 100644 test/link/objc/test.m create mode 100644 test/link/objcpp/Foo.h create mode 100644 test/link/objcpp/Foo.mm create mode 100644 test/link/objcpp/build.zig create mode 100644 test/link/objcpp/test.mm create mode 100644 test/link/static_lib_as_system_lib/a.c create mode 100644 test/link/static_lib_as_system_lib/a.h create mode 100644 test/link/static_lib_as_system_lib/build.zig create mode 100644 test/link/static_lib_as_system_lib/main.zig create mode 100644 test/link/tls/a.c create mode 100644 test/link/tls/build.zig create mode 100644 test/link/tls/main.zig delete mode 100644 test/standalone/link_common_symbols/a.c delete mode 100644 test/standalone/link_common_symbols/b.c delete mode 100644 test/standalone/link_common_symbols/build.zig delete mode 100644 test/standalone/link_common_symbols/c.c delete mode 100644 test/standalone/link_common_symbols/main.zig delete mode 100644 test/standalone/link_common_symbols_alignment/a.c delete mode 100644 test/standalone/link_common_symbols_alignment/build.zig delete mode 100644 test/standalone/link_common_symbols_alignment/main.zig delete mode 100644 test/standalone/link_frameworks/build.zig delete mode 100644 test/standalone/link_frameworks/main.c delete mode 100644 test/standalone/link_import_tls_dylib/a.c delete mode 100644 test/standalone/link_import_tls_dylib/build.zig delete mode 100644 test/standalone/link_import_tls_dylib/main.zig delete mode 100644 test/standalone/link_interdependent_static_c_libs/a.c delete mode 100644 test/standalone/link_interdependent_static_c_libs/a.h delete mode 100644 test/standalone/link_interdependent_static_c_libs/b.c delete mode 100644 test/standalone/link_interdependent_static_c_libs/b.h delete mode 100644 test/standalone/link_interdependent_static_c_libs/build.zig delete mode 100644 test/standalone/link_interdependent_static_c_libs/main.zig delete mode 100644 test/standalone/link_static_lib_as_system_lib/a.c delete mode 100644 test/standalone/link_static_lib_as_system_lib/a.h delete mode 100644 test/standalone/link_static_lib_as_system_lib/build.zig delete mode 100644 test/standalone/link_static_lib_as_system_lib/main.zig delete mode 100644 test/standalone/objc/Foo.h delete mode 100644 test/standalone/objc/Foo.m delete mode 100644 test/standalone/objc/build.zig delete mode 100644 test/standalone/objc/test.m delete mode 100644 test/standalone/objcpp/Foo.h delete mode 100644 test/standalone/objcpp/Foo.mm delete mode 100644 test/standalone/objcpp/build.zig delete mode 100644 test/standalone/objcpp/test.mm (limited to 'test') diff --git a/test/link.zig b/test/link.zig new file mode 100644 index 0000000000..3c1b268d86 --- /dev/null +++ b/test/link.zig @@ -0,0 +1,52 @@ +const std = @import("std"); +const builtin = @import("builtin"); +const tests = @import("tests.zig"); + +pub fn addCases(cases: *tests.StandaloneContext) void { + cases.addBuildFile("test/link/bss/build.zig", .{ + .build_modes = false, // we only guarantee zerofill for undefined in Debug + }); + + cases.addBuildFile("test/link/dylib/build.zig", .{ + .build_modes = true, + }); + + cases.addBuildFile("test/link/common_symbols/build.zig", .{ + .build_modes = true, + }); + + cases.addBuildFile("test/link/common_symbols_alignment/build.zig", .{ + .build_modes = true, + }); + + cases.addBuildFile("test/link/interdependent_static_c_libs/build.zig", .{ + .build_modes = true, + }); + + cases.addBuildFile("test/link/static_lib_as_system_lib/build.zig", .{ + .build_modes = true, + }); + + cases.addBuildFile("test/link/tls/build.zig", .{ + .build_modes = true, + }); + + if (builtin.os.tag == .macos) { + cases.addBuildFile("test/link/frameworks/build.zig", .{ + .build_modes = true, + .requires_macos_sdk = true, + }); + + // Try to build and run an Objective-C executable. + cases.addBuildFile("test/link/objc/build.zig", .{ + .build_modes = true, + .requires_macos_sdk = true, + }); + + // Try to build and run an Objective-C++ executable. + cases.addBuildFile("test/link/objcpp/build.zig", .{ + .build_modes = true, + .requires_macos_sdk = true, + }); + } +} diff --git a/test/link/bss/build.zig b/test/link/bss/build.zig new file mode 100644 index 0000000000..76e9bdb305 --- /dev/null +++ b/test/link/bss/build.zig @@ -0,0 +1,14 @@ +const Builder = @import("std").build.Builder; + +pub fn build(b: *Builder) void { + const mode = b.standardReleaseOptions(); + const test_step = b.step("test", "Test"); + + const exe = b.addExecutable("bss", "main.zig"); + b.default_step.dependOn(&exe.step); + exe.setBuildMode(mode); + + const run = exe.run(); + run.expectStdOutEqual("0, 1, 0\n"); + test_step.dependOn(&run.step); +} diff --git a/test/link/bss/main.zig b/test/link/bss/main.zig new file mode 100644 index 0000000000..c901f0bb27 --- /dev/null +++ b/test/link/bss/main.zig @@ -0,0 +1,13 @@ +const std = @import("std"); + +// Stress test zerofill layout +var buffer: [0x1000000]u64 = undefined; + +pub fn main() anyerror!void { + buffer[0x10] = 1; + try std.io.getStdOut().writer().print("{d}, {d}, {d}\n", .{ + buffer[0], + buffer[0x10], + buffer[0x1000000 - 1], + }); +} diff --git a/test/link/common_symbols/a.c b/test/link/common_symbols/a.c new file mode 100644 index 0000000000..829a96e507 --- /dev/null +++ b/test/link/common_symbols/a.c @@ -0,0 +1,6 @@ +int i; +int j; + +int add_to_i_and_j(int x) { + return x + i + j; +} diff --git a/test/link/common_symbols/b.c b/test/link/common_symbols/b.c new file mode 100644 index 0000000000..18e8a8c23b --- /dev/null +++ b/test/link/common_symbols/b.c @@ -0,0 +1,7 @@ +long i; +int j = 2; +int k; + +void incr_i() { + i++; +} diff --git a/test/link/common_symbols/build.zig b/test/link/common_symbols/build.zig new file mode 100644 index 0000000000..2f9f892e86 --- /dev/null +++ b/test/link/common_symbols/build.zig @@ -0,0 +1,16 @@ +const Builder = @import("std").build.Builder; + +pub fn build(b: *Builder) void { + const mode = b.standardReleaseOptions(); + + const lib_a = b.addStaticLibrary("a", null); + lib_a.addCSourceFiles(&.{ "c.c", "a.c", "b.c" }, &.{"-fcommon"}); + lib_a.setBuildMode(mode); + + const test_exe = b.addTest("main.zig"); + test_exe.setBuildMode(mode); + test_exe.linkLibrary(lib_a); + + const test_step = b.step("test", "Test it"); + test_step.dependOn(&test_exe.step); +} diff --git a/test/link/common_symbols/c.c b/test/link/common_symbols/c.c new file mode 100644 index 0000000000..fdf60b9ca8 --- /dev/null +++ b/test/link/common_symbols/c.c @@ -0,0 +1,5 @@ +extern int k; + +int common_defined_externally() { + return k; +} diff --git a/test/link/common_symbols/main.zig b/test/link/common_symbols/main.zig new file mode 100644 index 0000000000..255b5aa621 --- /dev/null +++ b/test/link/common_symbols/main.zig @@ -0,0 +1,16 @@ +const std = @import("std"); +const expect = std.testing.expect; + +extern fn common_defined_externally() c_int; +extern fn incr_i() void; +extern fn add_to_i_and_j(x: c_int) c_int; + +test "undef shadows common symbol: issue #9937" { + try expect(common_defined_externally() == 0); +} + +test "import C common symbols" { + incr_i(); + const res = add_to_i_and_j(2); + try expect(res == 5); +} diff --git a/test/link/common_symbols_alignment/a.c b/test/link/common_symbols_alignment/a.c new file mode 100644 index 0000000000..adff9d15f3 --- /dev/null +++ b/test/link/common_symbols_alignment/a.c @@ -0,0 +1,2 @@ +int foo; +__attribute__((aligned(4096))) int bar; diff --git a/test/link/common_symbols_alignment/build.zig b/test/link/common_symbols_alignment/build.zig new file mode 100644 index 0000000000..a62d86af4f --- /dev/null +++ b/test/link/common_symbols_alignment/build.zig @@ -0,0 +1,16 @@ +const Builder = @import("std").build.Builder; + +pub fn build(b: *Builder) void { + const mode = b.standardReleaseOptions(); + + const lib_a = b.addStaticLibrary("a", null); + lib_a.addCSourceFiles(&.{"a.c"}, &.{"-fcommon"}); + lib_a.setBuildMode(mode); + + const test_exe = b.addTest("main.zig"); + test_exe.setBuildMode(mode); + test_exe.linkLibrary(lib_a); + + const test_step = b.step("test", "Test it"); + test_step.dependOn(&test_exe.step); +} diff --git a/test/link/common_symbols_alignment/main.zig b/test/link/common_symbols_alignment/main.zig new file mode 100644 index 0000000000..3d3457c764 --- /dev/null +++ b/test/link/common_symbols_alignment/main.zig @@ -0,0 +1,9 @@ +const std = @import("std"); + +extern var foo: i32; +extern var bar: i32; + +test { + try std.testing.expect(@ptrToInt(&foo) % 4 == 0); + try std.testing.expect(@ptrToInt(&bar) % 4096 == 0); +} diff --git a/test/link/dylib/a.c b/test/link/dylib/a.c new file mode 100644 index 0000000000..199b31e1a0 --- /dev/null +++ b/test/link/dylib/a.c @@ -0,0 +1,7 @@ +#include + +char world[] = "world"; + +char* hello() { + return "Hello"; +} diff --git a/test/link/dylib/build.zig b/test/link/dylib/build.zig new file mode 100644 index 0000000000..a9dee4aafb --- /dev/null +++ b/test/link/dylib/build.zig @@ -0,0 +1,29 @@ +const std = @import("std"); +const Builder = std.build.Builder; + +pub fn build(b: *Builder) void { + const mode = b.standardReleaseOptions(); + + const test_step = b.step("test", "Test"); + + const dylib = b.addSharedLibrary("a", null, b.version(1, 0, 0)); + dylib.setBuildMode(mode); + dylib.addCSourceFile("a.c", &.{}); + dylib.linkLibC(); + dylib.install(); + + const exe = b.addExecutable("main", null); + exe.setBuildMode(mode); + exe.addCSourceFile("main.c", &.{}); + exe.linkSystemLibrary("a"); + exe.linkLibC(); + exe.addLibraryPath(b.pathFromRoot("zig-out/lib/")); + exe.addRPath(b.pathFromRoot("zig-out/lib")); + + const run = exe.run(); + run.cwd = b.pathFromRoot("."); + run.expectStdOutEqual("Hello world"); + + test_step.dependOn(b.getInstallStep()); + test_step.dependOn(&run.step); +} diff --git a/test/link/dylib/main.c b/test/link/dylib/main.c new file mode 100644 index 0000000000..be1647ddad --- /dev/null +++ b/test/link/dylib/main.c @@ -0,0 +1,9 @@ +#include + +char* hello(); +extern char world[]; + +int main() { + printf("%s %s", hello(), world); + return 0; +} diff --git a/test/link/frameworks/build.zig b/test/link/frameworks/build.zig new file mode 100644 index 0000000000..5700422a41 --- /dev/null +++ b/test/link/frameworks/build.zig @@ -0,0 +1,20 @@ +const std = @import("std"); +const Builder = std.build.Builder; + +pub fn build(b: *Builder) void { + const mode = b.standardReleaseOptions(); + + const test_step = b.step("test", "Test the program"); + + const exe = b.addExecutable("test", null); + b.default_step.dependOn(&exe.step); + exe.addCSourceFile("main.c", &[0][]const u8{}); + exe.setBuildMode(mode); + exe.linkLibC(); + // TODO when we figure out how to ship framework stubs for cross-compilation, + // populate paths to the sysroot here. + exe.linkFramework("Cocoa"); + + const run_cmd = exe.run(); + test_step.dependOn(&run_cmd.step); +} diff --git a/test/link/frameworks/main.c b/test/link/frameworks/main.c new file mode 100644 index 0000000000..b9dab990b2 --- /dev/null +++ b/test/link/frameworks/main.c @@ -0,0 +1,7 @@ +#include +#include + +int main() { + assert(objc_getClass("NSObject") > 0); + assert(objc_getClass("NSApplication") > 0); +} diff --git a/test/link/interdependent_static_c_libs/a.c b/test/link/interdependent_static_c_libs/a.c new file mode 100644 index 0000000000..ee9da97a3a --- /dev/null +++ b/test/link/interdependent_static_c_libs/a.c @@ -0,0 +1,4 @@ +#include "a.h" +int32_t add(int32_t a, int32_t b) { + return a + b; +} diff --git a/test/link/interdependent_static_c_libs/a.h b/test/link/interdependent_static_c_libs/a.h new file mode 100644 index 0000000000..7b45d54d56 --- /dev/null +++ b/test/link/interdependent_static_c_libs/a.h @@ -0,0 +1,2 @@ +#include +int32_t add(int32_t a, int32_t b); diff --git a/test/link/interdependent_static_c_libs/b.c b/test/link/interdependent_static_c_libs/b.c new file mode 100644 index 0000000000..ac2cc4f937 --- /dev/null +++ b/test/link/interdependent_static_c_libs/b.c @@ -0,0 +1,6 @@ +#include "a.h" +#include "b.h" + +int32_t sub(int32_t a, int32_t b) { + return add(a, -1 * b); +} diff --git a/test/link/interdependent_static_c_libs/b.h b/test/link/interdependent_static_c_libs/b.h new file mode 100644 index 0000000000..6047163145 --- /dev/null +++ b/test/link/interdependent_static_c_libs/b.h @@ -0,0 +1,2 @@ +#include +int32_t sub(int32_t a, int32_t b); diff --git a/test/link/interdependent_static_c_libs/build.zig b/test/link/interdependent_static_c_libs/build.zig new file mode 100644 index 0000000000..bd1b6100da --- /dev/null +++ b/test/link/interdependent_static_c_libs/build.zig @@ -0,0 +1,24 @@ +const Builder = @import("std").build.Builder; + +pub fn build(b: *Builder) void { + const mode = b.standardReleaseOptions(); + + const lib_a = b.addStaticLibrary("a", null); + lib_a.addCSourceFile("a.c", &[_][]const u8{}); + lib_a.setBuildMode(mode); + lib_a.addIncludePath("."); + + const lib_b = b.addStaticLibrary("b", null); + lib_b.addCSourceFile("b.c", &[_][]const u8{}); + lib_b.setBuildMode(mode); + lib_b.addIncludePath("."); + + const test_exe = b.addTest("main.zig"); + test_exe.setBuildMode(mode); + test_exe.linkLibrary(lib_a); + test_exe.linkLibrary(lib_b); + test_exe.addIncludePath("."); + + const test_step = b.step("test", "Test it"); + test_step.dependOn(&test_exe.step); +} diff --git a/test/link/interdependent_static_c_libs/main.zig b/test/link/interdependent_static_c_libs/main.zig new file mode 100644 index 0000000000..cb5d2e7b77 --- /dev/null +++ b/test/link/interdependent_static_c_libs/main.zig @@ -0,0 +1,8 @@ +const std = @import("std"); +const expect = std.testing.expect; +const c = @cImport(@cInclude("b.h")); + +test "import C sub" { + const result = c.sub(2, 1); + try expect(result == 1); +} diff --git a/test/link/objc/Foo.h b/test/link/objc/Foo.h new file mode 100644 index 0000000000..05cb7df39b --- /dev/null +++ b/test/link/objc/Foo.h @@ -0,0 +1,7 @@ +#import + +@interface Foo : NSObject + +- (NSString *)name; + +@end diff --git a/test/link/objc/Foo.m b/test/link/objc/Foo.m new file mode 100644 index 0000000000..6fc9b1edf0 --- /dev/null +++ b/test/link/objc/Foo.m @@ -0,0 +1,11 @@ +#import "Foo.h" + +@implementation Foo + +- (NSString *)name +{ + NSString *str = [[NSString alloc] initWithFormat:@"Zig"]; + return str; +} + +@end diff --git a/test/link/objc/build.zig b/test/link/objc/build.zig new file mode 100644 index 0000000000..e41fd48e71 --- /dev/null +++ b/test/link/objc/build.zig @@ -0,0 +1,22 @@ +const std = @import("std"); +const Builder = std.build.Builder; + +pub fn build(b: *Builder) void { + const mode = b.standardReleaseOptions(); + + const test_step = b.step("test", "Test the program"); + + const exe = b.addExecutable("test", null); + b.default_step.dependOn(&exe.step); + exe.addIncludePath("."); + exe.addCSourceFile("Foo.m", &[0][]const u8{}); + exe.addCSourceFile("test.m", &[0][]const u8{}); + exe.setBuildMode(mode); + exe.linkLibC(); + // TODO when we figure out how to ship framework stubs for cross-compilation, + // populate paths to the sysroot here. + exe.linkFramework("Foundation"); + + const run_cmd = exe.run(); + test_step.dependOn(&run_cmd.step); +} diff --git a/test/link/objc/test.m b/test/link/objc/test.m new file mode 100644 index 0000000000..3c81316788 --- /dev/null +++ b/test/link/objc/test.m @@ -0,0 +1,12 @@ +#import "Foo.h" +#import + +int main(int argc, char *argv[]) +{ + @autoreleasepool { + Foo *foo = [[Foo alloc] init]; + NSString *result = [foo name]; + assert([result isEqualToString:@"Zig"]); + return 0; + } +} diff --git a/test/link/objcpp/Foo.h b/test/link/objcpp/Foo.h new file mode 100644 index 0000000000..05cb7df39b --- /dev/null +++ b/test/link/objcpp/Foo.h @@ -0,0 +1,7 @@ +#import + +@interface Foo : NSObject + +- (NSString *)name; + +@end diff --git a/test/link/objcpp/Foo.mm b/test/link/objcpp/Foo.mm new file mode 100644 index 0000000000..6fc9b1edf0 --- /dev/null +++ b/test/link/objcpp/Foo.mm @@ -0,0 +1,11 @@ +#import "Foo.h" + +@implementation Foo + +- (NSString *)name +{ + NSString *str = [[NSString alloc] initWithFormat:@"Zig"]; + return str; +} + +@end diff --git a/test/link/objcpp/build.zig b/test/link/objcpp/build.zig new file mode 100644 index 0000000000..767578e225 --- /dev/null +++ b/test/link/objcpp/build.zig @@ -0,0 +1,24 @@ +const std = @import("std"); +const Builder = std.build.Builder; + +pub fn build(b: *Builder) void { + const mode = b.standardReleaseOptions(); + + const test_step = b.step("test", "Test the program"); + + const exe = b.addExecutable("test", null); + b.default_step.dependOn(&exe.step); + exe.addIncludePath("."); + exe.addCSourceFile("Foo.mm", &[0][]const u8{}); + exe.addCSourceFile("test.mm", &[0][]const u8{}); + exe.setBuildMode(mode); + exe.linkLibCpp(); + // TODO when we figure out how to ship framework stubs for cross-compilation, + // populate paths to the sysroot here. + exe.linkFramework("Foundation"); + + const run_cmd = exe.run(); + run_cmd.expectStdOutEqual("Hello from C++ and Zig"); + + test_step.dependOn(&run_cmd.step); +} diff --git a/test/link/objcpp/test.mm b/test/link/objcpp/test.mm new file mode 100644 index 0000000000..d27c543cdf --- /dev/null +++ b/test/link/objcpp/test.mm @@ -0,0 +1,14 @@ +#import "Foo.h" +#import +#include + +int main(int argc, char *argv[]) +{ + @autoreleasepool { + Foo *foo = [[Foo alloc] init]; + NSString *result = [foo name]; + std::cout << "Hello from C++ and " << [result UTF8String]; + assert([result isEqualToString:@"Zig"]); + return 0; + } +} diff --git a/test/link/static_lib_as_system_lib/a.c b/test/link/static_lib_as_system_lib/a.c new file mode 100644 index 0000000000..ee9da97a3a --- /dev/null +++ b/test/link/static_lib_as_system_lib/a.c @@ -0,0 +1,4 @@ +#include "a.h" +int32_t add(int32_t a, int32_t b) { + return a + b; +} diff --git a/test/link/static_lib_as_system_lib/a.h b/test/link/static_lib_as_system_lib/a.h new file mode 100644 index 0000000000..7b45d54d56 --- /dev/null +++ b/test/link/static_lib_as_system_lib/a.h @@ -0,0 +1,2 @@ +#include +int32_t add(int32_t a, int32_t b); diff --git a/test/link/static_lib_as_system_lib/build.zig b/test/link/static_lib_as_system_lib/build.zig new file mode 100644 index 0000000000..f39f3fac2a --- /dev/null +++ b/test/link/static_lib_as_system_lib/build.zig @@ -0,0 +1,23 @@ +const std = @import("std"); +const Builder = std.build.Builder; + +pub fn build(b: *Builder) void { + const mode = b.standardReleaseOptions(); + + const lib_a = b.addStaticLibrary("a", null); + lib_a.addCSourceFile("a.c", &[_][]const u8{}); + lib_a.setBuildMode(mode); + lib_a.addIncludePath("."); + lib_a.install(); + + const test_exe = b.addTest("main.zig"); + test_exe.setBuildMode(mode); + test_exe.linkSystemLibrary("a"); // force linking liba.a as -la + test_exe.addSystemIncludePath("."); + const search_path = std.fs.path.join(b.allocator, &[_][]const u8{ b.install_path, "lib" }) catch unreachable; + test_exe.addLibraryPath(search_path); + + const test_step = b.step("test", "Test it"); + test_step.dependOn(b.getInstallStep()); + test_step.dependOn(&test_exe.step); +} diff --git a/test/link/static_lib_as_system_lib/main.zig b/test/link/static_lib_as_system_lib/main.zig new file mode 100644 index 0000000000..0b9c46217f --- /dev/null +++ b/test/link/static_lib_as_system_lib/main.zig @@ -0,0 +1,8 @@ +const std = @import("std"); +const expect = std.testing.expect; +const c = @cImport(@cInclude("a.h")); + +test "import C add" { + const result = c.add(2, 1); + try expect(result == 3); +} diff --git a/test/link/tls/a.c b/test/link/tls/a.c new file mode 100644 index 0000000000..8602d02419 --- /dev/null +++ b/test/link/tls/a.c @@ -0,0 +1,5 @@ +_Thread_local int a; + +int getA() { + return a; +} diff --git a/test/link/tls/build.zig b/test/link/tls/build.zig new file mode 100644 index 0000000000..ebf15ca439 --- /dev/null +++ b/test/link/tls/build.zig @@ -0,0 +1,18 @@ +const Builder = @import("std").build.Builder; + +pub fn build(b: *Builder) void { + const mode = b.standardReleaseOptions(); + + const lib = b.addSharedLibrary("a", null, b.version(1, 0, 0)); + lib.setBuildMode(mode); + lib.addCSourceFile("a.c", &.{}); + lib.linkLibC(); + + const test_exe = b.addTest("main.zig"); + test_exe.setBuildMode(mode); + test_exe.linkLibrary(lib); + test_exe.linkLibC(); + + const test_step = b.step("test", "Test it"); + test_step.dependOn(&test_exe.step); +} diff --git a/test/link/tls/main.zig b/test/link/tls/main.zig new file mode 100644 index 0000000000..ab01616e31 --- /dev/null +++ b/test/link/tls/main.zig @@ -0,0 +1,15 @@ +const std = @import("std"); + +extern threadlocal var a: i32; +extern fn getA() i32; + +fn getA2() i32 { + return a; +} + +test { + a = 2; + try std.testing.expect(getA() == 2); + try std.testing.expect(2 == getA2()); + try std.testing.expect(getA() == getA2()); +} diff --git a/test/standalone.zig b/test/standalone.zig index c34f9467d6..92d0ea4aa3 100644 --- a/test/standalone.zig +++ b/test/standalone.zig @@ -13,31 +13,12 @@ pub fn addCases(cases: *tests.StandaloneContext) void { cases.addBuildFile("test/standalone/main_pkg_path/build.zig", .{}); cases.addBuildFile("test/standalone/shared_library/build.zig", .{}); cases.addBuildFile("test/standalone/mix_o_files/build.zig", .{}); - if (builtin.os.tag == .macos) { - // Zig's macOS linker does not yet support LTO for LLVM IR files: - // https://github.com/ziglang/zig/issues/8680 - cases.addBuildFile("test/standalone/mix_c_files/build.zig", .{ - .build_modes = false, - .cross_targets = true, - }); - } else { - cases.addBuildFile("test/standalone/mix_c_files/build.zig", .{ - .build_modes = true, - .cross_targets = true, - }); - } + cases.addBuildFile("test/standalone/mix_c_files/build.zig", .{ + .build_modes = true, + .cross_targets = true, + }); cases.addBuildFile("test/standalone/global_linkage/build.zig", .{}); cases.addBuildFile("test/standalone/static_c_lib/build.zig", .{}); - cases.addBuildFile("test/standalone/link_interdependent_static_c_libs/build.zig", .{}); - cases.addBuildFile("test/standalone/link_static_lib_as_system_lib/build.zig", .{}); - cases.addBuildFile("test/standalone/link_common_symbols/build.zig", .{}); - cases.addBuildFile("test/standalone/link_frameworks/build.zig", .{ - .requires_macos_sdk = true, - }); - cases.addBuildFile("test/standalone/link_common_symbols_alignment/build.zig", .{}); - if (builtin.os.tag == .macos) { - cases.addBuildFile("test/standalone/link_import_tls_dylib/build.zig", .{}); - } cases.addBuildFile("test/standalone/issue_339/build.zig", .{}); cases.addBuildFile("test/standalone/issue_8550/build.zig", .{}); cases.addBuildFile("test/standalone/issue_794/build.zig", .{}); @@ -69,16 +50,6 @@ pub fn addCases(cases: *tests.StandaloneContext) void { if (builtin.os.tag == .linux) { cases.addBuildFile("test/standalone/pie/build.zig", .{}); } - // Try to build and run an Objective-C executable. - cases.addBuildFile("test/standalone/objc/build.zig", .{ - .build_modes = true, - .requires_macos_sdk = true, - }); - // Try to build and run an Objective-C++ executable. - cases.addBuildFile("test/standalone/objcpp/build.zig", .{ - .build_modes = true, - .requires_macos_sdk = true, - }); // Ensure the development tools are buildable. cases.add("tools/gen_spirv_spec.zig"); diff --git a/test/standalone/link_common_symbols/a.c b/test/standalone/link_common_symbols/a.c deleted file mode 100644 index 829a96e507..0000000000 --- a/test/standalone/link_common_symbols/a.c +++ /dev/null @@ -1,6 +0,0 @@ -int i; -int j; - -int add_to_i_and_j(int x) { - return x + i + j; -} diff --git a/test/standalone/link_common_symbols/b.c b/test/standalone/link_common_symbols/b.c deleted file mode 100644 index 18e8a8c23b..0000000000 --- a/test/standalone/link_common_symbols/b.c +++ /dev/null @@ -1,7 +0,0 @@ -long i; -int j = 2; -int k; - -void incr_i() { - i++; -} diff --git a/test/standalone/link_common_symbols/build.zig b/test/standalone/link_common_symbols/build.zig deleted file mode 100644 index 2f9f892e86..0000000000 --- a/test/standalone/link_common_symbols/build.zig +++ /dev/null @@ -1,16 +0,0 @@ -const Builder = @import("std").build.Builder; - -pub fn build(b: *Builder) void { - const mode = b.standardReleaseOptions(); - - const lib_a = b.addStaticLibrary("a", null); - lib_a.addCSourceFiles(&.{ "c.c", "a.c", "b.c" }, &.{"-fcommon"}); - lib_a.setBuildMode(mode); - - const test_exe = b.addTest("main.zig"); - test_exe.setBuildMode(mode); - test_exe.linkLibrary(lib_a); - - const test_step = b.step("test", "Test it"); - test_step.dependOn(&test_exe.step); -} diff --git a/test/standalone/link_common_symbols/c.c b/test/standalone/link_common_symbols/c.c deleted file mode 100644 index fdf60b9ca8..0000000000 --- a/test/standalone/link_common_symbols/c.c +++ /dev/null @@ -1,5 +0,0 @@ -extern int k; - -int common_defined_externally() { - return k; -} diff --git a/test/standalone/link_common_symbols/main.zig b/test/standalone/link_common_symbols/main.zig deleted file mode 100644 index 255b5aa621..0000000000 --- a/test/standalone/link_common_symbols/main.zig +++ /dev/null @@ -1,16 +0,0 @@ -const std = @import("std"); -const expect = std.testing.expect; - -extern fn common_defined_externally() c_int; -extern fn incr_i() void; -extern fn add_to_i_and_j(x: c_int) c_int; - -test "undef shadows common symbol: issue #9937" { - try expect(common_defined_externally() == 0); -} - -test "import C common symbols" { - incr_i(); - const res = add_to_i_and_j(2); - try expect(res == 5); -} diff --git a/test/standalone/link_common_symbols_alignment/a.c b/test/standalone/link_common_symbols_alignment/a.c deleted file mode 100644 index adff9d15f3..0000000000 --- a/test/standalone/link_common_symbols_alignment/a.c +++ /dev/null @@ -1,2 +0,0 @@ -int foo; -__attribute__((aligned(4096))) int bar; diff --git a/test/standalone/link_common_symbols_alignment/build.zig b/test/standalone/link_common_symbols_alignment/build.zig deleted file mode 100644 index a62d86af4f..0000000000 --- a/test/standalone/link_common_symbols_alignment/build.zig +++ /dev/null @@ -1,16 +0,0 @@ -const Builder = @import("std").build.Builder; - -pub fn build(b: *Builder) void { - const mode = b.standardReleaseOptions(); - - const lib_a = b.addStaticLibrary("a", null); - lib_a.addCSourceFiles(&.{"a.c"}, &.{"-fcommon"}); - lib_a.setBuildMode(mode); - - const test_exe = b.addTest("main.zig"); - test_exe.setBuildMode(mode); - test_exe.linkLibrary(lib_a); - - const test_step = b.step("test", "Test it"); - test_step.dependOn(&test_exe.step); -} diff --git a/test/standalone/link_common_symbols_alignment/main.zig b/test/standalone/link_common_symbols_alignment/main.zig deleted file mode 100644 index 3d3457c764..0000000000 --- a/test/standalone/link_common_symbols_alignment/main.zig +++ /dev/null @@ -1,9 +0,0 @@ -const std = @import("std"); - -extern var foo: i32; -extern var bar: i32; - -test { - try std.testing.expect(@ptrToInt(&foo) % 4 == 0); - try std.testing.expect(@ptrToInt(&bar) % 4096 == 0); -} diff --git a/test/standalone/link_frameworks/build.zig b/test/standalone/link_frameworks/build.zig deleted file mode 100644 index 460e1675e0..0000000000 --- a/test/standalone/link_frameworks/build.zig +++ /dev/null @@ -1,34 +0,0 @@ -const std = @import("std"); -const Builder = std.build.Builder; -const CrossTarget = std.zig.CrossTarget; - -fn isRunnableTarget(t: CrossTarget) bool { - // TODO I think we might be able to run this on Linux via Darling. - // Add a check for that here, and return true if Darling is available. - if (t.isNative() and t.getOsTag() == .macos) - return true - else - return false; -} - -pub fn build(b: *Builder) void { - const mode = b.standardReleaseOptions(); - const target = b.standardTargetOptions(.{}); - - const test_step = b.step("test", "Test the program"); - - const exe = b.addExecutable("test", null); - b.default_step.dependOn(&exe.step); - exe.addCSourceFile("main.c", &[0][]const u8{}); - exe.setBuildMode(mode); - exe.setTarget(target); - exe.linkLibC(); - // TODO when we figure out how to ship framework stubs for cross-compilation, - // populate paths to the sysroot here. - exe.linkFramework("Cocoa"); - - if (isRunnableTarget(target)) { - const run_cmd = exe.run(); - test_step.dependOn(&run_cmd.step); - } -} diff --git a/test/standalone/link_frameworks/main.c b/test/standalone/link_frameworks/main.c deleted file mode 100644 index b9dab990b2..0000000000 --- a/test/standalone/link_frameworks/main.c +++ /dev/null @@ -1,7 +0,0 @@ -#include -#include - -int main() { - assert(objc_getClass("NSObject") > 0); - assert(objc_getClass("NSApplication") > 0); -} diff --git a/test/standalone/link_import_tls_dylib/a.c b/test/standalone/link_import_tls_dylib/a.c deleted file mode 100644 index 5c5aa5bae4..0000000000 --- a/test/standalone/link_import_tls_dylib/a.c +++ /dev/null @@ -1 +0,0 @@ -_Thread_local int a; diff --git a/test/standalone/link_import_tls_dylib/build.zig b/test/standalone/link_import_tls_dylib/build.zig deleted file mode 100644 index 332173fbb6..0000000000 --- a/test/standalone/link_import_tls_dylib/build.zig +++ /dev/null @@ -1,16 +0,0 @@ -const Builder = @import("std").build.Builder; - -pub fn build(b: *Builder) void { - const mode = b.standardReleaseOptions(); - - const lib = b.addSharedLibrary("a", null, b.version(1, 0, 0)); - lib.setBuildMode(mode); - lib.addCSourceFile("a.c", &.{}); - - const test_exe = b.addTest("main.zig"); - test_exe.setBuildMode(mode); - test_exe.linkLibrary(lib); - - const test_step = b.step("test", "Test it"); - test_step.dependOn(&test_exe.step); -} diff --git a/test/standalone/link_import_tls_dylib/main.zig b/test/standalone/link_import_tls_dylib/main.zig deleted file mode 100644 index 354c6f545e..0000000000 --- a/test/standalone/link_import_tls_dylib/main.zig +++ /dev/null @@ -1,7 +0,0 @@ -const std = @import("std"); - -extern threadlocal var a: i32; - -test { - try std.testing.expect(a == 0); -} diff --git a/test/standalone/link_interdependent_static_c_libs/a.c b/test/standalone/link_interdependent_static_c_libs/a.c deleted file mode 100644 index ee9da97a3a..0000000000 --- a/test/standalone/link_interdependent_static_c_libs/a.c +++ /dev/null @@ -1,4 +0,0 @@ -#include "a.h" -int32_t add(int32_t a, int32_t b) { - return a + b; -} diff --git a/test/standalone/link_interdependent_static_c_libs/a.h b/test/standalone/link_interdependent_static_c_libs/a.h deleted file mode 100644 index 7b45d54d56..0000000000 --- a/test/standalone/link_interdependent_static_c_libs/a.h +++ /dev/null @@ -1,2 +0,0 @@ -#include -int32_t add(int32_t a, int32_t b); diff --git a/test/standalone/link_interdependent_static_c_libs/b.c b/test/standalone/link_interdependent_static_c_libs/b.c deleted file mode 100644 index ac2cc4f937..0000000000 --- a/test/standalone/link_interdependent_static_c_libs/b.c +++ /dev/null @@ -1,6 +0,0 @@ -#include "a.h" -#include "b.h" - -int32_t sub(int32_t a, int32_t b) { - return add(a, -1 * b); -} diff --git a/test/standalone/link_interdependent_static_c_libs/b.h b/test/standalone/link_interdependent_static_c_libs/b.h deleted file mode 100644 index 6047163145..0000000000 --- a/test/standalone/link_interdependent_static_c_libs/b.h +++ /dev/null @@ -1,2 +0,0 @@ -#include -int32_t sub(int32_t a, int32_t b); diff --git a/test/standalone/link_interdependent_static_c_libs/build.zig b/test/standalone/link_interdependent_static_c_libs/build.zig deleted file mode 100644 index bd1b6100da..0000000000 --- a/test/standalone/link_interdependent_static_c_libs/build.zig +++ /dev/null @@ -1,24 +0,0 @@ -const Builder = @import("std").build.Builder; - -pub fn build(b: *Builder) void { - const mode = b.standardReleaseOptions(); - - const lib_a = b.addStaticLibrary("a", null); - lib_a.addCSourceFile("a.c", &[_][]const u8{}); - lib_a.setBuildMode(mode); - lib_a.addIncludePath("."); - - const lib_b = b.addStaticLibrary("b", null); - lib_b.addCSourceFile("b.c", &[_][]const u8{}); - lib_b.setBuildMode(mode); - lib_b.addIncludePath("."); - - const test_exe = b.addTest("main.zig"); - test_exe.setBuildMode(mode); - test_exe.linkLibrary(lib_a); - test_exe.linkLibrary(lib_b); - test_exe.addIncludePath("."); - - const test_step = b.step("test", "Test it"); - test_step.dependOn(&test_exe.step); -} diff --git a/test/standalone/link_interdependent_static_c_libs/main.zig b/test/standalone/link_interdependent_static_c_libs/main.zig deleted file mode 100644 index cb5d2e7b77..0000000000 --- a/test/standalone/link_interdependent_static_c_libs/main.zig +++ /dev/null @@ -1,8 +0,0 @@ -const std = @import("std"); -const expect = std.testing.expect; -const c = @cImport(@cInclude("b.h")); - -test "import C sub" { - const result = c.sub(2, 1); - try expect(result == 1); -} diff --git a/test/standalone/link_static_lib_as_system_lib/a.c b/test/standalone/link_static_lib_as_system_lib/a.c deleted file mode 100644 index ee9da97a3a..0000000000 --- a/test/standalone/link_static_lib_as_system_lib/a.c +++ /dev/null @@ -1,4 +0,0 @@ -#include "a.h" -int32_t add(int32_t a, int32_t b) { - return a + b; -} diff --git a/test/standalone/link_static_lib_as_system_lib/a.h b/test/standalone/link_static_lib_as_system_lib/a.h deleted file mode 100644 index 7b45d54d56..0000000000 --- a/test/standalone/link_static_lib_as_system_lib/a.h +++ /dev/null @@ -1,2 +0,0 @@ -#include -int32_t add(int32_t a, int32_t b); diff --git a/test/standalone/link_static_lib_as_system_lib/build.zig b/test/standalone/link_static_lib_as_system_lib/build.zig deleted file mode 100644 index f39f3fac2a..0000000000 --- a/test/standalone/link_static_lib_as_system_lib/build.zig +++ /dev/null @@ -1,23 +0,0 @@ -const std = @import("std"); -const Builder = std.build.Builder; - -pub fn build(b: *Builder) void { - const mode = b.standardReleaseOptions(); - - const lib_a = b.addStaticLibrary("a", null); - lib_a.addCSourceFile("a.c", &[_][]const u8{}); - lib_a.setBuildMode(mode); - lib_a.addIncludePath("."); - lib_a.install(); - - const test_exe = b.addTest("main.zig"); - test_exe.setBuildMode(mode); - test_exe.linkSystemLibrary("a"); // force linking liba.a as -la - test_exe.addSystemIncludePath("."); - const search_path = std.fs.path.join(b.allocator, &[_][]const u8{ b.install_path, "lib" }) catch unreachable; - test_exe.addLibraryPath(search_path); - - const test_step = b.step("test", "Test it"); - test_step.dependOn(b.getInstallStep()); - test_step.dependOn(&test_exe.step); -} diff --git a/test/standalone/link_static_lib_as_system_lib/main.zig b/test/standalone/link_static_lib_as_system_lib/main.zig deleted file mode 100644 index 0b9c46217f..0000000000 --- a/test/standalone/link_static_lib_as_system_lib/main.zig +++ /dev/null @@ -1,8 +0,0 @@ -const std = @import("std"); -const expect = std.testing.expect; -const c = @cImport(@cInclude("a.h")); - -test "import C add" { - const result = c.add(2, 1); - try expect(result == 3); -} diff --git a/test/standalone/objc/Foo.h b/test/standalone/objc/Foo.h deleted file mode 100644 index 05cb7df39b..0000000000 --- a/test/standalone/objc/Foo.h +++ /dev/null @@ -1,7 +0,0 @@ -#import - -@interface Foo : NSObject - -- (NSString *)name; - -@end diff --git a/test/standalone/objc/Foo.m b/test/standalone/objc/Foo.m deleted file mode 100644 index 6fc9b1edf0..0000000000 --- a/test/standalone/objc/Foo.m +++ /dev/null @@ -1,11 +0,0 @@ -#import "Foo.h" - -@implementation Foo - -- (NSString *)name -{ - NSString *str = [[NSString alloc] initWithFormat:@"Zig"]; - return str; -} - -@end diff --git a/test/standalone/objc/build.zig b/test/standalone/objc/build.zig deleted file mode 100644 index 1b3a90f90f..0000000000 --- a/test/standalone/objc/build.zig +++ /dev/null @@ -1,36 +0,0 @@ -const std = @import("std"); -const Builder = std.build.Builder; -const CrossTarget = std.zig.CrossTarget; - -fn isRunnableTarget(t: CrossTarget) bool { - // TODO I think we might be able to run this on Linux via Darling. - // Add a check for that here, and return true if Darling is available. - if (t.isNative() and t.getOsTag() == .macos) - return true - else - return false; -} - -pub fn build(b: *Builder) void { - const mode = b.standardReleaseOptions(); - const target = b.standardTargetOptions(.{}); - - const test_step = b.step("test", "Test the program"); - - const exe = b.addExecutable("test", null); - b.default_step.dependOn(&exe.step); - exe.addIncludePath("."); - exe.addCSourceFile("Foo.m", &[0][]const u8{}); - exe.addCSourceFile("test.m", &[0][]const u8{}); - exe.setBuildMode(mode); - exe.setTarget(target); - exe.linkLibC(); - // TODO when we figure out how to ship framework stubs for cross-compilation, - // populate paths to the sysroot here. - exe.linkFramework("Foundation"); - - if (isRunnableTarget(target)) { - const run_cmd = exe.run(); - test_step.dependOn(&run_cmd.step); - } -} diff --git a/test/standalone/objc/test.m b/test/standalone/objc/test.m deleted file mode 100644 index 3c81316788..0000000000 --- a/test/standalone/objc/test.m +++ /dev/null @@ -1,12 +0,0 @@ -#import "Foo.h" -#import - -int main(int argc, char *argv[]) -{ - @autoreleasepool { - Foo *foo = [[Foo alloc] init]; - NSString *result = [foo name]; - assert([result isEqualToString:@"Zig"]); - return 0; - } -} diff --git a/test/standalone/objcpp/Foo.h b/test/standalone/objcpp/Foo.h deleted file mode 100644 index 05cb7df39b..0000000000 --- a/test/standalone/objcpp/Foo.h +++ /dev/null @@ -1,7 +0,0 @@ -#import - -@interface Foo : NSObject - -- (NSString *)name; - -@end diff --git a/test/standalone/objcpp/Foo.mm b/test/standalone/objcpp/Foo.mm deleted file mode 100644 index 6fc9b1edf0..0000000000 --- a/test/standalone/objcpp/Foo.mm +++ /dev/null @@ -1,11 +0,0 @@ -#import "Foo.h" - -@implementation Foo - -- (NSString *)name -{ - NSString *str = [[NSString alloc] initWithFormat:@"Zig"]; - return str; -} - -@end diff --git a/test/standalone/objcpp/build.zig b/test/standalone/objcpp/build.zig deleted file mode 100644 index 688592793a..0000000000 --- a/test/standalone/objcpp/build.zig +++ /dev/null @@ -1,36 +0,0 @@ -const std = @import("std"); -const Builder = std.build.Builder; -const CrossTarget = std.zig.CrossTarget; - -fn isRunnableTarget(t: CrossTarget) bool { - // TODO I think we might be able to run this on Linux via Darling. - // Add a check for that here, and return true if Darling is available. - if (t.isNative() and t.getOsTag() == .macos) - return true - else - return false; -} - -pub fn build(b: *Builder) void { - const mode = b.standardReleaseOptions(); - const target = b.standardTargetOptions(.{}); - - const test_step = b.step("test", "Test the program"); - - const exe = b.addExecutable("test", null); - b.default_step.dependOn(&exe.step); - exe.addIncludePath("."); - exe.addCSourceFile("Foo.mm", &[0][]const u8{}); - exe.addCSourceFile("test.mm", &[0][]const u8{}); - exe.setBuildMode(mode); - exe.setTarget(target); - exe.linkLibCpp(); - // TODO when we figure out how to ship framework stubs for cross-compilation, - // populate paths to the sysroot here. - exe.linkFramework("Foundation"); - - if (isRunnableTarget(target)) { - const run_cmd = exe.run(); - test_step.dependOn(&run_cmd.step); - } -} diff --git a/test/standalone/objcpp/test.mm b/test/standalone/objcpp/test.mm deleted file mode 100644 index d27c543cdf..0000000000 --- a/test/standalone/objcpp/test.mm +++ /dev/null @@ -1,14 +0,0 @@ -#import "Foo.h" -#import -#include - -int main(int argc, char *argv[]) -{ - @autoreleasepool { - Foo *foo = [[Foo alloc] init]; - NSString *result = [foo name]; - std::cout << "Hello from C++ and " << [result UTF8String]; - assert([result isEqualToString:@"Zig"]); - return 0; - } -} diff --git a/test/tests.zig b/test/tests.zig index 3666ef1028..8bc415d28b 100644 --- a/test/tests.zig +++ b/test/tests.zig @@ -21,6 +21,7 @@ const assemble_and_link = @import("assemble_and_link.zig"); const translate_c = @import("translate_c.zig"); const run_translated_c = @import("run_translated_c.zig"); const gen_h = @import("gen_h.zig"); +const link = @import("link.zig"); // Implementations pub const TranslateCContext = @import("src/translate_c.zig").TranslateCContext; @@ -479,6 +480,27 @@ pub fn addStandaloneTests( return cases.step; } +pub fn addLinkTests( + b: *build.Builder, + test_filter: ?[]const u8, + modes: []const Mode, + enable_macos_sdk: bool, +) *build.Step { + const cases = b.allocator.create(StandaloneContext) catch unreachable; + cases.* = StandaloneContext{ + .b = b, + .step = b.step("test-link", "Run the linker tests"), + .test_index = 0, + .test_filter = test_filter, + .modes = modes, + .skip_non_native = true, + .enable_macos_sdk = enable_macos_sdk, + .target = .{}, + }; + link.addCases(cases); + return cases.step; +} + pub fn addCliTests(b: *build.Builder, test_filter: ?[]const u8, modes: []const Mode) *build.Step { _ = test_filter; _ = modes; -- cgit v1.2.3