-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.zig
372 lines (334 loc) · 11.8 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
const debug = std.debug;
const std = @import("std");
pub const Goku = @import("src/Goku.zig");
const bundled_lucide_icons = @as([]const []const u8, &.{
"github",
"apple",
"loader-pinwheel",
"arrow-down-to-line",
});
const BuildSteps = struct {
check: *std.Build.Step,
coverage: *std.Build.Step,
docs: *std.Build.Step,
generate_benchmark_site: *std.Build.Step,
run: *std.Build.Step,
run_benchmark: *std.Build.Step,
site: *std.Build.Step,
serve: *std.Build.Step,
@"test": *std.Build.Step,
pub fn init(b: *std.Build) BuildSteps {
return .{
.check = b.step(
"check",
"Check the Zig code",
),
.coverage = b.step(
"coverage",
"Analyze code coverage",
),
.docs = b.step(
"docs",
"Generate source code docs",
),
.generate_benchmark_site = b.step(
"generate-benchmark",
"Generate a site dir used to benchmark goku",
),
.run = b.step(
"run",
"Run the app",
),
.run_benchmark = b.step(
"run-benchmark",
"Run the benchmark",
),
.site = b.step(
"site",
"Build the Goku site",
),
.serve = b.step(
"serve",
"Serve the Goku site (for local previewing)",
),
.@"test" = b.step(
"test",
"Run unit tests",
),
};
}
pub fn deinit(self: @This()) void {
inline for (@typeInfo(@This()).@"struct".fields) |f| {
const step = @field(self, f.name);
debug.assert(step.dependencies.items.len > 0);
}
}
};
pub const Docs = struct {
compile: *std.Build.Step.Compile,
serve: *std.Build.Step.Run,
install: *std.Build.Step.InstallDir,
pub fn fromTests(compile: *std.Build.Step.Compile) Docs {
const b: *std.Build = compile.root_module.owner;
const target = compile.root_module.resolved_target.?;
const optimize = compile.root_module.optimize.?;
const install = compile.root_module.owner.addInstallDirectory(.{
.source_dir = compile.getEmittedDocs(),
.install_dir = .prefix,
.install_subdir = "docs",
});
const exe = b.addExecutable(.{
.name = b.fmt("serve-{s}", .{compile.name}),
.root_source_file = b.path("src/serve.zig"),
.target = target,
.optimize = optimize,
});
exe.root_module.addImport("zap", b.dependency("zap", .{}).module("zap"));
const serve = b.addRunArtifact(exe);
serve.addDirectoryArg(compile.getEmittedDocs());
return .{
.compile = compile,
.install = install,
.serve = serve,
};
}
};
pub fn build(b: *std.Build) void {
const build_steps = BuildSteps.init(b);
defer build_steps.deinit();
const wasm_option = b.option(
bool,
"wasm",
"Compile to webassembly (supported on e.g. wasmtime)",
) orelse false;
const tracy_enable = b.option(
bool,
"tracy_enable",
"Enable profiling",
) orelse false;
const target = if (wasm_option) b.resolveTargetQuery(.{
.cpu_arch = .wasm32,
.os_tag = .wasi,
}) else b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const tracy = b.dependency("tracy", .{
.target = target,
.optimize = optimize,
.tracy_enable = tracy_enable,
.tracy_no_exit = true,
.tracy_manual_lifetime = true,
});
const clap = b.dependency("clap", .{
.target = target,
.optimize = optimize,
});
const sqlite = b.dependency("sqlite", .{
.target = target,
.optimize = optimize,
});
const lucide = b.dependency("lucide", .{
.icons = bundled_lucide_icons,
});
const bulma = b.dependency("bulma", .{});
const htmx = b.dependency("htmx", .{});
const zap = b.dependency("zap", .{ .target = target, .optimize = optimize });
const c_mod = buildCModule(b, .{
.md4c = b.dependency("md4c", .{}),
.mustach = b.dependency("mustach", .{}),
.quickjs = b.dependency("quickjs", .{}),
.yaml = b.dependency("yaml", .{}),
.target = target,
.optimize = optimize,
});
const exe = b.addExecutable(.{
.name = "goku",
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});
exe.root_module.addImport("c", c_mod);
exe.root_module.addImport("tracy", tracy.module("tracy"));
exe.root_module.addImport("clap", clap.module("clap"));
exe.root_module.addImport("sqlite", sqlite.module("sqlite"));
exe.root_module.addImport("lucide", lucide.module("lucide"));
exe.root_module.addImport("bulma", bulma.module("bulma"));
exe.root_module.addImport("htmx", htmx.module("htmx"));
exe.linkLibrary(sqlite.artifact("sqlite"));
if (tracy_enable) {
exe.linkLibrary(tracy.artifact("tracy"));
exe.linkLibCpp();
}
b.installArtifact(exe);
const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args);
}
build_steps.run.dependOn(&run_cmd.step);
const exe_unit_tests = b.addTest(.{
// We provide a name to the unit tests so the generated
// docs will use it for the namespace.
.name = "goku",
.root_source_file = b.path("src/root.zig"),
.target = target,
.optimize = optimize,
//.test_runner = b.dependency("custom-test-runner", .{}).path("src/test_runner.zig"),
});
exe_unit_tests.root_module.addImport("bulma", bulma.module("bulma"));
exe_unit_tests.root_module.addImport("c", c_mod);
exe_unit_tests.root_module.addImport("clap", clap.module("clap"));
exe_unit_tests.root_module.addImport("htmx", htmx.module("htmx"));
exe_unit_tests.root_module.addImport("lucide", lucide.module("lucide"));
exe_unit_tests.root_module.addImport("sqlite", sqlite.module("sqlite"));
exe_unit_tests.root_module.addImport("tracy", tracy.module("tracy"));
exe_unit_tests.root_module.addImport("zap", zap.module("zap"));
exe_unit_tests.linkLibrary(sqlite.artifact("sqlite"));
if (tracy_enable) {
exe_unit_tests.linkLibrary(tracy.artifact("tracy"));
exe_unit_tests.linkLibCpp();
}
const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests);
build_steps.@"test".dependOn(&run_exe_unit_tests.step);
const run_coverage = b.addSystemCommand(&.{
"kcov",
"--clean",
"--include-pattern=src/,modules/",
"--exclude-pattern=.cache/zig/,test_runner.zig",
});
run_coverage.addDirectoryArg(b.path("kcov-output/"));
run_coverage.addArtifactArg(exe_unit_tests);
build_steps.coverage.dependOn(&run_coverage.step);
const exe_check = b.addExecutable(.{
.name = "goku",
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});
exe_check.root_module.addImport("c", c_mod);
exe_check.root_module.addImport("tracy", tracy.module("tracy"));
exe_check.root_module.addImport("clap", clap.module("clap"));
exe_check.root_module.addImport("sqlite", sqlite.module("sqlite"));
exe_check.root_module.addImport("lucide", lucide.module("lucide"));
exe_check.root_module.addImport("bulma", bulma.module("bulma"));
exe_check.root_module.addImport("htmx", htmx.module("htmx"));
exe_check.linkLibrary(sqlite.artifact("sqlite"));
if (tracy_enable) {
exe_check.linkLibrary(tracy.artifact("tracy"));
exe_check.linkLibCpp();
}
build_steps.check.dependOn(&exe_check.step);
const serve_exe = b.addExecutable(.{
.name = "serve",
.root_source_file = b.path("src/serve.zig"),
.target = target,
.optimize = optimize,
});
serve_exe.root_module.addImport("zap", zap.module("zap"));
b.installArtifact(serve_exe);
const serve_exe_check = b.addExecutable(.{
.name = "serve-check",
.root_source_file = b.path("src/serve.zig"),
.target = target,
.optimize = optimize,
});
serve_exe_check.root_module.addImport("zap", zap.module("zap"));
build_steps.check.dependOn(&serve_exe_check.step);
const benchmark_site = buildBenchmarkSite(b);
const install = b.addInstallDirectory(.{
.source_dir = benchmark_site,
.install_dir = .prefix,
.install_subdir = "benchmark-site",
});
build_steps.generate_benchmark_site.dependOn(&install.step);
const run_benchmark_cmd = b.addRunArtifact(exe);
run_benchmark_cmd.addDirectoryArg(benchmark_site);
run_benchmark_cmd.addArgs(&.{ "-o", "benchmark-build" });
build_steps.run_benchmark.dependOn(&run_benchmark_cmd.step);
var this_dep_hack: std.Build.Dependency = .{ .builder = b };
const build_site_cmd = Goku.build(&this_dep_hack, b.path("site"), b.path("build"));
if (b.args) |args| {
build_site_cmd.addArgs(args);
}
build_steps.site.dependOn(&build_site_cmd.step);
build_steps.site.dependOn(b.getInstallStep());
const run_serve_cmd = Goku.serve(&this_dep_hack, b.path("build"));
build_steps.serve.dependOn(build_steps.site);
build_steps.serve.dependOn(&run_serve_cmd.step);
const docs = Docs.fromTests(exe_unit_tests);
build_steps.docs.dependOn(&docs.serve.step);
}
const BuildCModuleOptions = struct {
md4c: *std.Build.Dependency,
mustach: *std.Build.Dependency,
quickjs: *std.Build.Dependency,
yaml: *std.Build.Dependency,
target: std.Build.ResolvedTarget,
optimize: std.builtin.OptimizeMode,
};
pub fn buildCModule(b: *std.Build, opts: BuildCModuleOptions) *std.Build.Module {
const md4c = opts.md4c;
const mustach = opts.mustach;
const quickjs = opts.quickjs;
const yaml = opts.yaml;
const target = opts.target;
const optimize = opts.optimize;
const c = b.addTranslateC(.{
.root_source_file = b.addWriteFiles().add("c.h",
\\#include <yaml.h>
\\#include <md4c-html.h>
\\#include <mustach.h>
\\#include <quickjs.h>
),
.target = target,
.optimize = optimize,
.link_libc = true,
});
const mod = c.createModule();
c.addIncludePath(md4c.artifact("md4c").getEmittedIncludeTree());
mod.linkLibrary(md4c.artifact("md4c"));
c.addIncludePath(mustach.artifact("mustach").getEmittedIncludeTree());
mod.linkLibrary(mustach.artifact("mustach"));
c.addIncludePath(quickjs.artifact("quickjs").getEmittedIncludeTree());
mod.linkLibrary(quickjs.artifact("quickjs"));
c.addIncludePath(yaml.artifact("yaml").getEmittedIncludeTree());
mod.linkLibrary(yaml.artifact("yaml"));
return mod;
}
pub fn buildBenchmarkSite(b: *std.Build) std.Build.LazyPath {
const wf = b.addWriteFiles();
_ = wf.add(
"pages/home-page.md",
\\---
\\id: home
\\slug: /
\\title: Home Page
\\---
\\
\\ # Home
\\
\\ This is the home page.
,
);
for (0..10) |i| {
_ = wf.add(
b.fmt("pages/page-{d}.md", .{i}),
b.fmt(
\\---
\\id: page-{d}
\\slug: /page-{d}
\\title: Hello, world {d}
\\---
\\
\\# Hello, world {d}
\\
\\
\\This is a paragraph with some **bolded** content.
\\
\\Check out the [home page](/).
,
.{ i, i, i, i },
),
);
}
return wf.getDirectory();
}