master
 1const std = @import("std");
 2const builtin = @import("builtin");
 3
 4pub fn build(b: *std.Build) void {
 5    const test_step = b.step("test", "Test it");
 6    b.default_step = test_step;
 7
 8    add(b, test_step, "test_c_Debug", "test_cpp_Debug", .Debug);
 9    add(b, test_step, "test_c_ReleaseFast", "test_cpp_ReleaseFast", .ReleaseFast);
10    add(b, test_step, "test_c_ReleaseSmall", "test_cpp_ReleaseSmall", .ReleaseSmall);
11    add(b, test_step, "test_c_ReleaseSafe", "test_cpp_ReleaseSafe", .ReleaseSafe);
12}
13
14fn add(
15    b: *std.Build,
16    test_step: *std.Build.Step,
17    c_name: []const u8,
18    cpp_name: []const u8,
19    optimize: std.builtin.OptimizeMode,
20) void {
21    const target = b.graph.host;
22
23    const c_mod = b.createModule(.{
24        .optimize = optimize,
25        .target = target,
26    });
27    c_mod.addCSourceFile(.{ .file = b.path("test.c"), .flags = &[0][]const u8{} });
28    c_mod.link_libc = true;
29
30    const exe_c = b.addExecutable(.{ .name = c_name, .root_module = c_mod });
31
32    const cpp_mod = b.createModule(.{
33        .optimize = optimize,
34        .target = target,
35    });
36    cpp_mod.addCSourceFile(.{ .file = b.path("test.cpp"), .flags = &[0][]const u8{} });
37    cpp_mod.link_libcpp = true;
38
39    const exe_cpp = b.addExecutable(.{ .name = cpp_name, .root_module = cpp_mod });
40
41    b.default_step.dependOn(&exe_cpp.step);
42
43    switch (target.result.os.tag) {
44        .windows => {
45            // https://github.com/ziglang/zig/issues/8531
46            exe_cpp.lto = .none;
47        },
48        .macos => {
49            // https://github.com/ziglang/zig/issues/8680
50            exe_cpp.lto = .none;
51            exe_c.lto = .none;
52        },
53        else => {},
54    }
55
56    const run_c_cmd = b.addRunArtifact(exe_c);
57    run_c_cmd.expectExitCode(0);
58    run_c_cmd.skip_foreign_checks = true;
59    test_step.dependOn(&run_c_cmd.step);
60
61    const run_cpp_cmd = b.addRunArtifact(exe_cpp);
62    run_cpp_cmd.expectExitCode(0);
63    run_cpp_cmd.skip_foreign_checks = true;
64    test_step.dependOn(&run_cpp_cmd.step);
65}