-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.zig
95 lines (74 loc) · 2.8 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
const std = @import("std");
pub fn build(b: *std.Build) !void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
// Dependencies
const network = b.dependency("network", .{});
// Steps
const install_step = b.getInstallStep();
const run_step = b.step("run", "Run the app");
const test_step = b.step("test", "Run unit tests");
// Build
const exe = b.addExecutable(.{
.name = "prc",
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});
linkDependencies(exe, .{ .network = network });
install_step.dependOn(&b.addInstallArtifact(exe, .{}).step);
// Run
const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(install_step);
if (b.args) |args| {
run_cmd.addArgs(args);
}
run_step.dependOn(&run_cmd.step);
// Test
const tests = try listTestFiles(b, b.allocator, "src");
for (tests) |test_path| {
const exe_test = b.addTest(.{
.root_source_file = test_path,
.target = target,
.optimize = optimize,
});
linkDependencies(exe_test, .{ .network = network });
test_step.dependOn(&b.addRunArtifact(exe_test).step);
}
}
pub fn linkDependencies(exe: *std.Build.Step.Compile, deps: anytype) void {
exe.linkLibC();
const info = @typeInfo(@TypeOf(deps));
if (info != .Struct) {
@compileError("deps must be a struct");
}
inline for (info.Struct.fields) |f| {
if (f.type != *std.Build.Dependency) {
@compileError("Invalid dependency type");
}
const mod_name = f.name;
const mod = @field(deps, mod_name);
exe.root_module.addImport(mod_name, mod.module(mod_name));
}
}
pub fn listTestFiles(b: *std.Build, allocator: std.mem.Allocator, sub_path: []const u8) ![]std.Build.LazyPath {
const trimmed_sub_path = std.mem.trimRight(u8, sub_path, std.fs.path.sep_str); // In case of trailing slash
var dir = try std.fs.cwd().openDir(trimmed_sub_path, .{ .iterate = true });
defer dir.close();
var walker = try dir.walk(allocator);
defer walker.deinit();
var test_files = std.ArrayList(std.Build.LazyPath).init(allocator);
while (true) {
if (walker.next()) |entry| {
if (entry == null) break;
const e = entry.?;
if (e.kind == .file) {
if (std.mem.endsWith(u8, e.basename, ".test.zig")) {
const relative_path = try std.fmt.allocPrint(allocator, "{s}" ++ std.fs.path.sep_str ++ "{s}", .{ trimmed_sub_path, e.path });
try test_files.append(b.path(relative_path));
}
}
} else |_| {}
}
return test_files.toOwnedSlice() catch unreachable;
}