Contrasts: build2 & Zig Build
Meson, Autotools and xmake are the tools you will meet most often after CMake. Two more systems are worth knowing as contrasts, because each one takes an idea further than the others: build2 integrates the build system and the package manager around C++ modules and strict versioning, and Zig's build system replaces the configuration language with the project's own programming language. Neither is installed in the course image, so this lesson uses sample files and sessions taken from their documentation, and runnable models where an idea can be shown on its own.
build2: build system + package manager, designed together
build2 is a C++ toolchain made of b (the build system), bpkg (the package manager) and bdep (the project-management front end), plus the cppget.org package repository. It was designed from scratch with C++ in mind:
- No generator step.
breadsbuildfiles and builds directly, like xmake, with its own dependency tracking and high parallelism. - Out-of-source configurations are first class:
bdep init -C ../build-gcc @gcc cc config.cxx=g++creates a configuration directory; one project can be initialised in several (GCC, Clang, debug, release) and built in all of them at once. - Packages and the build share one model. A package's
manifestdeclares its version and dependencies;bpkgbuilds dependencies from source in the same configuration, with the same compiler and options, so there is no binary-compatibility guessing. - C++20 modules were supported early, because the build system controls the whole compile graph.
A buildfile for a library and a program:
# buildfile
import libs = libhello%lib{hello}
exe{hello}: {hxx cxx}{**} $libs testscript# manifest
: 1
name: hello
version: 0.1.0
license: MIT
depends: * build2 >= 0.17.0
depends: libhello ^1.0.0Sample session (from the build2 toolchain introduction):
bdep new -t exe -l c++ hello # project skeleton with buildfile, manifest, tests
cd hello
bdep init -C ../hello-gcc @gcc cc config.cxx=g++
b # build in every initialised configuration
b testWhat to take away: build2 shows what a C++ build looks like when the package manager is not bolted on. Its downside is the same as its strength: you adopt the whole toolchain, and its package repository is much smaller than Conan's or vcpkg's.
Zig build: the build script is a program
The zig command is also a C/C++ compiler (zig cc, based on Clang) that ships libc headers and sources for dozens of targets, so cross-compiling C to aarch64-linux-musl or x86_64-windows-gnu needs no sysroot at all. Its build system goes one step further: build.zig is ordinary Zig code that constructs a build graph through an API. The Zig course covers it in Zig Build System; here is the C-project view.
// build.zig: building a C library and program with Zig 0.15
const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{}); // -Dtarget=aarch64-linux-musl
const optimize = b.standardOptimizeOption(.{}); // -Doptimize=ReleaseFast
const lib_mod = b.createModule(.{ .target = target, .optimize = optimize, .link_libc = true });
lib_mod.addCSourceFiles(.{ .files = &.{"src/greet.c"}, .flags = &.{"-Wall"} });
lib_mod.addIncludePath(b.path("include"));
const lib = b.addLibrary(.{ .name = "greet", .root_module = lib_mod, .linkage = .static });
const exe_mod = b.createModule(.{ .target = target, .optimize = optimize, .link_libc = true });
exe_mod.addCSourceFiles(.{ .files = &.{"src/main.c"} });
exe_mod.addIncludePath(b.path("include"));
exe_mod.linkLibrary(lib);
const exe = b.addExecutable(.{ .name = "hello", .root_module = exe_mod });
b.installArtifact(exe);
}The std.Build API changes between Zig releases (this file follows 0.15, like the Zig course); check the version a project pins in build.zig.zon (minimum_zig_version).
zig build # outputs in zig-out/bin
zig build -Dtarget=aarch64-linux-musl # cross-compile: no sysroot needed
zig build -Dtarget=x86_64-windows-gnu # a Windows .exe from LinuxDependencies are declared in build.zig.zon (URL + content hash) and fetched into a global cache, a design close to Meson's wrap files with hashes. Many C projects (for example, packages in the allyourcodebase GitHub organisation) are repackaged with build.zig files so Zig projects can consume them.
The spectrum of build languages
Every tool in this course sits somewhere between "pure declaration" and "general-purpose program":
| Tool | Build description | Can it run arbitrary logic? |
|---|---|---|
| Meson | its own DSL, not Turing-complete | only via explicit run_command/custom targets |
| CMake | its own scripting language | yes (loops, functions, file I/O) |
| build2 | buildfile DSL with variables, functions, scripts |
limited, plus testscript |
| Autotools | M4 + shell | yes, anything shell can do |
| xmake | Lua, declarative style + script functions | yes, in script scope |
| Zig | Zig itself | yes, it is a program |
The model below shows the core idea behind Zig's approach (and xmake's script scope): the "build file" is code that constructs a graph through an API, and the build system then walks the graph. It is the same DAG as in Build Graphs, only built with function calls instead of parsed from a file:
// A tiny build API in the style of std.Build: steps form a graph, run() walks it once.
class Build {
constructor() { this.steps = []; }
step(name, deps, action) { const s = { name, deps, action }; this.steps.push(s); return s; }
addObject(src) { return this.step(src.replace(/\.c$/, '.o'), [], () => `cc -c ${src}`); }
addLibrary(name, srcs) {
const objs = srcs.map((s) => this.addObject(s));
return this.step(`lib${name}.a`, objs, () => `ar rcs lib${name}.a ${objs.map((o) => o.name).join(' ')}`);
}
addExecutable(name, srcs, libs) {
const objs = srcs.map((s) => this.addObject(s));
return this.step(name, [...objs, ...libs], () => `cc -o ${name} ${objs.map((o) => o.name).join(' ')} ${libs.map((l) => l.name).join(' ')}`);
}
run(goal) {
const done = new Set();
const visit = (s) => { if (done.has(s)) return; s.deps.forEach(visit); done.add(s); console.log(`[${done.size}] ${s.action()}`); };
visit(goal);
}
}
// "build.zig": ordinary code decides what the graph looks like.
const b = new Build();
const withWindowsPort = false;
const lib = b.addLibrary('greet', ['greet.c', withWindowsPort ? 'win32.c' : 'posix.c']);
const exe = b.addExecutable('hello', ['main.c'], [lib]);
b.run(exe);[1] cc -c main.c
[2] cc -c greet.c
[3] cc -c posix.c
[4] ar rcs libgreet.a greet.o posix.o
[5] cc -o hello main.o libgreet.a
Test yourself
What makes Zig's build system different from Meson's?
- It generates Ninja files
build.zigis ordinary Zig code that builds the graph through an API, instead of a separate declarative language- It cannot compile C
- It requires CMake
Why can zig build -Dtarget=aarch64-linux-musl cross-compile C code without a sysroot?
- It downloads one
- Zig ships libc headers and sources (musl, glibc stubs, mingw) for many targets and builds what it needs
- It only compiles, never links
- It uses QEMU
What is build2's main design idea compared with CMake + Conan?
- A faster Make
- Build system and package manager designed together, building dependencies from source in the same configuration
- Java-based builds
- Only header-only libraries