-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.zig
99 lines (85 loc) · 2.91 KB
/
build.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
const std = @import("std");
const builtin = @import("builtin");
const zcc = @import("compile_commands");
const release_flags = &[_][]const u8{
"-DNDEBUG",
"-std=c++17",
};
const debug_flags = &[_][]const u8{
"-g",
"-std=c++17",
"-DFMT_EXCEPTIONS=1",
};
// effectively universal flags since the only thing we compile is tests
const testing_flags = &[_][]const u8{
"-DZIGLIKE_HEADER_TESTING",
"-DZIGLIKE_USE_FMT",
"-DZIGLIKE_NOEXCEPT=", // allow exceptions in testing mode
"-DFMT_HEADER_ONLY",
"-I./tests/",
"-I./include/",
};
const test_source_files = &[_][]const u8{
"opt/opt.cpp",
"res/res.cpp",
"slice/slice.cpp",
"joined_slice/joined_slice.cpp",
"factory/factory.cpp",
"status/status.cpp",
"defer/defer.cpp",
"stdmem/stdmem.cpp",
"enumerate/enumerate.cpp",
};
pub fn build(b: *std.Build) !void {
// options
const target = b.standardTargetOptions(.{});
const mode = b.standardOptimizeOption(.{});
var flags = std.ArrayList([]const u8).init(b.allocator);
defer flags.deinit();
try flags.appendSlice(if (mode == .Debug) debug_flags else release_flags);
try flags.appendSlice(testing_flags);
var tests = std.ArrayList(*std.Build.Step.Compile).init(b.allocator);
defer tests.deinit();
// actual public installation step
b.installDirectory(.{
.source_dir = .{ .src_path = .{
.owner = b,
.sub_path = "include/ziglike/",
} },
.install_dir = .header,
.install_subdir = "ziglike/",
});
const fmt = b.dependency("fmt", .{});
const fmt_include_path = b.pathJoin(&.{ fmt.builder.install_path, "include" });
try flags.append(b.fmt("-I{s}", .{fmt_include_path}));
const flags_owned = flags.toOwnedSlice() catch @panic("OOM");
for (test_source_files) |source_file| {
var test_exe = b.addExecutable(.{
.name = std.fs.path.stem(source_file),
.optimize = mode,
.target = target,
});
test_exe.addCSourceFile(.{
.file = .{ .src_path = .{
.owner = b,
.sub_path = b.pathJoin(&.{ "tests", source_file }),
} },
.flags = flags_owned,
});
test_exe.linkLibCpp();
test_exe.step.dependOn(fmt.builder.getInstallStep());
try tests.append(test_exe);
}
const run_tests_step = b.step("run_tests", "Compile and run all the tests");
const install_tests_step = b.step("install_tests", "Install all the tests but don't run them");
for (tests.items) |test_exe| {
const test_install = b.addInstallArtifact(test_exe, .{});
install_tests_step.dependOn(&test_install.step);
const test_run = b.addRunArtifact(test_exe);
if (b.args) |args| {
test_run.addArgs(args);
}
run_tests_step.dependOn(&test_run.step);
}
zcc.createStep(b, "cdb", try tests.toOwnedSlice());
}