authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-05-28 11:44:53+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-06-20 17:59:17+02:00
log38edef35bfcba5789ea50adc7c76dec504079812
tree66d7f7279a005a36c201cadd474b66035f5c1a44
parent74ed7c1f0998e9dd89aa3f3480fff845afd6b422

test: introduce link(er) tests - builds on standalone tests


72 files changed, 485 insertions(+), 392 deletions(-)

build.zig+1
......@@ -489,6 +489,7 @@ pub fn build(b: *Builder) !void {
489489
490490 toolchain_step.dependOn(tests.addCompareOutputTests(b, test_filter, modes));
491491 toolchain_step.dependOn(tests.addStandaloneTests(b, test_filter, modes, skip_non_native, enable_macos_sdk, target));
492 toolchain_step.dependOn(tests.addLinkTests(b, test_filter, modes, enable_macos_sdk));
492493 toolchain_step.dependOn(tests.addStackTraceTests(b, test_filter, modes));
493494 toolchain_step.dependOn(tests.addCliTests(b, test_filter, modes));
494495 toolchain_step.dependOn(tests.addAssembleAndLinkTests(b, test_filter, modes));
lib/std/build/RunStep.zig+1
......@@ -149,6 +149,7 @@ fn make(step: *Step) !void {
149149 const cwd = if (self.cwd) |cwd| self.builder.pathFromRoot(cwd) else self.builder.build_root;
150150
151151 var argv_list = ArrayList([]const u8).init(self.builder.allocator);
152
152153 for (self.argv.items) |arg| {
153154 switch (arg) {
154155 .bytes => |bytes| try argv_list.append(bytes),
test/link.zig created+52
......@@ -0,0 +1,52 @@
1const std = @import("std");
2const builtin = @import("builtin");
3const tests = @import("tests.zig");
4
5pub fn addCases(cases: *tests.StandaloneContext) void {
6 cases.addBuildFile("test/link/bss/build.zig", .{
7 .build_modes = false, // we only guarantee zerofill for undefined in Debug
8 });
9
10 cases.addBuildFile("test/link/dylib/build.zig", .{
11 .build_modes = true,
12 });
13
14 cases.addBuildFile("test/link/common_symbols/build.zig", .{
15 .build_modes = true,
16 });
17
18 cases.addBuildFile("test/link/common_symbols_alignment/build.zig", .{
19 .build_modes = true,
20 });
21
22 cases.addBuildFile("test/link/interdependent_static_c_libs/build.zig", .{
23 .build_modes = true,
24 });
25
26 cases.addBuildFile("test/link/static_lib_as_system_lib/build.zig", .{
27 .build_modes = true,
28 });
29
30 cases.addBuildFile("test/link/tls/build.zig", .{
31 .build_modes = true,
32 });
33
34 if (builtin.os.tag == .macos) {
35 cases.addBuildFile("test/link/frameworks/build.zig", .{
36 .build_modes = true,
37 .requires_macos_sdk = true,
38 });
39
40 // Try to build and run an Objective-C executable.
41 cases.addBuildFile("test/link/objc/build.zig", .{
42 .build_modes = true,
43 .requires_macos_sdk = true,
44 });
45
46 // Try to build and run an Objective-C++ executable.
47 cases.addBuildFile("test/link/objcpp/build.zig", .{
48 .build_modes = true,
49 .requires_macos_sdk = true,
50 });
51 }
52}
test/link/bss/build.zig created+14
......@@ -0,0 +1,14 @@
1const Builder = @import("std").build.Builder;
2
3pub fn build(b: *Builder) void {
4 const mode = b.standardReleaseOptions();
5 const test_step = b.step("test", "Test");
6
7 const exe = b.addExecutable("bss", "main.zig");
8 b.default_step.dependOn(&exe.step);
9 exe.setBuildMode(mode);
10
11 const run = exe.run();
12 run.expectStdOutEqual("0, 1, 0\n");
13 test_step.dependOn(&run.step);
14}
test/link/bss/main.zig created+13
......@@ -0,0 +1,13 @@
1const std = @import("std");
2
3// Stress test zerofill layout
4var buffer: [0x1000000]u64 = undefined;
5
6pub fn main() anyerror!void {
7 buffer[0x10] = 1;
8 try std.io.getStdOut().writer().print("{d}, {d}, {d}\n", .{
9 buffer[0],
10 buffer[0x10],
11 buffer[0x1000000 - 1],
12 });
13}
test/link/common_symbols/a.c created+6
......@@ -0,0 +1,6 @@
1int i;
2int j;
3
4int add_to_i_and_j(int x) {
5 return x + i + j;
6}
test/link/common_symbols/b.c created+7
......@@ -0,0 +1,7 @@
1long i;
2int j = 2;
3int k;
4
5void incr_i() {
6 i++;
7}
test/link/common_symbols/build.zig created+16
......@@ -0,0 +1,16 @@
1const Builder = @import("std").build.Builder;
2
3pub fn build(b: *Builder) void {
4 const mode = b.standardReleaseOptions();
5
6 const lib_a = b.addStaticLibrary("a", null);
7 lib_a.addCSourceFiles(&.{ "c.c", "a.c", "b.c" }, &.{"-fcommon"});
8 lib_a.setBuildMode(mode);
9
10 const test_exe = b.addTest("main.zig");
11 test_exe.setBuildMode(mode);
12 test_exe.linkLibrary(lib_a);
13
14 const test_step = b.step("test", "Test it");
15 test_step.dependOn(&test_exe.step);
16}
test/link/common_symbols/c.c created+5
......@@ -0,0 +1,5 @@
1extern int k;
2
3int common_defined_externally() {
4 return k;
5}
test/link/common_symbols/main.zig created+16
......@@ -0,0 +1,16 @@
1const std = @import("std");
2const expect = std.testing.expect;
3
4extern fn common_defined_externally() c_int;
5extern fn incr_i() void;
6extern fn add_to_i_and_j(x: c_int) c_int;
7
8test "undef shadows common symbol: issue #9937" {
9 try expect(common_defined_externally() == 0);
10}
11
12test "import C common symbols" {
13 incr_i();
14 const res = add_to_i_and_j(2);
15 try expect(res == 5);
16}
test/link/common_symbols_alignment/a.c created+2
......@@ -0,0 +1,2 @@
1int foo;
2__attribute__((aligned(4096))) int bar;
test/link/common_symbols_alignment/build.zig created+16
......@@ -0,0 +1,16 @@
1const Builder = @import("std").build.Builder;
2
3pub fn build(b: *Builder) void {
4 const mode = b.standardReleaseOptions();
5
6 const lib_a = b.addStaticLibrary("a", null);
7 lib_a.addCSourceFiles(&.{"a.c"}, &.{"-fcommon"});
8 lib_a.setBuildMode(mode);
9
10 const test_exe = b.addTest("main.zig");
11 test_exe.setBuildMode(mode);
12 test_exe.linkLibrary(lib_a);
13
14 const test_step = b.step("test", "Test it");
15 test_step.dependOn(&test_exe.step);
16}
test/link/common_symbols_alignment/main.zig created+9
......@@ -0,0 +1,9 @@
1const std = @import("std");
2
3extern var foo: i32;
4extern var bar: i32;
5
6test {
7 try std.testing.expect(@ptrToInt(&foo) % 4 == 0);
8 try std.testing.expect(@ptrToInt(&bar) % 4096 == 0);
9}
test/link/dylib/a.c created+7
......@@ -0,0 +1,7 @@
1#include <stdio.h>
2
3char world[] = "world";
4
5char* hello() {
6 return "Hello";
7}
test/link/dylib/build.zig created+29
......@@ -0,0 +1,29 @@
1const std = @import("std");
2const Builder = std.build.Builder;
3
4pub fn build(b: *Builder) void {
5 const mode = b.standardReleaseOptions();
6
7 const test_step = b.step("test", "Test");
8
9 const dylib = b.addSharedLibrary("a", null, b.version(1, 0, 0));
10 dylib.setBuildMode(mode);
11 dylib.addCSourceFile("a.c", &.{});
12 dylib.linkLibC();
13 dylib.install();
14
15 const exe = b.addExecutable("main", null);
16 exe.setBuildMode(mode);
17 exe.addCSourceFile("main.c", &.{});
18 exe.linkSystemLibrary("a");
19 exe.linkLibC();
20 exe.addLibraryPath(b.pathFromRoot("zig-out/lib/"));
21 exe.addRPath(b.pathFromRoot("zig-out/lib"));
22
23 const run = exe.run();
24 run.cwd = b.pathFromRoot(".");
25 run.expectStdOutEqual("Hello world");
26
27 test_step.dependOn(b.getInstallStep());
28 test_step.dependOn(&run.step);
29}
test/link/dylib/main.c created+9
......@@ -0,0 +1,9 @@
1#include <stdio.h>
2
3char* hello();
4extern char world[];
5
6int main() {
7 printf("%s %s", hello(), world);
8 return 0;
9}
test/link/frameworks/build.zig created+20
......@@ -0,0 +1,20 @@
1const std = @import("std");
2const Builder = std.build.Builder;
3
4pub fn build(b: *Builder) void {
5 const mode = b.standardReleaseOptions();
6
7 const test_step = b.step("test", "Test the program");
8
9 const exe = b.addExecutable("test", null);
10 b.default_step.dependOn(&exe.step);
11 exe.addCSourceFile("main.c", &[0][]const u8{});
12 exe.setBuildMode(mode);
13 exe.linkLibC();
14 // TODO when we figure out how to ship framework stubs for cross-compilation,
15 // populate paths to the sysroot here.
16 exe.linkFramework("Cocoa");
17
18 const run_cmd = exe.run();
19 test_step.dependOn(&run_cmd.step);
20}
test/link/frameworks/main.c created+7
......@@ -0,0 +1,7 @@
1#include <assert.h>
2#include <objc/runtime.h>
3
4int main() {
5 assert(objc_getClass("NSObject") > 0);
6 assert(objc_getClass("NSApplication") > 0);
7}
test/link/interdependent_static_c_libs/a.c created+4
......@@ -0,0 +1,4 @@
1#include "a.h"
2int32_t add(int32_t a, int32_t b) {
3 return a + b;
4}
test/link/interdependent_static_c_libs/a.h created+2
......@@ -0,0 +1,2 @@
1#include <stdint.h>
2int32_t add(int32_t a, int32_t b);
test/link/interdependent_static_c_libs/b.c created+6
......@@ -0,0 +1,6 @@
1#include "a.h"
2#include "b.h"
3
4int32_t sub(int32_t a, int32_t b) {
5 return add(a, -1 * b);
6}
test/link/interdependent_static_c_libs/b.h created+2
......@@ -0,0 +1,2 @@
1#include <stdint.h>
2int32_t sub(int32_t a, int32_t b);
test/link/interdependent_static_c_libs/build.zig created+24
......@@ -0,0 +1,24 @@
1const Builder = @import("std").build.Builder;
2
3pub fn build(b: *Builder) void {
4 const mode = b.standardReleaseOptions();
5
6 const lib_a = b.addStaticLibrary("a", null);
7 lib_a.addCSourceFile("a.c", &[_][]const u8{});
8 lib_a.setBuildMode(mode);
9 lib_a.addIncludePath(".");
10
11 const lib_b = b.addStaticLibrary("b", null);
12 lib_b.addCSourceFile("b.c", &[_][]const u8{});
13 lib_b.setBuildMode(mode);
14 lib_b.addIncludePath(".");
15
16 const test_exe = b.addTest("main.zig");
17 test_exe.setBuildMode(mode);
18 test_exe.linkLibrary(lib_a);
19 test_exe.linkLibrary(lib_b);
20 test_exe.addIncludePath(".");
21
22 const test_step = b.step("test", "Test it");
23 test_step.dependOn(&test_exe.step);
24}
test/link/interdependent_static_c_libs/main.zig created+8
......@@ -0,0 +1,8 @@
1const std = @import("std");
2const expect = std.testing.expect;
3const c = @cImport(@cInclude("b.h"));
4
5test "import C sub" {
6 const result = c.sub(2, 1);
7 try expect(result == 1);
8}
test/link/objc/Foo.h created+7
......@@ -0,0 +1,7 @@
1#import <Foundation/Foundation.h>
2
3@interface Foo : NSObject
4
5- (NSString *)name;
6
7@end
test/link/objc/Foo.m created+11
......@@ -0,0 +1,11 @@
1#import "Foo.h"
2
3@implementation Foo
4
5- (NSString *)name
6{
7 NSString *str = [[NSString alloc] initWithFormat:@"Zig"];
8 return str;
9}
10
11@end
test/link/objc/build.zig created+22
......@@ -0,0 +1,22 @@
1const std = @import("std");
2const Builder = std.build.Builder;
3
4pub fn build(b: *Builder) void {
5 const mode = b.standardReleaseOptions();
6
7 const test_step = b.step("test", "Test the program");
8
9 const exe = b.addExecutable("test", null);
10 b.default_step.dependOn(&exe.step);
11 exe.addIncludePath(".");
12 exe.addCSourceFile("Foo.m", &[0][]const u8{});
13 exe.addCSourceFile("test.m", &[0][]const u8{});
14 exe.setBuildMode(mode);
15 exe.linkLibC();
16 // TODO when we figure out how to ship framework stubs for cross-compilation,
17 // populate paths to the sysroot here.
18 exe.linkFramework("Foundation");
19
20 const run_cmd = exe.run();
21 test_step.dependOn(&run_cmd.step);
22}
test/link/objc/test.m created+12
......@@ -0,0 +1,12 @@
1#import "Foo.h"
2#import <assert.h>
3
4int main(int argc, char *argv[])
5{
6 @autoreleasepool {
7 Foo *foo = [[Foo alloc] init];
8 NSString *result = [foo name];
9 assert([result isEqualToString:@"Zig"]);
10 return 0;
11 }
12}
test/link/objcpp/Foo.h created+7
......@@ -0,0 +1,7 @@
1#import <Foundation/Foundation.h>
2
3@interface Foo : NSObject
4
5- (NSString *)name;
6
7@end
test/link/objcpp/Foo.mm created+11
......@@ -0,0 +1,11 @@
1#import "Foo.h"
2
3@implementation Foo
4
5- (NSString *)name
6{
7 NSString *str = [[NSString alloc] initWithFormat:@"Zig"];
8 return str;
9}
10
11@end
test/link/objcpp/build.zig created+24
......@@ -0,0 +1,24 @@
1const std = @import("std");
2const Builder = std.build.Builder;
3
4pub fn build(b: *Builder) void {
5 const mode = b.standardReleaseOptions();
6
7 const test_step = b.step("test", "Test the program");
8
9 const exe = b.addExecutable("test", null);
10 b.default_step.dependOn(&exe.step);
11 exe.addIncludePath(".");
12 exe.addCSourceFile("Foo.mm", &[0][]const u8{});
13 exe.addCSourceFile("test.mm", &[0][]const u8{});
14 exe.setBuildMode(mode);
15 exe.linkLibCpp();
16 // TODO when we figure out how to ship framework stubs for cross-compilation,
17 // populate paths to the sysroot here.
18 exe.linkFramework("Foundation");
19
20 const run_cmd = exe.run();
21 run_cmd.expectStdOutEqual("Hello from C++ and Zig");
22
23 test_step.dependOn(&run_cmd.step);
24}
test/link/objcpp/test.mm created+14
......@@ -0,0 +1,14 @@
1#import "Foo.h"
2#import <assert.h>
3#include <iostream>
4
5int main(int argc, char *argv[])
6{
7 @autoreleasepool {
8 Foo *foo = [[Foo alloc] init];
9 NSString *result = [foo name];
10 std::cout << "Hello from C++ and " << [result UTF8String];
11 assert([result isEqualToString:@"Zig"]);
12 return 0;
13 }
14}
test/link/static_lib_as_system_lib/a.c created+4
......@@ -0,0 +1,4 @@
1#include "a.h"
2int32_t add(int32_t a, int32_t b) {
3 return a + b;
4}
test/link/static_lib_as_system_lib/a.h created+2
......@@ -0,0 +1,2 @@
1#include <stdint.h>
2int32_t add(int32_t a, int32_t b);
test/link/static_lib_as_system_lib/build.zig created+23
......@@ -0,0 +1,23 @@
1const std = @import("std");
2const Builder = std.build.Builder;
3
4pub fn build(b: *Builder) void {
5 const mode = b.standardReleaseOptions();
6
7 const lib_a = b.addStaticLibrary("a", null);
8 lib_a.addCSourceFile("a.c", &[_][]const u8{});
9 lib_a.setBuildMode(mode);
10 lib_a.addIncludePath(".");
11 lib_a.install();
12
13 const test_exe = b.addTest("main.zig");
14 test_exe.setBuildMode(mode);
15 test_exe.linkSystemLibrary("a"); // force linking liba.a as -la
16 test_exe.addSystemIncludePath(".");
17 const search_path = std.fs.path.join(b.allocator, &[_][]const u8{ b.install_path, "lib" }) catch unreachable;
18 test_exe.addLibraryPath(search_path);
19
20 const test_step = b.step("test", "Test it");
21 test_step.dependOn(b.getInstallStep());
22 test_step.dependOn(&test_exe.step);
23}
test/link/static_lib_as_system_lib/main.zig created+8
......@@ -0,0 +1,8 @@
1const std = @import("std");
2const expect = std.testing.expect;
3const c = @cImport(@cInclude("a.h"));
4
5test "import C add" {
6 const result = c.add(2, 1);
7 try expect(result == 3);
8}
test/link/tls/a.c created+5
......@@ -0,0 +1,5 @@
1_Thread_local int a;
2
3int getA() {
4 return a;
5}
test/link/tls/build.zig created+18
......@@ -0,0 +1,18 @@
1const Builder = @import("std").build.Builder;
2
3pub fn build(b: *Builder) void {
4 const mode = b.standardReleaseOptions();
5
6 const lib = b.addSharedLibrary("a", null, b.version(1, 0, 0));
7 lib.setBuildMode(mode);
8 lib.addCSourceFile("a.c", &.{});
9 lib.linkLibC();
10
11 const test_exe = b.addTest("main.zig");
12 test_exe.setBuildMode(mode);
13 test_exe.linkLibrary(lib);
14 test_exe.linkLibC();
15
16 const test_step = b.step("test", "Test it");
17 test_step.dependOn(&test_exe.step);
18}
test/link/tls/main.zig created+15
......@@ -0,0 +1,15 @@
1const std = @import("std");
2
3extern threadlocal var a: i32;
4extern fn getA() i32;
5
6fn getA2() i32 {
7 return a;
8}
9
10test {
11 a = 2;
12 try std.testing.expect(getA() == 2);
13 try std.testing.expect(2 == getA2());
14 try std.testing.expect(getA() == getA2());
15}
test/standalone.zig+4-33
......@@ -13,31 +13,12 @@ pub fn addCases(cases: *tests.StandaloneContext) void {
1313 cases.addBuildFile("test/standalone/main_pkg_path/build.zig", .{});
1414 cases.addBuildFile("test/standalone/shared_library/build.zig", .{});
1515 cases.addBuildFile("test/standalone/mix_o_files/build.zig", .{});
16 if (builtin.os.tag == .macos) {
17 // Zig's macOS linker does not yet support LTO for LLVM IR files:
18 // https://github.com/ziglang/zig/issues/8680
19 cases.addBuildFile("test/standalone/mix_c_files/build.zig", .{
20 .build_modes = false,
21 .cross_targets = true,
22 });
23 } else {
24 cases.addBuildFile("test/standalone/mix_c_files/build.zig", .{
25 .build_modes = true,
26 .cross_targets = true,
27 });
28 }
16 cases.addBuildFile("test/standalone/mix_c_files/build.zig", .{
17 .build_modes = true,
18 .cross_targets = true,
19 });
2920 cases.addBuildFile("test/standalone/global_linkage/build.zig", .{});
3021 cases.addBuildFile("test/standalone/static_c_lib/build.zig", .{});
31 cases.addBuildFile("test/standalone/link_interdependent_static_c_libs/build.zig", .{});
32 cases.addBuildFile("test/standalone/link_static_lib_as_system_lib/build.zig", .{});
33 cases.addBuildFile("test/standalone/link_common_symbols/build.zig", .{});
34 cases.addBuildFile("test/standalone/link_frameworks/build.zig", .{
35 .requires_macos_sdk = true,
36 });
37 cases.addBuildFile("test/standalone/link_common_symbols_alignment/build.zig", .{});
38 if (builtin.os.tag == .macos) {
39 cases.addBuildFile("test/standalone/link_import_tls_dylib/build.zig", .{});
40 }
4122 cases.addBuildFile("test/standalone/issue_339/build.zig", .{});
4223 cases.addBuildFile("test/standalone/issue_8550/build.zig", .{});
4324 cases.addBuildFile("test/standalone/issue_794/build.zig", .{});
......@@ -69,16 +50,6 @@ pub fn addCases(cases: *tests.StandaloneContext) void {
6950 if (builtin.os.tag == .linux) {
7051 cases.addBuildFile("test/standalone/pie/build.zig", .{});
7152 }
72 // Try to build and run an Objective-C executable.
73 cases.addBuildFile("test/standalone/objc/build.zig", .{
74 .build_modes = true,
75 .requires_macos_sdk = true,
76 });
77 // Try to build and run an Objective-C++ executable.
78 cases.addBuildFile("test/standalone/objcpp/build.zig", .{
79 .build_modes = true,
80 .requires_macos_sdk = true,
81 });
8253
8354 // Ensure the development tools are buildable.
8455 cases.add("tools/gen_spirv_spec.zig");
test/standalone/link_common_symbols/a.c deleted-6
......@@ -1,6 +0,0 @@
1int i;
2int j;
3
4int add_to_i_and_j(int x) {
5 return x + i + j;
6}
test/standalone/link_common_symbols/b.c deleted-7
......@@ -1,7 +0,0 @@
1long i;
2int j = 2;
3int k;
4
5void incr_i() {
6 i++;
7}
test/standalone/link_common_symbols/build.zig deleted-16
......@@ -1,16 +0,0 @@
1const Builder = @import("std").build.Builder;
2
3pub fn build(b: *Builder) void {
4 const mode = b.standardReleaseOptions();
5
6 const lib_a = b.addStaticLibrary("a", null);
7 lib_a.addCSourceFiles(&.{ "c.c", "a.c", "b.c" }, &.{"-fcommon"});
8 lib_a.setBuildMode(mode);
9
10 const test_exe = b.addTest("main.zig");
11 test_exe.setBuildMode(mode);
12 test_exe.linkLibrary(lib_a);
13
14 const test_step = b.step("test", "Test it");
15 test_step.dependOn(&test_exe.step);
16}
test/standalone/link_common_symbols/c.c deleted-5
......@@ -1,5 +0,0 @@
1extern int k;
2
3int common_defined_externally() {
4 return k;
5}
test/standalone/link_common_symbols/main.zig deleted-16
......@@ -1,16 +0,0 @@
1const std = @import("std");
2const expect = std.testing.expect;
3
4extern fn common_defined_externally() c_int;
5extern fn incr_i() void;
6extern fn add_to_i_and_j(x: c_int) c_int;
7
8test "undef shadows common symbol: issue #9937" {
9 try expect(common_defined_externally() == 0);
10}
11
12test "import C common symbols" {
13 incr_i();
14 const res = add_to_i_and_j(2);
15 try expect(res == 5);
16}
test/standalone/link_common_symbols_alignment/a.c deleted-2
......@@ -1,2 +0,0 @@
1int foo;
2__attribute__((aligned(4096))) int bar;
test/standalone/link_common_symbols_alignment/build.zig deleted-16
......@@ -1,16 +0,0 @@
1const Builder = @import("std").build.Builder;
2
3pub fn build(b: *Builder) void {
4 const mode = b.standardReleaseOptions();
5
6 const lib_a = b.addStaticLibrary("a", null);
7 lib_a.addCSourceFiles(&.{"a.c"}, &.{"-fcommon"});
8 lib_a.setBuildMode(mode);
9
10 const test_exe = b.addTest("main.zig");
11 test_exe.setBuildMode(mode);
12 test_exe.linkLibrary(lib_a);
13
14 const test_step = b.step("test", "Test it");
15 test_step.dependOn(&test_exe.step);
16}
test/standalone/link_common_symbols_alignment/main.zig deleted-9
......@@ -1,9 +0,0 @@
1const std = @import("std");
2
3extern var foo: i32;
4extern var bar: i32;
5
6test {
7 try std.testing.expect(@ptrToInt(&foo) % 4 == 0);
8 try std.testing.expect(@ptrToInt(&bar) % 4096 == 0);
9}
test/standalone/link_frameworks/build.zig deleted-34
......@@ -1,34 +0,0 @@
1const std = @import("std");
2const Builder = std.build.Builder;
3const CrossTarget = std.zig.CrossTarget;
4
5fn isRunnableTarget(t: CrossTarget) bool {
6 // TODO I think we might be able to run this on Linux via Darling.
7 // Add a check for that here, and return true if Darling is available.
8 if (t.isNative() and t.getOsTag() == .macos)
9 return true
10 else
11 return false;
12}
13
14pub fn build(b: *Builder) void {
15 const mode = b.standardReleaseOptions();
16 const target = b.standardTargetOptions(.{});
17
18 const test_step = b.step("test", "Test the program");
19
20 const exe = b.addExecutable("test", null);
21 b.default_step.dependOn(&exe.step);
22 exe.addCSourceFile("main.c", &[0][]const u8{});
23 exe.setBuildMode(mode);
24 exe.setTarget(target);
25 exe.linkLibC();
26 // TODO when we figure out how to ship framework stubs for cross-compilation,
27 // populate paths to the sysroot here.
28 exe.linkFramework("Cocoa");
29
30 if (isRunnableTarget(target)) {
31 const run_cmd = exe.run();
32 test_step.dependOn(&run_cmd.step);
33 }
34}
test/standalone/link_frameworks/main.c deleted-7
......@@ -1,7 +0,0 @@
1#include <assert.h>
2#include <objc/runtime.h>
3
4int main() {
5 assert(objc_getClass("NSObject") > 0);
6 assert(objc_getClass("NSApplication") > 0);
7}
test/standalone/link_import_tls_dylib/a.c deleted-1
......@@ -1 +0,0 @@
1_Thread_local int a;
test/standalone/link_import_tls_dylib/build.zig deleted-16
......@@ -1,16 +0,0 @@
1const Builder = @import("std").build.Builder;
2
3pub fn build(b: *Builder) void {
4 const mode = b.standardReleaseOptions();
5
6 const lib = b.addSharedLibrary("a", null, b.version(1, 0, 0));
7 lib.setBuildMode(mode);
8 lib.addCSourceFile("a.c", &.{});
9
10 const test_exe = b.addTest("main.zig");
11 test_exe.setBuildMode(mode);
12 test_exe.linkLibrary(lib);
13
14 const test_step = b.step("test", "Test it");
15 test_step.dependOn(&test_exe.step);
16}
test/standalone/link_import_tls_dylib/main.zig deleted-7
......@@ -1,7 +0,0 @@
1const std = @import("std");
2
3extern threadlocal var a: i32;
4
5test {
6 try std.testing.expect(a == 0);
7}
test/standalone/link_interdependent_static_c_libs/a.c deleted-4
......@@ -1,4 +0,0 @@
1#include "a.h"
2int32_t add(int32_t a, int32_t b) {
3 return a + b;
4}
test/standalone/link_interdependent_static_c_libs/a.h deleted-2
......@@ -1,2 +0,0 @@
1#include <stdint.h>
2int32_t add(int32_t a, int32_t b);
test/standalone/link_interdependent_static_c_libs/b.c deleted-6
......@@ -1,6 +0,0 @@
1#include "a.h"
2#include "b.h"
3
4int32_t sub(int32_t a, int32_t b) {
5 return add(a, -1 * b);
6}
test/standalone/link_interdependent_static_c_libs/b.h deleted-2
......@@ -1,2 +0,0 @@
1#include <stdint.h>
2int32_t sub(int32_t a, int32_t b);
test/standalone/link_interdependent_static_c_libs/build.zig deleted-24
......@@ -1,24 +0,0 @@
1const Builder = @import("std").build.Builder;
2
3pub fn build(b: *Builder) void {
4 const mode = b.standardReleaseOptions();
5
6 const lib_a = b.addStaticLibrary("a", null);
7 lib_a.addCSourceFile("a.c", &[_][]const u8{});
8 lib_a.setBuildMode(mode);
9 lib_a.addIncludePath(".");
10
11 const lib_b = b.addStaticLibrary("b", null);
12 lib_b.addCSourceFile("b.c", &[_][]const u8{});
13 lib_b.setBuildMode(mode);
14 lib_b.addIncludePath(".");
15
16 const test_exe = b.addTest("main.zig");
17 test_exe.setBuildMode(mode);
18 test_exe.linkLibrary(lib_a);
19 test_exe.linkLibrary(lib_b);
20 test_exe.addIncludePath(".");
21
22 const test_step = b.step("test", "Test it");
23 test_step.dependOn(&test_exe.step);
24}
test/standalone/link_interdependent_static_c_libs/main.zig deleted-8
......@@ -1,8 +0,0 @@
1const std = @import("std");
2const expect = std.testing.expect;
3const c = @cImport(@cInclude("b.h"));
4
5test "import C sub" {
6 const result = c.sub(2, 1);
7 try expect(result == 1);
8}
test/standalone/link_static_lib_as_system_lib/a.c deleted-4
......@@ -1,4 +0,0 @@
1#include "a.h"
2int32_t add(int32_t a, int32_t b) {
3 return a + b;
4}
test/standalone/link_static_lib_as_system_lib/a.h deleted-2
......@@ -1,2 +0,0 @@
1#include <stdint.h>
2int32_t add(int32_t a, int32_t b);
test/standalone/link_static_lib_as_system_lib/build.zig deleted-23
......@@ -1,23 +0,0 @@
1const std = @import("std");
2const Builder = std.build.Builder;
3
4pub fn build(b: *Builder) void {
5 const mode = b.standardReleaseOptions();
6
7 const lib_a = b.addStaticLibrary("a", null);
8 lib_a.addCSourceFile("a.c", &[_][]const u8{});
9 lib_a.setBuildMode(mode);
10 lib_a.addIncludePath(".");
11 lib_a.install();
12
13 const test_exe = b.addTest("main.zig");
14 test_exe.setBuildMode(mode);
15 test_exe.linkSystemLibrary("a"); // force linking liba.a as -la
16 test_exe.addSystemIncludePath(".");
17 const search_path = std.fs.path.join(b.allocator, &[_][]const u8{ b.install_path, "lib" }) catch unreachable;
18 test_exe.addLibraryPath(search_path);
19
20 const test_step = b.step("test", "Test it");
21 test_step.dependOn(b.getInstallStep());
22 test_step.dependOn(&test_exe.step);
23}
test/standalone/link_static_lib_as_system_lib/main.zig deleted-8
......@@ -1,8 +0,0 @@
1const std = @import("std");
2const expect = std.testing.expect;
3const c = @cImport(@cInclude("a.h"));
4
5test "import C add" {
6 const result = c.add(2, 1);
7 try expect(result == 3);
8}
test/standalone/objc/Foo.h deleted-7
......@@ -1,7 +0,0 @@
1#import <Foundation/Foundation.h>
2
3@interface Foo : NSObject
4
5- (NSString *)name;
6
7@end
test/standalone/objc/Foo.m deleted-11
......@@ -1,11 +0,0 @@
1#import "Foo.h"
2
3@implementation Foo
4
5- (NSString *)name
6{
7 NSString *str = [[NSString alloc] initWithFormat:@"Zig"];
8 return str;
9}
10
11@end
test/standalone/objc/build.zig deleted-36
......@@ -1,36 +0,0 @@
1const std = @import("std");
2const Builder = std.build.Builder;
3const CrossTarget = std.zig.CrossTarget;
4
5fn isRunnableTarget(t: CrossTarget) bool {
6 // TODO I think we might be able to run this on Linux via Darling.
7 // Add a check for that here, and return true if Darling is available.
8 if (t.isNative() and t.getOsTag() == .macos)
9 return true
10 else
11 return false;
12}
13
14pub fn build(b: *Builder) void {
15 const mode = b.standardReleaseOptions();
16 const target = b.standardTargetOptions(.{});
17
18 const test_step = b.step("test", "Test the program");
19
20 const exe = b.addExecutable("test", null);
21 b.default_step.dependOn(&exe.step);
22 exe.addIncludePath(".");
23 exe.addCSourceFile("Foo.m", &[0][]const u8{});
24 exe.addCSourceFile("test.m", &[0][]const u8{});
25 exe.setBuildMode(mode);
26 exe.setTarget(target);
27 exe.linkLibC();
28 // TODO when we figure out how to ship framework stubs for cross-compilation,
29 // populate paths to the sysroot here.
30 exe.linkFramework("Foundation");
31
32 if (isRunnableTarget(target)) {
33 const run_cmd = exe.run();
34 test_step.dependOn(&run_cmd.step);
35 }
36}
test/standalone/objc/test.m deleted-12
......@@ -1,12 +0,0 @@
1#import "Foo.h"
2#import <assert.h>
3
4int main(int argc, char *argv[])
5{
6 @autoreleasepool {
7 Foo *foo = [[Foo alloc] init];
8 NSString *result = [foo name];
9 assert([result isEqualToString:@"Zig"]);
10 return 0;
11 }
12}
test/standalone/objcpp/Foo.h deleted-7
......@@ -1,7 +0,0 @@
1#import <Foundation/Foundation.h>
2
3@interface Foo : NSObject
4
5- (NSString *)name;
6
7@end
test/standalone/objcpp/Foo.mm deleted-11
......@@ -1,11 +0,0 @@
1#import "Foo.h"
2
3@implementation Foo
4
5- (NSString *)name
6{
7 NSString *str = [[NSString alloc] initWithFormat:@"Zig"];
8 return str;
9}
10
11@end
test/standalone/objcpp/build.zig deleted-36
......@@ -1,36 +0,0 @@
1const std = @import("std");
2const Builder = std.build.Builder;
3const CrossTarget = std.zig.CrossTarget;
4
5fn isRunnableTarget(t: CrossTarget) bool {
6 // TODO I think we might be able to run this on Linux via Darling.
7 // Add a check for that here, and return true if Darling is available.
8 if (t.isNative() and t.getOsTag() == .macos)
9 return true
10 else
11 return false;
12}
13
14pub fn build(b: *Builder) void {
15 const mode = b.standardReleaseOptions();
16 const target = b.standardTargetOptions(.{});
17
18 const test_step = b.step("test", "Test the program");
19
20 const exe = b.addExecutable("test", null);
21 b.default_step.dependOn(&exe.step);
22 exe.addIncludePath(".");
23 exe.addCSourceFile("Foo.mm", &[0][]const u8{});
24 exe.addCSourceFile("test.mm", &[0][]const u8{});
25 exe.setBuildMode(mode);
26 exe.setTarget(target);
27 exe.linkLibCpp();
28 // TODO when we figure out how to ship framework stubs for cross-compilation,
29 // populate paths to the sysroot here.
30 exe.linkFramework("Foundation");
31
32 if (isRunnableTarget(target)) {
33 const run_cmd = exe.run();
34 test_step.dependOn(&run_cmd.step);
35 }
36}
test/standalone/objcpp/test.mm deleted-14
......@@ -1,14 +0,0 @@
1#import "Foo.h"
2#import <assert.h>
3#include <iostream>
4
5int main(int argc, char *argv[])
6{
7 @autoreleasepool {
8 Foo *foo = [[Foo alloc] init];
9 NSString *result = [foo name];
10 std::cout << "Hello from C++ and " << [result UTF8String];
11 assert([result isEqualToString:@"Zig"]);
12 return 0;
13 }
14}
test/tests.zig+22
......@@ -21,6 +21,7 @@ const assemble_and_link = @import("assemble_and_link.zig");
2121const translate_c = @import("translate_c.zig");
2222const run_translated_c = @import("run_translated_c.zig");
2323const gen_h = @import("gen_h.zig");
24const link = @import("link.zig");
2425
2526// Implementations
2627pub const TranslateCContext = @import("src/translate_c.zig").TranslateCContext;
......@@ -479,6 +480,27 @@ pub fn addStandaloneTests(
479480 return cases.step;
480481}
481482
483pub fn addLinkTests(
484 b: *build.Builder,
485 test_filter: ?[]const u8,
486 modes: []const Mode,
487 enable_macos_sdk: bool,
488) *build.Step {
489 const cases = b.allocator.create(StandaloneContext) catch unreachable;
490 cases.* = StandaloneContext{
491 .b = b,
492 .step = b.step("test-link", "Run the linker tests"),
493 .test_index = 0,
494 .test_filter = test_filter,
495 .modes = modes,
496 .skip_non_native = true,
497 .enable_macos_sdk = enable_macos_sdk,
498 .target = .{},
499 };
500 link.addCases(cases);
501 return cases.step;
502}
503
482504pub fn addCliTests(b: *build.Builder, test_filter: ?[]const u8, modes: []const Mode) *build.Step {
483505 _ = test_filter;
484506 _ = modes;