TExcelTAD
Home › Meson, Autotools & xmake › The Build Directory & build.ninja

The Build Directory & build.ninja

Meson is strict about one thing above all: the source tree is read-only. Everything Meson generates goes into the build directory, and the configuration that went into a build directory (compiler, cross file, options) is fixed for its lifetime, except for the options you change explicitly with meson configure. In-source builds are refused outright:

bash · try it yourself
mkdir -p p && cd p
echo "project('p', 'c')" > meson.build
meson setup . 2>&1 | tail -2

ERROR: Source and build directories must not be the same. Create a pristine build directory.

What is in a build directory

bash · try it yourself
mkdir -p tool && cd tool
cat > meson.build <<'M'
project('tool', 'c')
executable('tool', 'main.c', 'util.c')
M
echo 'int util(void); int main(void) { return util(); }' > main.c
echo 'int util(void) { return 0; }' > util.c
meson setup build > /dev/null
meson compile -C build -j 1 > /dev/null
find build -maxdepth 2 -not -path '*/meson-private/*' | sort
build
build/.gitignore
build/.hgignore
build/.ninja_deps
build/.ninja_log
build/CACHEDIR.TAG
build/build.ninja
build/compile_commands.json
build/meson-info
build/meson-info/intro-benchmarks.json
build/meson-info/intro-buildoptions.json
build/meson-info/intro-buildsystem_files.json
build/meson-info/intro-compilers.json
build/meson-info/intro-dependencies.json
build/meson-info/intro-install_plan.json
build/meson-info/intro-installed.json
build/meson-info/intro-machines.json
build/meson-info/intro-projectinfo.json
build/meson-info/intro-targets.json
build/meson-info/intro-tests.json
build/meson-info/meson-info.json
build/meson-logs
build/meson-logs/meson-log.txt
build/meson-logs/meson-setup.txt
build/meson-private
build/tool
build/tool.p
build/tool.p/main.c.o
build/tool.p/util.c.o
Entry Purpose
build.ninja the generated build file, run by Ninja
compile_commands.json written on every setup, for clangd/clang-tidy and editors (CMake needs CMAKE_EXPORT_COMPILE_COMMANDS for this)
tool.p/ the object files of target tool (.p = "private")
meson-info/ the machine-readable description of the build, read by meson introspect and IDEs
meson-logs/meson-log.txt the full configure log: every compiler check, with the command and its output
meson-private/ Meson's internal state (pickled coredata.dat, build.dat, generated check programs). Never edit it
CACHEDIR.TAG tells backup tools the directory is a cache that can be skipped

A peek at build.ninja

You never edit build.ninja (it is regenerated whenever a meson.build changes), but reading it shows exactly what Meson asked for. The Make & Ninja course compares it with CMake's.

bash · try it yourself
mkdir -p tool && cd tool
cat > meson.build <<'M'
project('tool', 'c')
executable('tool', 'main.c')
M
echo 'int main(void) { return 0; }' > main.c
meson setup build > /dev/null
grep -A3 '^rule c_COMPILER
  

 build/build.ninja
echo ...
grep -A4 '^build tool.p/main.c.o' build/build.ninja
echo ...
grep -A2 '^build build.ninja' build/build.ninja
rule c_COMPILER
 command = /usr/local/bin/ccache cc $ARGS -MD -MQ $out -MF $DEPFILE -o $out -c $in
 deps = gcc
 depfile = $DEPFILE_UNQUOTED
...
build tool.p/main.c.o: c_COMPILER ../main.c
 DEPFILE = tool.p/main.c.o.d
 DEPFILE_UNQUOTED = tool.p/main.c.o.d
 ARGS = -Itool.p -I. -I.. -fdiagnostics-color=always -D_FILE_OFFSET_BITS=64 -Wall -Winvalid-pch -O0 -g

...
build build.ninja: REGENERATE_BUILD ../meson.build meson-private/coredata.dat
 pool = console

Three details stand out:

  • The compile rule uses deps = gcc with -MD -MQ … -MF …: GCC writes a header-dependency file, Ninja reads it into its own database and deletes it.
  • Every object gets -I flags for both the build directory (-Itool.p, -I.) and the source directory (-I..), so generated headers and real headers are found alike.
  • -fdiagnostics-color=always comes from the base option b_colorout (default always, because Ninja captures compiler output and would otherwise lose the colours). This image disables them through an empty GCC_COLORS; for plain CI logs, set -Db_colorout=never.
  • The last build build.ninja: REGENERATE_BUILD … statement makes build.ninja depend on every meson.build (and meson.options), which is how Meson re-runs itself.

Changing a configured build directory

Some things can change later, others can't:

You want to change How
an option (buildtype, warning_level, your own options) meson configure build -Dbuildtype=release (or meson setup --reconfigure build -D…)
CC/CXX, a cross or native file a new build directory, or meson setup --wipe build
nothing, just re-run the configure step meson setup --reconfigure build

The compiler cannot change because every compiler check, every flag Meson chose and every cached result in meson-private/ was computed for that compiler. Setting CC on a later command is silently ignored, which is safer than a half-switched build:

bash · try it yourself
mkdir -p tool && cd tool
cat > meson.build <<'M'
project('tool', 'c')
message('compiler is ' + meson.get_compiler('c').get_id())
M
meson setup build | grep Message
CC=clang meson setup --reconfigure build | grep Message
CC=clang meson setup --wipe build | grep Message
Message: compiler is gcc
Message: compiler is gcc
Message: compiler is clang

Running Ninja directly

meson compile is a convenience. Inside the build directory, plain ninja works too, and it has a few targets Meson generates for you:

bash · try it yourself
mkdir -p tool && cd tool
cat > meson.build <<'M'
project('tool', 'c')
executable('tool', 'main.c')
M
echo 'int main(void) { return 0; }' > main.c
meson setup build > /dev/null
cd build
ninja -j1 | tail -1
ninja -t targets | grep -E '^(all|clean|test|install|dist|reconfigure|scan-build|uninstall|clang-format|clang-tidy|coverage)\b' | sort
[2/2] Linking target tool
clean: phony
dist: phony
install: phony
reconfigure: REGENERATE_BUILD
test: phony
uninstall: phony

Test yourself

You configured build/ with GCC. Now you run CC=clang meson setup --reconfigure build. Which compiler does the build use?

  1. Clang
  2. GCC: the compiler is fixed when the build directory is created
  3. Both
  4. An error is printed

Which file in a Meson build directory holds every compiler check that was run, with its command and output?

  1. build.ninja
  2. meson-logs/meson-log.txt
  3. compile_commands.json
  4. CACHEDIR.TAG

How does a Meson build notice that meson.build changed?

  1. It doesn't; you must re-run meson setup
  2. build.ninja depends on every build file through a REGENERATE_BUILD statement
  3. A file watcher
  4. Meson compares hashes on every meson compile