Subprojects & Wrap Files
A subproject is another Meson project built as part of yours, from the subprojects/ directory. It is Meson's answer to CMake's FetchContent and to vendoring: if a dependency isn't installed on the system, build it from source alongside your project, and hand your targets the same kind of dependency object either way.
A subproject in a directory
The simplest subproject is a directory: subprojects/<name>/ containing a normal Meson project. The subproject declares a dependency object with declare_dependency() and announces it with meson.override_dependency(); then the parent simply calls dependency(), with a fallback:
mkdir -p app/subprojects/ring && cd app
# --- the subproject: an ordinary Meson project
cat > subprojects/ring/meson.build <<'M'
project('ring', 'c', version: '2.1.0')
ring_lib = static_library('ring', 'ring.c')
ring_dep = declare_dependency(link_with: ring_lib, include_directories: '.')
meson.override_dependency('ring', ring_dep)
M
echo 'int ring_size(void);' > subprojects/ring/ring.h
echo 'int ring_size(void) { return 64; }' > subprojects/ring/ring.c
# --- the parent project
cat > meson.build <<'M'
project('app', 'c')
ring = dependency('ring', version: '>=2.0', fallback: 'ring')
executable('app', 'main.c', dependencies: ring)
M
echo '#include <stdio.h>
#include "ring.h"
int main(void) { printf("ring %d\n", ring_size()); return 0; }' > main.c
meson setup build | grep -E 'ring|Subproj|fallback'
meson compile -C build -j 1 | grep '^\['
./build/appRun-time dependency ring found: NO (tried pkg-config and cmake)
Looking for a fallback subproject for the dependency ring
Executing subproject ring for machine: host
ring| Project name: ring
ring| Project version: 2.1.0
ring| C compiler for the host machine: /usr/local/bin/ccache cc (gcc 13.3.0 "cc (Ubuntu 13.3.0-6ubuntu2~24.04.1) 13.3.0")
ring| C linker for the host machine: cc ld.bfd 2.42
ring| Build targets in project: 1
ring| Subproject ring finished.
Dependency ring for host machine found: YES 2.1.0 (overridden)
Subprojects (for host machine)
ring: YES
[1/4] Compiling C object subprojects/ring/libring.a.p/ring.c.o
[2/4] Linking static target subprojects/ring/libring.a
[3/4] Compiling C object app.p/main.c.o
[4/4] Linking target app
ring 64
The order is the important part: Meson first looks on the system (pkg-config, then CMake); only because no ring.pc exists does it fall back to the subproject. A distribution packager who has ring installed gets the system library; a developer on a bare machine gets it built from source. The same meson.build serves both. --wrap-mode=nofallback forbids falling back (packagers use it), --force-fallback-for=ring forces the bundled copy.
Subproject lines in the output are prefixed with the subproject's name (ring|), and its targets are built under build/subprojects/ring/.
Wrap files
Copying third-party code into subprojects/ bloats the repository. A wrap file, subprojects/<name>.wrap, describes where to get the source instead:
| Wrap type | Gets the source from |
|---|---|
[wrap-file] |
a source archive (source_url, source_filename, source_hash SHA-256), optionally with a patch archive or a patch_directory of Meson build files for projects that don't use Meson |
[wrap-git] |
a git repository at a revision (tag or commit) |
[wrap-redirect] |
points at a wrap file inside another subproject (nested dependencies) |
[wrap-hg], [wrap-svn] |
Mercurial / Subversion |
The [provide] section says which dependency() names the wrap can satisfy, so the parent doesn't need fallback: at all.
WrapDB (wrapdb.mesonbuild.com) is a public collection of ready-made wrap files for hundreds of libraries, used through meson wrap install <name>. It needs the network, so in this course's offline sandbox it fails:
mkdir -p app/subprojects && cd app
echo "project('app', 'c')" > meson.build
meson wrap install zlib 2>&1 | tail -1WrapDB connection failed to https://wrapdb.mesonbuild.com/v2/releases.json with error <urlopen error [Errno -3] Temporary failure in name resolution>.
A wrap file working offline
A wrap-file doesn't have to download anything if the archive is already in subprojects/packagecache/: Meson checks the SHA-256 and unpacks it. That is how air-gapped and CI builds use wraps (pre-fill the cache, or run meson subprojects download once while online). This session builds a release tarball of a small library, puts it in the cache, and writes the wrap file for it:
# --- an upstream release tarball (normally downloaded from source_url)
mkdir -p upstream/tinyfmt-1.2.0 && cd upstream/tinyfmt-1.2.0
cat > meson.build <<'M'
project('tinyfmt', 'c', version: '1.2.0')
lib = static_library('tinyfmt', 'tinyfmt.c')
tinyfmt_dep = declare_dependency(link_with: lib, include_directories: '.')
M
echo 'const char *tinyfmt_version(void);' > tinyfmt.h
echo 'const char *tinyfmt_version(void) { return "1.2.0"; }' > tinyfmt.c
cd .. && tar --sort=name --mtime=@$SOURCE_DATE_EPOCH --owner=0 --group=0 -cf - tinyfmt-1.2.0 | gzip -n > tinyfmt-1.2.0.tar.gz
cd ..
# --- the consuming project, with the archive already in the package cache
mkdir -p app/subprojects/packagecache && cd app
cp ../upstream/tinyfmt-1.2.0.tar.gz subprojects/packagecache/
cat > subprojects/tinyfmt.wrap <<W
[wrap-file]
directory = tinyfmt-1.2.0
source_url = https://example.com/releases/tinyfmt-1.2.0.tar.gz
source_filename = tinyfmt-1.2.0.tar.gz
source_hash = $(sha256sum subprojects/packagecache/tinyfmt-1.2.0.tar.gz | cut -d' ' -f1)
[provide]
tinyfmt = tinyfmt_dep
W
cat > meson.build <<'M'
project('app', 'c')
executable('app', 'main.c', dependencies: dependency('tinyfmt'))
M
echo '#include <stdio.h>
#include "tinyfmt.h"
int main(void) { printf("tinyfmt %s\n", tinyfmt_version()); return 0; }' > main.c
meson setup build | grep -E 'tinyfmt|cache'
meson compile -C build -j 1 > /dev/null && ./build/app
ls subprojects
echo "--- a corrupted cache is caught by the hash"
echo garbage >> subprojects/packagecache/tinyfmt-1.2.0.tar.gz
rm -rf subprojects/tinyfmt-1.2.0
meson setup --wipe build 2>&1 | grep -iE 'hash|error' | cut -c1-90C compiler for the host machine: /usr/local/bin/ccache cc (gcc 13.3.0 "cc (Ubuntu 13.3.0-6ubuntu2~24.04.1) 13.3.0")
Run-time dependency tinyfmt found: NO (tried pkg-config and cmake)
Looking for a fallback subproject for the dependency tinyfmt
Using tinyfmt source from cache.
Executing subproject tinyfmt for machine: host
tinyfmt| Project name: tinyfmt
tinyfmt| Project version: 1.2.0
tinyfmt| C compiler for the host machine: /usr/local/bin/ccache cc (gcc 13.3.0 "cc (Ubuntu 13.3.0-6ubuntu2~24.04.1) 13.3.0")
tinyfmt| C linker for the host machine: cc ld.bfd 2.42
tinyfmt| Build targets in project: 1
tinyfmt| Subproject tinyfmt finished.
Dependency tinyfmt for host machine from subproject subprojects/tinyfmt-1.2.0 found: YES 1.2.0
tinyfmt: YES
tinyfmt 1.2.0
packagecache
tinyfmt-1.2.0
tinyfmt.wrap
--- a corrupted cache is caught by the hash
ERROR: Subproject tinyfmt is buildable: NO
meson.build:2:42: ERROR: Incorrect hash for source:
[provide] tinyfmt = tinyfmt_dep names the variable in the subproject that holds the dependency, so this subproject didn't even need override_dependency(). The archive was unpacked into subprojects/tinyfmt-1.2.0/ (add it to .gitignore; commit only the .wrap file).
Managing subprojects
meson subprojects works on all wraps at once: download (fetch everything, e.g. before going offline), update (move wrap-git checkouts to their new revision), checkout BRANCH, foreach CMD, and purge --confirm to delete downloaded sources and the cache. meson wrap manages the wrap files themselves: list, search, info, install, update, status against WrapDB (all online), and promote to move a nested subproject's wrap up to the top level.
Test yourself
dependency('ring', fallback: 'ring') on a machine where ring.pc is installed. What is used?
- The subproject, always
- The system library found by pkg-config; the subproject is only a fallback
- Both
- Configure fails because of the conflict
How can a [wrap-file] subproject be used without network access?
- It can't
- Put the archive in
subprojects/packagecache/; Meson verifiessource_hashand unpacks it - Set
source_urltooffline - Use
meson wrap install --offline
What does the [provide] section of a wrap file do?
- Lists files to install
- Maps dependency names to the subproject variable that holds them, so
dependency()falls back automatically - Pins the git revision
- Sets compiler flags