Something went wrong. Try again.
A sans-I/O reader and writer for Matroska and WebM in Zig.
Something went wrong. Try again.
zig-matroska build.zig
8.4 kB · 185 lines
Zig
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186// 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("matroska", .{ .root_source_file = b.path("src/root.zig"), .target = target, .optimize = optimize, });
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 = "matroska-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 ffmpeg and mkvmerge fixtures, and the fuzz targets. const integration_tests = b.addTest(.{ .name = "integration-test", .root_module = b.createModule(.{ .root_source_file = b.path("tests/root.zig"), .target = target, .optimize = optimize, .imports = &.{.{ .name = "matroska", .module = 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);
// Writes the muxer's output for what tools/oracle.py checks against // mkvmerge, mkvinfo and ffprobe, which is the one claim about the muxer // a Zig test cannot make on its own: that other software reads it back // as it was meant. const samples = b.addExecutable(.{ .name = "matroska-samples", .root_module = b.createModule(.{ .root_source_file = b.path("tools/samples.zig"), .target = b.graph.host, .optimize = .Debug, .imports = &.{.{ .name = "matroska", .module = b.createModule(.{ .root_source_file = b.path("src/root.zig"), .target = b.graph.host, .optimize = .Debug, }) }}, }), }); 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.addFileInput(b.path("tools/oracle.py")); oracle.addFileInput(b.path("tools/fixtures.py")); const oracle_step = b.step("oracle", "Check the muxer against mkvmerge, mkvinfo and ffprobe (needs the devshell)"); oracle_step.dependOn(&oracle.step);
// mkvdump: prints what is in a file, through `io.FileSource`. const mkvdump = b.addExecutable(.{ .name = "mkvdump", .root_module = b.createModule(.{ .root_source_file = b.path("tools/mkvdump.zig"), .target = target, .optimize = optimize, .imports = &.{.{ .name = "matroska", .module = mod }}, }), }); b.installArtifact(mkvdump); const run_mkvdump = b.addRunArtifact(mkvdump); run_mkvdump.stdio = .inherit; run_mkvdump.addPassthruArgs(); const example_step = b.step("example", "Run mkvdump: zig build example -- [--frames] <file>"); example_step.dependOn(&run_mkvdump.step);
// -- the schema ----------------------------------------------------------- // // src/schema.zig is generated from the XML in spec/ and committed, so that // a consumer of the library never needs zxml. `zig build schema` rewrites // it; the test step regenerates it in memory and fails if the committed // copy differs. const schema_files = [_][]const u8{ "spec/ebml_header.xml", "spec/ebml_matroska.xml" }; if (b.lazyDependency("zxml", .{ .target = b.graph.host, .optimize = .Debug })) |zxml| { const gen_mod = b.createModule(.{ .root_source_file = b.path("tools/gen_schema.zig"), .target = b.graph.host, .optimize = .Debug, .imports = &.{.{ .name = "zxml", .module = zxml.module("zxml") }}, }); const gen = b.addExecutable(.{ .name = "gen-schema", .root_module = gen_mod }); check_step.dependOn(&gen.step);
const run_gen = b.addRunArtifact(gen); run_gen.addArg("--out"); const generated = run_gen.addOutputFileArg("schema.zig"); for (schema_files) |f| run_gen.addFileArg(b.path(f)); const update = b.addUpdateSourceFiles(); update.addCopyFileToSource(generated, "src/schema.zig"); const schema_step = b.step("schema", "Regenerate src/schema.zig from spec/"); schema_step.dependOn(&update.step);
const check_gen = b.addRunArtifact(gen); check_gen.addArg("--check"); check_gen.addFileArg(b.path("src/schema.zig")); for (schema_files) |f| check_gen.addFileArg(b.path(f)); test_step.dependOn(&check_gen.step);
test_step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = gen_mod })).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. Named for the project, so that what it would install // is not mistaken for libmatroska. const library = b.addLibrary(.{ .name = "zig-matroska", .root_module = mod }); check_step.dependOn(&library.step);
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);}