-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.zig
141 lines (118 loc) · 4.24 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
const std = @import("std");
const VERSION: std.SemanticVersion = .{
.major = 0,
.minor = 2,
.patch = 9,
};
pub fn addTracy(b: *std.Build, step: *std.Build.Step.Compile) !void {
step.want_lto = false;
step.addCSourceFile(
.{
.file = b.path("../tracy/public/TracyClient.cpp"),
.flags = &.{
"-DTRACY_ENABLE=1",
"-fno-sanitize=undefined",
},
},
);
step.linkLibCpp();
}
fn getGitHash(allocator: std.mem.Allocator) ?[]const u8 {
var proc = std.process.Child.init(&.{ "git", "rev-parse", "HEAD" }, allocator);
proc.stdout_behavior = .Pipe;
_ = proc.spawn() catch return null;
const hash = proc.stdout.?.readToEndAlloc(allocator, 1024) catch return null;
errdefer allocator.free(hash);
_ = proc.wait() catch return null;
return std.mem.trim(u8, hash, "\n \t");
}
pub fn build(b: *std.Build) !void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const coverage = b.option(bool, "coverage", "Generate test coverage") orelse false;
const tracy = b.option(bool, "tracy", "Compile against tracy client") orelse false;
var opts = b.addOptions();
opts.addOption(bool, "tracy_enabled", tracy);
opts.addOption(std.SemanticVersion, "version", VERSION);
const hash = getGitHash(b.allocator);
defer if (hash) |h| b.allocator.free(h);
opts.addOption(?[]const u8, "git_hash", hash);
const time = b.dependency("time", .{
.target = target,
.optimize = optimize,
}).module("zig-datetime");
const farbe = b.dependency("farbe", .{
.target = target,
.optimize = optimize,
}).module("farbe");
const chrono = b.dependency("chrono", .{
.target = target,
.optimize = optimize,
}).module("chrono");
const clippy = b.dependency("clippy", .{
.target = target,
.optimize = optimize,
}).module("clippy");
const termui = b.dependency("termui", .{
.target = target,
.optimize = optimize,
}).module("termui");
const fuzzig = b.dependency("fuzzig", .{
.target = target,
.optimize = optimize,
}).module("fuzzig");
const exe = b.addExecutable(.{
.name = "nkt",
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});
exe.root_module.addImport("time", time);
exe.root_module.addImport("farbe", farbe);
exe.root_module.addImport("chrono", chrono);
exe.root_module.addImport("clippy", clippy);
exe.root_module.addImport("termui", termui);
exe.root_module.addImport("fuzzig", fuzzig);
exe.root_module.addImport("options", opts.createModule());
if (tracy) {
try addTracy(b, exe);
}
b.installArtifact(exe);
const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args);
}
const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);
const unit_tests = b.addTest(.{
.name = "test-nkt",
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});
unit_tests.root_module.addImport("time", time);
unit_tests.root_module.addImport("farbe", farbe);
unit_tests.root_module.addImport("chrono", chrono);
unit_tests.root_module.addImport("clippy", clippy);
unit_tests.root_module.addImport("termui", termui);
unit_tests.root_module.addImport("fuzzig", fuzzig);
unit_tests.root_module.addImport("options", opts.createModule());
const run_unit_tests = b.addRunArtifact(unit_tests);
const kcov = b.addSystemCommand(&.{ "kcov", "--include-path", ".", "kcov-out" });
kcov.addArtifactArg(unit_tests);
const test_step = b.step("test", "Run unit tests");
if (coverage) {
test_step.dependOn(&kcov.step);
} else {
test_step.dependOn(&run_unit_tests.step);
}
//Build step to generate docs:
const docs_step = b.step("docs", "Generate docs");
const docs = exe.getEmittedDocs();
docs_step.dependOn(&b.addInstallDirectory(.{
.source_dir = docs,
.install_dir = .prefix,
.install_subdir = "docs",
}).step);
}