#                                                   -*- 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.

#
# Prepend `$srcdir/config/gnu-warnings/` to the filename suffix(es) given as
# subroutine argument(s), remove comments starting with # and ending
# at EOL, replace spans of whitespace (including newlines) with spaces,
# and re-emit the file(s) thus filtered on the standard output stream.
#
load_gnu_arguments()
{
    set -- $(for arg; do
        sed 's,#.*$,,' $srcdir/config/gnu-warnings/${arg}
    done)
    IFS=' ' echo "$*"
}

# Get the compiler version in a way that works for gcc
# unless a compiler version is already known
#
#   cc_vendor:    The compiler name: gcc
#   cc_version:   Version number: 2.91.60, 2.7.2.1
#
if test "X-" = "X-$cc_flags_set"; then
    # PathScale compiler spits out gcc version string too. Need to
    # filter it out.
    # icc beginning with version 12 includes a "gcc version compatibility"
    # string, causing the gcc CFLAGS to be erroneously added.  The line
    # "grep -v 'icc version'" causes the discarding of any output
    # containing 'icc version'.  The cc_version for icc is correctly determined
    # and flags added in the intel-flags script.
    cc_version="`$CC $CFLAGS -v 2>&1 | grep -v 'PathScale' |\
        grep -v '^icc.*version' |\
        grep 'gcc version' | sed 's/.*gcc version \([-a-z0-9\.]*\).*/\1/'`"
    cc_vendor=`echo $cc_version |sed 's/\([a-z]*\).*/\1/'`
    cc_version=`echo $cc_version |sed 's/[-a-z]//g'`
    if test X = "X$cc_vendor" -a X != "X$cc_version"; then
        cc_vendor=gcc
    fi
    if test "-" != "$cc_vendor-$cc_version"; then
        echo "compiler '$CC' is GNU $cc_vendor-$cc_version"
    fi

    # Get the compiler version numbers
    cc_vers_major=`echo $cc_version | cut -f1 -d.`
    cc_vers_minor=`echo $cc_version | cut -f2 -d.`
    cc_vers_patch=`echo $cc_version | cut -f3 -d.`
    test -n "$cc_vers_major" || cc_vers_major=0
    test -n "$cc_vers_minor" || cc_vers_minor=0
    test -n "$cc_vers_patch" || cc_vers_patch=0
fi

# C and Fortran Compiler and Preprocessor Flags
# ---------------------------------------------------
#
# - Flags that end with `_CFLAGS' are always passed to the C compiler.
# - Flags that end with `_FFLAGS' are always passed to the Fortran
#   compiler.
# - Flags that end with `_CPPFLAGS' are passed to the C compiler
#   when compiling but not when linking.
#
#   DEBUG_CFLAGS
#   DEBUG_FFLAGS
#   DEBUG_CPPFLAGS  - Flags to pass to the compiler to create a
#                     library suitable for use with debugging
#                      tools. Usually this list will exclude
#                     optimization switches (like `-O') and include
#                     switches that turn on symbolic debugging support
#                     (like `-g').
#
#   PROD_CFLAGS
#   PROD_FFLAGS
#   PROD_CPPFLAGS   - Flags to pass to the compiler to create a
#                     production version of the library. These
#                     usually exclude symbolic debugging switches (like
#                     `-g') and include optimization switches (like
#                     `-O').
#
#   PROFILE_CFLAGS
#   PROFILE_FFLAGS
#   PROFILE_CPPFLAGS- Flags to pass to the compiler to create a
#                     library suitable for performance testing (like
#                     `-pg').  This may or may not include debugging or
#                     production flags.
#
#   FFLAGS
#   CFLAGS          - Flags can be added to these variable which
#                     might already be partially initialized. These
#                     flags will always be passed to the compiler and
#                     should include switches to turn on full warnings.
#
#                     WARNING: flags do not have to be added to the CFLAGS
#                     or FFLAGS variable if the compiler is the GNU gcc
#                     and gfortran compiler.
#
#                     FFLAGS and CFLAGS should contain *something* or else
#                     configure will probably add `-g'. For most systems
#                     this isn't a problem but some systems will disable
#                     optimizations in favor of the `-g'. The configure
#                     script will remove the `-g' flag in production mode
#                     only.
#
# These flags should be set according to the compiler being used.
# There are two ways to check the compiler. You can try using `-v' or
# `--version' to see if the compiler will print a version string.  You
# can use the value of $FOO_BASENAME which is the base name of the
# first word in $FOO, where FOO is either CC or F77 (note that the
# value of CC may have changed above).

if test "X-gcc" = "X-$cc_vendor"; then

    CFLAGS="$CFLAGS -std=c99"
    if test $cc_vers_major -ge 10; then
        CFLAGS="$CFLAGS -Wno-error=implicit-function-declaration"
    else
        CFLAGS="$CFLAGS"
    fi
    DEBUG_CFLAGS="-g -fverbose-asm"
    DEBUG_CPPFLAGS=
    PROD_CFLAGS="-O3 -fomit-frame-pointer"
    PROD_CPPFLAGS=
    PROFILE_CFLAGS="-pg"
    PROFILE_CPPFLAGS=

    #############################
    # Version-specific warnings #
    #############################

    # gcc >= 4.8
    if test $cc_vers_major -ge 5 -o $cc_vers_major -eq 4 -a $cc_vers_minor -ge 8; then
        CFLAGS="$CFLAGS $(load_gnu_arguments 4.8)"
        CFLAGS="$CFLAGS $(load_gnu_arguments no-developer-4.8)"
    fi

    # gcc >= 4.9
    if test $cc_vers_major -ge 5 -o $cc_vers_major -eq 4 -a $cc_vers_minor -ge 9; then
        CFLAGS="$CFLAGS $(load_gnu_arguments 4.9)"
    fi

    # gcc >= 5
    if test $cc_vers_major -ge 5; then
        CFLAGS="$CFLAGS $(load_gnu_arguments 5)"
    fi

    # gcc >= 6
    if test $cc_vers_major -ge 6; then
        CFLAGS="$CFLAGS $(load_gnu_arguments 6)"
    fi

    # gcc >= 7
    if test $cc_vers_major -ge 7; then
        CFLAGS="$CFLAGS $(load_gnu_arguments 7)"
    fi

    # gcc 8
    if test $cc_vers_major -ge 8; then
        CFLAGS="$CFLAGS $(load_gnu_arguments 8)"
        CFLAGS="$CFLAGS $(load_gnu_arguments no-developer-8)"
    fi

    # gcc 9
    if test $cc_vers_major -ge 9; then
        CFLAGS="$CFLAGS $(load_gnu_arguments 9)"
    fi

    # gcc >= 9.3
    if test $cc_vers_major -ge 10 -o $cc_vers_major -eq 9 -a $cc_vers_minor -ge 3; then
        CFLAGS="$CFLAGS $(load_gnu_arguments 9.3)"
    fi

    #################
    # Flags are set #
    #################
    cc_flags_set=yes
fi

# Clear cc info if no flags set
if test "X$cc_flags_set" = "X"; then
    cc_vendor=
    cc_version=
fi
