
# Copyright (C) Igor Sysoev
# Copyright (C) NGINX, Inc.


echo checking for C compiler: $CC
cat << END >> $NJS_AUTOCONF_ERR
----------------------------------------
checking for C compiler: $CC
END


# Allow error exit status.
set +e

if ! command -v "${CC}" >/dev/null; then
    echo
    echo $0: error: $CC not found.
    echo
    exit 1;
fi


if `/bin/sh -c "($CC -v)" 2>&1 | grep "gcc version" >> $NJS_AUTOCONF_ERR 2>&1`
then
    NJS_CC_NAME=gcc
    echo " + using GNU C compiler"
    NJS_CC_VERSION=`/bin/sh -c "($CC -v)" 2>&1 | grep "gcc version" 2>&1`
    echo " + $NJS_CC_VERSION"

else
if `/bin/sh -c "($CC -v)" 2>&1 | grep "clang version" >> $NJS_AUTOCONF_ERR 2>&1`
then
    NJS_CC_NAME=clang
    echo " + using Clang C compiler"
    NJS_CC_VERSION=`/bin/sh -c "($CC -v)" 2>&1 | grep "clang version" 2>&1`
    echo " + $NJS_CC_VERSION"

else
if `/bin/sh -c "($CC -v)" 2>&1 \
                 | grep "Apple LLVM version" >> $NJS_AUTOCONF_ERR 2>&1`
then
    NJS_CC_NAME=clang
    echo " + using Clang C compiler"
    NJS_CC_VERSION=`/bin/sh -c "($CC -v)" 2>&1 | grep "Apple LLVM version" 2>&1`
    echo " + $NJS_CC_VERSION"

else
if `/bin/sh -c "($CC -V)" 2>&1 | grep "Sun C" >> $NJS_AUTOCONF_ERR 2>&1`
then
    NJS_CC_NAME=SunC
    echo " + using Sun C compiler"
    NJS_CC_VERSION=`/bin/sh -c "($CC -V)" 2>&1 | grep "Sun C" 2>&1`
    echo " + $NJS_CC_VERSION"

fi # SunC
fi # Apple LLVM clang
fi # clang
fi # gcc


case $NJS_CC_NAME in

    gcc)
        njs_define=NJS_GCC . auto/define

        NJS_CFLAGS="$NJS_CFLAGS -pipe"
        NJS_CFLAGS="$NJS_CFLAGS -fPIC"

        # Do not export symbols except explicitly marked with NJS_EXPORT.
        NJS_CFLAGS="$NJS_CFLAGS -fvisibility=hidden"

        # c99/gnu99 conflict with Solaris XOPEN.
        #NJS_CFLAGS="$NJS_CFLAGS -std=gnu99"

        NJS_CFLAGS="$NJS_CFLAGS -O"
        #NJS_CFLAGS="$NJS_CFLAGS -O0"
        NJS_CFLAGS="$NJS_CFLAGS -W -Wall -Wextra"

        #NJS_CFLAGS="$NJS_CFLAGS -Wunused-result"
        NJS_CFLAGS="$NJS_CFLAGS -Wno-unused-parameter"
        #NJS_CFLAGS="$NJS_CFLAGS -Wshorten-64-to-32"
        NJS_CFLAGS="$NJS_CFLAGS -Wwrite-strings"

        # -O2 enables -fstrict-aliasing and -fstrict-overflow.
        #NJS_CFLAGS="$NJS_CFLAGS -O2"
        #NJS_CFLAGS="$NJS_CFLAGS -Wno-strict-aliasing"

        #NJS_CFLAGS="$NJS_CFLAGS -fomit-frame-pointer"
        #NJS_CFLAGS="$NJS_CFLAGS -momit-leaf-frame-pointer"

        # -Wstrict-overflow is supported by GCC 4.2+.
        #NJS_CFLAGS="$NJS_CFLAGS -Wstrict-overflow=5"

        NJS_CFLAGS="$NJS_CFLAGS -Wmissing-prototypes"

        # Stop on warning.
        NJS_CFLAGS="$NJS_CFLAGS -Werror"

        # Debug.
        NJS_CFLAGS="$NJS_CFLAGS -g"

        if [ "$NJS_ADDRESS_SANITIZER" = "YES" ]; then
            NJS_CFLAGS="$NJS_CFLAGS -fsanitize=address"
            NJS_CFLAGS="$NJS_CFLAGS -fno-omit-frame-pointer"
        fi
    ;;

    clang)
        njs_define=NJS_CLANG . auto/define

        NJS_CFLAGS="$NJS_CFLAGS -pipe"
        NJS_CFLAGS="$NJS_CFLAGS -fPIC"

        # Do not export symbols except explicitly marked with NJS_EXPORT.
        NJS_CFLAGS="$NJS_CFLAGS -fvisibility=hidden"

        NJS_CFLAGS="$NJS_CFLAGS -O"
        #NJS_CFLAGS="$NJS_CFLAGS -O0"
        NJS_CFLAGS="$NJS_CFLAGS -W -Wall -Wextra"

        #NJS_CFLAGS="$NJS_CFLAGS -Wunused-result"
        NJS_CFLAGS="$NJS_CFLAGS -Wno-unused-parameter"
        #NJS_CFLAGS="$NJS_CFLAGS -Wshorten-64-to-32"
        NJS_CFLAGS="$NJS_CFLAGS -Wwrite-strings"
        #NJS_CFLAGS="$NJS_CFLAGS -O2"
        #NJS_CFLAGS="$NJS_CFLAGS -fomit-frame-pointer"
        NJS_CFLAGS="$NJS_CFLAGS -fstrict-aliasing"
        NJS_CFLAGS="$NJS_CFLAGS -Wstrict-overflow=5"

        NJS_CFLAGS="$NJS_CFLAGS -Wmissing-prototypes"

        # Stop on warning.
        NJS_CFLAGS="$NJS_CFLAGS -Werror"

        # Debug.

        if [ "$NJS_SYSTEM_PLATFORM" != "powerpc" ]; then
            # "-g" flag causes the "unknown pseudo-op: `.cfi_sections'"
            # error on PowerPC Clang.
            NJS_CFLAGS="$NJS_CFLAGS -g"
        fi

        if [ "$NJS_ADDRESS_SANITIZER" = "YES" ]; then
            NJS_CFLAGS="$NJS_CFLAGS -fsanitize=address"
            NJS_CFLAGS="$NJS_CFLAGS -fno-omit-frame-pointer"
            NJS_CFLAGS="$NJS_CFLAGS -fsanitize-address-use-after-scope"
        fi
    ;;

    SunC)
        njs_define=NJS_SUNC . auto/define

        NJS_CFLAGS="$NJS_CFLAGS -fPIC"
        # Optimization.
        NJS_CFLAGS="$NJS_CFLAGS -O -fast"
        # Stop on warning.
        NJS_CFLAGS="$NJS_CFLAGS -errwarn=%all"
        # Debug.
        NJS_CFLAGS="$NJS_CFLAGS -g"

        if [ "$NJS_ADDRESS_SANITIZER" = "YES" ]; then
            echo " - Address sanitizer is not supported by $NJS_CC_NAME"
        fi
    ;;

    *)
    ;;

esac

njs_feature="-Wl,-E"
njs_feature_name=
njs_feature_run=value
njs_feature_path=
njs_feature_incs=
njs_feature_libs=-Wl,-E
njs_feature_test="int main(void) { return 0; }"

. auto/feature

if [ $njs_found = yes ]; then
    NJS_LINK="$NJS_LINK -Wl,-E"
fi

if [ "$NJS_DEBUG" = "YES" ]; then
        njs_define=NJS_DEBUG . auto/define
fi

if [ "$NJS_DEBUG_MEMORY" = "YES" ]; then
        njs_define=NJS_DEBUG_MEMORY . auto/define
fi

if [ "$NJS_DEBUG_OPCODE" = "YES" ]; then
        njs_define=NJS_DEBUG_OPCODE . auto/define
fi

if [ "$NJS_DEBUG_GENERATOR" = "YES" ]; then
        njs_define=NJS_DEBUG_GENERATOR . auto/define
fi

# Stop on error exit status again.
set -e
