forked from libnexpod/libnexpod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.zig
232 lines (212 loc) · 7.46 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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
const std = @import("std");
pub fn build(b: *std.Build) !void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{ .preferred_optimize_mode = .ReleaseSafe });
const clap = b.dependency("clap", .{
.target = target,
.optimize = optimize,
});
const zeit = b.dependency("zeit", .{
.target = target,
.optimize = optimize,
});
const log_module = b.addModule("logging", .{
.root_source_file = b.path("src/utils/logging.zig"),
.target = target,
.optimize = optimize,
});
const utils_module = b.addModule("utils", .{
.root_source_file = b.path("src/utils/utils.zig"),
.target = target,
.optimize = optimize,
});
// create shim only target for building
const shim_target = b.step("libnexpod-host-shim", "Only the host shim");
const shim = b.addExecutable(.{
.name = "libnexpod-host-shim",
.root_source_file = b.path("src/shim/shim.zig"),
.target = target,
.optimize = optimize,
});
shim_target.dependOn(&b.addInstallArtifact(shim, .{
.dest_dir = .{
.override = .{
.custom = "libexec/libnexpod/",
},
},
}).step);
b.getInstallStep().dependOn(shim_target);
// create lib only target for building
const lib = b.addModule("libnexpod", .{
.root_source_file = b.path("src/lib/lib.zig"),
.target = target,
.optimize = optimize,
});
const lib_modules = [_]Module{
.{
.name = "logging",
.module = log_module,
},
.{
.name = "utils",
.module = utils_module,
},
.{
.name = "zeit",
.module = zeit.module("zeit"),
},
};
addModules(lib, &lib_modules);
// create daemon only target for building
const daemon_target = b.step("libnexpodd", "Only the daemon");
const daemon = b.addExecutable(.{
.name = "libnexpodd",
.root_source_file = b.path("src/daemon/daemon.zig"),
.target = target,
.optimize = optimize,
});
const daemon_modules = [_]Module{
.{
.name = "clap",
.module = clap.module("clap"),
},
.{
.name = "logging",
.module = log_module,
},
.{
.name = "utils",
.module = utils_module,
},
};
addModules(&daemon.root_module, &daemon_modules);
daemon_target.dependOn(&b.addInstallArtifact(daemon, .{
.dest_dir = .{
.override = .{
.custom = "libexec/libnexpod/",
},
},
}).step);
b.getInstallStep().dependOn(daemon_target);
// tests
const test_step = b.step("test", "Run all tests");
// unit tests
const unittest_step = b.step("unittests", "Run unit tests");
test_step.dependOn(unittest_step);
// for the utils
try addTestCases(b, test_step, "src/utils", &[_]Module{}, &target, &optimize, true);
// for lib
const lib_unit_tests = b.step("libunittests", "Run only the unit tests for the library");
try addTestCases(b, lib_unit_tests, "src/lib", &lib_modules, &target, &optimize, true);
unittest_step.dependOn(lib_unit_tests);
// for shim
const shim_unit_tests = b.step("shimunittests", "Run only the unit tests of the shim");
try addTestCases(b, shim_unit_tests, "src/shim", &[_]Module{}, &target, &optimize, false);
unittest_step.dependOn(shim_unit_tests);
// for daemon
const daemon_unit_tests = b.step("daemonunittests", "Run only the unit tests of the daemon");
try addTestCases(b, daemon_unit_tests, "src/daemon", &daemon_modules, &target, &optimize, false);
unittest_step.dependOn(daemon_unit_tests);
// system tests
const systemtest_step = b.step("systemtests", "Run system tests");
test_step.dependOn(systemtest_step);
try addSystemTests(b, .{
.root_case = systemtest_step,
.dir_path = "tests",
.modules = &[_]Module{.{
.name = "libnexpod",
.module = lib,
}},
.target = &target,
.optimize = &optimize,
.libc = false,
.daemon = daemon,
});
const docs = b.step("docs", "generate documentation");
{
const lib_doc_helper = b.addObject(.{
.name = "lib",
.root_source_file = b.path("src/lib/lib.zig"),
.target = target,
.optimize = optimize,
});
const lib_docs = lib_doc_helper.getEmittedDocs();
docs.dependOn(&b.addInstallDirectory(.{
.source_dir = lib_docs,
.install_dir = .prefix,
.install_subdir = "libnexpod/docs",
}).step);
}
}
fn addSystemTests(b: *std.Build, args: struct {
root_case: *std.Build.Step,
dir_path: []const u8,
modules: []const Module,
target: *const std.Build.ResolvedTarget,
optimize: *const std.builtin.OptimizeMode,
libc: bool,
daemon: *std.Build.Step.Compile,
}) !void {
const setup_check_build = b.addExecutable(.{
.name = "setup_check",
.root_source_file = b.path("tests/list-images.zig"),
.target = b.host,
.optimize = .ReleaseSafe,
});
addModules(&setup_check_build.root_module, args.modules);
const setup_check = b.addRunArtifact(setup_check_build);
var dir = try b.build_root.handle.openDir(args.dir_path, .{ .iterate = true });
defer dir.close();
var iter = dir.iterate();
while (try iter.next()) |entry| {
if (!std.mem.eql(u8, ".zig", std.fs.path.extension(entry.name))) {
continue;
}
const path = try std.mem.concat(b.allocator, u8, &[_][]const u8{ args.dir_path, "/", entry.name });
const test_case = b.addExecutable(.{
.name = entry.name,
.root_source_file = b.path(path),
.optimize = args.optimize.*,
.target = args.target.*,
});
if (args.libc) {
test_case.linkLibC();
}
addModules(&test_case.root_module, args.modules);
test_case.step.dependOn(&setup_check.step);
var run_test_case = b.addRunArtifact(test_case);
run_test_case.addFileArg(args.daemon.getEmittedBin());
args.root_case.dependOn(&run_test_case.step);
}
}
fn addTestCases(b: *std.Build, root_case: *std.Build.Step, dir_path: []const u8, modules: []const Module, target: *const std.Build.ResolvedTarget, optimize: *const std.builtin.OptimizeMode, libc: bool) !void {
var dir = try b.build_root.handle.openDir(dir_path, .{ .iterate = true });
defer dir.close();
var iter = dir.iterate();
while (try iter.next()) |entry| {
if (!std.mem.eql(u8, ".zig", std.fs.path.extension(entry.name))) {
continue;
}
const path = try std.mem.concat(b.allocator, u8, &[_][]const u8{ dir_path, "/", entry.name });
const test_case = b.addTest(.{
.root_source_file = b.path(path),
.target = target.*,
.optimize = optimize.*,
});
if (libc) {
test_case.linkLibC();
}
addModules(&test_case.root_module, modules);
const run_test_case = b.addRunArtifact(test_case);
root_case.dependOn(&run_test_case.step);
}
}
const Module = struct {
name: []const u8,
module: *std.Build.Module,
};
fn addModules(node: *std.Build.Module, modules: []const Module) void {
for (modules) |mod| {
node.addImport(mod.name, mod.module);
}
}