// 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 z2d_dep = b.dependency("z2d", .{ .target = target, .optimize = optimize }); // One module. The codecs, the pixel bridge and the sandbox live together // because Zig only analyses what is referenced: a program that decodes a // BMP and never mentions the sandbox does not compile the sandbox, so // splitting them would cost a dependency edge to save nothing. const mod = b.addModule("z2dimg", .{ .root_source_file = b.path("src/root.zig"), .target = target, .imports = libraryImports(b, target, optimize), }); // A test executable covers one module, so each needs its own. Missing one // out would not fail: its tests would simply never run. const test_step = b.step("test", "Run tests"); test_step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = mod })).step); // The round-trip tests, which drive the library the way a consumer does: // through the module, from bytes to a surface and back to bytes. A module // of their own so that `zig build test` runs them and a consumer never // compiles them. const e2e_mod = b.createModule(.{ .root_source_file = b.path("tests/roundtrip.zig"), .target = target, .optimize = optimize, .imports = &.{ .{ .name = "z2dimg", .module = mod }, .{ .name = "z2d", .module = z2d_dep.module("z2d") }, }, }); test_step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = e2e_mod })).step); // The PNG tests, which are a module of their own because they carry their // fixtures with them: twenty-six files built by `tools/mkpng.py` and // checked against Pillow before being written down. const png_mod = b.createModule(.{ .root_source_file = b.path("tests/png.zig"), .target = target, .optimize = optimize, .imports = &.{ .{ .name = "z2dimg", .module = mod }, .{ .name = "z2d", .module = z2d_dep.module("z2d") }, }, }); test_step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = png_mod })).step); // The JPEG tests, likewise carrying their own fixtures: fifteen files // written by Pillow, with the pixels libjpeg decodes them back to. const jpeg_mod = b.createModule(.{ .root_source_file = b.path("tests/jpeg.zig"), .target = target, .optimize = optimize, .imports = &.{ .{ .name = "z2dimg", .module = mod }, .{ .name = "z2d", .module = z2d_dep.module("z2d") }, }, }); test_step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = jpeg_mod })).step); // The EXIF tests, with their own fixtures again: files Pillow wrote with // an orientation in them, and the orientation Pillow reads back. const exif_mod = b.createModule(.{ .root_source_file = b.path("tests/exif.zig"), .target = target, .optimize = optimize, .imports = &.{ .{ .name = "z2dimg", .module = mod }, .{ .name = "z2d", .module = z2d_dep.module("z2d") }, }, }); test_step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = exif_mod })).step); // The ICC tests, which are about carrying a profile from one file to // another rather than about either codec. const icc_mod = b.createModule(.{ .root_source_file = b.path("tests/icc.zig"), .target = target, .optimize = optimize, .imports = &.{ .{ .name = "z2dimg", .module = mod }, .{ .name = "z2d", .module = z2d_dep.module("z2d") }, }, }); test_step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = icc_mod })).step); // The XBM tests, whose fixtures are hand-written because the shapes worth // testing are the ones no tool would produce today. const xbm_mod = b.createModule(.{ .root_source_file = b.path("tests/xbm.zig"), .target = target, .optimize = optimize, .imports = &.{ .{ .name = "z2dimg", .module = mod }, .{ .name = "z2d", .module = z2d_dep.module("z2d") }, }, }); test_step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = xbm_mod })).step); // The XPM tests, whose fixtures are hand-written for the same reason // XBM's are. const xpm_mod = b.createModule(.{ .root_source_file = b.path("tests/xpm.zig"), .target = target, .optimize = optimize, .imports = &.{ .{ .name = "z2dimg", .module = mod }, .{ .name = "z2d", .module = z2d_dep.module("z2d") }, }, }); test_step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = xpm_mod })).step); // The GIF tests: the container, with the compression's own tests living // beside the compressor. const gif_mod = b.createModule(.{ .root_source_file = b.path("tests/gif.zig"), .target = target, .optimize = optimize, .imports = &.{ .{ .name = "z2dimg", .module = mod }, .{ .name = "z2d", .module = z2d_dep.module("z2d") }, }, }); test_step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = gif_mod })).step); // The TGA tests, against files ImageMagick wrote in every kind the // format has. const tga_mod = b.createModule(.{ .root_source_file = b.path("tests/tga.zig"), .target = target, .optimize = optimize, .imports = &.{ .{ .name = "z2dimg", .module = mod }, .{ .name = "z2d", .module = z2d_dep.module("z2d") }, }, }); test_step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = tga_mod })).step); // The Netpbm tests: all seven magic numbers, and the two places the // family contradicts itself about which way round black and white go. const netpbm_mod = b.createModule(.{ .root_source_file = b.path("tests/netpbm.zig"), .target = target, .optimize = optimize, .imports = &.{ .{ .name = "z2dimg", .module = mod }, .{ .name = "z2d", .module = z2d_dep.module("z2d") }, }, }); test_step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = netpbm_mod })).step); // The animation tests: frames, the canvas, and the disposal state // machine between them. const anim_mod = b.createModule(.{ .root_source_file = b.path("tests/anim.zig"), .target = target, .optimize = optimize, .imports = &.{ .{ .name = "z2dimg", .module = mod }, .{ .name = "z2d", .module = z2d_dep.module("z2d") }, }, }); test_step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = anim_mod })).step); // The PCX tests, against files Pillow and ImageMagick wrote in every // shape the format has. const pcx_mod = b.createModule(.{ .root_source_file = b.path("tests/pcx.zig"), .target = target, .optimize = optimize, .imports = &.{ .{ .name = "z2dimg", .module = mod }, .{ .name = "z2d", .module = z2d_dep.module("z2d") }, }, }); test_step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = pcx_mod })).step); // The ICO tests, against icons Pillow and ImageMagick wrote in both the // shapes an entry can hold. const ico_mod = b.createModule(.{ .root_source_file = b.path("tests/ico.zig"), .target = target, .optimize = optimize, .imports = &.{ .{ .name = "z2dimg", .module = mod }, .{ .name = "z2d", .module = z2d_dep.module("z2d") }, }, }); test_step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = ico_mod })).step); // The WebP tests, against files libwebp wrote at several effort levels // and the pixels Pillow reads back out of them. const webp_mod = b.createModule(.{ .root_source_file = b.path("tests/webp.zig"), .target = target, .optimize = optimize, .imports = &.{ .{ .name = "z2dimg", .module = mod }, .{ .name = "z2d", .module = z2d_dep.module("z2d") }, }, }); test_step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = webp_mod })).step); // The hardening tests: the ways image decoders have actually been broken, // one test per class of vulnerability found in a real one. const hardening_mod = b.createModule(.{ .root_source_file = b.path("tests/hardening.zig"), .target = target, .optimize = optimize, .imports = &.{ .{ .name = "z2dimg", .module = mod }, .{ .name = "z2d", .module = z2d_dep.module("z2d") }, }, }); test_step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = hardening_mod })).step); // The fuzz targets: what a decoder must do with input nobody wrote. They // are ordinary tests as well as fuzz tests, so `zig build test` exercises // the same properties on the seeds checked in beside them. const fuzz_mod = b.createModule(.{ .root_source_file = b.path("tests/fuzz.zig"), .target = target, .optimize = optimize, .imports = &.{ .{ .name = "z2dimg", .module = mod }, .{ .name = "z2d", .module = z2d_dep.module("z2d") }, }, }); // The tools below run on this machine as part of the build, so they are // built for it rather than for `-Dtarget`, and a tool built for one // target cannot import a module built for another. Zig does not refuse // the mixture: it compiles the library against the tool's standard // library, with the tool's idea of the operating system and the target's // idea of the CPU. With `-Dtarget=x86_64-freebsd` that is a Linux tool // compiling the Capsicum backend, which fails for want of libc, or, where // nothing reaches the backend, `zig build samples` compiling and then // dying of a general protection fault in the deflate encoder's vector // code. So the tools get the library a second time, built for the host. // With no `-Dtarget` the two are the same thing and cost nothing extra. const host = b.graph.host; const host_z2d = b.dependency("z2d", .{ .target = host, .optimize = optimize }).module("z2d"); const host_mod = b.createModule(.{ .root_source_file = b.path("src/root.zig"), .target = host, .imports = libraryImports(b, host, optimize), }); const host_fuzz_mod = b.createModule(.{ .root_source_file = b.path("tests/fuzz.zig"), .target = host, .optimize = optimize, .imports = &.{ .{ .name = "z2dimg", .module = host_mod }, .{ .name = "z2d", .module = host_z2d }, }, }); // Zig's fuzzer takes one test at a time and keeps a coverage file per // test, so naming a target is what you want when a finding is being // chased: `zig build fuzz --fuzz -Dfuzz-filter=bmp`. const fuzz_filter = b.option( []const u8, "fuzz-filter", "Fuzz or test only the targets whose name contains this", ); const fuzz_tests = b.addTest(.{ .root_module = fuzz_mod, .filters = if (fuzz_filter) |f| &.{f} else &.{}, // The self-hosted backend, which Debug uses by default, emits no // coverage instrumentation: under it `--fuzz` finds an empty table // of program counters and has nothing to steer by. LLVM does. .use_llvm = true, }); test_step.dependOn(&b.addRunArtifact(fuzz_tests).step); // A step of its own for `zig build fuzz --fuzz`, holding nothing else: // the fuzzer takes over the terminal and runs until it is 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, from a // corpus of real files and with allocation failure injected on request; // `tools/fuzz.zig` says what it adds. Optimised, // because a fuzzer's whole job is how many inputs it gets through, and // ReleaseSafe keeps every check that makes a failure a failure. const fuzz_run = b.addExecutable(.{ .name = "z2dimg-fuzz", .root_module = b.createModule(.{ .root_source_file = b.path("tools/fuzz.zig"), .target = b.graph.host, .optimize = .safe, .imports = &.{.{ .name = "fuzz_targets", .module = host_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); // Writes the encoder's output for `tools/check_samples.py` to hold // against libjpeg, which is the one claim about the encoder that a Zig // test cannot make on its own: a test here can check this encoder against // this decoder, and does, but it cannot run Pillow. const samples = b.addExecutable(.{ .name = "z2dimg-samples", .root_module = b.createModule(.{ .root_source_file = b.path("tools/samples.zig"), .target = b.graph.host, .optimize = .debug, .imports = &.{ .{ .name = "z2dimg", .module = host_mod }, .{ .name = "z2d", .module = host_z2d }, }, }), }); const run_samples = b.addRunArtifact(samples); run_samples.stdio = .inherit; const samples_step = b.step("samples", "Write encoder output to zig-out/samples"); samples_step.dependOn(&b.addInstallDirectory(.{ .source_dir = run_samples.addOutputDirectoryArg("samples"), .install_dir = .prefix, .install_subdir = "samples", }).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); check_step.dependOn(&samples.step); // -- the library, and its documentation ----------------------------------- // // A consumer of this project imports the module above and never links // against a compiled object, so this artifact exists for two other // reasons. Zig emits the API documentation as a side effect of compiling, // and something has to do that compiling; and `zig build` with nothing // installed produces an empty output directory, which a Nix build rejects // as a derivation that built nothing. // // What `getEmittedDocs` produces 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 = "z2dimg", .root_module = mod }); b.installArtifact(library); 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. It is the same reason `zig std` runs a server rather // than 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); run_docs_server.addDirectoryArg(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. test_step.dependOn(&b.addRunArtifact( b.addTest(.{ .root_module = docs_server.root_module }), ).step); check_step.dependOn(&docs_server.step); } /// The library's own dependencies, built for `target`. fn libraryImports( b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode, ) []const std.Build.Module.Import { const args = .{ .target = target, .optimize = optimize }; // RIFF is the container WebP arrives in --- and WAV and AVI, which is why // it is not in here --- and VP8 is the video codec a lossy WebP carries // one keyframe of. Neither has anything to say about images, so neither // lives here. return b.allocator.dupe(std.Build.Module.Import, &.{ .{ .name = "z2d", .module = b.dependency("z2d", args).module("z2d") }, .{ .name = "riff", .module = b.dependency("riff", args).module("riff") }, .{ .name = "vp8", .module = b.dependency("vp8", args).module("vp8") }, }) catch @panic("OOM"); }