TExcelTAD
Home › Meson, Autotools & xmake › Mini Project 2: Install, Consume & Compare

Mini Project 2: Install, Consume & Compare

Part 1 built strkit with Meson and with Autotools. This part adds the third build, xmake, then proves that all three installations are usable by somebody who knows nothing about how they were built, and ends with the comparison.

Build 3: xmake

The same sources and the same requirements: shared libstrkit.so.1.2.0 with soname libstrkit.so.1, a generated version header, the skcount program, a test, and an install with a pkg-config file.

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 xmake build ----------
cat > xmake.lua <<'L'
set_project("strkit")
set_version("1.2.0", {soname = true})
add_rules("mode.debug", "mode.release")
set_languages("c17")
set_warnings("allextra")

target("strkit")
    set_kind("shared")
    add_files("src/strkit.c")
    add_includedirs("include", {public = true})
    add_headerfiles("include/strkit.h")
    -- same @VERSION@ template as Meson and Autotools
    add_configfiles("src/strkit_version.h.in", {pattern = "@(.-)@"})
    set_configdir("$(builddir)/config")
    add_includedirs("$(builddir)/config")
    add_rules("utils.install.pkgconfig_importfiles")

target("skcount")
    set_kind("binary")
    add_deps("strkit")
    add_files("tools/skcount.c")

target("test_strkit")
    set_kind("binary")
    set_default(false)              -- not built by a plain `xmake`, not installed
    add_deps("strkit")
    add_files("tests/test_strkit.c")
    add_tests("default")
L
xmake -j1 > /dev/null 2>&1
xmake test 2>&1 | grep 'tests passed' | sed 's/, spent .*//'
xmake install -o $HOME/sdk > /dev/null 2>&1
echo "--- installed"
cd ~/sdk && find . -type f -o -type l | sort
readelf -d lib/libstrkit.so.1.2.0 | grep 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"
echo "--- fix: give skcount a RUNPATH relative to itself, then reinstall"
cd ~/strkit
sed -i 's|^    add_files("tools/skcount.c")|&\n    add_rpathdirs("@loader_path/../lib")|' xmake.lua
xmake -j1 > /dev/null 2>&1 && xmake install -o $HOME/sdk > /dev/null 2>&1
readelf -d ~/sdk/bin/skcount | grep -E 'RUNPATH' | sed 's/^ *0x[0-9a-f]* //'
~/sdk/bin/skcount "hello build systems"
100% tests passed, 0 test(s) failed out of 1
--- installed
./bin/skcount
./include/strkit.h
./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: [$ORIGIN]
# Generated by Xmake
prefix=${pcfiledir}/../..
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir=${prefix}/include

Name: strkit
Description: strkit
Version: 1.2.0
Libs: -L${libdir} -lstrkit
Cflags: -I${includedir}
bin/skcount: error while loading shared libraries: libstrkit.so.1: cannot open shared object file: No such file or directory
--- fix: give skcount a RUNPATH relative to itself, then reinstall
(RUNPATH)            Library runpath: [$ORIGIN/../lib:$ORIGIN]
strkit 1.2.0: 3 words: HELLO BUILD SYSTEMS

Notes on the xmake build:

  • add_configfiles(…, {pattern = "@(.-)@"}) makes xmake read @VERSION@ placeholders (a Lua pattern), so all three builds share one template. xmake's default syntax is ${VERSION}.
  • set_default(false) keeps the test program out of the default build and the install; xmake test builds it on demand.
  • The pkg-config file is relocatable: prefix=${pcfiledir}/../.. computes the prefix from where the .pc file is found, so the installed tree can be moved or unpacked anywhere. The Meson and Autotools files contain the absolute prefix instead.
  • The first install failed the last requirement: xmake gives programs the RUNPATH $ORIGIN ("next to me"), which is right in the build tree, where skcount and libstrkit.so share build/linux/x86_64/release/, but wrong after install, where the library is in ../lib. add_rpathdirs("@loader_path/../lib") (xmake's portable spelling; it becomes $ORIGIN/../lib on Linux and @loader_path/../lib on macOS) fixes it, and because it is relative, the installed tree can still be moved. Checking the installed program, not just the build tree, is exactly why requirement 6 exists.
  • The generated .pc uses the target name as its Description; set one with the rule's options or ship your own template if it matters.

Consuming all three

The real test of an installed library: a separate project that finds it only through pkg-config. This session installs the three builds side by side (~/sdk-meson, ~/sdk-auto, ~/sdk-xmake; Meson gets --libdir=lib so the trees line up) and then builds the same consumer against each, once with plain cc and once as a Meson project using dependency('strkit').

bash · try it yourself
write_sources() {   # the same strkit sources as before, into directory $1
mkdir -p $1/include $1/src $1/tools $1/tests
cat > $1/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 > $1/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
printf '#include <stdio.h>\n#include "strkit.h"\nint main(void) { printf("%%s\\n", strkit_version()); return 0; }\n' > $1/tools/skcount.c
echo '#define STRKIT_VERSION "@VERSION@"' > $1/src/strkit_version.h.in
}

# ---------- 1. Meson ----------
write_sources m && cd m
cat > meson.build <<'M'
project('strkit', 'c', version: '1.2.0')
conf = configuration_data({'VERSION': meson.project_version()})
vh = configure_file(input: 'src/strkit_version.h.in', output: 'strkit_version.h', configuration: conf)
lib = library('strkit', 'src/strkit.c', vh, include_directories: 'include',
  version: meson.project_version(), soversion: '1', install: true)
install_headers('include/strkit.h')
import('pkgconfig').generate(lib, description: 'Small string utilities')
M
meson setup build --prefix=$HOME/sdk-meson --libdir=lib > /dev/null && meson install -C build --quiet > /dev/null
cd ~

# ---------- 2. Autotools ----------
write_sources a && cd a && mkdir -p m4
cat > configure.ac <<'AC'
AC_INIT([strkit], [1.2.0])
AC_CONFIG_AUX_DIR([build-aux])
AC_CONFIG_MACRO_DIRS([m4])
AM_INIT_AUTOMAKE([foreign subdir-objects])
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_CPPFLAGS = -I$(srcdir)/include -I$(builddir)/src
lib_LTLIBRARIES = libstrkit.la
libstrkit_la_SOURCES = src/strkit.c
libstrkit_la_LDFLAGS = -version-info 3:0:2
include_HEADERS = include/strkit.h
pkgconfig_DATA = strkit.pc
AM
printf 'prefix=@prefix@\nexec_prefix=@exec_prefix@\nlibdir=@libdir@\nincludedir=@includedir@\n\nName: strkit\nDescription: Small string utilities\nVersion: @VERSION@\nLibs: -L${libdir} -lstrkit\nCflags: -I${includedir}\n' > strkit.pc.in
autoreconf -fi > /dev/null 2>&1 && ./configure --quiet --prefix=$HOME/sdk-auto && make -j1 -s install > /dev/null 2>&1
cd ~

# ---------- 3. xmake ----------
write_sources x && cd x
cat > xmake.lua <<'L'
set_version("1.2.0", {soname = true})
add_rules("mode.release")
target("strkit")
    set_kind("shared")
    add_files("src/strkit.c")
    add_includedirs("include", {public = true})
    add_headerfiles("include/strkit.h")
    add_configfiles("src/strkit_version.h.in", {pattern = "@(.-)@"})
    set_configdir("$(builddir)/config")
    add_includedirs("$(builddir)/config")
    add_rules("utils.install.pkgconfig_importfiles")
L
xmake -j1 > /dev/null 2>&1 && xmake install -o $HOME/sdk-xmake > /dev/null 2>&1
cd ~

# ---------- the consumer: knows only "pkg-config strkit" ----------
mkdir -p consumer && cd consumer
cat > main.c <<'C'
#include <stdio.h>
#include <stdlib.h>
#include <strkit.h>
int main(void) {
    char *u = strkit_upper("consumed");
    printf("strkit %s -> %s (%zu word)\n", strkit_version(), u, strkit_count_words(u));
    free(u);
    return 0;
}
C
cat > meson.build <<'M'
project('consumer', 'c')
executable('consumer', 'main.c', dependencies: dependency('strkit', version: '>=1.2'))
M
for sdk in sdk-meson sdk-auto sdk-xmake; do
  export PKG_CONFIG_PATH=$HOME/$sdk/lib/pkgconfig LD_LIBRARY_PATH=$HOME/$sdk/lib
  echo "=== $sdk: $(pkg-config --modversion strkit), flags: $(pkg-config --cflags --libs strkit | sed "s|$HOME|~|g")"
  cc main.c $(pkg-config --cflags --libs strkit) -o consumer-cc && ./consumer-cc
  rm -rf build && meson setup build > /dev/null && meson compile -C build -j 1 > /dev/null && ./build/consumer
done
echo "=== the three install trees"
for sdk in sdk-meson sdk-auto sdk-xmake; do (cd ~/$sdk && find . -type f -o -type l | sort | tr '\n' ' '; echo); done
echo "=== move the xmake tree: its relocatable .pc still works"
mv ~/sdk-xmake ~/moved && PKG_CONFIG_PATH=~/moved/lib/pkgconfig pkg-config --libs strkit | sed "s|$HOME|~|g"
=== sdk-meson: 1.2.0, flags: -I~/sdk-meson/include -L~/sdk-meson/lib -lstrkit
strkit 1.2.0 -> CONSUMED (1 word)
strkit 1.2.0 -> CONSUMED (1 word)
=== sdk-auto: 1.2.0, flags: -I~/sdk-auto/include -L~/sdk-auto/lib -lstrkit
strkit 1.2.0 -> CONSUMED (1 word)
strkit 1.2.0 -> CONSUMED (1 word)
=== sdk-xmake: 1.2.0, flags: -I~/sdk-xmake/lib/pkgconfig/../../include -L~/sdk-xmake/lib/pkgconfig/../../lib -lstrkit
strkit 1.2.0 -> CONSUMED (1 word)
strkit 1.2.0 -> CONSUMED (1 word)
=== the three install trees
./include/strkit.h ./lib/libstrkit.so ./lib/libstrkit.so.1 ./lib/libstrkit.so.1.2.0 ./lib/pkgconfig/strkit.pc
./include/strkit.h ./lib/libstrkit.la ./lib/libstrkit.so ./lib/libstrkit.so.1 ./lib/libstrkit.so.1.2.0 ./lib/pkgconfig/strkit.pc
./include/strkit.h ./lib/libstrkit.so ./lib/libstrkit.so.1 ./lib/libstrkit.so.1.2.0 ./lib/pkgconfig/strkit.pc
=== move the xmake tree: its relocatable .pc still works
-L~/moved/lib/pkgconfig/../../lib -lstrkit

All six builds of the consumer (three installs × two consumers) produced the same program output, which is the point: pkg-config is the contract, and once a library ships a correct .pc file, nobody downstream needs to know whether it was built with Meson, Autotools, xmake or CMake. The differences are all inside the install trees.

The comparison

What the three builds of strkit taught, side by side:

Meson Autotools + libtool xmake
Build files written by hand meson.build (≈20 lines) configure.ac + Makefile.am + strkit.pc.in (≈45 lines) xmake.lua (≈30 lines)
Generated files to keep out of git none in the source tree configure, Makefile.in, aclocal.m4, build-aux/, m4/l*.m4 .xmake/, build/
Version header from one template configure_file() AC_CONFIG_FILES add_configfiles(…, {pattern = "@(.-)@"})
Soname libstrkit.so.1 soversion: '1' -version-info 3:0:2 (computed) set_version(…, {soname = true}) (from the major version)
Default libdir lib/x86_64-linux-gnu (multiarch) lib lib
Extra installed files none libstrkit.la none
.pc file generated, absolute prefix hand-written template, absolute prefix generated, relocatable (${pcfiledir})
Installed program finds the library no RPATH (needs ldconfig/LD_LIBRARY_PATH or install_rpath) RUNPATH to the absolute libdir (libtool relink) $ORIGIN only, broken after install until add_rpathdirs("@loader_path/../lib")
Test runner meson test make check xmake test
Out-of-tree build always mkdir build && ../configure build/ inside the tree
Release tarball meson dist (from git, self-testing) make distcheck (self-testing) xmake pack

Things to try on your own:

  1. Add make distcheck to the Autotools build, then break it by referencing a file that isn't distributed.
  2. Make the Meson build install a relocatable program: install_rpath: '$ORIGIN/../lib' on the executable (and --libdir=lib).
  3. Package the xmake build with a CMake config too: add utils.install.cmake_importfiles and consume it with find_package(strkit) from a CMake project (see Modern CMake).
  4. Add a fourth column: build strkit with CMake and generate the .pc file with configure_file().

Test yourself

Why could the consumer be built against all three installs without any change?

  1. The three libraries are the same file
  2. It only depends on the pkg-config contract (strkit.pc: flags, version), not on how the library was built
  3. Meson rebuilt the library each time
  4. pkg-config caches the first result

What makes xmake's generated strkit.pc relocatable?

  1. It has no paths
  2. prefix=${pcfiledir}/../..: pkg-config computes the prefix from the .pc file's own location
  3. It uses environment variables
  4. It is regenerated at install time

Which build left an extra libstrkit.la file in the install tree?

  1. Meson
  2. Autotools (libtool)
  3. xmake
  4. All three