TExcelTAD
Home › Meson, Autotools & xmake › Mini Project 1: One Library, Three Builds

Mini Project 1: One Library, Three Builds

The Mini Project builds the same small C library and program three times, with Meson, Autotools and xmake, so you can compare the tools on identical code instead of on toy examples chosen to flatter each one.

Part Does
1. One Library, Three Builds (this lesson) the library + program + tests with Meson and with Autotools, each installed with a shared library, header, program and pkg-config file
2. Install, Consume & Compare the xmake build, then one session that installs all three side by side, consumes each through pkg-config (from plain cc and from a Meson project), and compares the results

The project: strkit

strkit is a tiny string utility library with a command-line tool:

text
strkit/
├── include/strkit.h        public API
├── src/strkit.c            implementation
├── tools/skcount.c         the program: counts words and upper-cases its argument
└── tests/test_strkit.c     unit tests
c
/* include/strkit.h */
#pragma once
#include <stddef.h>
const char *strkit_version(void);
size_t strkit_count_words(const char *s);
char *strkit_upper(const char *s);          /* caller frees */

The requirements are the same for every build system:

  1. a shared library libstrkit.so.1.2.0 with the soname libstrkit.so.1 (the ABI is at major version 1), built with warnings enabled;
  2. the version string comes from the build system, through a generated header, not from a hand-edited #define;
  3. the program skcount linked against the library;
  4. a unit test run by the build system's test runner;
  5. install puts bin/skcount, include/strkit.h, the library with its symlinks, and lib/pkgconfig/strkit.pc into the prefix;
  6. everything verified in the session: tests pass, the installed tree is listed, the soname is checked, and the installed program runs.

Every session recreates the sources from scratch with heredocs (sessions don't share files), so you can copy any one of them and run it.

Build 1: Meson

bash · try it yourself
mkdir -p strkit/include strkit/src strkit/tools strkit/tests && cd strkit
# ---------- sources (identical in every build) ----------
cat > include/strkit.h <<'H'
#pragma once
#include <stddef.h>
const char *strkit_version(void);
size_t strkit_count_words(const char *s);
char *strkit_upper(const char *s);
H
cat > src/strkit.c <<'C'
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include "strkit.h"
#include "strkit_version.h"
const char *strkit_version(void) { return STRKIT_VERSION; }
size_t strkit_count_words(const char *s) {
    size_t n = 0; int in = 0;
    for (; *s; s++) { if (isspace((unsigned char)*s)) in = 0; else if (!in) { in = 1; n++; } }
    return n;
}
char *strkit_upper(const char *s) {
    char *r = malloc(strlen(s) + 1);
    if (!r) return NULL;
    for (size_t i = 0;; i++) { r[i] = (char)toupper((unsigned char)s[i]); if (!s[i]) break; }
    return r;
}
C
cat > tools/skcount.c <<'C'
#include <stdio.h>
#include <stdlib.h>
#include "strkit.h"
int main(int argc, char **argv) {
    const char *text = argc > 1 ? argv[1] : "";
    char *up = strkit_upper(text);
    printf("strkit %s: %zu words: %s\n", strkit_version(), strkit_count_words(text), up);
    free(up);
    return 0;
}
C
cat > tests/test_strkit.c <<'C'
#include <stdlib.h>
#include <string.h>
#include "strkit.h"
int main(void) {
    char *u = strkit_upper("abc 1");
    int ok = strkit_count_words("  one two  three ") == 3 && strkit_count_words("") == 0 && strcmp(u, "ABC 1") == 0;
    free(u);
    return ok ? 0 : 1;
}
C
echo '#define STRKIT_VERSION "@VERSION@"' > src/strkit_version.h.in
# ---------- the Meson build ----------
cat > meson.build <<'M'
project('strkit', 'c', version: '1.2.0', meson_version: '>=1.1',
  default_options: ['warning_level=2', 'c_std=c17', 'buildtype=debugoptimized'])

conf = configuration_data()
conf.set('VERSION', meson.project_version())
version_h = configure_file(input: 'src/strkit_version.h.in', output: 'strkit_version.h', configuration: conf)

inc = include_directories('include')
strkit = library('strkit', 'src/strkit.c', version_h,
  include_directories: inc,
  version: meson.project_version(), soversion: '1',
  install: true)
strkit_dep = declare_dependency(link_with: strkit, include_directories: inc)
meson.override_dependency('strkit', strkit_dep)    # usable as a subproject, too
install_headers('include/strkit.h')

executable('skcount', 'tools/skcount.c', dependencies: strkit_dep, install: true)
test('strkit', executable('test_strkit', 'tests/test_strkit.c', dependencies: strkit_dep))

import('pkgconfig').generate(strkit, description: 'Small string utilities')
M
meson setup build --prefix=$HOME/sdk > /dev/null
meson compile -C build -j 1 > /dev/null
meson test -C build --num-processes 1 | grep -E '^ *1/1|^Ok|^Fail'
meson install -C build --quiet --no-rebuild
echo "--- installed"
cd ~/sdk && find . -type f -o -type l | sort
readelf -d lib/x86_64-linux-gnu/libstrkit.so.1.2.0 | grep SONAME
cat lib/x86_64-linux-gnu/pkgconfig/strkit.pc
LD_LIBRARY_PATH=lib/x86_64-linux-gnu bin/skcount "hello build systems"
1/1 strkit:strkit OK              0.01s
Ok:                1
Fail:              0
--- installed
./bin/skcount
./include/strkit.h
./lib/x86_64-linux-gnu/libstrkit.so
./lib/x86_64-linux-gnu/libstrkit.so.1
./lib/x86_64-linux-gnu/libstrkit.so.1.2.0
./lib/x86_64-linux-gnu/pkgconfig/strkit.pc
 0x000000000000000e (SONAME)             Library soname: [libstrkit.so.1]
prefix=/home/learner/sdk
includedir=${prefix}/include
libdir=${prefix}/lib/x86_64-linux-gnu

Name: strkit
Description: Small string utilities
Version: 1.2.0
Libs: -L${libdir} -lstrkit
Cflags: -I${includedir}
strkit 1.2.0: 3 words: HELLO BUILD SYSTEMS

Notes on the Meson build:

  • configure_file() produced strkit_version.h in the build directory from the project version; the library's sources list includes it so the dependency is explicit.
  • Meson put the library in lib/x86_64-linux-gnu (Debian multiarch, its default libdir on this system), and the .pc file follows. Pass --libdir=lib for a flat layout.
  • The installed skcount has no RPATH (Meson strips the build-tree RPATH on install), so it needs LD_LIBRARY_PATH when installed to a private prefix. In a system prefix, ldconfig would make that unnecessary; alternatively set install_rpath: get_option('prefix') / get_option('libdir') on the executable.
  • meson.override_dependency() means another Meson project can use strkit as a subproject with a plain dependency('strkit').

Build 2: Autotools (with libtool)

bash · try it yourself
mkdir -p strkit/include strkit/src strkit/tools strkit/tests strkit/m4 && cd strkit
# ---------- sources (identical in every build) ----------
cat > include/strkit.h <<'H'
#pragma once
#include <stddef.h>
const char *strkit_version(void);
size_t strkit_count_words(const char *s);
char *strkit_upper(const char *s);
H
cat > src/strkit.c <<'C'
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include "strkit.h"
#include "strkit_version.h"
const char *strkit_version(void) { return STRKIT_VERSION; }
size_t strkit_count_words(const char *s) {
    size_t n = 0; int in = 0;
    for (; *s; s++) { if (isspace((unsigned char)*s)) in = 0; else if (!in) { in = 1; n++; } }
    return n;
}
char *strkit_upper(const char *s) {
    char *r = malloc(strlen(s) + 1);
    if (!r) return NULL;
    for (size_t i = 0;; i++) { r[i] = (char)toupper((unsigned char)s[i]); if (!s[i]) break; }
    return r;
}
C
cat > tools/skcount.c <<'C'
#include <stdio.h>
#include <stdlib.h>
#include "strkit.h"
int main(int argc, char **argv) {
    const char *text = argc > 1 ? argv[1] : "";
    char *up = strkit_upper(text);
    printf("strkit %s: %zu words: %s\n", strkit_version(), strkit_count_words(text), up);
    free(up);
    return 0;
}
C
cat > tests/test_strkit.c <<'C'
#include <stdlib.h>
#include <string.h>
#include "strkit.h"
int main(void) {
    char *u = strkit_upper("abc 1");
    int ok = strkit_count_words("  one two  three ") == 3 && strkit_count_words("") == 0 && strcmp(u, "ABC 1") == 0;
    free(u);
    return ok ? 0 : 1;
}
C
echo '#define STRKIT_VERSION "@VERSION@"' > src/strkit_version.h.in
# ---------- the Autotools build ----------
cat > configure.ac <<'AC'
AC_INIT([strkit], [1.2.0])
AC_CONFIG_SRCDIR([src/strkit.c])
AC_CONFIG_AUX_DIR([build-aux])
AC_CONFIG_MACRO_DIRS([m4])
AM_INIT_AUTOMAKE([foreign -Wall -Werror subdir-objects])
AM_SILENT_RULES([yes])
AM_PROG_AR
LT_INIT([disable-static])
AC_PROG_CC
PKG_INSTALLDIR
AC_CONFIG_FILES([Makefile strkit.pc src/strkit_version.h])
AC_OUTPUT
AC
cat > Makefile.am <<'AM'
ACLOCAL_AMFLAGS = -I m4
AM_CFLAGS = -Wall -Wextra -std=c17
AM_CPPFLAGS = -I$(srcdir)/include -I$(builddir)/src

lib_LTLIBRARIES = libstrkit.la
libstrkit_la_SOURCES = src/strkit.c
nodist_libstrkit_la_SOURCES = src/strkit_version.h
# 1.2.0 with ABI major 1: current - age = 1, age = 2, revision = 0
libstrkit_la_LDFLAGS = -version-info 3:0:2
include_HEADERS = include/strkit.h

bin_PROGRAMS = skcount
skcount_SOURCES = tools/skcount.c
skcount_LDADD = libstrkit.la

check_PROGRAMS = tests/test_strkit
tests_test_strkit_LDADD = libstrkit.la
TESTS = tests/test_strkit

pkgconfig_DATA = strkit.pc
AM
cat > strkit.pc.in <<'PC'
prefix=@prefix@
exec_prefix=@exec_prefix@
libdir=@libdir@
includedir=@includedir@

Name: strkit
Description: Small string utilities
Version: @VERSION@
Libs: -L${libdir} -lstrkit
Cflags: -I${includedir}
PC
autoreconf -fi > /dev/null 2>&1
mkdir build && cd build
../configure --quiet --prefix=$HOME/sdk
make -j1 > /dev/null
make -j1 check 2>&1 | grep -E '^(PASS|FAIL):|^# (TOTAL|FAIL)'
make -j1 install > /dev/null 2>&1
echo "--- installed"
cd ~/sdk && find . -type f -o -type l | sort
readelf -d lib/libstrkit.so.1.2.0 | grep -E 'SONAME'
readelf -d bin/skcount | grep -E 'RUNPATH|RPATH' | sed 's/^ *0x[0-9a-f]* //'
cat lib/pkgconfig/strkit.pc
bin/skcount "hello build systems"
PASS: tests/test_strkit
# TOTAL: 1
# FAIL:  0
--- installed
./bin/skcount
./include/strkit.h
./lib/libstrkit.la
./lib/libstrkit.so
./lib/libstrkit.so.1
./lib/libstrkit.so.1.2.0
./lib/pkgconfig/strkit.pc
 0x000000000000000e (SONAME)             Library soname: [libstrkit.so.1]
(RUNPATH)            Library runpath: [/home/learner/sdk/lib]
prefix=/home/learner/sdk
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir=${prefix}/include

Name: strkit
Description: Small string utilities
Version: 1.2.0
Libs: -L${libdir} -lstrkit
Cflags: -I${includedir}
strkit 1.2.0: 3 words: HELLO BUILD SYSTEMS

Notes on the Autotools build:

  • The generated header uses the same template as Meson, @VERSION@, because AC_CONFIG_FILES substitutes @VAR@ in any file; nodist_ keeps the generated header out of the release tarball, while its template src/strkit_version.h.in (like strkit.pc.in) is distributed automatically because it is an AC_CONFIG_FILES input.
  • The build ran out of tree (build/), hence -I$(srcdir)/include -I$(builddir)/src.
  • LT_INIT([disable-static]) builds only the shared library; libtool still installs the .la file.
  • The installed skcount does run without LD_LIBRARY_PATH: libtool relinked it at install time with a RUNPATH to $HOME/sdk/lib because the prefix is not a system directory. Meson and libtool made opposite default choices here.
  • -version-info 3:0:2 was chosen backwards from the required file name: current − age = 1 (soname), age = 2, revision = 0.

Continue with Part 2: the xmake build, consumers, and the comparison.

Test yourself

Why does the installed Autotools skcount run without LD_LIBRARY_PATH, while the Meson one doesn't?

  1. Autotools links statically
  2. libtool relinks programs at install time with a RUNPATH to a non-system libdir; Meson strips the build RPATH and adds none unless install_rpath is set
  3. Meson's library is broken
  4. The Autotools build installs into /usr/lib

Which -version-info gives libstrkit.so.1.2.0 with soname libstrkit.so.1?

  1. 1:2:0
  2. 3:0:2
  3. 1:0:2
  4. 2:0:1