#!/bin/bash
# Copyright © 2019 Collabora Ltd.
# SPDX-License-Identifier: BSD-3-Clause
# (See debian/copyright)

set -e
set -u
set -x

# Test that we can compile a simple C project using make and the
# -m32 compiler option.

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

echo "1..2"

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

cp -a examples/make-glib/* "$tmpdir"

gnu_tuple="$(dpkg-architecture -ai386 -qDEB_HOST_GNU_TYPE 2>/dev/null)"
CC="cc -m32"
CXX="c++ -m32"
PKG_CONFIG="${gnu_tuple}-pkg-config"

make -C "$tmpdir" V=1 CC="$CC" CXX="$CXX" PKG_CONFIG="$PKG_CONFIG"
file "$tmpdir/hello"
grep -F /lib/ld-linux.so.2 "$tmpdir/hello"
"$tmpdir/hello"
make -C "$tmpdir" V=1 CC="$CC" CXX="$CXX" PKG_CONFIG="$PKG_CONFIG" clean

echo "ok 1 - gcc"

CC="clang -m32"
CXX="clang++ -m32"

make -C "$tmpdir" V=1 CC="$CC" CXX="$CXX" PKG_CONFIG="$PKG_CONFIG"
file "$tmpdir/hello"
grep -F /lib/ld-linux.so.2 "$tmpdir/hello"
"$tmpdir/hello"
make -C "$tmpdir" V=1 CC="$CC" CXX="$CXX" PKG_CONFIG="$PKG_CONFIG" clean

echo "ok 2 - clang"

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