meson introspect & meson dist
Two commands finish the Meson tour: meson introspect, which lets tools (IDEs, scripts, CI) read a configured build as JSON, and meson dist, which turns a git checkout into a release tarball and proves that the tarball builds.
meson introspect
Every meson setup writes the build's description into build/meson-info/intro-*.json, and meson introspect build --<what> prints it: --targets, --dependencies, --buildoptions, --tests, --installed, --projectinfo, --buildsystem-files, --compilers, --machines… This is the stable interface IDEs such as VS Code (the Meson extension), CLion, Qt Creator and GNOME Builder use, so you never need to parse build.ninja.
The image has no jq, so the session reads the JSON with a few lines of Python:
mkdir -p proj && cd proj
cat > meson.build <<'M'
project('proj', 'c', version: '0.3.0', license: 'MIT')
crypt = dependency('libxcrypt')
lib = library('core', 'core.c', install: true)
exe = executable('tool', 'tool.c', link_with: lib, dependencies: crypt, install: true)
test('smoke', exe)
M
echo 'int core(void) { return 0; }' > core.c
echo 'int core(void); int main(void) { return core(); }' > tool.c
meson setup build --prefix=/usr > /dev/null
echo "--- project"
meson introspect build --projectinfo
echo "--- targets"
meson introspect build --targets > targets.json
python3 - <<'PY'
import json
for t in json.load(open("targets.json")):
srcs = [s.split("/proj/")[1] for g in t["target_sources"] for s in g.get("sources", [])]
print(f'{t["name"]:5} {t["type"]:15} installed={t["installed"]} sources={srcs}')
PY
echo "--- dependencies, tests, install plan"
meson introspect build --dependencies > deps.json
python3 -c 'import json; [print(d["name"], d["version"]) for d in json.load(open("deps.json"))]'
meson introspect build --tests > tests.json
python3 -c 'import json; [print(t["name"], t["cmd"][0].replace("/home/learner/proj/", "")) for t in json.load(open("tests.json"))]'
meson introspect build --installed > installed.json
python3 -c 'import json; [print(v) for v in sorted(json.load(open("installed.json")).values())]'--- project
{"version": "0.3.0", "descriptive_name": "proj", "license": ["MIT"], "license_files": [], "subproject_dir": "subprojects", "subprojects": []}
--- targets
core shared library installed=True sources=['core.c']
tool executable installed=True sources=['tool.c']
--- dependencies, tests, install plan
libxcrypt 4.4.36
smoke build/tool
/usr/bin/tool
/usr/lib/x86_64-linux-gnu/libcore.so
meson introspect meson.build --targets (with the build file instead of a build directory) works even before meson setup, from the source tree alone, though with less detail.
meson dist
meson dist makes a source release:
- it exports the committed content of the git (or Mercurial) repository, so build directories, editor files and untracked files never leak into a release;
- it includes subprojects that are used (or excludes them with
--no-include-subprojects) and runs anymeson.add_dist_script()hooks (e.g. to add generated documentation); - it packs the result as
build/meson-dist/<name>-<version>.tar.xz(or--formats gztar,zip) plus a.sha256sumfile; - it unpacks the archive, configures, builds, tests and installs it in a temporary directory. A tarball that doesn't build is never produced silently.
mkdir -p proj && cd proj
cat > meson.build <<'M'
project('proj', 'c', version: '0.3.0')
exe = executable('tool', 'tool.c')
test('smoke', exe)
M
echo 'int main(void) { return 0; }' > tool.c
echo 'build*/' > .gitignore
git init -q && git add . && git commit -qm "release 0.3.0"
echo 'scratch notes' > NOTES.txt # untracked: must not be released
meson setup build > /dev/null
meson dist -C build --formats gztar 2>&1 | grep -E '^(Created|Testing|Distribution|Ok:|Fail:)|Running all tests'
echo "--- contents"
tar -tzf build/meson-dist/proj-0.3.0.tar.gz | sortTesting distribution package /home/learner/proj/build/meson-dist/proj-0.3.0.tar.gz
[0/1] Running all tests
Ok: 1
Fail: 0
Distribution package /home/learner/proj/build/meson-dist/proj-0.3.0.tar.gz tested
Created /home/learner/proj/build/meson-dist/proj-0.3.0.tar.gz
--- contents
proj-0.3.0/
proj-0.3.0/.gitignore
proj-0.3.0/meson.build
proj-0.3.0/tool.c
NOTES.txt was not committed, so it is not in the tarball. Uncommitted changes to tracked files stop meson dist with an error unless you pass --allow-dirty. The tarball is the input to distribution packaging, and the Release Engineering course builds on it.
Note: compare this with Autotools'
make distcheck: the same idea (build the tarball, then prove it builds), but Autotools needs every distributed file listed inMakefile.am, while Meson takes the version-control system's word for it.
Test yourself
Which files end up in a meson dist tarball?
- Everything in the source directory
- The files committed to the git repository (plus used subprojects and dist-script output)
- Only files listed in
meson.build - The build directory
What does meson dist do after creating the archive?
- Uploads it
- Unpacks, configures, builds, tests and installs it in a temporary directory
- Signs it with GPG
- Nothing
An IDE wants the list of targets and their sources from a Meson build. What should it use?
- Parse
build.ninja meson introspect build --targets(JSON)ninja -t targets- Read
meson-private/build.dat