-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.zig
More file actions
475 lines (412 loc) · 22.7 KB
/
build.zig
File metadata and controls
475 lines (412 loc) · 22.7 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
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
const std = @import("std");
const builtin = @import("builtin");
/// Link platform-specific networking deps (TLS, sockets) for Windows.
fn linkPlatformNetDeps(artifact: *std.Build.Step.Compile) void {
const rt = artifact.root_module.resolved_target orelse return;
if (rt.result.os.tag == .windows) {
artifact.linkSystemLibrary("ws2_32");
artifact.linkSystemLibrary("advapi32"); // RtlGenRandom
artifact.linkSystemLibrary("crypt32"); // Cert* APIs
artifact.linkSystemLibrary("secur32"); // Schannel (std.crypto.tls)
}
}
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
// ─────────────────────────────────────────────
// Build options visible via @import("build_options")
// Toggle: zig build test-all -Dverbose-tests
// ─────────────────────────────────────────────
const verbose_tests = b.option(bool, "verbose-tests", "Print debug logs in tests") orelse false;
const build_opts = b.addOptions();
build_opts.addOption(bool, "verbose_tests", verbose_tests);
// ─────────────────────────────────────────────
// Utility modules (declare EARLY so everyone can import them)
// ─────────────────────────────────────────────
const utils_fs_mod = b.createModule(.{
.root_source_file = b.path("src/utils/fs.zig"),
.target = target,
.optimize = optimize,
});
utils_fs_mod.addOptions("build_options", build_opts);
const utils_hash_manifest_mod = b.createModule(.{
.root_source_file = b.path("src/utils/hash_manifest.zig"),
.target = target,
.optimize = optimize,
});
utils_hash_manifest_mod.addOptions("build_options", build_opts);
// ─────────────────────────────────────────────
// Public library surface (root.zig is API entry)
// ─────────────────────────────────────────────
const docz_module = b.createModule(.{
.root_source_file = b.path("root.zig"),
.target = target,
.optimize = optimize,
});
docz_module.addOptions("build_options", build_opts);
// ─────────────────────────────────────────────
// Preview server modules (under src/web_preview/*)
// Kept OUT of docz_module to avoid cycles.
// ─────────────────────────────────────────────
const web_preview_hot_mod = b.createModule(.{
.root_source_file = b.path("src/web_preview/hot_reload.zig"),
.target = target,
.optimize = optimize,
});
web_preview_hot_mod.addOptions("build_options", build_opts);
// helpers as separate named modules so tests can import them cleanly
const web_preview_limits_mod = b.createModule(.{
.root_source_file = b.path("src/web_preview/limits.zig"),
.target = target,
.optimize = optimize,
});
web_preview_limits_mod.addOptions("build_options", build_opts);
const web_preview_timeout_mod = b.createModule(.{
.root_source_file = b.path("src/web_preview/timeout.zig"),
.target = target,
.optimize = optimize,
});
web_preview_timeout_mod.addOptions("build_options", build_opts);
const web_preview_mod = b.createModule(.{
.root_source_file = b.path("src/web_preview/server.zig"),
.target = target,
.optimize = optimize,
});
web_preview_mod.addOptions("build_options", build_opts);
web_preview_mod.addImport("docz", docz_module);
web_preview_mod.addImport("web_preview_hot", web_preview_hot_mod);
web_preview_mod.addImport("web_preview_limits", web_preview_limits_mod);
web_preview_mod.addImport("web_preview_timeout", web_preview_timeout_mod);
web_preview_mod.addImport("utils_fs", utils_fs_mod);
// ─────────────────────────────────────────────
// CLI root module
// ─────────────────────────────────────────────
const cli_root_module = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});
cli_root_module.addOptions("build_options", build_opts);
cli_root_module.addImport("docz", docz_module);
cli_root_module.addImport("web_preview", web_preview_mod);
cli_root_module.addImport("web_preview_hot", web_preview_hot_mod);
// ─────────────────────────────────────────────
// Converters (internal modules)
// ─────────────────────────────────────────────
const html_import_mod = b.createModule(.{
.root_source_file = b.path("src/convert/html/import.zig"),
.target = target,
.optimize = optimize,
});
html_import_mod.addOptions("build_options", build_opts);
html_import_mod.addImport("docz", docz_module);
const html_export_mod = b.createModule(.{
.root_source_file = b.path("src/convert/html/export.zig"),
.target = target,
.optimize = optimize,
});
html_export_mod.addOptions("build_options", build_opts);
html_export_mod.addImport("docz", docz_module);
// html export uses utils_fs helpers
html_export_mod.addImport("utils_fs", utils_fs_mod);
// root.zig does: @import("html_export")
docz_module.addImport("html_export", html_export_mod);
const md_import_mod = b.createModule(.{
.root_source_file = b.path("src/convert/markdown/import.zig"),
.target = target,
.optimize = optimize,
});
md_import_mod.addOptions("build_options", build_opts);
md_import_mod.addImport("docz", docz_module);
const md_export_mod = b.createModule(.{
.root_source_file = b.path("src/convert/markdown/export.zig"),
.target = target,
.optimize = optimize,
});
md_export_mod.addOptions("build_options", build_opts);
md_export_mod.addImport("docz", docz_module);
const latex_import_mod = b.createModule(.{
.root_source_file = b.path("src/convert/latex/import.zig"),
.target = target,
.optimize = optimize,
});
latex_import_mod.addOptions("build_options", build_opts);
latex_import_mod.addImport("docz", docz_module);
const latex_export_mod = b.createModule(.{
.root_source_file = b.path("src/convert/latex/export.zig"),
.target = target,
.optimize = optimize,
});
latex_export_mod.addOptions("build_options", build_opts);
latex_export_mod.addImport("docz", docz_module);
// Make converters addressable from CLI
cli_root_module.addImport("html_import", html_import_mod);
cli_root_module.addImport("html_export", html_export_mod);
cli_root_module.addImport("md_import", md_import_mod);
cli_root_module.addImport("md_export", md_export_mod);
cli_root_module.addImport("latex_import", latex_import_mod);
cli_root_module.addImport("latex_export", latex_export_mod);
// ─────────────────────────────────────────────
// Tools: vendor (module + exe) and vendor-verify (HTTP-free)
// ─────────────────────────────────────────────
const vendor_mod = b.createModule(.{
.root_source_file = b.path("tools/vendor.zig"),
.target = target,
.optimize = optimize,
});
// tools/vendor.zig does @import("utils_fs")
vendor_mod.addImport("utils_fs", utils_fs_mod);
const vendor_tool = b.addExecutable(.{
.name = "docz-vendor",
.root_module = vendor_mod,
});
linkPlatformNetDeps(vendor_tool);
b.installArtifact(vendor_tool);
const vendor_verify_mod = b.createModule(.{
.root_source_file = b.path("tools/vendor_verify_main.zig"),
.target = target,
.optimize = optimize,
});
// vendor_verify_main -> vendor_core.zig -> @import("utils_fs")
vendor_verify_mod.addImport("utils_fs", utils_fs_mod);
const vendor_verify_exe = b.addExecutable(.{
.name = "docz-vendor-verify",
.root_module = vendor_verify_mod,
});
// ─────────────────────────────────────────────
// CLI executable
// ─────────────────────────────────────────────
const exe = b.addExecutable(.{
.name = "docz",
.root_module = cli_root_module,
});
linkPlatformNetDeps(exe);
b.installArtifact(exe);
// A second installed exe name for e2e (prevents locking on Windows).
const exe_name = if (builtin.os.tag == .windows) "docz.exe" else "docz";
const e2e_name = if (builtin.os.tag == .windows) "docz-e2e.exe" else "docz-e2e";
const e2e_install = b.addInstallArtifact(exe, .{ .dest_sub_path = e2e_name });
// Paths exposed to tests via build_options.
const docz_rel = b.fmt("zig-out/bin/{s}", .{exe_name});
const e2e_rel = b.fmt("zig-out/bin/{s}", .{e2e_name});
const docz_abs = b.getInstallPath(.bin, exe_name);
const e2e_abs = b.getInstallPath(.bin, e2e_name);
build_opts.addOption([]const u8, "docz_relpath", docz_rel);
build_opts.addOption([]const u8, "e2e_relpath", e2e_rel);
build_opts.addOption([]const u8, "docz_abspath", docz_abs);
build_opts.addOption([]const u8, "e2e_abspath", e2e_abs);
// ─────────────────────────────────────────────
// Convenience run steps
// ─────────────────────────────────────────────
const run_cmd = b.addRunArtifact(exe);
if (b.args) |args| run_cmd.addArgs(args);
const run_step = b.step("run", "Run the Docz CLI");
run_step.dependOn(&run_cmd.step);
const prev = b.addRunArtifact(exe);
prev.addArg("preview");
if (b.args) |args| prev.addArgs(args);
const prev_step = b.step("preview", "Run the web preview server");
prev_step.dependOn(&prev.step);
// ─────────────────────────────────────────────
// Vendor pipeline steps
// ─────────────────────────────────────────────
const vendor_cfg = b.option([]const u8, "vendor-config", "Path to tools/vendor.config") orelse "tools/vendor.config";
const vendor_bootstrap = b.addRunArtifact(vendor_tool);
vendor_bootstrap.addArgs(&.{ "bootstrap", "--config", vendor_cfg });
const vendor_step = b.step("vendor", "Fetch/lock/verify third_party assets from config");
vendor_step.dependOn(&vendor_bootstrap.step);
const vendor_freeze = b.addRunArtifact(vendor_tool);
vendor_freeze.addArg("freeze");
const vendor_freeze_step = b.step("vendor-freeze", "Write third_party/VENDOR.lock");
vendor_freeze_step.dependOn(&vendor_freeze.step);
const vendor_checks = b.addRunArtifact(vendor_tool);
vendor_checks.addArg("checksums");
const vendor_checks_step = b.step("vendor-checksums", "Write/refresh CHECKSUMS.sha256");
vendor_checks_step.dependOn(&vendor_checks.step);
const run_vendor_verify = b.addRunArtifact(vendor_verify_exe);
const step_vendor_verify = b.step("vendor-verify", "Verify third_party checksums (no HTTP)");
step_vendor_verify.dependOn(&run_vendor_verify.step);
// ─────────────────────────────────────────────
// Unit tests (library + converters) — embedded `test {}` blocks
// ─────────────────────────────────────────────
const unit_tests = b.addTest(.{ .root_module = docz_module });
linkPlatformNetDeps(unit_tests);
const unit_run = b.addRunArtifact(unit_tests);
const html_import_unit = b.addTest(.{ .root_module = html_import_mod });
linkPlatformNetDeps(html_import_unit);
const html_import_unit_run = b.addRunArtifact(html_import_unit);
const html_export_unit = b.addTest(.{ .root_module = html_export_mod });
linkPlatformNetDeps(html_export_unit);
const html_export_unit_run = b.addRunArtifact(html_export_unit);
const md_import_unit = b.addTest(.{ .root_module = md_import_mod });
linkPlatformNetDeps(md_import_unit);
const md_import_unit_run = b.addRunArtifact(md_import_unit);
const md_export_unit = b.addTest(.{ .root_module = md_export_mod });
linkPlatformNetDeps(md_export_unit);
const md_export_unit_run = b.addRunArtifact(md_export_unit);
const latex_import_unit = b.addTest(.{ .root_module = latex_import_mod });
linkPlatformNetDeps(latex_import_unit);
const latex_import_unit_run = b.addRunArtifact(latex_import_unit);
const latex_export_unit = b.addTest(.{ .root_module = latex_export_mod });
linkPlatformNetDeps(latex_export_unit);
const latex_export_unit_run = b.addRunArtifact(latex_export_unit);
const unit_step = b.step("test", "Run unit tests");
unit_step.dependOn(&unit_run.step);
unit_step.dependOn(&html_import_unit_run.step);
unit_step.dependOn(&html_export_unit_run.step);
unit_step.dependOn(&md_import_unit_run.step);
unit_step.dependOn(&md_export_unit_run.step);
unit_step.dependOn(&latex_import_unit_run.step);
unit_step.dependOn(&latex_export_unit_run.step);
// ─────────────────────────────────────────────
// CLI unit tests (module-under-test style)
// ─────────────────────────────────────────────
const cli_common_mod = b.createModule(.{
.root_source_file = b.path("src/cli/common.zig"),
.target = target,
.optimize = optimize,
});
cli_common_mod.addOptions("build_options", build_opts);
cli_common_mod.addImport("docz", docz_module);
const cli_convert_mod = b.createModule(.{
.root_source_file = b.path("src/cli/convert.zig"),
.target = target,
.optimize = optimize,
});
cli_convert_mod.addOptions("build_options", build_opts);
cli_convert_mod.addImport("docz", docz_module);
cli_convert_mod.addImport("html_import", html_import_mod);
cli_convert_mod.addImport("html_export", html_export_mod);
cli_convert_mod.addImport("md_import", md_import_mod);
cli_convert_mod.addImport("md_export", md_export_mod);
cli_convert_mod.addImport("latex_import", latex_import_mod);
cli_convert_mod.addImport("latex_export", latex_export_mod);
const cli_build_mod = b.createModule(.{
.root_source_file = b.path("src/cli/build_cmd.zig"),
.target = target,
.optimize = optimize,
});
cli_build_mod.addOptions("build_options", build_opts);
cli_build_mod.addImport("docz", docz_module);
cli_build_mod.addImport("html_export", html_export_mod);
const cli_preview_mod = b.createModule(.{
.root_source_file = b.path("src/cli/preview.zig"),
.target = target,
.optimize = optimize,
});
cli_preview_mod.addOptions("build_options", build_opts);
cli_preview_mod.addImport("docz", docz_module);
cli_preview_mod.addImport("web_preview", web_preview_mod);
cli_preview_mod.addImport("web_preview_hot", web_preview_hot_mod);
const cli_enable_mod = b.createModule(.{
.root_source_file = b.path("src/cli/enable_wasm.zig"),
.target = target,
.optimize = optimize,
});
cli_enable_mod.addOptions("build_options", build_opts);
cli_enable_mod.addImport("docz", docz_module);
const cli_common_unit = b.addTest(.{ .root_module = cli_common_mod });
const cli_convert_unit = b.addTest(.{ .root_module = cli_convert_mod });
const cli_build_unit = b.addTest(.{ .root_module = cli_build_mod });
const cli_preview_unit = b.addTest(.{ .root_module = cli_preview_mod });
const cli_enable_unit = b.addTest(.{ .root_module = cli_enable_mod });
linkPlatformNetDeps(cli_common_unit);
linkPlatformNetDeps(cli_convert_unit);
linkPlatformNetDeps(cli_build_unit);
linkPlatformNetDeps(cli_preview_unit);
linkPlatformNetDeps(cli_enable_unit);
const cli_unit_step = b.step("test-cli", "Run CLI unit tests");
cli_unit_step.dependOn(&b.addRunArtifact(cli_common_unit).step);
cli_unit_step.dependOn(&b.addRunArtifact(cli_convert_unit).step);
cli_unit_step.dependOn(&b.addRunArtifact(cli_build_unit).step);
cli_unit_step.dependOn(&b.addRunArtifact(cli_preview_unit).step);
cli_unit_step.dependOn(&b.addRunArtifact(cli_enable_unit).step);
// ─────────────────────────────────────────────
// Integration tests (aggregate runner pattern)
// ─────────────────────────────────────────────
// These are reusable APIs for the integration & e2e tests
const test_net_mod = b.createModule(.{
.root_source_file = b.path("tests/support/net.zig"),
.target = target,
.optimize = optimize,
});
test_net_mod.addImport("web_preview", web_preview_mod);
const integration_module = b.createModule(.{
.root_source_file = b.path("tests/test_all_integration.zig"),
.target = target,
.optimize = optimize,
});
integration_module.addOptions("build_options", build_opts);
integration_module.addImport("test_net", test_net_mod);
integration_module.addImport("docz", docz_module);
integration_module.addImport("html_import", html_import_mod);
integration_module.addImport("html_export", html_export_mod);
integration_module.addImport("md_import", md_import_mod);
integration_module.addImport("md_export", md_export_mod);
integration_module.addImport("latex_import", latex_import_mod);
integration_module.addImport("latex_export", latex_export_mod);
integration_module.addImport("vendor", vendor_mod);
integration_module.addImport("web_preview", web_preview_mod);
integration_module.addImport("web_preview_hot", web_preview_hot_mod);
// tests/integration/web_preview_routes.zig does @import("utils_fs")
integration_module.addImport("utils_fs", utils_fs_mod);
// helpers visible to tests by module name
integration_module.addImport("web_preview_limits", web_preview_limits_mod);
integration_module.addImport("web_preview_timeout", web_preview_timeout_mod);
integration_module.addImport("utils_hash_manifest", utils_hash_manifest_mod);
const integration_tests = b.addTest(.{ .root_module = integration_module });
linkPlatformNetDeps(integration_tests);
const integration_run = b.addRunArtifact(integration_tests);
// Auto-stop the preview server inside integration tests after 2 seconds.
integration_run.setEnvironmentVariable("DOCZ_TEST_AUTOSTOP_MS", "2000");
const integration_step = b.step("test-integration", "Run integration tests");
integration_step.dependOn(&integration_run.step);
// ─────────────────────────────────────────────
// End-to-end tests
// ─────────────────────────────────────────────
const e2e_module = b.createModule(.{
.root_source_file = b.path("tests/test_all_e2e.zig"),
.target = target,
.optimize = optimize,
});
e2e_module.addOptions("build_options", build_opts);
e2e_module.addImport("test_net", test_net_mod);
e2e_module.addImport("docz", docz_module);
e2e_module.addImport("web_preview_limits", web_preview_limits_mod);
e2e_module.addImport("web_preview_timeout", web_preview_timeout_mod);
e2e_module.addImport("utils_hash_manifest", utils_hash_manifest_mod);
// If e2e tests ever import utils_fs directly, keep this here (harmless otherwise)
e2e_module.addImport("utils_fs", utils_fs_mod);
e2e_module.addImport("web_preview", web_preview_mod);
const e2e_tests = b.addTest(.{ .root_module = e2e_module });
linkPlatformNetDeps(e2e_tests);
const e2e_run = b.addRunArtifact(e2e_tests);
e2e_run.setEnvironmentVariable("DOCZ_BIN", e2e_abs);
e2e_run.step.dependOn(&e2e_install.step);
const e2e_step = b.step("test-e2e", "Run end-to-end tests");
e2e_step.dependOn(&e2e_run.step);
// ─────────────────────────────────────────────
// Version helper (nice for CI logs)
// ─────────────────────────────────────────────
const version_mod = b.createModule(.{
.root_source_file = b.path("tools/print_version.zig"),
.target = target,
.optimize = optimize,
});
const version_exe = b.addExecutable(.{
.name = "docz-version",
.root_module = version_mod,
});
const step_version = b.step("version", "Print Docz build info");
step_version.dependOn(&b.addRunArtifact(version_exe).step);
// ─────────────────────────────────────────────
// Aggregate: test-all (ensure vendor verify & version print first)
// ─────────────────────────────────────────────
const all_tests = b.step("test-all", "Run unit + integration + e2e tests");
all_tests.dependOn(step_vendor_verify);
all_tests.dependOn(step_version);
all_tests.dependOn(unit_step);
all_tests.dependOn(cli_unit_step);
all_tests.dependOn(integration_step);
all_tests.dependOn(e2e_step);
}