#!/usr/bin/env bash # A libwai archive as a C developer uses it (wai/quickstart/build-libwai.sh makes one): # # bash wai/quickstart/test/c-library.sh /libwai--.tar.gz # # The archive is checked against the SHA-256 beside it and unpacked into a new directory; its # wai.pc must answer for version with its prefix there; then c/hello.c is compiled # (C11, every warning an error) with the flags `pkg-config --cflags --libs wai` gives, linked, and # run, and must print its one line. The program it links must not look for libwai when it runs. # Exit 0 when all of that holds, 1 saying what did not. set -euo pipefail die() { echo "c-library.sh: $*" >&2; exit 1; } [ $# -eq 1 ] || { echo "usage: bash wai/quickstart/test/c-library.sh -.tar.gz>" >&2; exit 2; } HERE=$(cd "$(dirname "$0")" && pwd) ARCHIVE=$(cd "$(dirname "$1")" && pwd)/$(basename "$1") base=$(basename "$ARCHIVE" .tar.gz) case "$base" in libwai-*-*-*) ;; *) die "$ARCHIVE: not a libwai--.tar.gz" ;; esac rest=${base#libwai-} version=${rest%%-*} command -v pkg-config >/dev/null || die "no pkg-config on PATH" command -v cc >/dev/null || die "no C compiler (cc) on PATH" ( cd "$(dirname "$ARCHIVE")" && sha256sum -c "$(basename "$ARCHIVE").sha256" >/dev/null ) \ || die "$(basename "$ARCHIVE") does not match its .sha256" T=$(mktemp -d) trap 'rm -rf "$T"' EXIT tar -C "$T" -xzf "$ARCHIVE" D="$T/$base" [ -d "$D" ] || die "the archive does not hold $base/" for f in include/wai.h lib/libwai.a lib/pkgconfig/wai.pc README.md LICENSE; do [ -f "$D/$f" ] || die "the archive has no $base/$f" done export PKG_CONFIG_PATH="$D/lib/pkgconfig" unset PKG_CONFIG_LIBDIR PKG_CONFIG_SYSROOT_DIR pkg-config --exists wai || die "pkg-config does not find wai in the archive's lib/pkgconfig" got=$(pkg-config --modversion wai) [ "$got" = "$version" ] || die "wai.pc says version $got, the archive's name $version" declared=$(pkg-config --variable=prefix wai) prefix=$(cd "$declared" 2>/dev/null && pwd) || die "wai.pc's prefix, $declared, is no directory" [ "$prefix" = "$(cd "$D" && pwd)" ] || die "wai.pc's prefix is $prefix, not the directory it was unpacked into" # shellcheck disable=SC2046 # the flags are words cc -std=c11 -Wall -Wextra -Werror $(pkg-config --cflags wai) "$HERE/c/hello.c" $(pkg-config --libs wai) -o "$T/hello" \ || die "hello.c does not compile and link against the archive through pkg-config" # Read whole, then searched: under pipefail, `ldd | grep -q` fails when grep stops at its first # match before ldd has written the rest (SIGPIPE), which would pass the very case it looks for. if command -v ldd >/dev/null; then deps=$(ldd "$T/hello" 2>&1 || true) ! grep -q 'libwai' <<<"$deps" || die "the program looks for libwai at run time: $(grep libwai <<<"$deps")" fi out=$("$T/hello") || die "the program failed" case "$out" in libwai:*) ;; *) die "the program printed: $out" ;; esac echo "c-library.sh: $base: hello.c compiled and linked through pkg-config ($(pkg-config --libs wai | sed "s#$D##g")), and ran: $out"