/
build.zig
88 lines (79 loc) · 2.95 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
const std = @import("std");
const Build = std.Build;
const FileSource = Build.FileSource;
pub fn build(b: *Build) void {
const optimize = b.standardOptimizeOption(.{});
const target = b.standardTargetOptions(.{});
const simargs_dep = b.dependency("simargs", .{});
const table_dep = b.dependency("table-helper", .{});
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");
b.modules.put("build_info", opt.createModule()) catch @panic("OOM");
b.modules.put("simargs", simargs_dep.module("simargs")) catch @panic("OOM");
b.modules.put("table-helper", table_dep.module("table-helper")) catch @panic("OOM");
var all_tests = std.ArrayList(*Build.Step).init(b.allocator);
inline for (.{
.{
buildCli(b, "tree", optimize, target),
"tree",
},
.{
buildCli(b, "loc", optimize, target),
"loc",
},
.{
buildCli(b, "pidof", optimize, target),
"pidof",
},
.{
buildCli(b, "yes", optimize, target),
"yes",
},
}) |prog| {
if (prog.@"0") |exe| {
var deps = b.modules.iterator();
while (deps.next()) |dep| {
exe.addModule(dep.key_ptr.*, dep.value_ptr.*);
}
const name = prog.@"1";
b.installArtifact(exe);
const run_cmd = b.addRunArtifact(exe);
if (b.args) |args| {
run_cmd.addArgs(args);
}
b.step("run-" ++ name, "Run " ++ name)
.dependOn(&run_cmd.step);
all_tests.append(buildTestStep(b, name, target)) catch @panic("OOM");
}
}
const test_all_step = b.step("test", "Run all tests");
for (all_tests.items) |step| {
test_all_step.dependOn(step);
}
}
fn buildTestStep(b: *std.Build, comptime name: []const u8, target: std.zig.CrossTarget) *Build.Step {
const exe_tests = b.addTest(.{
.root_source_file = .{ .path = "src/" ++ 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 buildCli(b: *std.Build, comptime name: []const u8, optimize: std.builtin.Mode, target: std.zig.CrossTarget) ?*Build.CompileStep {
if (std.mem.eql(u8, name, "pidof")) {
if (target.getOsTag() != .macos) {
return null;
}
}
return b.addExecutable(.{
.name = name,
.root_source_file = FileSource.relative("src/" ++ name ++ ".zig"),
.target = target,
.optimize = optimize,
});
}