// 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(.{}); const zregex = b.dependency("zregex", .{ .target = target, .optimize = optimize, }); // The terminal code makes raw system calls on Linux and goes through libc // everywhere else. Linking libc on Linux sends it down the second path, // which is the only way to run that path's tests without a Mac or a BSD. const link_libc = b.option(bool, "link-libc", "Link libc (on Linux, tests the path other systems take)"); // The library module, importable as `@import("expect")`. const mod = b.addModule("expect", .{ .root_source_file = b.path("src/root.zig"), .target = target, .optimize = optimize, .link_libc = link_libc, .imports = &.{ .{ .name = "zregex", .module = zregex.module("zregex") }, }, }); const test_step = b.step("test", "Run tests"); // Compiles everything for the chosen target without running any of it, // which is how the other platforms are checked from this one. const check_step = b.step("check", "Compile everything without running it"); const mod_tests = b.addTest(.{ .root_module = mod }); test_step.dependOn(&b.addRunArtifact(mod_tests).step); check_step.dependOn(&mod_tests.step); // The example. It is also what exercises the library on Windows, where // the tests are skipped for want of a `/bin/sh`, so `check` builds it. const example = b.addExecutable(.{ .name = "python-calculator", .root_module = b.createModule(.{ .root_source_file = b.path("examples/python.zig"), .target = target, .optimize = optimize, .imports = &.{ .{ .name = "expect", .module = mod }, }, }), }); check_step.dependOn(&example.step); // Installed, since it is the only thing here that is not a library. b.installArtifact(example); const run_example = b.addRunArtifact(example); run_example.stdio = .inherit; if (b.args) |args| run_example.addArgs(args); const example_step = b.step("example", "Run the example, which drives an interactive Python"); example_step.dependOn(&run_example.step); // API documentation, emitted by the compiler as a side effect of building // the module as a library: a WebAssembly viewer, its javascript, and a // tar of the sources it reads. const library = b.addLibrary(.{ .name = "expect", .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); // The 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 -- the same reason `zig std` runs a server. 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.addArg(b.getInstallPath(.prefix, "docs")); 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); // It has a test of its own; without this it would never run. And nothing // else builds it, so it belongs in `check` too, or it can stop compiling // and no test will notice. test_step.dependOn(&b.addRunArtifact( b.addTest(.{ .root_module = docs_server.root_module }), ).step); check_step.dependOn(&docs_server.step); }