TExcelTAD
Home › Meson, Autotools & xmake › Field Notes: Meson & xmake

Field Notes: Meson & xmake

Practical lessons from real Meson and xmake projects: the rules that look annoying until you understand them, and the settings that make CI builds fast and predictable.

1. Meson has no globbing, and the workaround is a trap

New Meson users look for glob('src/*.c') and don't find it. That is deliberate. A build system only re-runs its configure step when a build file changes; adding src/new.c changes no build file, so a glob evaluated at configure time silently goes stale. CMake has the same problem with file(GLOB) (see CMake Field Notes); Meson simply doesn't offer the footgun.

People then write the "workaround": run find or ls from meson.build. This session shows why it is wrong:

bash · try it yourself
mkdir -p g/src && cd g
cat > meson.build <<'M'
project('g', 'c')
# anti-pattern: globbing through a shell command at configure time
srcs = run_command('sh', '-c', 'ls src/*.c', check: true).stdout().strip().split('\n')
message('sources: ' + ' '.join(srcs))
executable('app', srcs)
M
echo 'int helper(void); int main(void) { return helper(); }' > src/main.c
echo 'int helper(void) { return 0; }' > src/helper.c
meson setup build | grep Message
meson compile -C build -j 1 > /dev/null && echo "build 1 ok"
echo "--- add src/extra.c and use it"
echo 'int extra(void) { return 0; }' > src/extra.c
echo 'int helper(void); int extra(void); int main(void) { return helper() + extra(); }' > src/main.c
meson compile -C build -j 1 2>&1 | grep -E 'undefined reference|Regenerat|^\[' | sed 's/^.*: //' | sort -u
Message: sources: src/helper.c src/main.c
build 1 ok
--- add src/extra.c and use it
[1/2] Compiling C object app.p/src_main.c.o
[2/2] Linking target app
undefined reference to `extra'

The new file never reached the build: no reconfigure happened, and the link failed with a confusing undefined reference. The fix is to list sources explicitly (in each directory's meson.build, with files()), and to treat the list as part of the code review. Editors and meson rewrite target app add src/extra.c make that cheap. If you really generate the list, write it into a file that meson.build reads with fs.read(), so Meson tracks that file as a build input and reconfigures when it changes.

2. Pin and check wraps like any other dependency

  • Every wrap-file carries a source_hash; every wrap-git should name a tag or commit, not a branch (revision = head makes builds non-reproducible).
  • Commit the .wrap files, ignore the downloaded directories (subprojects/*/ except your own in-tree subprojects, and subprojects/packagecache/).
  • CI that must not touch the network: run meson subprojects download once while online and cache subprojects/packagecache/, then configure with --wrap-mode=nodownload. Distribution packagers use --wrap-mode=nofallback to guarantee system libraries are used.

3. Watch the defaults that differ from CMake

Meson default Consequence What to do
buildtype=debug -O0 -g unless told otherwise pass --buildtype=release (or debugoptimized) for benchmarks and packages
b_colorout=always escape codes in CI logs -Db_colorout=never in CI
ccache used automatically if on PATH surprising cache hits/misses, ccache in the compiler name fine on developer machines; set CC="cc" explicitly or remove ccache from PATH to measure raw compile time
b_asneeded=true, b_lundef=true shared libraries get -Wl,--no-undefined: a missing dependency fails at link time good: keep it; fix the missing dependencies:
default_library=shared library() builds .so files packagers choose; embedded projects often set default_options: ['default_library=static']

4. xmake's package cache

xmake keeps three things per user, all under ~/.xmake/ by default:

Path Holds Size
~/.xmake/repositories/ clones of package-recipe repositories (xmake-repo, your own) small
~/.xmake/cache/packages/ downloaded source archives and git checkouts medium
~/.xmake/packages/<l>/<name>/<version>/<hash>/ installed packages, one directory per configuration hash (platform, arch, toolchain, mode, configs) large

Practical consequences:

  • Cache ~/.xmake/packages in CI, keyed on xmake.lua (and the lock file). A warm cache turns a dependency build of minutes into a check of seconds; a toolchain or config change produces a new hash directory instead of corrupting the old one.
  • xmake require --list shows which installed package directory a project uses; xmake require --clean removes cached downloads and packages no project references any more; xmake require --force rebuilds them.
  • Pin versions in add_requires("zlib 1.3.1") rather than "zlib" (latest), and enable set_policy("package.requires_lock", true) to write xmake-requires.lock.
  • On machines without internet access, set xmake g --network=private and point at an internal mirror of the recipe repository with add_repositories(), plus a pre-filled package directory.
bash · try it yourself
xmake g --help | grep -E -- '--(cachedir|network|ccache)='
xmake require --help | grep -E -- '^ +-(l|c|f), '
        --ccache=CCACHE                    Enable or disable the c/c++ compiler cache.
        --cachedir=CACHEDIR                The global cache directory.
        --network=NETWORK                  Set the network mode. (default: public)
    -c, --clean                      Clear all package caches and uninstall all not-referenced packages.
    -f, --force                      Force to reinstall all package dependencies.
    -l, --list                       List all package dependencies in project.

5. Keep xmake's generated state out of git

xmake writes .xmake/ (configuration and detection cache) and build/ into the project directory. Add both to .gitignore. When something behaves oddly after a toolchain upgrade, xmake f -c (clear configuration) is the equivalent of deleting a Meson build directory.

Test yourself

Why is run_command('ls', 'src/*.c') in meson.build a bad idea?

  1. ls is slow
  2. It runs only at configure time; adding a file changes no build input, so Meson doesn't reconfigure and the new file is silently ignored
  3. Meson forbids run_command
  4. It breaks cross-compiling

What should CI cache to speed up an xmake project with many add_requires?

  1. .xmake/
  2. ~/.xmake/packages (installed packages, keyed by configuration hash)
  3. build/.objs
  4. Nothing can be cached

Which Meson setting stops subprojects from downloading anything during configure?

  1. --wrap-mode=nodownload
  2. -Db_colorout=never
  3. --buildtype=plain
  4. --no-subprojects