-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.zig
More file actions
117 lines (100 loc) · 4.18 KB
/
build.zig
File metadata and controls
117 lines (100 loc) · 4.18 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
const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
// The target and optimize mode must be passed when creating the module.
const minish_mod = b.addModule("minish", .{
.root_source_file = b.path("src/lib.zig"),
.target = target,
.optimize = optimize,
});
// Unit tests
const test_mod = b.createModule(.{
.root_source_file = b.path("src/lib.zig"),
.target = target,
.optimize = optimize,
});
const tests = b.addTest(.{
.root_module = test_mod,
});
const run_tests = b.addRunArtifact(tests);
b.step("test", "Run unit tests").dependOn(&run_tests.step);
// API Documentation
const docs_step = b.step("docs", "Generate API documentation");
const doc_path = "docs/api";
const io = b.graph.io;
// 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: makePath 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_path,
"-fno-emit-bin",
});
gen_docs_cmd.step.dependOn(&ensure_docs_dir.step);
docs_step.dependOn(&gen_docs_cmd.step);
// Examples (only when developing minish itself, not when used as a dependency)
if (b.build_root.handle.openDir(io, "examples", .{ .iterate = true })) |examples_dir| {
var dir = examples_dir;
defer dir.close(io);
const run_all_step = 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 stem = entry.name[0 .. entry.name.len - 4];
const src_rel = b.fmt("examples/{s}", .{entry.name});
const example_mod = b.addModule(stem, .{
.root_source_file = b.path(src_rel),
.target = target,
.optimize = optimize,
});
const exe = b.addExecutable(.{
.name = stem,
.root_module = example_mod,
});
exe.root_module.addImport("minish", minish_mod);
b.installArtifact(exe);
const run_cmd = b.addRunArtifact(exe);
const step_name = b.fmt("run-{s}", .{stem});
const run_example_step = b.step(step_name, b.fmt("Run example {s}", .{stem}));
run_example_step.dependOn(&run_cmd.step);
run_all_step.dependOn(run_example_step);
}
} else |err| switch (err) {
// Used as a library dependency: no examples directory at the import root.
error.FileNotFound => {},
// Surface other errors (permissions, IO) instead of swallowing them.
else => @panic(@errorName(err)),
}
}
/// Build step that ensures a directory (relative to the build root) exists.
/// Runs `std.fs.Dir.makePath` 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);
}
};