Skip to content

Commit 88accba

Browse files
committed
WIP: another workaround for hardcoded compiler versions
1 parent 7def0b7 commit 88accba

File tree

1 file changed

+46
-23
lines changed

1 file changed

+46
-23
lines changed

eng/common/native/init-compiler.sh

Lines changed: 46 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
#
33
# This file detects the C/C++ compiler and exports it to the CC/CXX environment variables
44
#
5-
# NOTE: some scripts source this file and rely on stdout being empty, make sure to not output anything here!
5+
# NOTE: some scripts source this file and rely on stdout being empty, make sure
6+
# to not output *anything* here, unless it is an error message that fails the
7+
# build.
68

79
if [ -z "$build_arch" ] || [ -z "$compiler" ]; then
810
echo "Usage..."
@@ -58,10 +60,23 @@ check_version_exists() {
5860
echo "$desired_version"
5961
}
6062

63+
set_compiler_version_from_CC() {
64+
version="$("$CC" -dumpversion)"
65+
if [ "$version" = "" ]; then
66+
echo "Error: $CC -dumpversion didn't provide a version"
67+
exit 1
68+
fi
69+
# gcc and clang often display 3 part versions. However, gcc can show only 1 part in some environments.
70+
IFS=. read -r majorVersion minorVersion _ <<EOF
71+
$version
72+
EOF
73+
}
74+
6175
if [ -z "$CLR_CC" ]; then
6276

6377
# Set default versions
6478
if [ -z "$majorVersion" ]; then
79+
6580
# note: gcc (all versions) and clang versions higher than 6 do not have minor version in file name, if it is zero.
6681
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"
6782
elif [ "$compiler" = "gcc" ]; then versions="13 12 11 10 9 8 7 6 5 4.9"; fi
@@ -74,28 +89,30 @@ if [ -z "$CLR_CC" ]; then
7489
done
7590

7691
if [ -z "$majorVersion" ]; then
77-
if command -v "$compiler" > /dev/null; then
78-
if [ "$(uname)" != "Darwin" ]; then
79-
echo "Warning: Specific version of $compiler not found, falling back to use the one in PATH."
80-
fi
81-
CC="$(command -v "$compiler")"
82-
CXX="$(command -v "$cxxCompiler")"
83-
else
84-
echo "No usable version of $compiler found."
92+
if [ "$(uname)" != "Darwin" ]; then
93+
echo "Error: Specific version of $compiler not found"
8594
exit 1
8695
fi
96+
97+
if ! command -v "$compiler" > /dev/null; then
98+
echo "Error: No usable version of $compiler found."
99+
exit 1
100+
fi
101+
102+
CC="$(command -v "$compiler")"
103+
CXX="$(command -v "$cxxCompiler")"
104+
set_compiler_version_from_CC
87105
else
88-
if [ "$compiler" = "clang" ] && [ "$majorVersion" -lt 5 ]; then
89-
if [ "$build_arch" = "arm" ] || [ "$build_arch" = "armel" ]; then
90-
if command -v "$compiler" > /dev/null; then
91-
echo "Warning: Found clang version $majorVersion which is not supported on arm/armel architectures, falling back to use clang from PATH."
92-
CC="$(command -v "$compiler")"
93-
CXX="$(command -v "$cxxCompiler")"
94-
else
95-
echo "Found clang version $majorVersion which is not supported on arm/armel architectures, and there is no clang in PATH."
96-
exit 1
97-
fi
106+
if ( [ "$compiler" = "clang" ] && [ "$majorVersion" -lt 5 ] ) && ( [ "$build_arch" = "arm" ] || [ "$build_arch" = "armel" ] ); then
107+
# If a major version was provided explicitly, and it was too old, find a newer compiler instead
108+
if ! command -v "$compiler" > /dev/null; then
109+
echo "Found clang version $majorVersion which is not supported on arm/armel architectures, and there is no clang in PATH."
110+
exit 1
98111
fi
112+
113+
CC="$(command -v "$compiler")"
114+
CXX="$(command -v "$cxxCompiler")"
115+
set_compiler_version_from_CC
99116
fi
100117
fi
101118
else
@@ -110,6 +127,7 @@ if [ -z "$CLR_CC" ]; then
110127
CC="$(command -v "$compiler$desired_version")"
111128
CXX="$(command -v "$cxxCompiler$desired_version")"
112129
if [ -z "$CXX" ]; then CXX="$(command -v "$cxxCompiler")"; fi
130+
set_compiler_version_from_CC
113131
fi
114132
else
115133
if [ ! -f "$CLR_CC" ]; then
@@ -118,17 +136,22 @@ else
118136
fi
119137
CC="$CLR_CC"
120138
CXX="$CLR_CXX"
139+
set_compiler_version_from_CC
121140
fi
122141

123142
if [ -z "$CC" ]; then
124143
echo "Unable to find $compiler."
125144
exit 1
126145
fi
127146

128-
# Only lld version >= 9 can be considered stable. lld supports s390x starting from 18.0.
129-
if [ "$compiler" = "clang" ] && [ -n "$majorVersion" ] && [ "$majorVersion" -ge 9 ] && ([ "$build_arch" != "s390x" ] || [ "$majorVersion" -ge 18 ]); then
130-
if "$CC" -fuse-ld=lld -Wl,--version >/dev/null 2>&1; then
131-
LDFLAGS="-fuse-ld=lld"
147+
if [ "$(uname)" != "Darwin" ]; then
148+
# On Darwin, we always want to use the Apple linker.
149+
150+
# Only lld version >= 9 can be considered stable. lld supports s390x starting from 18.0.
151+
if [ "$compiler" = "clang" ] && [ -n "$majorVersion" ] && [ "$majorVersion" -ge 9 ] && ( [ "$build_arch" != "s390x" ] || [ "$majorVersion" -ge 18 ] ); then
152+
if "$CC" -fuse-ld=lld -Wl,--version >/dev/null 2>&1; then
153+
LDFLAGS="-fuse-ld=lld"
154+
fi
132155
fi
133156
fi
134157

0 commit comments

Comments
 (0)