Toolkit: muon, samurai & meson wrap
Meson's file format and Ninja's file format are both small, documented languages, so they have second implementations. Together with Meson's own helper commands, they form the toolkit around a Meson build.
muon: Meson in C
muon is an independent implementation of the Meson language, written in C99 with no dependencies. It reads the same meson.build files and writes the same kind of build.ninja, and it is small enough to bootstrap a system from scratch (Meson itself needs Python). It also includes a formatter, an analyzer (finds unused variables and type errors without configuring), and its own built-in Ninja-compatible build tool, based on samurai.
muon is not in the course image, so this is a sample session (from muon's command-line interface; output shortened):
muon setup build # like meson setup
muon samu -C build # build with the samurai built into muon
muon -C build test # run tests
muon analyze # static analysis of meson.build files
muon fmt -i meson.build # format in placemuon implements most, but not all, of Meson: some modules (gnome, qt, rust, cmake subprojects) and newer features may be missing, so a project that wants to support it should test with it in CI. Its main users are distributions and bootstrap environments that cannot assume Python.
samurai: Ninja in C
samurai (samu) is a Ninja-compatible build tool in about 3,000 lines of C99. It reads build.ninja, supports the same options most people use (-C, -j, -n, -v, -t subtools such as clean, compdb, targets, query, graph) and the .ninja_log/.ninja_deps files. Meson accepts it as its backend tool: if ninja is not found, meson setup looks for samu, and the NINJA environment variable can point at either. Alpine Linux, for example, ships samurai as its ninja.
Two things to remember when you meet it: the output format of progress lines differs slightly from Ninja's, and samurai implements Ninja's file format up to the features Meson and CMake generate, not every experimental Ninja option.
The meson wrap tool
meson wrap manages wrap files against WrapDB (all of these need the network; the offline failure was shown in Subprojects & Wrap Files):
meson wrap --help | sed -n '/^Commands:/,$p'Commands:
{list,search,install,update,info,status,promote,update-db}
list show all available projects
search search the db by name
install install the specified project
update Update wrap files from WrapDB (Since
0.63.0)
info show available versions of a project
status show installed and available versions of
your projects
promote bring a subsubproject up to the master
project
update-db Update list of projects available in
WrapDB (Since 0.61.0)
Typical online workflow:
meson wrap search json # find projects
meson wrap info nlohmann_json # list available versions
meson wrap install nlohmann_json # writes subprojects/nlohmann_json.wrap
meson wrap status # which installed wraps have newer versions
meson wrap update nlohmann_jsonBuilt into meson: format, rewrite, env2mfile, devenv
These are part of Meson itself and work offline:
mkdir -p p && cd p
cat > meson.build <<'M'
project('p','c',version:'1.0')
executable('app',['main.c'],install:true)
M
echo 'int main(void) { return 0; }' > main.c
echo 'int util;' > util.c
echo "--- meson format: one canonical style (Meson 1.5+)"
meson format -i meson.build && cat meson.build
echo "--- meson rewrite: edit meson.build from scripts or IDEs"
meson rewrite target app add util.c
meson rewrite kwargs set project / version 1.1
cat meson.build
echo "--- meson env2mfile: turn today's environment into a native or cross file"
CC=clang CFLAGS=-O1 meson env2mfile --native -o native.ini 2>&1
grep -E "^(c|ar|pkg-config) =|^c_args" native.ini
echo "--- meson devenv: a shell with the build tree on PATH"
meson setup build > /dev/null
meson devenv -C build sh -c 'command -v app || echo "(app not built yet)"; echo "${PATH%%:*}"'--- meson format: one canonical style (Meson 1.5+)
project('p', 'c', version: '1.0')
executable('app', ['main.c'], install: true)
--- meson rewrite: edit meson.build from scripts or IDEs
project('p', 'c', version : '1.1')
executable('app', ['main.c', 'util.c'], install: true)
--- meson env2mfile: turn today's environment into a native or cross file
NOTICE: This functionality is experimental and subject to change.
c = ['clang']
ar = ['/usr/bin/ar']
pkg-config = ['/usr/bin/pkg-config']
c_args = ['-O1']
--- meson devenv: a shell with the build tree on PATH
(app not built yet)
/home/learner/p/build/
| Command | Use it for |
|---|---|
meson format (meson fmt) |
consistent formatting; --check-only in CI, options in a meson.format file |
meson rewrite |
adding/removing sources, changing keyword arguments from tools and IDEs (it touches only what you ask for; its spacing may need a meson format pass afterwards) |
meson env2mfile |
turning CC/CFLAGS-style environments or Debian cross setups (--debarch arm64) into a cross or native file |
meson devenv |
a shell where uninstalled programs, libraries and PKG_CONFIG_PATH point into the build tree |
meson introspect |
JSON about the build for tools (lesson) |
Editors: the VS Code Meson extension (syntax, formatting, targets from introspection), and GNOME Builder, Qt Creator, CLion and KDevelop support Meson projects natively. Every Meson build directory also has a compile_commands.json for clangd.
Test yourself
What is muon?
- A Meson plugin
- An independent implementation of the Meson language in C, with no Python dependency
- A Ninja replacement only
- A package registry
Your build container has samu but no ninja. Can Meson use it?
- No
- Yes: Meson looks for
samuwhenninjaisn't found (or use theNINJAenvironment variable) - Only with a plugin
- Only on Alpine
Which command adds util.c to the sources of target app from a script, without editing the file by hand?
meson configure -Dsources+=util.cmeson rewrite target app add util.cmeson format --add util.cmeson wrap install util.c