#                                                   -*- 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/clang-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_clang_arguments()
{
    set -- $(for arg; do
        sed 's,#.*$,,' $srcdir/config/clang-warnings/${arg}
    done)
    IFS=' ' echo "$*"
}
# Get the compiler version in a way that works for clang
# unless a compiler version is already known
#
#   cc_vendor:    The compiler name: clang
#   cc_version:   Version number: 6.0.0, 7.3.0, ... 10.0.1
#
if test "X-" = "X-$cc_flags_set"; then
    # clang -v will return version number following "clang" on Linux machines,
    # but on Xcode the version number will follow "Apple LLVM version"
    # Note that the Xcode reported LLVM version doesn't match the canonical
    # LLVM version, so you'll need to do different version checks for
    # Xcode.
    cc_version="`$CC $CFLAGS -v 2>&1 |\
        grep 'clang version' | sed 's/.*clang version \([-a-z0-9\.]*\).*/\1/'`"
    if test -n "$cc_version"; then
        cc_vendor="clang"
    else
        cc_version="`$CC $CFLAGS $H4_CFLAGS -v 2>&1 |\
            grep 'Apple LLVM version' | sed 's/.*Apple LLVM version \([-a-z0-9\.]*\).*/\1/'`"
        if test -n "$cc_version"; then
            cc_vendor="Apple LLVM"
        fi
    fi
    if test "X-" != "X-$cc_version"; then

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

if test "X-clang" = "X-$cc_vendor" -o "X-Apple LLVM" = "X-$cc_vendor"; then
    echo "compiler '$CC' is $cc_vendor-$cc_version"

    CFLAGS="$CFLAGS -Wno-error=implicit-function-declaration"
    CFLAGS="$CFLAGS -std=c99"

    DEBUG_CFLAGS="-g -ftrapv -fno-common"
    # -Og is only understood by clang 4+ and Xcode 9+
    # Otherwise use -O1 (which is what -Og usually equates to)
    if test "X-clang" = "X-$cc_vendor" -a $cc_vers_major -ge 4 -o "X-Apple LLVM" = "X-$cc_vendor" -a $cc_vers_major -ge 9; then
        DEBUG_CFLAGS="$DEBUG_CFLAGS -Og"
    else
        DEBUG_CFLAGS="$DEBUG_CFLAGS -O1"
    fi
    DEBUG_CPPFLAGS=

    PROD_CFLAGS="-O3"
    PROD_CPPFLAGS=

    PROFILE_CFLAGS="-pg"
    PROFILE_CPPFLAGS=

    ###########
    # General #
    ###########

    CFLAGS="$CFLAGS $(load_clang_arguments general)"
    CFLAGS="$CFLAGS $(load_clang_arguments no-developer-general)"

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

