-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
build.zig
215 lines (191 loc) · 6.14 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
const std = @import("std");
const Build = std.Build;
pub fn build(b: *Build) !void {
const optimize = b.standardOptimizeOption(.{});
const target = b.standardTargetOptions(.{});
var all_tests = std.ArrayList(*Build.Step).init(b.allocator);
try addModules(b, target, &all_tests);
try buildBinaries(b, optimize, target, &all_tests);
try buildExamples(b, optimize, target, &all_tests);
const test_all_step = b.step("test", "Run all tests");
for (all_tests.items) |step| {
test_all_step.dependOn(step);
}
}
const Source = union(enum) {
bin: []const u8,
mod: []const u8,
ex: []const u8,
const Self = @This();
fn name(self: Self) []const u8 {
return switch (self) {
.bin, .mod, .ex => |v| v,
};
}
fn path(self: Self) []const u8 {
return switch (self) {
.bin => |_| "src/bin",
.mod => |_| "src/mod",
.ex => |_| "examples",
};
}
fn need_test(self: Self) bool {
return switch (self) {
.bin, .mod => true,
.ex => false,
};
}
};
fn addModules(
b: *std.Build,
target: std.Build.ResolvedTarget,
all_tests: *std.ArrayList(*Build.Step),
) !void {
inline for (.{ "pretty-table", "simargs" }) |name| {
_ = b.addModule(name, .{
.root_source_file = b.path("src/mod/" ++ name ++ ".zig"),
});
try all_tests.append(buildTestStep(b, .{ .mod = name }, target));
}
const opt = b.addOptions();
opt.addOption(
[]const u8,
"build_date",
b.option([]const u8, "build_date", "Build date") orelse
b.fmt("{d}", .{std.time.milliTimestamp()}),
);
opt.addOption(
[]const u8,
"git_commit",
b.option([]const u8, "git_commit", "Git commit") orelse
"Unknown",
);
try b.modules.put("build_info", opt.createModule());
}
fn buildExamples(
b: *std.Build,
optimize: std.builtin.Mode,
target: std.Build.ResolvedTarget,
all_tests: *std.ArrayList(*Build.Step),
) !void {
inline for (.{
"simargs-demo",
"pretty-table-demo",
}) |name| {
try buildBinary(b, .{ .ex = name }, optimize, target, false, all_tests);
}
}
fn buildBinaries(
b: *std.Build,
optimize: std.builtin.Mode,
target: std.Build.ResolvedTarget,
all_tests: *std.ArrayList(*Build.Step),
) !void {
const is_ci = b.option(bool, "is_ci", "Build in CI") orelse false;
inline for (.{
"tree",
"loc",
"pidof",
"yes",
"night-shift",
"dark-mode",
"repeat",
"tcp-proxy",
}) |name| {
try buildBinary(b, .{ .bin = name }, optimize, target, is_ci, all_tests);
}
// TODO: move util out of `bin`
try all_tests.append(buildTestStep(b, .{ .bin = "util" }, target));
}
fn buildBinary(
b: *std.Build,
comptime source: Source,
optimize: std.builtin.Mode,
target: std.Build.ResolvedTarget,
is_ci: bool,
all_tests: *std.ArrayList(*Build.Step),
) !void {
if (makeCompileStep(b, source, optimize, target, is_ci)) |exe| {
var deps = b.modules.iterator();
while (deps.next()) |dep| {
exe.root_module.addImport(dep.key_ptr.*, dep.value_ptr.*);
}
b.installArtifact(exe);
const run_cmd = b.addRunArtifact(exe);
if (b.args) |args| {
run_cmd.addArgs(args);
}
const prog_name = comptime source.name();
b.step("run-" ++ prog_name, "Run " ++ prog_name)
.dependOn(&run_cmd.step);
if (source.need_test()) {
try all_tests.append(buildTestStep(b, source, target));
}
}
}
fn buildTestStep(
b: *std.Build,
comptime source: Source,
target: std.Build.ResolvedTarget,
) *Build.Step {
const name = comptime source.name();
const path = comptime source.path();
const exe_tests = b.addTest(.{
.root_source_file = b.path(path ++ "/" ++ name ++ ".zig"),
.target = target,
});
const test_step = b.step("test-" ++ name, "Run " ++ name ++ " tests");
// https://github.com/ziglang/zig/issues/15009#issuecomment-1475350701
test_step.dependOn(&b.addRunArtifact(exe_tests).step);
return test_step;
}
fn makeCompileStep(
b: *std.Build,
comptime source: Source,
optimize: std.builtin.Mode,
target: std.Build.ResolvedTarget,
is_ci: bool,
) ?*Build.Step.Compile {
const name = comptime source.name();
const path = comptime source.path();
const is_darwin = target.result.isDarwin();
if (std.mem.eql(u8, name, "night-shift") or std.mem.eql(u8, name, "dark-mode")) {
// if (target.getOsTag() != .macos) {
if (is_ci) {
// zig build -Dtarget=aarch64-macos will throw error
// error: warning(link): library not found for '-lobjc'
// warning(link): Library search paths:
// warning(link): framework not found for '-framework CoreBrightness'
// warning(link): Framework search paths:
// warning(link): /System/Library/PrivateFrameworks
// so disable this in CI environment.
return null;
}
}
const exe = b.addExecutable(.{
.name = name,
.root_source_file = b.path(path ++ "/" ++ name ++ ".zig"),
.target = target,
.optimize = optimize,
});
if (std.mem.eql(u8, name, "night-shift")) {
exe.linkSystemLibrary("objc");
exe.addFrameworkPath(.{ .cwd_relative = "/System/Library/PrivateFrameworks" });
exe.linkFramework("CoreBrightness");
} else if (std.mem.eql(u8, name, "dark-mode")) {
exe.addFrameworkPath(.{ .cwd_relative = "/System/Library/PrivateFrameworks" });
exe.linkFramework("SkyLight");
} else if (std.mem.eql(u8, name, "tcp-proxy")) {
exe.linkLibC();
} else if (std.mem.eql(u8, name, "pidof")) {
// only build for macOS
if (is_darwin) {
exe.linkLibC();
} else {
return null;
}
}
const install_step = b.step("install-" ++ name, "Install " ++ name);
install_step.dependOn(&b.addInstallArtifact(exe, .{}).step);
return exe;
}