Something went wrong. Try again.
Reads and writes the Ogg container format in Zig: pages, packets, grouping and chaining, with a page layer that allocates nothing.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
const std = @import("std");
pub fn build(b: *std.Build) void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{});
// The library, which is what a consumer imports: // // const ogg = b.dependency("ogg", .{ ... }); // exe.root_module.addImport("ogg", ogg.module("ogg")); const mod = b.addModule("ogg", .{ .root_source_file = b.path("src/root.zig"), .target = target, .optimize = optimize, });
const exe = b.addExecutable(.{ .name = "zig-ogg", .root_module = b.createModule(.{ .root_source_file = b.path("src/main.zig"), .target = target, .optimize = optimize, .imports = &.{.{ .name = "ogg", .module = mod }}, }), }); // The command-line tool prints its own version, which comes from the one // place a version is written down. exe.root_module.addAnonymousImport("build.zig.zon", .{ .root_source_file = b.path("build.zig.zon"), }); b.installArtifact(exe);
const run_step = b.step("run", "Run the command-line tool"); const run_cmd = b.addRunArtifact(exe); run_cmd.step.dependOn(b.getInstallStep()); run_cmd.addPassthruArgs(); run_step.dependOn(&run_cmd.step);
const test_step = b.step("test", "Run the unit tests"); const mod_tests = b.addTest(.{ .root_module = mod }); test_step.dependOn(&b.addRunArtifact(mod_tests).step); const exe_tests = b.addTest(.{ .root_module = exe.root_module }); test_step.dependOn(&b.addRunArtifact(exe_tests).step);
// Compiling everything without running it, for a quick check that an edit // did not break a path the tests do not reach. const check_step = b.step("check", "Compile everything without running it"); check_step.dependOn(&mod_tests.step); check_step.dependOn(&exe.step);
// `zig build fuzz --fuzz` drives these; without `--fuzz` they run their // corpus once, which is worth having in the test suite on its own. const fuzz_mod = b.createModule(.{ .root_source_file = b.path("src/tests/fuzz.zig"), .target = target, .optimize = optimize, .imports = &.{.{ .name = "ogg", .module = mod }}, }); // LLVM rather than the self-hosted backend, which emits no coverage // instrumentation: without it the fuzzer has nothing to steer by and a // run ends in "pcs_len was zero" rather than in a finding. const fuzz_tests = b.addTest(.{ .root_module = fuzz_mod, .use_llvm = true }); const fuzz_step = b.step("fuzz", "Run the fuzz tests"); fuzz_step.dependOn(&b.addRunArtifact(fuzz_tests).step); test_step.dependOn(&b.addRunArtifact(fuzz_tests).step); check_step.dependOn(&fuzz_tests.step);
// The same targets, without Zig's fuzzer: `tools/fuzz.zig` says why. const fuzz_runner = b.addExecutable(.{ .name = "fuzz-run", .root_module = b.createModule(.{ .root_source_file = b.path("tools/fuzz.zig"), .target = target, .optimize = optimize, .imports = &.{.{ .name = "fuzz_targets", .module = fuzz_mod }}, }), }); const fuzz_run_step = b.step("fuzz-run", "Fuzz without Zig's fuzzer"); const fuzz_run = b.addRunArtifact(fuzz_runner); fuzz_run.addPassthruArgs(); fuzz_run_step.dependOn(&fuzz_run.step); check_step.dependOn(&fuzz_runner.step);
// The documentation is a side effect of compiling the module, so the // library is built 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 -- which is why it has to be served rather than opened. const library = b.addLibrary(.{ .name = "ogg", .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);
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);
// 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);}