2016-03-24 20:27:53 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
set -e
|
|
|
|
|
2020-05-07 11:27:06 +00:00
|
|
|
function err() {
|
2017-05-10 20:44:23 +00:00
|
|
|
echo "$@" >&2
|
2016-03-24 20:27:53 +00:00
|
|
|
}
|
|
|
|
|
2020-05-07 11:27:06 +00:00
|
|
|
function usage() {
|
2016-03-24 20:27:53 +00:00
|
|
|
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"
|
2019-01-02 12:47:47 +00:00
|
|
|
err " --ld=path Linker to use"
|
2017-05-10 20:44:23 +00:00
|
|
|
err " --lto=[y|n] Attempt to configure for LTO"
|
2019-07-23 17:33:36 +00:00
|
|
|
err " --cross-prefix=prefix Use the given prefix for the cross compiler toolchain"
|
2016-03-24 20:27:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for i in "$@"; do
|
|
|
|
case "$i" in
|
2020-05-07 11:27:06 +00:00
|
|
|
-h | --help)
|
2016-03-24 20:27:53 +00:00
|
|
|
usage
|
|
|
|
exit 0
|
|
|
|
;;
|
|
|
|
--cc=*)
|
2019-02-12 19:43:49 +00:00
|
|
|
if [[ -n "${i#*=}" ]]; then
|
|
|
|
CC="${i#*=}"
|
|
|
|
fi
|
2016-03-24 20:27:53 +00:00
|
|
|
;;
|
|
|
|
--cxx=*)
|
2019-02-12 19:43:49 +00:00
|
|
|
if [[ -n "${i#*=}" ]]; then
|
|
|
|
CXX="${i#*=}"
|
|
|
|
fi
|
2016-03-24 20:27:53 +00:00
|
|
|
;;
|
|
|
|
--lto=*)
|
2019-02-12 19:43:49 +00:00
|
|
|
if [[ -n "${i#*=}" ]]; then
|
|
|
|
LTO="${i#*=}"
|
|
|
|
fi
|
2016-03-24 20:27:53 +00:00
|
|
|
;;
|
2019-01-02 12:47:47 +00:00
|
|
|
--ld=*)
|
2019-02-12 19:43:49 +00:00
|
|
|
if [[ -n "${i#*=}" ]]; then
|
|
|
|
LD="${i#*=}"
|
|
|
|
fi
|
2019-01-02 12:47:47 +00:00
|
|
|
;;
|
2019-07-23 17:33:36 +00:00
|
|
|
--cross-prefix=*)
|
|
|
|
if [[ -n "${i#*=}" ]]; then
|
|
|
|
CROSS_PREFIX="${i#*=}"
|
|
|
|
fi
|
|
|
|
;;
|
2016-03-24 20:27:53 +00:00
|
|
|
--)
|
|
|
|
break
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
err "Unrecognized option $i"
|
|
|
|
usage
|
|
|
|
exit 1
|
2020-05-07 11:27:06 +00:00
|
|
|
;;
|
2016-03-24 20:27:53 +00:00
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
2019-07-24 17:15:08 +00:00
|
|
|
OS=$(uname)
|
|
|
|
|
2019-02-12 19:43:49 +00:00
|
|
|
: ${CC=cc}
|
|
|
|
: ${CXX=c++}
|
2019-07-24 17:15:08 +00:00
|
|
|
: ${LD=}
|
2019-02-12 19:43:49 +00:00
|
|
|
: ${LTO=n}
|
2019-07-23 17:33:36 +00:00
|
|
|
: ${CROSS_PREFIX=}
|
2019-02-12 19:43:49 +00:00
|
|
|
|
2019-07-24 17:15:08 +00:00
|
|
|
if [ -z "$LD" ]; then
|
|
|
|
if [ "$OS" = "Linux" ]; then
|
|
|
|
LD=ld
|
|
|
|
fi
|
|
|
|
if [ "$OS" = "FreeBSD" ]; then
|
|
|
|
LD=ld.lld
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2018-02-09 22:09:03 +00:00
|
|
|
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 }')
|
2016-03-24 20:27:53 +00:00
|
|
|
if [ "$CC_TYPE" != "$CXX_TYPE" ]; then
|
2017-05-10 20:44:23 +00:00
|
|
|
err "C compiler is $CC_TYPE but C++ compiler is $CXX_TYPE"
|
|
|
|
err "This may result in errors"
|
2016-03-24 20:27:53 +00:00
|
|
|
fi
|
|
|
|
|
2019-01-02 12:47:47 +00:00
|
|
|
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
|
2020-05-07 11:27:06 +00:00
|
|
|
;;
|
2019-01-02 12:47:47 +00:00
|
|
|
esac
|
|
|
|
|
2016-03-24 20:27:53 +00:00
|
|
|
CCAR="ar"
|
|
|
|
if [ "$LTO" = "y" ]; then
|
2017-05-10 20:44:23 +00:00
|
|
|
if [ "$CC_TYPE" = "clang" ]; then
|
|
|
|
if [ "$LD_TYPE" != "gold" ]; then
|
|
|
|
err "Using LTO with clang requires the gold linker."
|
|
|
|
exit 1
|
|
|
|
fi
|
2019-01-02 12:47:47 +00:00
|
|
|
CCAR="llvm-ar"
|
2017-05-10 20:44:23 +00:00
|
|
|
else
|
|
|
|
CCAR="gcc-ar"
|
|
|
|
fi
|
2016-03-24 20:27:53 +00:00
|
|
|
fi
|
|
|
|
|
2019-07-25 13:01:09 +00:00
|
|
|
if [ -n "$CROSS_PREFIX" ]; then
|
2019-07-23 17:33:36 +00:00
|
|
|
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
|
2020-05-07 11:27:06 +00:00
|
|
|
if hash $CC 2> /dev/null; then
|
2019-07-23 17:33:36 +00:00
|
|
|
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
|
2020-05-07 11:27:06 +00:00
|
|
|
if hash $CXX 2> /dev/null; then
|
2019-07-23 17:33:36 +00:00
|
|
|
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
|
|
|
|
|
2019-01-02 12:47:47 +00:00
|
|
|
function set_default() {
|
2019-08-29 16:18:37 +00:00
|
|
|
echo "DEFAULT_$1=$2"
|
2019-01-02 12:47:47 +00:00
|
|
|
echo "ifeq (\$(origin $1),default)"
|
2019-07-24 17:15:08 +00:00
|
|
|
echo "$1=$2"
|
2019-01-02 12:47:47 +00:00
|
|
|
echo "endif"
|
|
|
|
echo ""
|
|
|
|
}
|
|
|
|
|
2020-03-26 21:05:01 +00:00
|
|
|
set_default CC "$CC"
|
|
|
|
set_default CXX "$CXX"
|
|
|
|
set_default LD "$LD"
|
2019-01-02 12:47:47 +00:00
|
|
|
|
2016-03-24 20:27:53 +00:00
|
|
|
echo "CCAR=$CCAR"
|
2017-05-10 20:46:08 +00:00
|
|
|
echo "CC_TYPE=$CC_TYPE"
|
2019-01-02 12:47:47 +00:00
|
|
|
echo "LD_TYPE=$LD_TYPE"
|
2019-07-23 17:33:36 +00:00
|
|
|
|
2019-07-25 13:01:09 +00:00
|
|
|
if [ -n "$CROSS_PREFIX" ]; then
|
2019-07-23 17:33:36 +00:00
|
|
|
echo "CROSS_PREFIX=$CROSS_PREFIX"
|
|
|
|
fi
|