Meson Fast build system

Version 1.12 · source

Start here

meson setup build
Configure into build/
meson compile -C build
Build everything
meson test -C build
Run the tests
meson install -C build
Install the project
meson configure build
Show options
meson configure build -D<opt>=<val>
Change an option
meson setup build --wipe
Reset the build dir
meson introspect build --targets
List targets as JSON

Concepts

  1. meson.build project definition
  2. Build dir options + Ninja files
  3. Artifacts targets
  4. Test results testlog
  5. Install tree prefix / DESTDIR
Configure once per build dir, then build, test and install from it

Setup

meson setup <builddir>
Configure source in cwd into builddir
meson setup <builddir> <srcdir>
Configure another source dir
meson setup <builddir> --buildtype=<type>
debug, debugoptimized, release, plain
meson setup <builddir> --prefix=<dir>
Set the install prefix
meson setup <builddir> -D<opt>=<val>
Set a project or built-in option
meson setup <builddir> --default-library=<kind>
shared, static or both
meson setup <builddir> --reconfigure
Re-run configure with same options
meson setup <builddir> --wipe
Delete and recreate the build dir
meson setup <builddir> --native-file <file>
Select native toolchain/properties
meson setup <builddir> --cross-file <file>
Cross-compile with a machine file
meson init -l <lang> -n <name>
Create a skeleton project

Configure

meson configure <builddir>
List all options and values
meson configure <builddir> -D<opt>=<val>
Change an option later
-Dwarning_level=<0-3|everything>
Compiler warning level
-Dwerror=true
Treat warnings as errors
-Db_sanitize=<kind>
address, undefined, thread
-Db_lto=true
Enable link-time optimization
-Dc_args=<flags>
Extra C compiler flags
-Dwrap_mode=<mode>
nofallback, nodownload, forcefallback

Build

meson compile -C <builddir>
Build all targets
meson compile -C <builddir> <target>
Build one target
meson compile -C <builddir> -j <N>
Limit parallel jobs
meson compile -C <builddir> -v
Show full command lines
meson compile -C <builddir> --clean
Remove build outputs
ninja -C <builddir> <target>
Call Ninja directly

Test

meson test -C <builddir>
Run all tests
meson test -C <builddir> --print-errorlogs
Show output of failures
meson test -C <builddir> <name>
Run tests by name
meson test -C <builddir> --list
List tests without running
meson test -C <builddir> --suite <suite>
Run one suite
meson test -C <builddir> --repeat <N>
Repeat each test N times
meson test -C <builddir> --gdb <name>
Run one test under gdb
meson test -C <builddir> --wrapper=<cmd>
Run tests via e.g. valgrind

Install and dist

meson install -C <builddir>
Install to the prefix
meson install -C <builddir> --destdir <dir>
Stage into a root dir
meson install -C <builddir> --dry-run
Show what would be installed
meson dist -C <builddir>
Create a release tarball
meson dist -C <builddir> --formats <fmt>
xztar, gztar or zip

Subprojects and wraps

meson wrap search <name>
Search WrapDB
meson wrap install <name>
Add a wrap file to subprojects/
meson wrap update <name>
Update a wrap to the latest
meson subprojects download
Fetch all subprojects
meson subprojects update
Update subprojects to wrap revisions

Introspect and tools

meson introspect <builddir> --targets
List build targets (JSON)
meson introspect <builddir> --buildoptions
List all options (JSON)
meson introspect <builddir> --dependencies
List dependencies (JSON)
meson devenv -C <builddir>
Shell with the build's environment
meson devenv -C <builddir> <cmd>
Run one command in that env
meson --version
Print the Meson version

meson.build

project('<name>', 'c', version: '<x.y>')
Declare project and languages
executable('<name>', '<main.c>')
Define an executable
library('<name>', <sources>)
Shared or static per default_library
shared_library('<name>', <sources>)
Always a shared library
dependency('<name>')
Find a lib via pkg-config/CMake
dependency('<n>', required: false)
Optional dependency
include_directories('<dir>')
Add include paths
subdir('<dir>')
Descend into a directory
test('<name>', <exe>)
Register a test
install_headers('<file.h>')
Install public headers
configure_file(input: .., output: .., configuration: ..)
Generate a file from a template
declare_dependency(link_with: <lib>)
Expose a library to parents
meson_options.txt
Project-defined options file
option('<name>', type: 'boolean', value: true)
Declare an option

Recipes

Minimal project with a test
project('app', 'c', version: '0.1')exe = executable('app', 'main.c', install: true)test('smoke', exe)
Build, test, install
meson setup build --buildtype=release --prefix=/usr/localmeson compile -C buildmeson test -C build --print-errorlogsmeson install -C build --destdir /tmp/stage
Library with a dependency
zdep = dependency('zlib')lib = library('core', 'core.c', dependencies: zdep)core_dep = declare_dependency(link_with: lib,  include_directories: include_directories('.'))