Something went wrong. Try again.
A sans-I/O Zig library that reads and writes DEC sixel graphics as z2d surfaces.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192// 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 z2d = b.dependency("z2d", .{ .target = target, .optimize = optimize }).module("z2d");
const mod = b.addModule("sixel", .{ .root_source_file = b.path("src/root.zig"), .target = target, .optimize = optimize, .imports = &.{.{ .name = "z2d", .module = z2d }}, });
const test_step = b.step("test", "Run tests"); const check_step = b.step("check", "Compile everything without running it");
// `use_llvm` because the self-hosted backend, Debug's default, emits no // coverage instrumentation: without it `zig build test --fuzz` has no // table of program counters to work from, and kcov reports nothing. const unit_tests = b.addTest(.{ .name = "sixel-test", .root_module = mod, .use_llvm = true }); test_step.dependOn(&b.addRunArtifact(unit_tests).step); check_step.dependOn(&unit_tests.step);
// The tests that use the library the way a consumer does, through the // module: the streaming relay against readers that hand over a byte at a // time, round trips through the encoder, the libsixel fixtures, and the // fuzz targets. const integration_mod = b.createModule(.{ .root_source_file = b.path("tests/root.zig"), .target = target, .optimize = optimize, .imports = &.{ .{ .name = "sixel", .module = mod }, .{ .name = "z2d", .module = z2d }, }, }); const integration_tests = b.addTest(.{ .name = "integration-test", .root_module = integration_mod, .use_llvm = true, }); test_step.dependOn(&b.addRunArtifact(integration_tests).step); check_step.dependOn(&integration_tests.step);
// Line coverage of the library by both test binaries, merged, in // zig-out/coverage/index.html. Needs kcov, which the devshell has on // Linux. const coverage_step = b.step("coverage", "Measure test coverage of src/ with kcov"); const merge = b.addSystemCommand(&.{ "kcov", "--merge" }); const merged = merge.addOutputDirectoryArg("coverage"); for ([_]*std.Build.Step.Compile{ unit_tests, integration_tests }) |t| { const kcov = b.addSystemCommand(&.{"kcov"}); kcov.addPrefixedDirectoryArg("--include-path=", b.path("src")); merge.addDirectoryArg(kcov.addOutputDirectoryArg(t.name)); kcov.addArtifactArg(t); // The test runner reports on stderr, which a Run step with output // arguments otherwise takes as a failure; the exit code is the // verdict. kcov.expectExitCode(0); } coverage_step.dependOn(&b.addInstallDirectory(.{ .source_dir = merged, .install_dir = .prefix, .install_subdir = "coverage", }).step);
// The test pictures, shared with the samples tool below. const host_z2d = b.dependency("z2d", .{ .target = b.graph.host, .optimize = optimize }).module("z2d"); const host_sixel = hostModule(b, optimize); const pictures = b.createModule(.{ .root_source_file = b.path("tests/pictures.zig"), .target = b.graph.host, .optimize = optimize, .imports = &.{ .{ .name = "sixel", .module = host_sixel }, .{ .name = "z2d", .module = host_z2d }, }, });
// Writes the encoder's output for what tools/oracle.py checks against // libsixel, which is the one claim about the encoder a Zig test cannot // make on its own: that something else reads it back the same. const samples = b.addExecutable(.{ .name = "sixel-samples", .root_module = b.createModule(.{ .root_source_file = b.path("tools/samples.zig"), .target = b.graph.host, .optimize = .Debug, .imports = &.{ .{ .name = "sixel", .module = host_sixel }, .{ .name = "z2d", .module = host_z2d }, .{ .name = "pictures", .module = pictures }, }, }), }); check_step.dependOn(&samples.step); const run_samples = b.addRunArtifact(samples); const samples_dir = run_samples.addOutputDirectoryArg("samples"); const oracle = b.addSystemCommand(&.{ "python3", "tools/oracle.py", "check" }); oracle.addDirectoryArg(samples_dir); oracle.addDirectoryArg(b.path("tests/fixtures")); const oracle_step = b.step("oracle", "Check the encoder and decoder against libsixel (needs the devshell)"); oracle_step.dependOn(&oracle.step);
// A sixel filter for trying the library by hand: copies stdin to stdout, // and replaces each image with a line saying what it was. const example = b.addExecutable(.{ .name = "sixel-relay", .root_module = b.createModule(.{ .root_source_file = b.path("tools/relay.zig"), .target = target, .optimize = optimize, .imports = &.{ .{ .name = "sixel", .module = mod }, .{ .name = "z2d", .module = z2d }, }, }), }); check_step.dependOn(&example.step); const run_example = b.addRunArtifact(example); run_example.stdio = .inherit; run_example.addPassthruArgs(); const example_step = b.step("example", "Run the relay example over stdin"); example_step.dependOn(&run_example.step);
// -- the library, and its documentation ----------------------------------- // // A consumer imports the module and never links a compiled object, so // this artifact exists for the documentation Zig emits as a side effect // of compiling it, and so that `zig build` installs something. // Named for the project rather than the module, so that what it installs // is not mistaken for libsixel. const library = b.addLibrary(.{ .name = "zig-sixel", .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);
// The viewer fetches `sources.tar` and `main.wasm` at runtime, which a // browser refuses from a `file://` page, so reading the docs locally // means serving them. 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.addDirectoryArg(library.getEmittedDocs()); run_docs_server.addArg(b.fmt("{d}", .{docs_port})); // It runs until interrupted, so its output has to reach the terminal. 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);
test_step.dependOn(&b.addRunArtifact( b.addTest(.{ .root_module = docs_server.root_module }), ).step); check_step.dependOn(&docs_server.step);}
/// The library again, built for the machine running the build, for the tools/// that run as part of it. A tool built for one target cannot import a/// module built for another.fn hostModule(b: *std.Build, optimize: std.builtin.OptimizeMode) *std.Build.Module { const host = b.graph.host; return b.createModule(.{ .root_source_file = b.path("src/root.zig"), .target = host, .optimize = optimize, .imports = &.{.{ .name = "z2d", .module = b.dependency("z2d", .{ .target = host, .optimize = optimize }).module("z2d"), }}, });}