| author | |
| committer | |
| log | 353c19468df02db925a7b147cabb398d7e63de68 |
| tree | 44a88892e011455e4e728bd2d8e265b4977b9f31 |
| parent | 33889e1974137a54d1a88b87f57462de2a7cc145 |
based on https://github.com/ziglang/zig/issues/85314 files changed, 78 insertions(+), 0 deletions(-)
test/standalone.zig+6| ... | ... | @@ -13,6 +13,12 @@ pub fn addCases(cases: *tests.StandaloneContext) void { |
| 13 | 13 | cases.addBuildFile("test/standalone/main_pkg_path/build.zig", .{}); |
| 14 | 14 | cases.addBuildFile("test/standalone/shared_library/build.zig", .{}); |
| 15 | 15 | cases.addBuildFile("test/standalone/mix_o_files/build.zig", .{}); |
| 16 | if (std.Target.current.os.tag == .macos) { | |
| 17 | // TODO zld cannot link llvm-ir object files for LTO yet. | |
| 18 | cases.addBuildFile("test/standalone/mix_c_files/build.zig", .{ .build_modes = false, .cross_targets = true }); | |
| 19 | } else { | |
| 20 | cases.addBuildFile("test/standalone/mix_c_files/build.zig", .{ .build_modes = true, .cross_targets = true }); | |
| 21 | } | |
| 16 | 22 | cases.addBuildFile("test/standalone/global_linkage/build.zig", .{}); |
| 17 | 23 | cases.addBuildFile("test/standalone/static_c_lib/build.zig", .{}); |
| 18 | 24 | cases.addBuildFile("test/standalone/link_interdependent_static_c_libs/build.zig", .{}); |
test/standalone/mix_c_files/build.zig created+34| ... | ... | @@ -0,0 +1,34 @@ |
| 1 | const std = @import("std"); | |
| 2 | const Builder = std.build.Builder; | |
| 3 | const CrossTarget = std.zig.CrossTarget; | |
| 4 | ||
| 5 | fn isUnpecifiedTarget(t: CrossTarget) bool { | |
| 6 | return t.cpu_arch == null and t.abi == null and t.os_tag == null; | |
| 7 | } | |
| 8 | fn isRunnableTarget(t: CrossTarget) bool { | |
| 9 | if (t.isNative()) return true; | |
| 10 | ||
| 11 | return (t.getOsTag() == std.Target.current.os.tag and | |
| 12 | t.getCpuArch() == std.Target.current.cpu.arch); | |
| 13 | } | |
| 14 | ||
| 15 | pub fn build(b: *Builder) void { | |
| 16 | const mode = b.standardReleaseOptions(); | |
| 17 | const target = b.standardTargetOptions(.{}); | |
| 18 | ||
| 19 | const test_step = b.step("test", "Test the program"); | |
| 20 | ||
| 21 | const exe = b.addExecutable("test", "main.zig"); | |
| 22 | exe.addCSourceFile("test.c", &[_][]const u8{"-std=c11"}); | |
| 23 | exe.setBuildMode(mode); | |
| 24 | exe.linkLibC(); | |
| 25 | exe.setTarget(target); | |
| 26 | b.default_step.dependOn(&exe.step); | |
| 27 | ||
| 28 | if (isRunnableTarget(target)) { | |
| 29 | const run_cmd = exe.run(); | |
| 30 | test_step.dependOn(&run_cmd.step); | |
| 31 | } else { | |
| 32 | test_step.dependOn(&exe.step); | |
| 33 | } | |
| 34 | } |
test/standalone/mix_c_files/main.zig created+30| ... | ... | @@ -0,0 +1,30 @@ |
| 1 | const std = @import("std"); | |
| 2 | ||
| 3 | extern fn add_C(x: i32) i32; | |
| 4 | extern fn add_C_zig(x: i32) i32; | |
| 5 | extern threadlocal var C_k: c_int; | |
| 6 | ||
| 7 | export var zig_k: c_int = 1; | |
| 8 | export fn add_zig(x: i32) i32 { | |
| 9 | return x + zig_k + C_k; | |
| 10 | } | |
| 11 | export fn add_may_panic(x: i32) i32 { | |
| 12 | if (x < 0) @panic("negative int"); | |
| 13 | return x + zig_k; | |
| 14 | } | |
| 15 | ||
| 16 | pub fn main() anyerror!void { | |
| 17 | var x: i32 = 0; | |
| 18 | x = add_zig(x); | |
| 19 | x = add_C(x); | |
| 20 | x = add_C_zig(x); | |
| 21 | ||
| 22 | C_k = 200; | |
| 23 | zig_k = 2; | |
| 24 | x = add_zig(x); | |
| 25 | x = add_C(x); | |
| 26 | x = add_C_zig(x); | |
| 27 | ||
| 28 | const u = @intCast(u32, x); | |
| 29 | try std.testing.expect(u / 100 == u % 100); | |
| 30 | } |
test/standalone/mix_c_files/test.c created+8| ... | ... | @@ -0,0 +1,8 @@ |
| 1 | ||
| 2 | extern int zig_k; | |
| 3 | extern int add_may_panic(int); | |
| 4 | ||
| 5 | _Thread_local int C_k = 100; | |
| 6 | int unused(int x) { return x*x; } | |
| 7 | int add_C(int x) { return x+zig_k+C_k; } | |
| 8 | int add_C_zig(int x) { return add_may_panic(x) + C_k; } |