numam-spdk/scripts/detect_cc.sh
Seth Howell a988124db9 make: override scan-build for unit tests.
The unit test mock macro function doesn't mesh well with the scan-build
static analysis tool. So we need to reset cc to the user provided value
or default before running the unit tests.

Change-Id: Ib636f6e396b09f9bf1cb8f930e6befa56ce99848
Signed-off-by: Seth Howell <seth.howell@intel.com>
Reviewed-on: https://review.gerrithub.io/c/spdk/spdk/+/466816
Tested-by: SPDK CI Jenkins <sys_sgci@intel.com>
Reviewed-by: Ben Walker <benjamin.walker@intel.com>
Reviewed-by: Jim Harris <james.r.harris@intel.com>
Reviewed-by: Shuhei Matsumoto <shuhei.matsumoto.xt@hitachi.com>
2019-09-11 16:30:38 +00:00

189 lines
3.6 KiB
Bash
Executable File

#!/usr/bin/env bash
set -e
function err()
{
echo "$@" >&2
}
function usage()
{
err "Detect compiler and linker versions, generate mk/cc.mk"
err ""
err "Usage: ./detect_cc.sh [OPTION]..."
err ""
err "Defaults for the options are specified in brackets."
err ""
err "General:"
err " -h, --help Display this help and exit"
err " --cc=path C compiler to use"
err " --cxx=path C++ compiler to use"
err " --ld=path Linker to use"
err " --lto=[y|n] Attempt to configure for LTO"
err " --cross-prefix=prefix Use the given prefix for the cross compiler toolchain"
}
for i in "$@"; do
case "$i" in
-h|--help)
usage
exit 0
;;
--cc=*)
if [[ -n "${i#*=}" ]]; then
CC="${i#*=}"
fi
;;
--cxx=*)
if [[ -n "${i#*=}" ]]; then
CXX="${i#*=}"
fi
;;
--lto=*)
if [[ -n "${i#*=}" ]]; then
LTO="${i#*=}"
fi
;;
--ld=*)
if [[ -n "${i#*=}" ]]; then
LD="${i#*=}"
fi
;;
--cross-prefix=*)
if [[ -n "${i#*=}" ]]; then
CROSS_PREFIX="${i#*=}"
fi
;;
--)
break
;;
*)
err "Unrecognized option $i"
usage
exit 1
esac
done
OS=$(uname)
: ${CC=cc}
: ${CXX=c++}
: ${LD=}
: ${LTO=n}
: ${CROSS_PREFIX=}
if [ -z "$LD" ]; then
if [ "$OS" = "Linux" ]; then
LD=ld
fi
if [ "$OS" = "FreeBSD" ]; then
LD=ld.lld
fi
fi
CC_TYPE=$($CC -v 2>&1 | grep -o -E '\w+ version' | head -1 | awk '{ print $1 }')
CXX_TYPE=$($CXX -v 2>&1 | grep -o -E '\w+ version' | head -1 | awk '{ print $1 }')
if [ "$CC_TYPE" != "$CXX_TYPE" ]; then
err "C compiler is $CC_TYPE but C++ compiler is $CXX_TYPE"
err "This may result in errors"
fi
LD_TYPE=$($LD --version 2>&1 | head -n1 | awk '{print $1, $2}')
case "$LD_TYPE" in
"GNU ld"*)
LD_TYPE=bfd
;;
"GNU gold"*)
LD_TYPE=gold
;;
"LLD"*)
LD_TYPE=lld
;;
*)
err "Unsupported linker: $LD"
exit 1
esac
CCAR="ar"
if [ "$LTO" = "y" ]; then
if [ "$CC_TYPE" = "clang" ]; then
if [ "$LD_TYPE" != "gold" ]; then
err "Using LTO with clang requires the gold linker."
exit 1
fi
CCAR="llvm-ar"
else
CCAR="gcc-ar"
fi
fi
if [ -n "$CROSS_PREFIX" ]; then
expected_prefix=$($CC -dumpmachine)
if [ ! "$expected_prefix" = "$CROSS_PREFIX" ]; then
err "Cross prefix specified ($CROSS_PREFIX) does not match prefix of $CC ($expected_prefix)."
# Try to fix this automatically. Maybe the user set CROSS_PREFIX but not CC.
CC=$CROSS_PREFIX-$CC
if hash $CC 2>/dev/null; then
expected_prefix=$($CC -dumpmachine)
if [ "$expected_prefix" = "$CROSS_PREFIX" ]; then
err "Automatically changed CC to $CC"
else
err "Set CC to the appropriate compiler."
exit 1
fi
else
err "Set CC to the appropriate compiler."
exit 1
fi
fi
expected_prefix=$($CXX -dumpmachine)
if [ ! "$expected_prefix" = "$CROSS_PREFIX" ]; then
err "Cross prefix specified ($CROSS_PREFIX) does not match prefix of $CXX ($expected_prefix)."
# Try to fix this automatically. Maybe the user set CROSS_PREFIX but not CXX.
CXX=$CROSS_PREFIX-$CXX
if hash $CXX 2>/dev/null; then
expected_prefix=$($CXX -dumpmachine)
if [ "$expected_prefix" = "$CROSS_PREFIX" ]; then
err "Automatically changed CXX to $CXX"
else
err "Set CXX to the appropriate compiler."
exit 1
fi
else
err "Set CXX to the appropriate compiler."
exit 1
fi
fi
fi
function set_default() {
echo "DEFAULT_$1=$2"
echo "ifeq (\$(origin $1),default)"
echo "$1=$2"
echo "endif"
echo ""
}
set_default CC $CC
set_default CXX $CXX
set_default LD $LD
echo "CCAR=$CCAR"
echo "CC_TYPE=$CC_TYPE"
echo "LD_TYPE=$LD_TYPE"
if [ -n "$CROSS_PREFIX" ]; then
echo "CROSS_PREFIX=$CROSS_PREFIX"
fi