#                                                   -*- shell-script -*-
#
# This file is part of the HDF4 build script. It is processed shortly
# after configure starts and defines, among other things, flags for
# the various compilation modes.

# Choosing and Fortran Compilers
# --------------------------------------
#
# The user should be able to specify the compiler by setting the CC
# and F77 environment variables to the name of the compiler and any
# switches it requires for proper operation. If CC is unset then this
# script may set it. If CC is unset by time this script completes then
# configure will try `gcc' and `cc' in that order (perhaps some others
# too).
#
# Note: Code later in this file may depend on the value of $CC_BASENAME
#       in order to distinguish between different compilers when
#       deciding which compiler command-line switches to use.  This
#       variable is set based on the incoming value of $CC and is only
#       used within this file.

# The default compiler is `clang'.
if test "X-$CC" = "X-"; then
    CC=clang
    CC_BASENAME=clang
fi

# Figure out GNU C compiler flags
. $srcdir/config/gnu-flags

# Figure out Intel oneAPI C compiler flags
. $srcdir/config/oneapi-flags

# Figure out Intel classic C compiler flags
. $srcdir/config/intel-flags

# Figure out Clang C compiler flags
. $srcdir/config/clang-flags

# Use gfortran as the default F77 compiler.
if test "X-$F77" = "X-"; then
  F77=gfortran
  F77_BASENAME=gfortran
fi

# Figure out GNU FC compiler flags
. $srcdir/config/gnu-fflags

# compiler version strings

# check if the compiler_version_info is already set
if test -z "$cc_version_info"; then

case $CC in
    # whatever matches *pgcc* will also match *gcc*, so this one must come first
    *pgcc*)
        cc_version_info=`$CC $CFLAGS $H4_CFLAGS -V 2>&1 | grep 'pgcc'`
        ;;

    *gcc*)
        cc_version_info=`$CC $CFLAGS $H4_CFLAGS --version 2>&1 | grep -v 'PathScale' |\
            grep 'GCC' | sed 's/\(.*(GCC) [-a-z0-9\. ]*\).*/\1/'`
        ;;

    *icc*|*icx*)
        cc_version_info=`$CC $CCFLAGS $H4_CCFLAGS -V 2>&1 | grep 'Version' |\
            sed 's/\(Intel.* Compiler\).*\( Version [a-z0-9\.]*\).*\( Build [0-9]*\)/\1\2\3/'`
        ;;

    *clang*)
        cc_version_info="`$CC $CFLAGS $H4_CFLAGS --version 2>&1 |\
            grep 'clang version' | sed 's/.*clang version \([-a-z0-9\.]*\).*/\1/'`"
        ;;

    *)
        echo "No match to get cc_version_info for $CC"
        ;;
esac

fi

# get fortran version info
case $F77 in
    *gfortran*)
        fc_version_info=`$F77 $FFLAGS --version 2>&1 |\
            grep 'GCC' | sed 's/\(.*(GCC) [-a-z0-9\. ]*\).*/\1/'`
        fc_version="`$F77 $FFLAGS -v 2>&1 |grep 'gcc version' |\
                sed 's/.*gcc version \([-a-z0-9\.]*\).*/\1/'`"
        if test X != "X$fc_version"; then
            fc_version=`echo $fc_version |sed 's/[-a-z]//g'`
        fi
        ;;

    *ifc*|*ifort*|*ifx*)
        fc_version_info=`$F77 $FFLAGS -V 2>&1 | grep 'Version' |\
            sed 's/\(Intel.* Compiler\).*\( Version [a-z0-9\.]*\).*\( Build [0-9]*\)/\1\2\3/'`
        fc_version="`$F77 $FFLAGS -V 2>&1 |grep '^Intel'`"
        if test X != "X$fc_version"; then
            fc_version="`echo $fc_version |sed 's/.*Version \([-a-z0-9\.\-]*\).*/\1/'`"
        fi
        ;;

    *f95*)
        # Figure out which compiler we are using: pgf90 or Absoft f95
        RM='rm -f'
        tmpfile=/tmp/cmpver.$$
        $F77 -V >$tmpfile
        if test -s "$tmpfile"; then
            if( grep -s 'Absoft' $tmpfile > /dev/null) then
                FC_BASENAME=f95
            fi
        fi
        $RM $tmpfile
        fc_version_info=`$F77 -V | grep Absoft`
        ;;

    *g95*|*g77*)
        fc_version_info=`$F77 $FFLAGS --version 2>&1 |\
            grep 'GCC'`
        ;;

    *pgf90*)
        fc_version_info=`$F77 $FFLAGS -V 2>&1 | grep 'pgf90'`
        fc_version="`$F77 $FFLAGS -V 2>&1 |grep '^pgf90 '`"
        if test X != "X$fc_version"; then
            fc_version=`echo $fc_version |sed 's/pgf90 \([-a-z0-9\.\-]*\).*/\1/'`
        fi
        ;;

     *)
        echo "No match to get fc_version_info for $F77"
        ;;
esac

if test X != "X$fc_version"; then
    # Get the compiler version numbers
    fc_vers_major=`echo $fc_version | cut -f1 -d.`
fi
