TExcelTAD
Home › Meson, Autotools & xmake › meson test, meson install & pkg-config Files

meson test, meson install & pkg-config Files

A library is not finished when it compiles: it needs tests, install rules that put headers, libraries and metadata in the right places, and a pkg-config file so other projects can find it. Meson has all three built in.

test()

test('name', executable) registers a test. An exit code of 0 is a pass, 77 a skip, anything else a failure; should_fail: true inverts it. meson test builds anything that is out of date (here everything was already built by meson compile) and runs every test:

bash · try it yourself
mkdir -p mathx/tests && cd mathx
cat > meson.build <<'M'
project('mathx', 'c')
lib = static_library('mathx', 'mathx.c')
t = executable('test_mathx', 'tests/test_mathx.c', link_with: lib)
test('gcd', t, args: ['gcd'])
test('lcm', t, args: ['lcm'])
test('negative input is rejected', t, args: ['neg'], should_fail: true)
test('slow suite', t, args: ['gcd'], suite: 'slow', timeout: 60)
M
cat > mathx.c <<'SRC'
int gcd(int a, int b) { while (b) { int t = a % b; a = b; b = t; } return a; }
int lcm(int a, int b) { return a / gcd(a, b) * b; }
SRC
cat > tests/test_mathx.c <<'SRC'
#include <string.h>
int gcd(int, int); int lcm(int, int);
int main(int argc, char **argv) {
    if (argc < 2) return 1;
    if (!strcmp(argv[1], "gcd")) return gcd(12, 18) == 6 ? 0 : 1;
    if (!strcmp(argv[1], "lcm")) return lcm(4, 6) == 12 ? 0 : 1;
    if (!strcmp(argv[1], "neg")) return 2;   /* the CLI refuses negative input with exit code 2 */
    return 1;
}
SRC
meson setup build > /dev/null
meson compile -C build -j 1 | grep '^\['
meson test -C build --num-processes 1
echo "--- only one suite"
meson test -C build --num-processes 1 --suite slow | grep -E '^ *[0-9]+/|^Ok'
[1/4] Compiling C object libmathx.a.p/mathx.c.o
[2/4] Linking static target libmathx.a
[3/4] Compiling C object test_mathx.p/tests_test_mathx.c.o
[4/4] Linking target test_mathx
ninja: Entering directory `/home/learner/mathx/build'
ninja: no work to do.
1/4 mathx:gcd                        OK              0.01s
2/4 mathx:lcm                        OK              0.00s
3/4 mathx:negative input is rejected EXPECTEDFAIL    0.01s   exit status 2
4/4 slow - mathx:slow suite          OK              0.00s

Ok:                3
Expected Fail:     1
Fail:              0

Full log written to /home/learner/mathx/build/meson-logs/testlog.txt
--- only one suite
1/1 slow - mathx:slow suite OK              0.00s
Ok:                1

Useful meson test options: --suite NAME/--no-suite NAME, a test name to run just that test, --repeat N, --wrapper valgrind (or --gdb), --print-errorlogs, --setup NAME (for setups defined with add_test_setup()). Results and logs go to build/meson-logs/testlog.txt and testlog.json (and JUnit XML, testlog.junit.xml, for CI).

Install rules

Targets are installed when you say install: true. Headers use install_headers(), other files install_data(), whole trees install_subdir(). Destinations default to the standard directories (bindir, libdir, includedir…), all relative to prefix.

pkg-config files with the pkgconfig module

Modules extend Meson; import('pkgconfig') gives a generate() function that writes a correct .pc file for a library target, including its dependencies, and installs it into libdir/pkgconfig:

bash · try it yourself
mkdir -p ring/include && cd ring
cat > meson.build <<'M'
project('ring', 'c', version: '2.1.0')
ring = library('ring', 'ring.c',
  include_directories: 'include',
  version: meson.project_version(), soversion: '2',
  install: true)
install_headers('include/ring.h', subdir: 'ring')        # -> include/ring/ring.h
executable('ringdemo', 'demo.c', link_with: ring, include_directories: 'include', install: true)

pkg = import('pkgconfig')
pkg.generate(ring,
  description: 'A tiny ring buffer',
  subdirs: 'ring')                                        # adds -I${includedir}/ring
M
echo 'int ring_size(void);' > include/ring.h
echo 'int ring_size(void) { return 64; }' > ring.c
echo '#include <stdio.h>
#include "ring.h"
int main(void) { printf("%d\n", ring_size()); return 0; }' > demo.c
meson setup build --prefix=/usr --libdir=lib > /dev/null
meson compile -C build -j 1 > /dev/null
DESTDIR=$PWD/stage meson install -C build --no-rebuild
echo "--- staged tree"
cd stage && find . -type f -o -type l | sort
echo "--- usr/lib/pkgconfig/ring.pc"
cat usr/lib/pkgconfig/ring.pc
Installing libring.so.2.1.0 to /home/learner/ring/stage/usr/lib
Installing ringdemo to /home/learner/ring/stage/usr/bin
Installing /home/learner/ring/include/ring.h to /home/learner/ring/stage/usr/include/ring
Installing /home/learner/ring/build/meson-private/ring.pc to /home/learner/ring/stage/usr/lib/pkgconfig
Installing symlink pointing to libring.so.2.1.0 to /home/learner/ring/stage/usr/lib/libring.so.2
Installing symlink pointing to libring.so.2 to /home/learner/ring/stage/usr/lib/libring.so
--- staged tree
./usr/bin/ringdemo
./usr/include/ring/ring.h
./usr/lib/libring.so
./usr/lib/libring.so.2
./usr/lib/libring.so.2.1.0
./usr/lib/pkgconfig/ring.pc
--- usr/lib/pkgconfig/ring.pc
prefix=/usr
includedir=${prefix}/include
libdir=${prefix}/lib

Name: ring
Description: A tiny ring buffer
Version: 2.1.0
Libs: -L${libdir} -lring
Cflags: -I${includedir}/ring

Points to notice:

  • DESTDIR stages the install under another root without changing the paths baked into files (the .pc file still says prefix=/usr). Package builders (deb, rpm, Flatpak) all work this way. meson install --destdir stage is equivalent.
  • --no-rebuild skips the implicit "build first" step; meson install builds anything out of date by default.
  • The executable's build-tree RPATH was removed while installing, and the soname symlinks were recreated in the staged lib/.
  • pkg.generate(ring) took the name, version and library from the target. Give it requires:/libraries: for dependencies; dependencies the library was built with are added automatically (to Requires.private / Libs.private for a shared library).

Testing the installed package

The real test of a .pc file is a consumer that knows nothing about the build tree. Point PKG_CONFIG_PATH at the staged tree and use PKG_CONFIG_SYSROOT_DIR so the /usr paths in the file are looked up inside stage/:

bash · try it yourself
mkdir -p ring/include && cd ring
cat > meson.build <<'M'
project('ring', 'c', version: '2.1.0')
ring = library('ring', 'ring.c', install: true)
install_headers('include/ring.h')
import('pkgconfig').generate(ring, description: 'A tiny ring buffer')
M
echo 'int ring_size(void);' > include/ring.h
echo 'int ring_size(void) { return 64; }' > ring.c
meson setup build --prefix=/usr --libdir=lib > /dev/null
meson install -C build --destdir $HOME/stage > /dev/null
cd ~
export PKG_CONFIG_PATH=$PWD/stage/usr/lib/pkgconfig PKG_CONFIG_SYSROOT_DIR=$PWD/stage
pkg-config --modversion ring
pkg-config --cflags --libs ring
echo '#include <stdio.h>
#include <ring.h>
int main(void) { printf("ring_size() = %d\n", ring_size()); return 0; }' > use.c
gcc use.c $(pkg-config --cflags --libs ring) -o use
LD_LIBRARY_PATH=$PWD/stage/usr/lib ./use
2.1.0
-I/home/learner/stage/usr/include -L/home/learner/stage/usr/lib -lring
ring_size() = 64

Test yourself

Which exit code makes meson test report a test as skipped?

  1. 1
  2. 77
  3. 125
  4. 255

What does DESTDIR=$PWD/stage meson install -C build change?

  1. The prefix stored in installed files
  2. Only where the files are written: everything goes under stage/, with paths inside files unchanged
  3. The build type
  4. It installs only headers

What does import('pkgconfig').generate(mylib) produce?

  1. A .pc file for mylib, installed into libdir/pkgconfig
  2. A pkg-config binary
  3. A CMake config file
  4. A wrap file