-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.zig
More file actions
152 lines (134 loc) · 5.06 KB
/
build.zig
File metadata and controls
152 lines (134 loc) · 5.06 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
const std = @import("std");
const zcc = @import("compile_commands");
const Executable = struct {
name: []const u8,
cpp_sources: []const []const u8,
include_paths: []const []const u8,
fn init(name: []const u8, cpp_sources: []const []const u8, include_paths: []const []const u8) Executable {
return Executable{
.name = name,
.cpp_sources = cpp_sources,
.include_paths = include_paths,
};
}
fn build(self: Executable, b: *std.Build, optimize: std.builtin.OptimizeMode, target: std.Build.ResolvedTarget, targets: *std.ArrayList(*std.Build.Step.Compile)) void {
const exe = b.addExecutable(.{ .name = self.name, .root_module = b.createModule(.{
.target = target,
.optimize = optimize,
}) });
for (self.cpp_sources) |source| {
exe.addCSourceFile(.{
.file = b.path(source),
.flags = &[_][]const u8{"-std=c++20"},
});
}
exe.linkLibCpp();
for (self.include_paths) |include_path| {
exe.addIncludePath(b.path(include_path));
}
b.installArtifact(exe);
targets.append(b.allocator, exe) catch @panic("OOM");
const run_cmd_name = std.fmt.allocPrint(b.allocator, "run-{s}", .{self.name}) catch @panic("OOM");
const run_cmd_info = std.fmt.allocPrint(b.allocator, "Run the {s} app", .{self.name}) catch @panic("OOM");
const run_step = b.step(run_cmd_name, run_cmd_info);
const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args);
}
run_step.dependOn(&run_cmd.step);
}
};
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
var targets: std.ArrayList(*std.Build.Step.Compile) = .empty;
defer targets.deinit(b.allocator);
Executable.init("wc", &[_][]const u8{
"src/wc/main.cpp",
"src/wc/wc.cpp",
"src/wc/options.cpp",
"src/wc/params.cpp",
}, &[_][]const u8{
"src/wc",
}).build(b, optimize, target, &targets);
Executable.init("true", &[_][]const u8{
"src/true/main.cpp",
}, &[_][]const u8{
"src/true",
}).build(b, optimize, target, &targets);
Executable.init("false", &[_][]const u8{
"src/false/main.cpp",
}, &[_][]const u8{
"src/false",
}).build(b, optimize, target, &targets);
const googletest = b.dependency("googletest", .{
.target = target,
.optimize = optimize,
});
const gtest = b.addLibrary(.{ .name = "gtest", .linkage = .static, .root_module = b.createModule(.{
.target = target,
.optimize = optimize,
}) });
gtest.linkLibCpp();
gtest.addCSourceFiles(
.{
.root = googletest.path("googletest/src"),
.files = &.{
"gtest-all.cc",
"gtest-assertion-result.cc",
"gtest-death-test.cc",
"gtest-filepath.cc",
"gtest-matchers.cc",
"gtest-port.cc",
"gtest-printers.cc",
"gtest-test-part.cc",
"gtest-typed-test.cc",
"gtest.cc",
"gtest_main.cc",
},
.flags = &.{
"-std=c++17",
},
},
);
targets.append(b.allocator, gtest) catch @panic("OOM");
gtest.addIncludePath(googletest.path("googletest"));
gtest.addIncludePath(googletest.path("googletest/src"));
gtest.addIncludePath(googletest.path("googletest/include"));
gtest.addIncludePath(googletest.path("googletest/include/internal"));
gtest.addIncludePath(googletest.path("googletest/include/internal/custom"));
// Build test executables
const options_test = b.addExecutable(.{ .name = "option_test", .root_module = b.createModule(.{
.target = target,
.optimize = optimize,
}) });
const options_test_cpp_sources = [_][]const u8{
"tests/wc/options.test.cpp",
"src/wc/options.cpp",
"src/wc/params.cpp",
};
for (options_test_cpp_sources) |source| {
options_test.addCSourceFile(.{
.file = b.path(source),
.flags = &[_][]const u8{"-std=c++20"},
});
}
options_test.linkLibCpp();
options_test.linkLibrary(gtest);
options_test.addIncludePath(b.path("src/wc"));
options_test.addIncludePath(googletest.path("googletest/include"));
b.installDirectory(
.{
.source_dir = googletest.path("googletest/include"),
.install_dir = .header,
.install_subdir = "tests",
},
);
b.installArtifact(options_test);
const run_test = b.addRunArtifact(options_test);
const run_test_step = b.step("test", "Run the tests");
run_test_step.dependOn(&run_test.step);
targets.append(b.allocator, options_test) catch @panic("OOM");
_ = zcc.createStep(b, "cdb", targets.toOwnedSlice(b.allocator) catch @panic("OOM"));
}