Something went wrong. Try again.
An MPEG audio codec in Zig that decodes Layers I, II and III and encodes Layers II and III.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157// 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(.{});
const mod = b.addModule("mp3", .{ .root_source_file = b.path("src/root.zig"), .target = target, .optimize = optimize, });
const exe = b.addExecutable(.{ .name = "zig-mp3", .root_module = b.createModule(.{ .root_source_file = b.path("src/main.zig"), .target = target, .optimize = optimize, .imports = &.{.{ .name = "mp3", .module = mod }}, }), }); b.installArtifact(exe);
const run_cmd = b.addRunArtifact(exe); run_cmd.step.dependOn(b.getInstallStep()); run_cmd.addPassthruArgs(); const run_step = b.step("run", "Decode an MP3 file to WAV"); run_step.dependOn(&run_cmd.step);
// How many inputs each fuzz target sees on a plain `zig build test`. // Raise it for a campaign: `zig build test -Dfuzz-rounds=50000`. const fuzz_rounds = b.option( usize, "fuzz-rounds", "inputs per fuzz target when running the tests (default 400)", ) orelse 400; const options = b.addOptions(); options.addOption(usize, "fuzz_rounds", fuzz_rounds);
// The end-to-end tests embed sample streams and reference decodings. // They hang off a second module so that the published library does not // carry them, and so that a consumer never has to have `testdata` at all. const test_mod = b.createModule(.{ .root_source_file = b.path("src/root.zig"), .target = target, .optimize = optimize, }); for ([_][]const u8{ "testdata/stereo44.mp3", "testdata/stereo44.pcm", "testdata/mono22.mp3", "testdata/mono22.pcm", "testdata/mono11.mp3", "testdata/mono11.pcm", "testdata/layer1.mp1", "testdata/layer1.pcm", "testdata/layer2.mp2", "testdata/layer2.pcm", }) |name| { test_mod.addAnonymousImport(name, .{ .root_source_file = b.path(name) }); } test_mod.addImport("build_options", options.createModule());
// LLVM rather than the self-hosted backend, which emits no coverage // instrumentation: without it `--fuzz` has an empty table of program // counters to steer by, in Debug as well as in the release modes. const mod_tests = b.addTest(.{ .root_module = test_mod, .use_llvm = true }); const exe_tests = b.addTest(.{ .root_module = exe.root_module });
const test_step = b.step("test", "Run tests"); test_step.dependOn(&b.addRunArtifact(mod_tests).step); test_step.dependOn(&b.addRunArtifact(exe_tests).step);
// `zig build fuzz` runs the fuzz targets over their corpus, which is what // `zig build test` does too; `zig build fuzz --fuzz` hands them to the // fuzzer instead. Separate so that the latter does not also drag in // every other test. const fuzz_tests = b.addTest(.{ .name = "fuzz", .root_module = test_mod, .filters = &.{"fuzz"}, .use_llvm = true, }); const fuzz_step = b.step("fuzz", "Run the fuzz targets; add --fuzz to fuzz for real"); fuzz_step.dependOn(&b.addRunArtifact(fuzz_tests).step);
// Always optimized: a benchmark of a Debug build measures the safety // checks rather than the decoder. const bench = b.addExecutable(.{ .name = "bench", .root_module = b.createModule(.{ .root_source_file = b.path("src/bench.zig"), .target = target, .optimize = .ReleaseFast, }), }); for ([_][]const u8{ "testdata/stereo44.mp3", "testdata/layer2.mp2", "testdata/layer1.mp1", }) |name| { bench.root_module.addAnonymousImport(name, .{ .root_source_file = b.path(name) }); } const bench_step = b.step("bench", "Measure decoding throughput"); bench_step.dependOn(&b.addRunArtifact(bench).step);
// Zig emits the API documentation as a side effect of compiling, so this // builds the library purely to get at it. The result is a static site: // index.html, the viewer's wasm and javascript, and the sources it reads // from. const docs_library = b.addLibrary(.{ .name = "mp3", .root_module = mod, });
const install_docs = b.addInstallDirectory(.{ .source_dir = docs_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 generated 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. This is the same reason `zig std` runs a // server rather than just opening a file. 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 built for the machine running the build, never for // 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); // The install path cannot be asked for while configuring, so the server // is pointed at the generated directory instead; it is the same content. run_docs_server.addDirectoryArg(docs_library.getEmittedDocs()); run_docs_server.addArg(b.fmt("{d}", .{docs_port})); // The server 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 tests of its own; without this they would never run. const docs_server_tests = b.addTest(.{ .root_module = docs_server.root_module }); test_step.dependOn(&b.addRunArtifact(docs_server_tests).step);}