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

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=

    ####################
    # General warnings #
    ####################

    # Add various general warning flags in gnu-warnings for gcc versions 4.8 and later.
    if test $cc_vers_major -eq 4 -a $cc_vers_minor -ge 8 -o $cc_vers_major -gt 4; then
        CFLAGS="$CFLAGS $(load_gnu_arguments general)"
        CFLAGS="$CFLAGS $(load_gnu_arguments no-developer-general)"
    fi

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