-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild.zig
More file actions
146 lines (136 loc) · 5.81 KB
/
build.zig
File metadata and controls
146 lines (136 loc) · 5.81 KB
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
const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const io = b.graph.io;
const lib_source = b.path("src/lib.zig");
const lib_module = b.addModule("ordered", .{
.root_source_file = lib_source,
.target = target,
.optimize = optimize,
});
const lib = b.addLibrary(.{
.name = "ordered",
.root_module = lib_module,
});
b.installArtifact(lib);
// --- Docs Setup ---
const docs_step = b.step("docs", "Generate API documentation");
const doc_install_path = "docs/api";
// Zig's `-femit-docs=<path>` writes the leaf dir but does not create
// intermediate parents, and git does not track empty directories, so a
// fresh checkout may have no `docs/` at all. Create it portably here
// (idempotent: createDirPath is a no-op when the directory already exists).
const ensure_docs_dir = EnsureDirStep.create(b, "docs");
const gen_docs_cmd = b.addSystemCommand(&[_][]const u8{
b.graph.zig_exe,
"build-lib",
"src/lib.zig",
"-femit-docs=" ++ doc_install_path,
"-fno-emit-bin",
});
gen_docs_cmd.step.dependOn(&ensure_docs_dir.step);
docs_step.dependOn(&gen_docs_cmd.step);
// --- Tests ---
const lib_unit_tests = b.addTest(.{
.root_module = lib_module,
});
const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests);
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&run_lib_unit_tests.step);
// --- Examples ---
const examples_path = "examples";
if (b.build_root.handle.openDir(io, examples_path, .{ .iterate = true })) |examples_dir| {
var dir = examples_dir;
defer dir.close(io);
const run_all_examples = b.step("run-all", "Run all examples");
var it = dir.iterate();
while (it.next(io) catch null) |entry| {
if (entry.kind != .file) continue;
if (!std.mem.endsWith(u8, entry.name, ".zig")) continue;
const exe_name = entry.name[0 .. entry.name.len - 4];
const exe_path = b.fmt("{s}/{s}", .{ examples_path, entry.name });
const exe_module = b.addModule(exe_name, .{
.root_source_file = b.path(exe_path),
.target = target,
.optimize = optimize,
});
exe_module.addImport("ordered", lib_module);
const exe = b.addExecutable(.{
.name = exe_name,
.root_module = exe_module,
});
b.installArtifact(exe);
const run_cmd = b.addRunArtifact(exe);
const run_step_name = b.fmt("run-{s}", .{exe_name});
const run_step_desc = b.fmt("Run the {s} example", .{exe_name});
const run_step = b.step(run_step_name, run_step_desc);
run_step.dependOn(&run_cmd.step);
run_all_examples.dependOn(run_step);
}
} else |err| switch (err) {
// Used as a library dependency: no examples directory at the import root.
error.FileNotFound, error.NotDir => {},
else => @panic(@errorName(err)),
}
// --- Benchmarks ---
const benches_path = "benches";
if (b.build_root.handle.openDir(io, benches_path, .{ .iterate = true })) |benches_dir| {
var dir = benches_dir;
defer dir.close(io);
const bench_all = b.step("bench-all", "Run all benchmarks");
var it = dir.iterate();
while (it.next(io) catch null) |entry| {
if (entry.kind != .file) continue;
if (!std.mem.endsWith(u8, entry.name, ".zig")) continue;
const bench_name = entry.name[0 .. entry.name.len - 4];
const bench_path = b.fmt("{s}/{s}", .{ benches_path, entry.name });
const bench_module = b.addModule(bench_name, .{
.root_source_file = b.path(bench_path),
.target = target,
.optimize = .ReleaseFast, // Use ReleaseFast for benchmarks
});
bench_module.addImport("ordered", lib_module);
const bench_exe = b.addExecutable(.{
.name = bench_name,
.root_module = bench_module,
});
b.installArtifact(bench_exe);
const run_bench_cmd = b.addRunArtifact(bench_exe);
const bench_step_name = b.fmt("bench-{s}", .{bench_name});
const bench_step_desc = b.fmt("Run the {s} benchmark", .{bench_name});
const bench_step = b.step(bench_step_name, bench_step_desc);
bench_step.dependOn(&run_bench_cmd.step);
bench_all.dependOn(bench_step);
}
} else |err| switch (err) {
error.FileNotFound, error.NotDir => {},
else => @panic(@errorName(err)),
}
}
/// Build step that ensures a directory (relative to the build root) exists.
/// Runs `std.fs.Dir.createDirPath` at make-time, so it only fires when a
/// step that depends on it is actually being built. Portable across Linux,
/// macOS, and Windows.
const EnsureDirStep = struct {
step: std.Build.Step,
sub_path: []const u8,
fn create(b: *std.Build, sub_path: []const u8) *EnsureDirStep {
const self = b.allocator.create(EnsureDirStep) catch @panic("OOM");
self.* = .{
.step = std.Build.Step.init(.{
.id = .custom,
.name = b.fmt("ensure {s}/", .{sub_path}),
.owner = b,
.makeFn = make,
}),
.sub_path = sub_path,
};
return self;
}
fn make(step: *std.Build.Step, options: std.Build.Step.MakeOptions) anyerror!void {
_ = options;
const self: *EnsureDirStep = @fieldParentPtr("step", step);
try step.owner.build_root.handle.createDirPath(step.owner.graph.io, self.sub_path);
}
};