#!/bin/sh
# autopkgtest: verify libcmaple-dev's headers and libraries are usable
set -e

WORKDIR="${AUTOPKGTEST_TMP:-$(mktemp -d)}"

cat > "$WORKDIR/test.cpp" << 'EOF'
#include <cmaple/cmaple.h>
#include <iostream>

int main() {
    std::cout << "CMAPLE version: " << cmaple::getVersion() << std::endl;
    return 0;
}
EOF

# These flags mirror what upstream's own CMakeLists.txt sets when
# building libcmaple itself, and headers depend on all of them:
#
# - utils/tools.h pulls in "#include <cmaple_config.h>" with angle
#   brackets (not a relative include); that generated header lives at
#   /usr/include/cmaple/cmaple_config.h, so besides the normal
#   -I/usr/include we need -I/usr/include/cmaple explicitly.
# - NUM_STATES is baked into template code throughout the headers
#   (SeqRegion/SeqRegions etc.) via add_definitions(-DNUM_STATES=4);
#   consumers must define it identically or these templates won't
#   even parse.
# - utils/matrix.h has SIMD (SIMDe) template code directly in the
#   header, evaluated at the *consumer's* compile time, so the same
#   -m... ISA flags used to build libcmaple are needed here too, or
#   GCC warns about (and risks) an ABI mismatch.
CMAPLE_CXXFLAGS="-I/usr/include/cmaple -DNUM_STATES=4 \
-msse -msse2 -msse3 -mssse3 -msse4 -msse4.1 -msse4.2 -mavx"

echo "== compiling and linking against the shared library =="
g++ -std=gnu++20 $CMAPLE_CXXFLAGS "$WORKDIR/test.cpp" -o "$WORKDIR/test_shared" -lcmaple
"$WORKDIR/test_shared"

echo "== compiling and linking against the static library =="
g++ -std=gnu++20 -fopenmp $CMAPLE_CXXFLAGS "$WORKDIR/test.cpp" -o "$WORKDIR/test_static" \
    -Wl,-Bstatic -lcmaple -Wl,-Bdynamic -lz
"$WORKDIR/test_static"

echo "OK: libcmaple-dev headers/shared/static libs all usable"
