Something went wrong. Try again.
Zig bindings to Google's WUFFS image decoders for PNG, JPEG, GIF and Netpbm
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297// SPDX-FileCopyrightText: © 2024 Mitchell Hashimoto, Ghostty contributors// SPDX-FileCopyrightText: © 2024-2026 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
const std = @import("std");
/// Translate-c as a package, since Zig 0.17 no longer translates C itself./// This is its zig-0.17.x branch; it translates with Aro rather than Clang/// and wraps the whole thing in a `Translator` whose `addIncludePath` and `defineCMacro` reach/// the translation and the compiled C alike, which is exactly the pair that/// must not drift apart here.const Translator = @import("translate_c").Translator;
/// All the C macros defined so that the header matches the build.////// These have to reach the translation and the compilation alike: the/// declarations Zig is given and the code that is actually built come from/// the same file read twice, and a module enabled for one and not the other/// is either a missing symbol at link time or a decoder that quietly is not/// there.const defines = [_][]const u8{ "WUFFS_CONFIG__MODULES", "WUFFS_CONFIG__MODULE__AUX__BASE", "WUFFS_CONFIG__MODULE__AUX__IMAGE", "WUFFS_CONFIG__MODULE__BASE", "WUFFS_CONFIG__MODULE__ADLER32", "WUFFS_CONFIG__MODULE__BMP", "WUFFS_CONFIG__MODULE__CRC32", "WUFFS_CONFIG__MODULE__DEFLATE", "WUFFS_CONFIG__MODULE__GIF", "WUFFS_CONFIG__MODULE__JPEG", "WUFFS_CONFIG__MODULE__LZW", "WUFFS_CONFIG__MODULE__NETPBM", "WUFFS_CONFIG__MODULE__PNG", "WUFFS_CONFIG__MODULE__ZLIB",};
pub fn build(b: *std.Build) !void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{});
const wuffs = b.dependency("wuffs", .{}); const translate_c = b.dependency("translate_c", .{});
// Windows always has a libc available. const windows = target.result.os.tag == .windows;
// Libc is linked wherever there is one to link, and freestanding, which // has none, goes without. WUFFS needs very little of it: the handful of // declarations in include/, and calloc and free for the // `wuffs_*__decoder__alloc` constructors it generates, which // src/c_allocator.zig supplies in Zig when there is no libc to supply // them. const docs_port = b.option(u16, "docs-port", "Port for `zig build docs-serve` (default 8000)") orelse 8000;
const link_libc = b.option( bool, "link-libc", "Link libc (default: unless the target is freestanding)", ) orelse (target.result.os.tag != .freestanding);
const module = b.addModule("wuffs", .{ .root_source_file = b.path("src/main.zig"), .target = target, .optimize = optimize, .link_libc = link_libc, });
module.addImport("wuffs_c", try translateWuffs(b, translate_c, wuffs, .{ .target = target, .optimize = optimize, .link_libc = link_libc, .windows = windows, }));
const test_module = b.createModule(.{ .root_source_file = b.path("src/main.zig"), .target = target, .optimize = optimize, .link_libc = link_libc, });
test_module.addImport("wuffs_c", try translateWuffs(b, translate_c, wuffs, .{ .target = target, .optimize = optimize, .link_libc = link_libc, .windows = windows, }));
const pixels = b.dependency("pixels", .{});
inline for (.{ "000000", "FFFFFF" }) |color| { // No "ppm": the fixtures are plain ASCII P3, which WUFFS' netpbm // decoder does not support. src/netpbm.zig tests binary P5 and P6 // data of its own instead. inline for (.{ "gif", "jpg", "png" }) |extension| { const filename = std.fmt.comptimePrint("1x1#{s}.{s}", .{ color, extension }); test_module.addAnonymousImport(filename, .{ .root_source_file = pixels.path(filename), }); } }
// A Zig consumer takes the module and compiles it into their own // program, so the static library is not really what this package is for. // It is built and installed anyway, because it is the only way `zig // build` compiles the published module at all, and because a package // with nothing to install is not a package a Nix build can express. const library = b.addLibrary(.{ .name = "wuffs", .root_module = module, });
b.installArtifact(library);
const unit_tests = b.addTest(.{ .root_module = test_module });
const run_unit_tests = b.addRunArtifact(unit_tests);
const test_step = b.step("test", "Run unit tests"); test_step.dependOn(&run_unit_tests.step);
// Compile everything without running it. Nothing else in the graph // builds the public module -- only the test module, which is a separate // copy -- so without this a build error reachable only from `module` // would go unnoticed. const check_step = b.step("check", "Compile everything without running it"); check_step.dependOn(&library.step); check_step.dependOn(&unit_tests.step);
// And the same code linked into a wasm32-freestanding program, which is // the target that has no libc at all and so is the one that proves // include/ and src/c_allocator.zig are doing their job. It is built // whatever -Dtarget says, because the point is to notice when a change // breaks the libc-free build. // // A program rather than a library, because without libc the failure is a // link error rather than a compile error and a static library never // links. See tools/freestanding.zig. The test binary cannot stand in for // it either: Zig's test runner needs a process, an environment and a way // to write to stderr, none of which freestanding has. const freestanding_target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding, });
const freestanding_wuffs = b.createModule(.{ .root_source_file = b.path("src/main.zig"), .target = freestanding_target, .optimize = .ReleaseSmall, .link_libc = false, });
freestanding_wuffs.addImport("wuffs_c", try translateWuffs(b, translate_c, wuffs, .{ .target = freestanding_target, .optimize = .ReleaseSmall, .link_libc = false, .windows = false, }));
const freestanding = b.addExecutable(.{ .name = "wuffs-freestanding", .root_module = b.createModule(.{ .root_source_file = b.path("tools/freestanding.zig"), .target = freestanding_target, .optimize = .ReleaseSmall, }), }); freestanding.entry = .disabled; freestanding.rdynamic = true; freestanding.root_module.addImport("wuffs", freestanding_wuffs);
check_step.dependOn(&freestanding.step);
// The API documentation. Zig writes it out of the doc comments as a side // effect of compiling, which is the other thing the library above is good // for: 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 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);
// It has to be served rather than opened: the viewer fetches sources.tar // and main.wasm at runtime and a browser refuses either from a file:// // page, which is the same reason `zig std` runs a server. 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);}
const TranslateOptions = struct { target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode, link_libc: bool, windows: bool,};
/// Translate WUFFS' single-file C release into Zig and compile it, as a/// module to be imported under the name `wuffs_c`.////// The one file is read twice and for two different purposes: translate-c/// reads the declarations out of it, and the C compiler builds the code/// behind them when WUFFS_IMPLEMENTATION is defined -- which is why that/// macro is in the compile flags and not among the `defineCMacro` calls.fn translateWuffs( b: *std.Build, translate_c: *std.Build.Dependency, wuffs: *std.Build.Dependency, options: TranslateOptions,) !*std.Build.Module { const source = wuffs.path("release/c/wuffs-v0.4.c");
const translator: Translator = .init(translate_c, .{ .name = "wuffs", .c_source_file = source, .target = options.target, .optimize = options.optimize, .link_libc = options.link_libc, });
// WUFFS only needs stdlib.h and string.h from libc, and only for a // handful of declarations. We provide minimal versions of these headers // so that WUFFS can be translated and compiled without libc, notably for // freestanding targets (wasm32) but this also avoids requiring an Apple // SDK for translate-c on macOS. They go first, so that they are found in // preference to the real ones. // // `Translator.addIncludePath` and `defineCMacro` apply to the translation // and to `translator.mod` together, so there is no second set of calls to // keep in step with this one. if (!options.windows) translator.addIncludePath(b.path("include")); translator.addIncludePath(wuffs.path("release/c"));
for (defines) |name| translator.defineCMacro(name, "1");
translator.mod.addCSourceFile(.{ .file = source, .flags = try cFlags(b, options.windows), });
return translator.mod;}
/// The flags WUFFS' C release is compiled with. `b.allocator` is an arena/// that lives as long as the build, so the list is never freed.fn cFlags(b: *std.Build, windows: bool) ![]const []const u8 { var flags: std.ArrayList([]const u8) = .empty;
try flags.append(b.allocator, "-DWUFFS_IMPLEMENTATION");
// Disable ubsan on Windows to avoid undefined __ubsan_handle_* // references: Zig's ubsan runtime can't be bundled on Windows (its // /exclude-symbols directives break the MSVC linker), so these handlers // would go unresolved. This affects both the MSVC and GNU ABIs. if (windows) { try flags.append(b.allocator, "-fno-sanitize=undefined"); try flags.append(b.allocator, "-fno-sanitize-trap=undefined"); }
for (defines) |key| { try flags.append(b.allocator, b.fmt("-D{s}=1", .{key})); }
return flags.items;}