2023-01-04 23:26:05 +00:00
|
|
|
#!/bin/sh
|
2020-04-30 12:44:30 +00:00
|
|
|
#
|
2021-09-14 13:05:57 +00:00
|
|
|
# This file detects the C/C++ compiler and exports it to the CC/CXX environment variables
|
2020-04-30 12:44:30 +00:00
|
|
|
#
|
2021-12-28 14:23:49 +00:00
|
|
|
# NOTE: some scripts source this file and rely on stdout being empty, make sure to not output anything here!
|
2020-04-30 12:44:30 +00:00
|
|
|
|
2023-01-04 23:26:05 +00:00
|
|
|
if [ -z "$build_arch" ] || [ -z "$compiler" ]; then
|
2020-04-30 12:44:30 +00:00
|
|
|
echo "Usage..."
|
2023-01-04 23:26:05 +00:00
|
|
|
echo "build_arch=<ARCH> compiler=<NAME> init-compiler.sh"
|
2021-09-14 13:05:57 +00:00
|
|
|
echo "Specify the target architecture."
|
2020-04-30 12:44:30 +00:00
|
|
|
echo "Specify the name of compiler (clang or gcc)."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2021-12-28 14:23:49 +00:00
|
|
|
case "$compiler" in
|
|
|
|
clang*|-clang*|--clang*)
|
|
|
|
# clangx.y or clang-x.y
|
|
|
|
version="$(echo "$compiler" | tr -d '[:alpha:]-=')"
|
2023-01-04 23:26:05 +00:00
|
|
|
majorVersion="${version%%.*}"
|
|
|
|
[ -z "${version##*.*}" ] && minorVersion="${version#*.}"
|
|
|
|
|
|
|
|
if [ -z "$minorVersion" ] && [ -n "$majorVersion" ] && [ "$majorVersion" -le 6 ]; then
|
2021-12-28 14:23:49 +00:00
|
|
|
minorVersion=0;
|
|
|
|
fi
|
|
|
|
compiler=clang
|
|
|
|
;;
|
|
|
|
|
|
|
|
gcc*|-gcc*|--gcc*)
|
|
|
|
# gccx.y or gcc-x.y
|
|
|
|
version="$(echo "$compiler" | tr -d '[:alpha:]-=')"
|
2023-01-04 23:26:05 +00:00
|
|
|
majorVersion="${version%%.*}"
|
|
|
|
[ -z "${version##*.*}" ] && minorVersion="${version#*.}"
|
2021-12-28 14:23:49 +00:00
|
|
|
compiler=gcc
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
2020-04-30 12:44:30 +00:00
|
|
|
cxxCompiler="$compiler++"
|
2021-10-09 02:38:41 +00:00
|
|
|
|
2021-09-14 13:05:57 +00:00
|
|
|
# clear the existing CC and CXX from environment
|
|
|
|
CC=
|
|
|
|
CXX=
|
|
|
|
LDFLAGS=
|
|
|
|
|
2023-01-04 23:26:05 +00:00
|
|
|
if [ "$compiler" = "gcc" ]; then cxxCompiler="g++"; fi
|
2020-04-30 12:44:30 +00:00
|
|
|
|
|
|
|
check_version_exists() {
|
|
|
|
desired_version=-1
|
|
|
|
|
|
|
|
# Set up the environment to be used for building with the desired compiler.
|
|
|
|
if command -v "$compiler-$1.$2" > /dev/null; then
|
|
|
|
desired_version="-$1.$2"
|
|
|
|
elif command -v "$compiler$1$2" > /dev/null; then
|
|
|
|
desired_version="$1$2"
|
|
|
|
elif command -v "$compiler-$1$2" > /dev/null; then
|
|
|
|
desired_version="-$1$2"
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo "$desired_version"
|
|
|
|
}
|
|
|
|
|
2023-01-04 23:26:05 +00:00
|
|
|
if [ -z "$CLR_CC" ]; then
|
2020-04-30 12:44:30 +00:00
|
|
|
|
|
|
|
# Set default versions
|
2023-01-04 23:26:05 +00:00
|
|
|
if [ -z "$majorVersion" ]; then
|
2020-04-30 12:44:30 +00:00
|
|
|
# note: gcc (all versions) and clang versions higher than 6 do not have minor version in file name, if it is zero.
|
2024-03-13 12:11:30 +00:00
|
|
|
if [ "$compiler" = "clang" ]; then versions="18 17 16 15 14 13 12 11 10 9 8 7 6.0 5.0 4.0 3.9 3.8 3.7 3.6 3.5"
|
2023-05-03 14:45:08 +00:00
|
|
|
elif [ "$compiler" = "gcc" ]; then versions="13 12 11 10 9 8 7 6 5 4.9"; fi
|
2023-01-04 23:26:05 +00:00
|
|
|
|
|
|
|
for version in $versions; do
|
|
|
|
_major="${version%%.*}"
|
|
|
|
[ -z "${version##*.*}" ] && _minor="${version#*.}"
|
|
|
|
desired_version="$(check_version_exists "$_major" "$_minor")"
|
|
|
|
if [ "$desired_version" != "-1" ]; then majorVersion="$_major"; break; fi
|
2020-04-30 12:44:30 +00:00
|
|
|
done
|
|
|
|
|
2023-01-04 23:26:05 +00:00
|
|
|
if [ -z "$majorVersion" ]; then
|
2020-04-30 12:44:30 +00:00
|
|
|
if command -v "$compiler" > /dev/null; then
|
2023-01-04 23:26:05 +00:00
|
|
|
if [ "$(uname)" != "Darwin" ]; then
|
|
|
|
echo "Warning: Specific version of $compiler not found, falling back to use the one in PATH."
|
2020-04-30 12:44:30 +00:00
|
|
|
fi
|
2021-09-14 13:05:57 +00:00
|
|
|
CC="$(command -v "$compiler")"
|
|
|
|
CXX="$(command -v "$cxxCompiler")"
|
2020-04-30 12:44:30 +00:00
|
|
|
else
|
2023-01-04 23:26:05 +00:00
|
|
|
echo "No usable version of $compiler found."
|
2020-04-30 12:44:30 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
else
|
2023-01-04 23:26:05 +00:00
|
|
|
if [ "$compiler" = "clang" ] && [ "$majorVersion" -lt 5 ]; then
|
|
|
|
if [ "$build_arch" = "arm" ] || [ "$build_arch" = "armel" ]; then
|
2020-04-30 12:44:30 +00:00
|
|
|
if command -v "$compiler" > /dev/null; then
|
2023-01-04 23:26:05 +00:00
|
|
|
echo "Warning: Found clang version $majorVersion which is not supported on arm/armel architectures, falling back to use clang from PATH."
|
2021-09-14 13:05:57 +00:00
|
|
|
CC="$(command -v "$compiler")"
|
|
|
|
CXX="$(command -v "$cxxCompiler")"
|
2020-04-30 12:44:30 +00:00
|
|
|
else
|
2023-01-04 23:26:05 +00:00
|
|
|
echo "Found clang version $majorVersion which is not supported on arm/armel architectures, and there is no clang in PATH."
|
2020-04-30 12:44:30 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
desired_version="$(check_version_exists "$majorVersion" "$minorVersion")"
|
2023-01-04 23:26:05 +00:00
|
|
|
if [ "$desired_version" = "-1" ]; then
|
|
|
|
echo "Could not find specific version of $compiler: $majorVersion $minorVersion."
|
2020-04-30 12:44:30 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2023-01-04 23:26:05 +00:00
|
|
|
if [ -z "$CC" ]; then
|
2021-09-14 13:05:57 +00:00
|
|
|
CC="$(command -v "$compiler$desired_version")"
|
|
|
|
CXX="$(command -v "$cxxCompiler$desired_version")"
|
2023-01-04 23:26:05 +00:00
|
|
|
if [ -z "$CXX" ]; then CXX="$(command -v "$cxxCompiler")"; fi
|
2020-04-30 12:44:30 +00:00
|
|
|
fi
|
|
|
|
else
|
2023-01-04 23:26:05 +00:00
|
|
|
if [ ! -f "$CLR_CC" ]; then
|
|
|
|
echo "CLR_CC is set but path '$CLR_CC' does not exist"
|
2020-04-30 12:44:30 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
2021-09-14 13:05:57 +00:00
|
|
|
CC="$CLR_CC"
|
|
|
|
CXX="$CLR_CXX"
|
2020-04-30 12:44:30 +00:00
|
|
|
fi
|
|
|
|
|
2023-01-04 23:26:05 +00:00
|
|
|
if [ -z "$CC" ]; then
|
|
|
|
echo "Unable to find $compiler."
|
2020-04-30 12:44:30 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2024-03-13 12:11:30 +00:00
|
|
|
# Only lld version >= 9 can be considered stable. lld supports s390x starting from 18.0.
|
|
|
|
if [ "$compiler" = "clang" ] && [ -n "$majorVersion" ] && [ "$majorVersion" -ge 9 ] && ([ "$build_arch" != "s390x" ] || [ "$majorVersion" -ge 18 ]); then
|
2021-11-27 14:22:43 +00:00
|
|
|
if "$CC" -fuse-ld=lld -Wl,--version >/dev/null 2>&1; then
|
2021-11-26 14:15:30 +00:00
|
|
|
LDFLAGS="-fuse-ld=lld"
|
2021-09-14 13:05:57 +00:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
SCAN_BUILD_COMMAND="$(command -v "scan-build$desired_version")"
|
|
|
|
|
|
|
|
export CC CXX LDFLAGS SCAN_BUILD_COMMAND
|