Skip to content

Commit fbda28c

Browse files
committed
WIP: another workaround for hardcoded compiler versions
1 parent bbbb686 commit fbda28c

File tree

1 file changed

+49
-23
lines changed

1 file changed

+49
-23
lines changed

eng/common/native/init-compiler.sh

+49-23
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,6 +60,27 @@ check_version_exists() {
5860
echo "$desired_version"
5961
}
6062

63+
set_compiler_version_from_CC() {
64+
if [ "$(uname)" == "Darwin" ]; then
65+
# On Darwin, the versions from -version/-dumpversion refer to Xcode
66+
# versions, not llvm versions, so we can't rely on them.
67+
unset majorVersion
68+
unset minorVersion
69+
return
70+
fi
71+
72+
version="$("$CC" -dumpversion)"
73+
if [ "$version" = "" ]; then
74+
echo "Error: $CC -dumpversion didn't provide a version"
75+
exit 1
76+
fi
77+
78+
# gcc and clang often display 3 part versions. However, gcc can show only 1 part in some environments.
79+
IFS=. read -r majorVersion minorVersion _ <<EOF
80+
$version
81+
EOF
82+
}
83+
6184
if [ -z "$CLR_CC" ]; then
6285

6386
# Set default versions
@@ -74,28 +97,25 @@ if [ -z "$CLR_CC" ]; then
7497
done
7598

7699
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."
100+
if ! command -v "$compiler" > /dev/null; then
101+
echo "Error: No usable version of $compiler found."
85102
exit 1
86103
fi
104+
105+
CC="$(command -v "$compiler")"
106+
CXX="$(command -v "$cxxCompiler")"
107+
set_compiler_version_from_CC
87108
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
109+
if ( [ "$compiler" = "clang" ] && [ "$majorVersion" -lt 5 ] ) && ( [ "$build_arch" = "arm" ] || [ "$build_arch" = "armel" ] ); then
110+
# If a major version was provided explicitly, and it was too old, find a newer compiler instead
111+
if ! command -v "$compiler" > /dev/null; then
112+
echo "Found clang version $majorVersion which is not supported on arm/armel architectures, and there is no clang in PATH."
113+
exit 1
98114
fi
115+
116+
CC="$(command -v "$compiler")"
117+
CXX="$(command -v "$cxxCompiler")"
118+
set_compiler_version_from_CC
99119
fi
100120
fi
101121
else
@@ -110,6 +130,7 @@ if [ -z "$CLR_CC" ]; then
110130
CC="$(command -v "$compiler$desired_version")"
111131
CXX="$(command -v "$cxxCompiler$desired_version")"
112132
if [ -z "$CXX" ]; then CXX="$(command -v "$cxxCompiler")"; fi
133+
set_compiler_version_from_CC
113134
fi
114135
else
115136
if [ ! -f "$CLR_CC" ]; then
@@ -118,17 +139,22 @@ else
118139
fi
119140
CC="$CLR_CC"
120141
CXX="$CLR_CXX"
142+
set_compiler_version_from_CC
121143
fi
122144

123145
if [ -z "$CC" ]; then
124146
echo "Unable to find $compiler."
125147
exit 1
126148
fi
127149

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"
150+
if [ "$(uname)" != "Darwin" ]; then
151+
# On Darwin, we always want to use the Apple linker.
152+
153+
# Only lld version >= 9 can be considered stable. lld supports s390x starting from 18.0.
154+
if [ "$compiler" = "clang" ] && [ -n "$majorVersion" ] && [ "$majorVersion" -ge 9 ] && ( [ "$build_arch" != "s390x" ] || [ "$majorVersion" -ge 18 ] ); then
155+
if "$CC" -fuse-ld=lld -Wl,--version >/dev/null 2>&1; then
156+
LDFLAGS="-fuse-ld=lld"
157+
fi
132158
fi
133159
fi
134160

0 commit comments

Comments
 (0)