Skip to content

Commit a95cc4d

Browse files
authored
Parse compiler version in init-compiler.sh (#63126)
When user passes versioned native compiler argument such as `-clang10.1` or `-gcc9.2` to the top-level `:/build.sh`, `:/eng/build.sh` transforms that to msbuild property called `<Compiler>` with raw/unprocessed value. In coreclr, libs, corehost and tests partitions, we end up calling `:/eng/native/build-commons.sh`, which slices the compiler name, major and minor versions, then calls `:/eng/native/gen-buildsys.sh` with individual components. That is just a pass-thru script for the final `:/eng/common/native/init-compiler.sh`, where those arguments are actually used. In `mono.proj` and `NativeExports.csproj`, we do not use `build-commons` script, but instead, invoke `init-compiler.sh` directly with the raw `<Compiler>` property. That causes an error when versioned native compiler is specified on the command line. This PR fixes this issue by pushing version parsing code into `init-compiler.sh` so all its consumers get the same behavior.
1 parent 4c83c3b commit a95cc4d

File tree

10 files changed

+36
-63
lines changed

10 files changed

+36
-63
lines changed

eng/common/native/init-compiler.sh

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,45 @@
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 to not output anything here!
66

77
if [[ "$#" -lt 3 ]]; then
88
echo "Usage..."
9-
echo "init-compiler.sh <script directory> <Architecture> <compiler> <compiler major version> <compiler minor version>"
9+
echo "init-compiler.sh <script directory> <Architecture> <compiler>"
1010
echo "Specify the script directory."
1111
echo "Specify the target architecture."
1212
echo "Specify the name of compiler (clang or gcc)."
13-
echo "Specify the major version of compiler."
14-
echo "Specify the minor version of compiler."
1513
exit 1
1614
fi
1715

1816
nativescriptroot="$1"
1917
build_arch="$2"
2018
compiler="$3"
19+
20+
case "$compiler" in
21+
clang*|-clang*|--clang*)
22+
# clangx.y or clang-x.y
23+
version="$(echo "$compiler" | tr -d '[:alpha:]-=')"
24+
parts=(${version//./ })
25+
majorVersion="${parts[0]}"
26+
minorVersion="${parts[1]}"
27+
if [[ -z "$minorVersion" && "$majorVersion" -le 6 ]]; then
28+
minorVersion=0;
29+
fi
30+
compiler=clang
31+
;;
32+
33+
gcc*|-gcc*|--gcc*)
34+
# gccx.y or gcc-x.y
35+
version="$(echo "$compiler" | tr -d '[:alpha:]-=')"
36+
parts=(${version//./ })
37+
majorVersion="${parts[0]}"
38+
minorVersion="${parts[1]}"
39+
compiler=gcc
40+
;;
41+
esac
42+
2143
cxxCompiler="$compiler++"
22-
majorVersion="$4"
23-
minorVersion="$5"
2444

2545
. "$nativescriptroot"/../pipeline-logging-functions.sh
2646

eng/native/build-commons.sh

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ build_native()
117117
scan_build=scan-build
118118
fi
119119

120-
nextCommand="\"$__RepoRootDir/eng/native/gen-buildsys.sh\" \"$cmakeDir\" \"$intermediatesDir\" $platformArch $__Compiler \"$__CompilerMajorVersion\" \"$__CompilerMinorVersion\" $__BuildType \"$generator\" $scan_build $cmakeArgs"
120+
nextCommand="\"$__RepoRootDir/eng/native/gen-buildsys.sh\" \"$cmakeDir\" \"$intermediatesDir\" $platformArch $__Compiler $__BuildType \"$generator\" $scan_build $cmakeArgs"
121121
echo "Invoking $nextCommand"
122122
eval $nextCommand
123123

@@ -304,15 +304,7 @@ while :; do
304304
;;
305305

306306
clang*|-clang*)
307-
__Compiler=clang
308-
# clangx.y or clang-x.y
309-
version="$(echo "$lowerI" | tr -d '[:alpha:]-=')"
310-
parts=(${version//./ })
311-
__CompilerMajorVersion="${parts[0]}"
312-
__CompilerMinorVersion="${parts[1]}"
313-
if [[ -z "$__CompilerMinorVersion" && "$__CompilerMajorVersion" -le 6 ]]; then
314-
__CompilerMinorVersion=0;
315-
fi
307+
__Compiler="$lowerI"
316308
;;
317309

318310
cmakeargs|-cmakeargs)
@@ -340,12 +332,7 @@ while :; do
340332
;;
341333

342334
gcc*|-gcc*)
343-
__Compiler=gcc
344-
# gccx.y or gcc-x.y
345-
version="$(echo "$lowerI" | tr -d '[:alpha:]-=')"
346-
parts=(${version//./ })
347-
__CompilerMajorVersion="${parts[0]}"
348-
__CompilerMinorVersion="${parts[1]}"
335+
__Compiler="$lowerI"
349336
;;
350337

351338
keepnativesymbols|-keepnativesymbols)

eng/native/gen-buildsys.sh

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,11 @@ scriptroot="$( cd -P "$( dirname "$0" )" && pwd )"
77

88
if [[ "$#" -lt 4 ]]; then
99
echo "Usage..."
10-
echo "gen-buildsys.sh <path to top level CMakeLists.txt> <path to intermediate directory> <Architecture> <compiler> <compiler major version> <compiler minor version> [build flavor] [ninja] [scan-build] [cmakeargs]"
10+
echo "gen-buildsys.sh <path to top level CMakeLists.txt> <path to intermediate directory> <Architecture> <compiler> [build flavor] [ninja] [scan-build] [cmakeargs]"
1111
echo "Specify the path to the top level CMake file."
1212
echo "Specify the path that the build system files are generated in."
1313
echo "Specify the target architecture."
1414
echo "Specify the name of compiler (clang or gcc)."
15-
echo "Specify the major version of compiler."
16-
echo "Specify the minor version of compiler."
1715
echo "Optionally specify the build configuration (flavor.) Defaults to DEBUG."
1816
echo "Optionally specify 'scan-build' to enable build with clang static analyzer."
1917
echo "Use the Ninja generator instead of the Unix Makefiles generator."
@@ -23,12 +21,10 @@ fi
2321

2422
build_arch="$3"
2523
compiler="$4"
26-
majorVersion="$5"
27-
minorVersion="$6"
2824

2925
if [[ "$compiler" != "default" ]]; then
3026
nativescriptroot="$( cd -P "$scriptroot/../common/native" && pwd )"
31-
source "$nativescriptroot/init-compiler.sh" "$nativescriptroot" "$build_arch" "$compiler" "$majorVersion" "$minorVersion"
27+
source "$nativescriptroot/init-compiler.sh" "$nativescriptroot" "$build_arch" "$compiler"
3228

3329
CCC_CC="$CC"
3430
CCC_CXX="$CXX"
@@ -43,7 +39,7 @@ scan_build=OFF
4339
generator="Unix Makefiles"
4440
__UnprocessedCMakeArgs=""
4541

46-
for i in "${@:7}"; do
42+
for i in "${@:5}"; do
4743
upperI="$(echo "$i" | tr "[:lower:]" "[:upper:]")"
4844
case "$upperI" in
4945
# Possible build types are DEBUG, CHECKED, RELEASE, RELWITHDEBINFO.

src/coreclr/build-runtime.sh

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,6 @@ __CodeCoverage=0
116116

117117
# Set the various build properties here so that CMake and MSBuild can pick them up
118118
__Compiler=clang
119-
__CompilerMajorVersion=
120-
__CompilerMinorVersion=
121119
__CommonMSBuildArgs=
122120
__ConfigureOnly=0
123121
__CrossBuild=0

src/coreclr/tools/aot/ObjWriter/build.sh

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ RepoRoot="$2"
66
BuildArch="$3"
77
TargetArch="$4"
88
BuildType="${5:-Release}"
9-
CompilerId="$(echo "$6" | tr "[:upper:]" "[:lower:]")"
9+
Compiler="$(echo "$6" | tr "[:upper:]" "[:lower:]")"
10+
Compiler="${Compiler:-clang}"
1011

1112
LLVMBranch="release/12.x"
1213

@@ -16,24 +17,6 @@ if [ $# -lt 4 ]; then
1617
exit 1
1718
fi
1819

19-
if [ -z "$CompilerId" ]; then
20-
Compiler=clang
21-
CompilerMajorVer=
22-
CompilerMinorVer=
23-
# Expecting a compiler id similar to -clang9 or -gcc10.2. See also eng/native/build-commons.sh.
24-
elif [[ "$CompilerId" =~ ^-?([a-z]+)(-?([0-9]+)(\.([0-9]+))?)?$ ]]; then
25-
Compiler=${BASH_REMATCH[1]}
26-
CompilerMajorVer=${BASH_REMATCH[3]}
27-
CompilerMinorVer=${BASH_REMATCH[5]}
28-
if [[ "$Compiler" == "clang" && -n "$CompilerMajorVer" && "$CompilerMajorVer" -le 6
29-
&& -z "$CompilerMinorVer" ]]; then
30-
CompilerMinorVer=0
31-
fi
32-
else
33-
echo "Unexpected compiler identifier '$6'"
34-
exit 1
35-
fi
36-
3720
cd "$ArtifactsDir" || exit 1
3821
PatchApplied=0
3922

@@ -81,7 +64,7 @@ fi
8164
# [build flavor] [ninja] [scan-build] [cmakeargs]
8265
"${RepoRoot}eng/native/gen-buildsys.sh" \
8366
"$PWD" "$PWD/build/$TargetArch" "$TargetArch" \
84-
"$Compiler" "$CompilerMajorVer" "$CompilerMinorVer" "$BuildType" \
67+
"$Compiler" "$BuildType" \
8568
-DCMAKE_BUILD_TYPE="$BuildType" \
8669
-DCMAKE_INSTALL_PREFIX=install \
8770
-DLLVM_BUILD_TOOLS=0 \

src/libraries/System.Runtime.InteropServices/tests/TestAssets/NativeExports/NativeExports.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
<PropertyGroup>
2828
<NativeCompiler>$(Compiler)</NativeCompiler>
2929
<NativeCompiler Condition="'$(NativeCompiler)' == ''">clang</NativeCompiler>
30-
<NativeCompiler Condition="$(NativeCompiler.StartsWith('-'))">$(NativeCompiler.TrimStart('-'))</NativeCompiler>
3130
</PropertyGroup>
3231

3332
<Exec Command="bash -c 'source &quot;$(RepositoryEngineeringDir)/common/native/init-compiler.sh&quot; &quot;$(RepositoryEngineeringDir)/common/native&quot; $(TargetArchitecture) $(NativeCompiler) &amp;&amp; echo $CC'"

src/mono/mono.proj

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,6 @@
4949
<MonoCCompiler Condition="'$(MonoCCompiler)' == ''">clang</MonoCCompiler>
5050
<_CompilerTargetArch Condition="'$(RealTargetArchitecture)' == ''">$(Platform)</_CompilerTargetArch>
5151
<_CompilerTargetArch Condition="'$(RealTargetArchitecture)' != ''">$(RealTargetArchitecture)</_CompilerTargetArch>
52-
<!-- when we use runtime/build.sh -gcc, it is passed to eng/build.sh which sets the raw value to $(Compiler); strip the leading hyphens. -->
53-
<MonoCCompiler Condition="$(MonoCCompiler.StartsWith('-'))">$(MonoCCompiler.TrimStart('-'))</MonoCCompiler>
54-
<MonoCxxCompiler Condition="'$(MonoCCompiler)' == 'clang'">clang++</MonoCxxCompiler>
55-
<MonoCxxCompiler Condition="'$(MonoCCompiler)' == 'gcc'">g++</MonoCxxCompiler>
5652
<RepositoryEngineeringCommonDir>$([MSBuild]::NormalizeDirectory('$(RepositoryEngineeringDir)', 'common'))</RepositoryEngineeringCommonDir>
5753
<CrossToolchainFile>$([MSBuild]::NormalizePath('$(RepositoryEngineeringCommonDir)', 'cross', 'toolchain.cmake'))</CrossToolchainFile>
5854
</PropertyGroup>

src/native/corehost/build.sh

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ __TargetOS=Linux
1616
__BuildType=Debug
1717
__CMakeArgs=""
1818
__Compiler=clang
19-
__CompilerMajorVersion=
20-
__CompilerMinorVersion=
2119
__CrossBuild=0
2220
__PortableBuild=1
2321
__RootBinDir="$__RepoRootDir/artifacts"

src/native/libs/build-native.sh

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ __TargetOS=Linux
3131
__BuildType=Debug
3232
__CMakeArgs=""
3333
__Compiler=clang
34-
__CompilerMajorVersion=
35-
__CompilerMinorVersion=
3634
__CrossBuild=0
3735
__PortableBuild=1
3836
__RootBinDir="$__RepoRootDir/artifacts"

src/tests/build.sh

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -293,8 +293,6 @@ export __ProjectDir
293293
__SkipTestWrappers=0
294294
__BuildTestWrappersOnly=0
295295
__Compiler=clang
296-
__CompilerMajorVersion=
297-
__CompilerMinorVersion=
298296
__ConfigureOnly=0
299297
__CopyNativeProjectsAfterCombinedTestBuild=true
300298
__CopyNativeTestBinaries=0
@@ -394,7 +392,7 @@ if [[ "$__RebuildTests" -ne 0 ]]; then
394392
echo "Removing test build dir: ${__TestBinDir}"
395393
rm -rf "${__TestBinDir}"
396394
echo "Removing test intermediate dir: ${__TestIntermediatesDir}"
397-
rm -rf "${__TestIntermediatesDir}"
395+
rm -rf "${__TestIntermediatesDir}"
398396
fi
399397

400398
build_Tests

0 commit comments

Comments
 (0)