2020-04-30 12:44:30 +00:00
#!/usr/bin/env bash
#
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-09-14 13:05:57 +00:00
if [ [ " $# " -lt 2 ] ] ; then
2020-04-30 12:44:30 +00:00
echo "Usage..."
2021-09-14 13:05:57 +00:00
echo "init-compiler.sh <Architecture> <compiler> <compiler major version> <compiler minor version>"
echo "Specify the target architecture."
2020-04-30 12:44:30 +00:00
echo "Specify the name of compiler (clang or gcc)."
echo "Specify the major version of compiler."
echo "Specify the minor version of compiler."
exit 1
fi
2021-09-14 13:05:57 +00:00
. " $( cd -P " $( dirname " $0 " ) " && pwd ) " /../pipeline-logging-functions.sh
2020-04-30 12:44:30 +00:00
2021-09-14 13:05:57 +00:00
build_arch = " $1 "
compiler = " $2 "
2020-04-30 12:44:30 +00:00
cxxCompiler = " $compiler ++ "
2021-09-14 13:05:57 +00:00
majorVersion = " $3 "
minorVersion = " $4 "
2020-04-30 12:44:30 +00:00
2021-09-14 13:05:57 +00:00
# clear the existing CC and CXX from environment
CC =
CXX =
LDFLAGS =
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 "
}
2021-09-14 13:05:57 +00:00
if [ [ -z " $CLR_CC " ] ] ; then
2020-04-30 12:44:30 +00:00
# Set default versions
2021-09-14 13:05:57 +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.
2021-09-14 13:05:57 +00:00
if [ [ " $compiler " = = "clang" ] ] ; then versions = ( 12 11 10 9 8 7 6.0 5.0 4.0 3.9 3.8 3.7 3.6 3.5 )
elif [ [ " $compiler " = = "gcc" ] ] ; then versions = ( 11 10 9 8 7 6 5 4.9 ) ; fi
2020-04-30 12:44:30 +00:00
for version in " ${ versions [@] } " ; do
parts = ( ${ version //./ } )
desired_version = " $( check_version_exists " ${ parts [0] } " " ${ parts [1] } " ) "
2021-09-14 13:05:57 +00:00
if [ [ " $desired_version " != "-1" ] ] ; then majorVersion = " ${ parts [0] } " ; break; fi
2020-04-30 12:44:30 +00:00
done
2021-09-14 13:05:57 +00:00
if [ [ -z " $majorVersion " ] ] ; then
2020-04-30 12:44:30 +00:00
if command -v " $compiler " > /dev/null; then
2021-09-14 13:05:57 +00:00
if [ [ " $( uname) " != "Darwin" ] ] ; then
2020-04-30 12:44:30 +00:00
Write-PipelineTelemetryError -category "Build" -type "warning" " Specific version of $compiler not found, falling back to use the one in PATH. "
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
Write-PipelineTelemetryError -category "Build" " No usable version of $compiler found. "
exit 1
fi
else
2021-09-14 13:05:57 +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
Write-PipelineTelemetryError -category "Build" -type "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
Write-PipelineTelemetryError -category "Build" " Found clang version $majorVersion which is not supported on arm/armel architectures, and there is no clang in PATH. "
exit 1
fi
fi
fi
fi
else
desired_version = " $( check_version_exists " $majorVersion " " $minorVersion " ) "
2021-09-14 13:05:57 +00:00
if [ [ " $desired_version " = = "-1" ] ] ; then
2020-04-30 12:44:30 +00:00
Write-PipelineTelemetryError -category "Build" " Could not find specific version of $compiler : $majorVersion $minorVersion . "
exit 1
fi
fi
2021-09-14 13:05:57 +00:00
if [ [ -z " $CC " ] ] ; then
CC = " $( command -v " $compiler $desired_version " ) "
CXX = " $( command -v " $cxxCompiler $desired_version " ) "
if [ [ -z " $CXX " ] ] ; then CXX = " $( command -v " $cxxCompiler " ) " ; fi
2020-04-30 12:44:30 +00:00
fi
else
2021-09-14 13:05:57 +00:00
if [ [ ! -f " $CLR_CC " ] ] ; then
2020-04-30 12:44:30 +00:00
Write-PipelineTelemetryError -category "Build" " CLR_CC is set but path ' $CLR_CC ' does not exist "
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
2021-09-14 13:05:57 +00:00
if [ [ -z " $CC " ] ] ; then
Write-PipelineTelemetryError -category "Build" " Unable to find $compiler . "
2020-04-30 12:44:30 +00:00
exit 1
fi
2021-09-14 13:05:57 +00:00
if [ [ " $compiler " = = "clang" ] ] ; then
if command -v " lld $desired_version " > /dev/null; then
# Only lld version >= 9 can be considered stable
if [ [ " $majorVersion " -ge 9 ] ] ; then
LDFLAGS = "-fuse-ld=lld"
fi
fi
fi
SCAN_BUILD_COMMAND = " $( command -v " scan-build $desired_version " ) "
export CC CXX LDFLAGS SCAN_BUILD_COMMAND