// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT const std = @import("std"); pub fn build(b: *std.Build) void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); // File names are `file:` URIs, and `@include` paths are references // resolved against them. const uri = b.dependency("uri", .{ .target = target, .optimize = optimize }).module("uri"); const mod = b.addModule("struthio", .{ .root_source_file = b.path("src/root.zig"), .target = target, .optimize = optimize, .imports = &.{.{ .name = "uri", .module = uri }}, }); // `struthio fmt`, which formats programs as `zig fmt` formats Zig, and // `struthio lsp`, a language server. const exe = b.addExecutable(.{ .name = "struthio", .root_module = b.createModule(.{ .root_source_file = b.path("src/main.zig"), .target = target, .optimize = optimize, .imports = &.{.{ .name = "struthio", .module = mod }}, }), }); b.installArtifact(exe); const run_exe = b.addRunArtifact(exe); run_exe.addPassthruArgs(); const run_step = b.step("run", "Run the struthio command, with arguments after --"); run_step.dependOn(&run_exe.step); // Narrows every test executable below to the tests whose name contains // this. `-Dfuzz-filter` is separate and narrows the fuzz module only. const test_filter = b.option( []const u8, "test-filter", "Run only the tests whose name contains this", ); const filters: []const []const u8 = if (test_filter) |f| &.{f} else &.{}; const test_step = b.step("test", "Run tests"); test_step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = mod, .filters = filters, })).step); // The command's own tests, which are the language server's. test_step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = exe.root_module, .filters = filters, })).step); // The language from the outside: programs in files, and what they // evaluate to. test_step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = b.createModule(.{ .root_source_file = b.path("tests/golden.zig"), .target = target, .optimize = optimize, .imports = &.{.{ .name = "struthio", .module = mod }}, }), .filters = filters, })).step); // The fuzz targets: what the parser and the evaluator must do with a // program nobody sensible wrote. They are ordinary tests as well, so // `zig build test` runs the same properties on the seeds beside them. const fuzz_mod = b.createModule(.{ .root_source_file = b.path("tests/fuzz.zig"), .target = target, .optimize = optimize, .imports = &.{.{ .name = "struthio", .module = mod }}, }); const fuzz_filter = b.option( []const u8, "fuzz-filter", "Fuzz or test only the targets whose name contains this", ); // LLVM, because the self-hosted backend that Debug uses otherwise emits // no coverage instrumentation, and Zig's fuzzer then ends with // "corrupted coverage file: pcs_len was zero". const fuzz_tests = b.addTest(.{ .root_module = fuzz_mod, .filters = if (fuzz_filter) |f| &.{f} else &.{}, .use_llvm = true, }); test_step.dependOn(&b.addRunArtifact(fuzz_tests).step); // A step of its own for `zig build fuzz --fuzz`, which takes over the // terminal and runs until stopped, so it must not be reached by // `zig build test`. const fuzz_step = b.step("fuzz", "The fuzz targets: add --fuzz to fuzz them"); fuzz_step.dependOn(&b.addRunArtifact(fuzz_tests).step); // The loop that drives those same targets without Zig's fuzzer: a run // bounded by a count or a seed is the same on every machine, which is // what the workflow wants; `tools/fuzz.zig` says more. // ReleaseSafe, because a fuzzer's whole job is how many inputs it gets // through, and that keeps every check that makes a failure a failure. const fuzz_run = b.addExecutable(.{ .name = "struthio-fuzz", .root_module = b.createModule(.{ .root_source_file = b.path("tools/fuzz.zig"), .target = b.graph.host, .optimize = .ReleaseSafe, .imports = &.{.{ .name = "fuzz_targets", .module = fuzz_mod }}, }), }); const run_fuzz = b.addRunArtifact(fuzz_run); run_fuzz.stdio = .inherit; run_fuzz.addPassthruArgs(); const fuzz_run_step = b.step("fuzz-run", "Fuzz the targets with a loop of our own"); fuzz_run_step.dependOn(&run_fuzz.step); // Nothing else builds it, so without this it could stop compiling and // `zig build test` would not notice. const check_step = b.step("check", "Compile everything without running it"); check_step.dependOn(&fuzz_run.step); // -- documentation ------------------------------------------------------- // // Zig emits the API documentation as a side effect of compiling, so the // module is built as a library purely to get at it. What comes out is not // a page but a program: a WebAssembly viewer, its javascript, and a tar of // the sources it reads from. const library = b.addLibrary(.{ .name = "struthio", .root_module = mod }); const install_docs = b.addInstallDirectory(.{ .source_dir = library.getEmittedDocs(), .install_dir = .prefix, .install_subdir = "docs", }); const docs_step = b.step("docs", "Build the API documentation into zig-out/docs"); docs_step.dependOn(&install_docs.step); // That viewer fetches `sources.tar` and `main.wasm` at runtime, which a // browser refuses to do from a `file://` page, so reading the docs // locally means serving them. const docs_port = b.option(u16, "docs-port", "Port for `zig build docs-serve` (default 8000)") orelse 8000; const docs_server = b.addExecutable(.{ .name = "docs-server", .root_module = b.createModule(.{ .root_source_file = b.path("tools/docs_server.zig"), // Always the machine running the build, never whatever -Dtarget // the library is being built for. .target = b.graph.host, .optimize = .Debug, }), }); const run_docs_server = b.addRunArtifact(docs_server); run_docs_server.step.dependOn(&install_docs.step); run_docs_server.addDirectoryArg(library.getEmittedDocs()); run_docs_server.addArg(b.fmt("{d}", .{docs_port})); // It runs until interrupted, so its output has to reach the terminal // rather than being captured by the build runner. run_docs_server.stdio = .inherit; const docs_serve_step = b.step("docs-serve", "Serve the API documentation over HTTP"); docs_serve_step.dependOn(&run_docs_server.step); // The server has a test of its own; without this it would never run. test_step.dependOn(&b.addRunArtifact( b.addTest(.{ .root_module = docs_server.root_module }), ).step); check_step.dependOn(&docs_server.step); }