#!/bin/sh
# vim: ft=sh ts=4 sw=4 et
#---------------------------------------------------------------------------#
# Copyright (C) 2012 The University of Melbourne.
# Copyright (C) 2014, 2018, 2021-2025 The Mercury team.
# This file may only be copied under the terms of the GNU General
# Public License - see the file COPYING in the Mercury distribution.
#---------------------------------------------------------------------------#
#
# This script prepares the Mercury source tree for building with a
# C cross-compiler. Please see README.cross.md for details.
#
#---------------------------------------------------------------------------#

set -eu

host=
hostcc=

for arg
do
    case $arg in
        --host=*)
            host=${arg#--host=}
            shift 1
            ;;
        --with-cc=*)
            hostcc=${arg#--with-cc=}
            shift 1
            ;;
        *)
            break
            ;;
    esac
done

if test -z "$host"
then
    echo "You must pass --host=HOST, e.g. x86_64-w64-mingw32"
    exit 1
fi

hostcc=${hostcc:-${CC:-}}
if test -z "$hostcc"
then
    # If no C compiler is specified, then assume we want to use a gcc
    # cross-compiler.
    hostcc="${host}-gcc"
fi

# On Debian, clang -v may report "Debian clang version".
if $hostcc -v 2>&1 | grep -q 'clang version '
then
    cc_type=clang
elif $hostcc -v 2>&1 | grep -q '^gcc version '
then
    cc_type=gcc
else
    echo "You need to specify a C compiler."
    exit 1
fi

if mmc --version 2>&1 | grep -q Mercury
then
    true
else
    echo "You need a working native mmc in your PATH."
    exit 2
fi

if test configure -ot configure.ac
then
    aclocal -I m4 && autoconf
fi

if ! test -f configure.ac
then
    echo "You need to run this script at the top of the Mercury source tree."
    exit 3
fi

# Set configuration values that can only be determined by AC_TRY_RUN when
# running ./configure on the the host platform.
case "$cc_type:$host" in
    gcc:i686-*-mingw32* | gcc:x86_64-*-mingw32*)
        # Taken from the config.cache file after running configure -C in msys.
        mercury_cv_cc_type=gcc
        mercury_cv_siginfo_t=no
        mercury_cv_pc_access=no
        mercury_cv_is_bigender=no
        mercury_cv_is_littleender=yes
        mercury_cv_normal_system_retval=no
        mercury_cv_can_do_pending_io=no
        mercury_cv_gcc_labels=no
        mercury_cv_asm_labels=no
        mercury_cv_gcc_model_fast=no
        mercury_cv_gcc_model_reg=yes
        mercury_cv_cannot_use_structure_assignment=yes
        ;;
    clang:x86_64-w64-mingw32 | clang:aarch64-w64-mingw32)
        # Taken from the config.cache file after running configure -C in MSYS2
        # (on x86-64 and ARM64), using llvm-mingw for the compiler.
        mercury_cv_cc_type=clang
        mercury_cv_siginfo_t=no
        mercury_cv_pc_access=no
        mercury_cv_is_bigender=no
        mercury_cv_is_littleender=yes
        mercury_cv_normal_system_retval=no
        mercury_cv_can_do_pending_io=no
        mercury_cv_gcc_labels=no
        mercury_cv_asm_labels=no
        mercury_cv_gcc_model_fast=no
        mercury_cv_gcc_model_reg=no
        mercury_cv_cannot_use_structure_assignment=yes
        ;;

    gcc:aarch64-*linux-gnu | gcc:aarch64-*linux-musl)
        # Taken from the config.cache file after running configure -C
        # - in a Debian 10 arm64 environment (for glibc)
        # - in a Alpine Linux aarch64 environment (for musl)
        mercury_cv_cc_type=gcc
        mercury_cv_siginfo_t=yes
        mercury_cv_pc_access=no
        mercury_cv_is_bigender=no
        mercury_cv_is_littleender=yes
        mercury_cv_normal_system_retval=yes
        mercury_cv_can_do_pending_io=yes
        mercury_cv_gcc_labels=yes
        mercury_cv_asm_labels=yes
        mercury_cv_gcc_model_fast=yes
        mercury_cv_gcc_model_reg=yes
        mercury_cv_cannot_use_structure_assignment=no
        ;;
    clang:x86_64-*freebsd* | clang:x86_64-*darwin* | clang:aarch64-*darwin* |\
        clang:aarch64-*linux-gnu | clang:aarch64-*linux-musl)
        # Taken from the config.cache file after running configure -C
        # - in a FreeBSD 13.0 x86-64 environment
        # - in a macOS 10.14 x86-64 environment
        # - in a macOS 12.4 aarch64 environment
        # - in a Debian 12 arm64 environment (for glibc)
        # - in a Alpine Linux aarch64 environment (for musl)
        mercury_cv_cc_type=clang
        mercury_cv_siginfo_t=yes
        mercury_cv_pc_access=no
        mercury_cv_is_bigender=no
        mercury_cv_is_littleender=yes
        mercury_cv_normal_system_retval=yes
        mercury_cv_can_do_pending_io=yes
        mercury_cv_gcc_labels=no
        mercury_cv_asm_labels=no
        mercury_cv_gcc_model_fast=no
        mercury_cv_gcc_model_reg=no
        mercury_cv_cannot_use_structure_assignment=yes
        ;;
    *)
        echo "unknown host: $host" >&2
        exit 1
        ;;
esac

mercury_cv_cc_type=$mercury_cv_cc_type \
mercury_cv_siginfo_t=$mercury_cv_siginfo_t \
mercury_cv_pc_access=$mercury_cv_pc_access \
mercury_cv_is_bigender=$mercury_cv_is_bigender \
mercury_cv_is_littleender=$mercury_cv_is_littleender \
mercury_cv_normal_system_retval=$mercury_cv_normal_system_retval \
mercury_cv_can_do_pending_io=$mercury_cv_can_do_pending_io \
mercury_cv_gcc_labels=$mercury_cv_gcc_labels \
mercury_cv_asm_labels=$mercury_cv_asm_labels \
mercury_cv_gcc_model_fast=$mercury_cv_gcc_model_fast \
mercury_cv_gcc_model_reg=$mercury_cv_gcc_model_reg \
mercury_cv_cannot_use_structure_assignment=$mercury_cv_cannot_use_structure_assignment \
sh configure "$@" \
    --host="$host" \
    --with-cc="$hostcc"

echo
echo "If you wish to run mmake in the subdirectories, you will need to set"
echo "MMAKE_DIR=$(pwd)/scripts"
echo

exit
