#!/bin/bash
# Copyright © 2019-2023 Collabora Ltd.
# SPDX-License-Identifier: BSD-3-Clause

set -e
set -u
set -x

case "$(dpkg --print-architecture)" in
    (amd64)
        ;;
    (*)
        echo "1..0 # SKIP - not x86_64"
        exit 0
        ;;
esac

# Test that we can compile a simple project using meson and ninja.

tmpdir="$(mktemp -d)"
cleanup () {
    rm -fr "$tmpdir"
}
trap cleanup 0 INT QUIT ABRT PIPE TERM

# Meson wants a real locale
export LC_ALL=C.UTF-8

n=0
failed=

gcc_major=$(gcc -dumpversion)
clang_major=$(clang -dumpversion | cut -d. -f1)

for compiler in \
    clang \
    "clang-${clang_major}" \
    gcc \
    "gcc-${gcc_major}" \
; do
    n=$((n + 1))

    case "$compiler" in
        (clang | "clang-${clang_major}" | gcc | "gcc-${gcc_major}")
            # default version, guaranteed to be present in SDK
            ;;

        (gcc*)
            if ! command -v "i686-linux-gnu-$compiler" >/dev/null; then
                echo "ok $n # SKIP i686-linux-gnu-$compiler not in PATH"
                continue
            fi
            ;;

        (clang*)
            if ! command -v "$compiler" >/dev/null; then
                echo "ok $n # SKIP $compiler not in PATH"
                continue
            fi
            ;;
    esac

    if \
        meson setup \
            --cross-file "i686-linux-gnu-${compiler}.txt" \
            "$tmpdir/b" \
            "$(pwd)/examples/glib" \
        && ninja -v -C "$tmpdir/b" \
        && "$tmpdir/b/hello" \
        && ninja -v -C "$tmpdir/b" clean \
    ; then
        echo "ok $n - $compiler"
    else
        echo "not ok $n - $compiler"
        failed=1
    fi

    rm -fr "$tmpdir/b"
done

echo "1..$n"

if [ -n "$failed" ]; then
    exit 1
fi

# vim:set sw=4 sts=4 et ft=sh:
