-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbuild.zig
More file actions
132 lines (112 loc) · 4.6 KB
/
build.zig
File metadata and controls
132 lines (112 loc) · 4.6 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
const std = @import("std");
const fs = std.fs;
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
// --- Library Setup ---
const lib_source = b.path("src/lib.zig");
const lib_module = b.createModule(.{
.root_source_file = lib_source,
.target = target,
.optimize = optimize,
});
const lib = b.addLibrary(.{
.name = "chilli",
.root_module = lib_module,
});
b.installArtifact(lib);
// Export the module so downstream projects can use it
_ = b.addModule("chilli", .{
.root_source_file = lib_source,
.target = target,
.optimize = optimize,
});
// --- 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, // Use the same zig that is running the build
"build-lib",
"src/lib.zig",
"-femit-docs=" ++ doc_install_path,
});
gen_docs_cmd.step.dependOn(&ensure_docs_dir.step);
docs_step.dependOn(&gen_docs_cmd.step);
// --- Test Setup ---
const test_module = b.createModule(.{
.root_source_file = lib_source,
.target = target,
.optimize = optimize,
});
const lib_unit_tests = b.addTest(.{
.root_module = test_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);
// --- Example Setup ---
const examples_path = "examples";
const io = b.graph.io;
examples_blk: {
// If the examples directory isn't present (common when used as a dependency),
// skip setting up example artifacts instead of panicking.
var examples_dir = b.build_root.handle.openDir(io, examples_path, .{ .iterate = true }) catch |err| {
if (err == error.FileNotFound or err == error.NotDir) break :examples_blk;
@panic("Can't open 'examples' directory");
};
defer examples_dir.close(io);
var dir_iter = examples_dir.iterate();
while (dir_iter.next(io) catch @panic("Failed to iterate examples")) |entry| {
if (!std.mem.endsWith(u8, entry.name, ".zig")) continue;
const exe_name = fs.path.stem(entry.name);
const exe_path = b.fmt("{s}/{s}", .{ examples_path, entry.name });
const exe_module = b.createModule(.{
.root_source_file = b.path(exe_path),
.target = target,
.optimize = optimize,
});
exe_module.addImport("chilli", 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);
}
}
}
/// 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);
}
};