summaryrefslogtreecommitdiffstats
path: root/src/spdk/scripts/detect_cc.sh
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 18:45:59 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 18:45:59 +0000
commit19fcec84d8d7d21e796c7624e521b60d28ee21ed (patch)
tree42d26aa27d1e3f7c0b8bd3fd14e7d7082f5008dc /src/spdk/scripts/detect_cc.sh
parentInitial commit. (diff)
downloadceph-19fcec84d8d7d21e796c7624e521b60d28ee21ed.tar.xz
ceph-19fcec84d8d7d21e796c7624e521b60d28ee21ed.zip
Adding upstream version 16.2.11+ds.upstream/16.2.11+dsupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/spdk/scripts/detect_cc.sh')
-rwxr-xr-xsrc/spdk/scripts/detect_cc.sh186
1 files changed, 186 insertions, 0 deletions
diff --git a/src/spdk/scripts/detect_cc.sh b/src/spdk/scripts/detect_cc.sh
new file mode 100755
index 000000000..a05ff9adf
--- /dev/null
+++ b/src/spdk/scripts/detect_cc.sh
@@ -0,0 +1,186 @@
+#!/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