Skip to content

Commit 8d07cce

Browse files
authored
Share init-compiler with arcade (#59018)
* Share init-compiler with arcade * Fixup merge conflict * typo
1 parent 7073360 commit 8d07cce

4 files changed

Lines changed: 62 additions & 182 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -1,39 +1,32 @@
11
#!/usr/bin/env bash
22
#
3-
# This file locates the native compiler with the given name and version and sets the environment variables to locate it.
3+
# This file detects the C/C++ compiler and exports it to the CC/CXX environment variables
44
#
55

6-
source="${BASH_SOURCE[0]}"
7-
8-
# resolve $SOURCE until the file is no longer a symlink
9-
while [[ -h $source ]]; do
10-
scriptroot="$( cd -P "$( dirname "$source" )" && pwd )"
11-
source="$(readlink "$source")"
12-
13-
# if $source was a relative symlink, we need to resolve it relative to the path where the
14-
# symlink file was located
15-
[[ $source != /* ]] && source="$scriptroot/$source"
16-
done
17-
scriptroot="$( cd -P "$( dirname "$source" )" && pwd )"
18-
19-
if [ $# -lt 0 ]
20-
then
6+
if [[ "$#" -lt 2 ]]; then
217
echo "Usage..."
22-
echo "find-native-compiler.sh <compiler> <compiler major version> <compiler minor version>"
8+
echo "init-compiler.sh <Architecture> <compiler> <compiler major version> <compiler minor version>"
9+
echo "Specify the target architecture."
2310
echo "Specify the name of compiler (clang or gcc)."
2411
echo "Specify the major version of compiler."
2512
echo "Specify the minor version of compiler."
2613
exit 1
2714
fi
2815

29-
. $scriptroot/../pipeline-logging-functions.sh
16+
. "$( cd -P "$( dirname "$0" )" && pwd )"/../pipeline-logging-functions.sh
3017

31-
compiler="$1"
18+
build_arch="$1"
19+
compiler="$2"
3220
cxxCompiler="$compiler++"
33-
majorVersion="$2"
34-
minorVersion="$3"
21+
majorVersion="$3"
22+
minorVersion="$4"
3523

36-
if [ "$compiler" = "gcc" ]; then cxxCompiler="g++"; fi
24+
# clear the existing CC and CXX from environment
25+
CC=
26+
CXX=
27+
LDFLAGS=
28+
29+
if [[ "$compiler" == "gcc" ]]; then cxxCompiler="g++"; fi
3730

3831
check_version_exists() {
3932
desired_version=-1
@@ -50,38 +43,38 @@ check_version_exists() {
5043
echo "$desired_version"
5144
}
5245

53-
if [ -z "$CLR_CC" ]; then
46+
if [[ -z "$CLR_CC" ]]; then
5447

5548
# Set default versions
56-
if [ -z "$majorVersion" ]; then
49+
if [[ -z "$majorVersion" ]]; then
5750
# note: gcc (all versions) and clang versions higher than 6 do not have minor version in file name, if it is zero.
58-
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 )
59-
elif [ "$compiler" = "gcc" ]; then versions=( 9 8 7 6 5 4.9 ); fi
51+
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 )
52+
elif [[ "$compiler" == "gcc" ]]; then versions=( 11 10 9 8 7 6 5 4.9 ); fi
6053

6154
for version in "${versions[@]}"; do
6255
parts=(${version//./ })
6356
desired_version="$(check_version_exists "${parts[0]}" "${parts[1]}")"
64-
if [ "$desired_version" != "-1" ]; then majorVersion="${parts[0]}"; break; fi
57+
if [[ "$desired_version" != "-1" ]]; then majorVersion="${parts[0]}"; break; fi
6558
done
6659

67-
if [ -z "$majorVersion" ]; then
60+
if [[ -z "$majorVersion" ]]; then
6861
if command -v "$compiler" > /dev/null; then
69-
if [ "$(uname)" != "Darwin" ]; then
62+
if [[ "$(uname)" != "Darwin" ]]; then
7063
Write-PipelineTelemetryError -category "Build" -type "warning" "Specific version of $compiler not found, falling back to use the one in PATH."
7164
fi
72-
export CC="$(command -v "$compiler")"
73-
export CXX="$(command -v "$cxxCompiler")"
65+
CC="$(command -v "$compiler")"
66+
CXX="$(command -v "$cxxCompiler")"
7467
else
7568
Write-PipelineTelemetryError -category "Build" "No usable version of $compiler found."
7669
exit 1
7770
fi
7871
else
79-
if [ "$compiler" = "clang" ] && [ "$majorVersion" -lt 5 ]; then
80-
if [ "$build_arch" = "arm" ] || [ "$build_arch" = "armel" ]; then
72+
if [[ "$compiler" == "clang" && "$majorVersion" -lt 5 ]]; then
73+
if [[ "$build_arch" == "arm" || "$build_arch" == "armel" ]]; then
8174
if command -v "$compiler" > /dev/null; then
8275
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."
83-
export CC="$(command -v "$compiler")"
84-
export CXX="$(command -v "$cxxCompiler")"
76+
CC="$(command -v "$compiler")"
77+
CXX="$(command -v "$cxxCompiler")"
8578
else
8679
Write-PipelineTelemetryError -category "Build" "Found clang version $majorVersion which is not supported on arm/armel architectures, and there is no clang in PATH."
8780
exit 1
@@ -91,31 +84,40 @@ if [ -z "$CLR_CC" ]; then
9184
fi
9285
else
9386
desired_version="$(check_version_exists "$majorVersion" "$minorVersion")"
94-
if [ "$desired_version" = "-1" ]; then
87+
if [[ "$desired_version" == "-1" ]]; then
9588
Write-PipelineTelemetryError -category "Build" "Could not find specific version of $compiler: $majorVersion $minorVersion."
9689
exit 1
9790
fi
9891
fi
9992

100-
if [ -z "$CC" ]; then
101-
export CC="$(command -v "$compiler$desired_version")"
102-
export CXX="$(command -v "$cxxCompiler$desired_version")"
103-
if [ -z "$CXX" ]; then export CXX="$(command -v "$cxxCompiler")"; fi
93+
if [[ -z "$CC" ]]; then
94+
CC="$(command -v "$compiler$desired_version")"
95+
CXX="$(command -v "$cxxCompiler$desired_version")"
96+
if [[ -z "$CXX" ]]; then CXX="$(command -v "$cxxCompiler")"; fi
10497
fi
10598
else
106-
if [ ! -f "$CLR_CC" ]; then
99+
if [[ ! -f "$CLR_CC" ]]; then
107100
Write-PipelineTelemetryError -category "Build" "CLR_CC is set but path '$CLR_CC' does not exist"
108101
exit 1
109102
fi
110-
export CC="$CLR_CC"
111-
export CXX="$CLR_CXX"
103+
CC="$CLR_CC"
104+
CXX="$CLR_CXX"
112105
fi
113106

114-
if [ -z "$CC" ]; then
115-
Write-PipelineTelemetryError -category "Build" "Unable to find $compiler."
107+
if [[ -z "$CC" ]]; then
108+
Write-PipelineTelemetryError -category "Build" "Unable to find $compiler."
116109
exit 1
117110
fi
118111

119-
export CCC_CC="$CC"
120-
export CCC_CXX="$CXX"
121-
export SCAN_BUILD_COMMAND="$(command -v "scan-build$desired_version")"
112+
if [[ "$compiler" == "clang" ]]; then
113+
if command -v "lld$desired_version" > /dev/null; then
114+
# Only lld version >= 9 can be considered stable
115+
if [[ "$majorVersion" -ge 9 ]]; then
116+
LDFLAGS="-fuse-ld=lld"
117+
fi
118+
fi
119+
fi
120+
121+
SCAN_BUILD_COMMAND="$(command -v "scan-build$desired_version")"
122+
123+
export CC CXX LDFLAGS SCAN_BUILD_COMMAND

eng/native/gen-buildsys.sh

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,12 @@ compiler="$4"
2626
majorVersion="$5"
2727
minorVersion="$6"
2828

29-
source "$scriptroot/init-compiler.sh" "$build_arch" "$compiler" "$majorVersion" "$minorVersion"
29+
if [[ "$compiler" != "default" ]]; then
30+
source "$scriptroot/../common/native/init-compiler.sh" "$build_arch" "$compiler" "$majorVersion" "$minorVersion"
3031

31-
CCC_CC="$CC"
32-
CCC_CXX="$CXX"
32+
CCC_CC="$CC"
33+
CCC_CXX="$CXX"
34+
fi
3335

3436
export CCC_CC CCC_CXX
3537

eng/native/init-compiler.sh

Lines changed: 0 additions & 126 deletions
This file was deleted.

src/mono/mono.proj

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@
6161
<MonoCCompiler Condition="$(MonoCCompiler.StartsWith('-'))">$(MonoCCompiler.TrimStart('-'))</MonoCCompiler>
6262
<MonoCxxCompiler Condition="'$(MonoCCompiler)' == 'clang'">clang++</MonoCxxCompiler>
6363
<MonoCxxCompiler Condition="'$(MonoCCompiler)' == 'gcc'">g++</MonoCxxCompiler>
64+
<RepositoryEngineeringCommonDir>$([MSBuild]::NormalizeDirectory('$(RepositoryEngineeringDir)', 'common'))</RepositoryEngineeringCommonDir>
65+
<CrossToolchainFile>$([MSBuild]::NormalizePath('$(RepositoryEngineeringCommonDir)', 'cross', 'toolchain.cmake'))</CrossToolchainFile>
6466
</PropertyGroup>
6567

6668
<!-- default thread suspend for specific platforms -->
@@ -224,7 +226,7 @@
224226

225227
<!-- ARM Linux cross build options on CI -->
226228
<ItemGroup Condition="'$(TargetsAndroid)' != 'true' and '$(MonoCrossDir)' != '' and ('$(TargetArchitecture)' == 'arm' or '$(TargetArchitecture)' == 'arm64')">
227-
<_MonoCMakeArgs Include="-DCMAKE_TOOLCHAIN_FILE=$([MSBuild]::NormalizePath('$(RepositoryEngineeringDir)', 'common', 'cross', 'toolchain.cmake'))" />
229+
<_MonoCMakeArgs Include="-DCMAKE_TOOLCHAIN_FILE=$(CrossToolchainFile)" />
228230
<_MonoBuildEnv Condition="'$(Platform)' == 'arm64'" Include="TARGET_BUILD_ARCH=arm64" />
229231
<_MonoBuildEnv Condition="'$(Platform)' == 'arm'" Include="TARGET_BUILD_ARCH=arm" />
230232
<_MonoBuildEnv Condition="'$(Platform)' == 'arm64'" Include="PKG_CONFIG_PATH=$(MonoCrossDir)/usr/lib/aarch64-linux-gnu/pkgconfig" />
@@ -233,21 +235,21 @@
233235

234236
<!-- x64 illumos cross build options -->
235237
<ItemGroup Condition="'$(TargetsIllumos)' == 'true' and '$(MonoCrossDir)' != ''">
236-
<_MonoCMakeArgs Include="-DCMAKE_TOOLCHAIN_FILE=$([MSBuild]::NormalizePath('$(RepositoryEngineeringDir)', 'common', 'cross', 'toolchain.cmake'))" />
238+
<_MonoCMakeArgs Include="-DCMAKE_TOOLCHAIN_FILE=$(CrossToolchainFile)" />
237239
<_MonoBuildEnv Include="TARGET_BUILD_ARCH=x64" />
238240
<_MonoBuildEnv Include="PKG_CONFIG_PATH=$(MonoCrossDir)/lib/pkgconfig" />
239241
</ItemGroup>
240242

241243
<!-- s390x Linux cross build options -->
242244
<ItemGroup Condition="'$(MonoCrossDir)' != '' and '$(TargetArchitecture)' == 's390x'">
243-
<_MonoCMakeArgs Include="-DCMAKE_TOOLCHAIN_FILE=$([MSBuild]::NormalizePath('$(RepositoryEngineeringDir)', 'common', 'cross', 'toolchain.cmake'))" />
245+
<_MonoCMakeArgs Include="-DCMAKE_TOOLCHAIN_FILE=$(CrossToolchainFile)" />
244246
<_MonoBuildEnv Include="TARGET_BUILD_ARCH=s390x" />
245247
<_MonoBuildEnv Include="PKG_CONFIG_PATH=$(MonoCrossDir)/usr/lib/s390x-linux-gnu/pkgconfig" />
246248
</ItemGroup>
247249

248250
<!-- x64 FreeBSD cross build options -->
249251
<ItemGroup Condition="'$(TargetsFreeBSD)' == 'true' and '$(MonoCrossDir)' != ''">
250-
<_MonoCMakeArgs Include="-DCMAKE_TOOLCHAIN_FILE=$([MSBuild]::NormalizePath('$(RepositoryEngineeringDir)', 'common', 'cross', 'toolchain.cmake'))" />
252+
<_MonoCMakeArgs Include="-DCMAKE_TOOLCHAIN_FILE=$(CrossToolchainFile)" />
251253
<_MonoBuildEnv Include="TARGET_BUILD_ARCH=x64" />
252254
</ItemGroup>
253255

@@ -464,7 +466,7 @@
464466
<PropertyGroup>
465467
<EMSDK_PATH>$([MSBuild]::EnsureTrailingSlash('$(EMSDK_PATH)'))</EMSDK_PATH>
466468
<_MonoCMakeConfigureCommand>cmake @(_MonoCMakeArgs, ' ') $(MonoCMakeExtraArgs) &quot;$(MonoProjectRoot.TrimEnd('\/'))&quot;</_MonoCMakeConfigureCommand>
467-
<_MonoCMakeConfigureCommand Condition="'$(TargetsBrowser)' != 'true' and '$(_MonoRunInitCompiler)' != 'false' and '$(OS)' != 'Windows_NT'">bash -c 'source $(RepositoryEngineeringDir)native/init-compiler.sh $(_CompilerTargetArch) $(MonoCCompiler) &amp;&amp; @(_MonoBuildEnv, ' ') $(_MonoCMakeConfigureCommand)'</_MonoCMakeConfigureCommand>
469+
<_MonoCMakeConfigureCommand Condition="'$(TargetsBrowser)' != 'true' and '$(_MonoRunInitCompiler)' != 'false' and '$(OS)' != 'Windows_NT'">bash -c 'source $(RepositoryEngineeringCommonDir)native/init-compiler.sh $(_CompilerTargetArch) $(MonoCCompiler) &amp;&amp; @(_MonoBuildEnv, ' ') $(_MonoCMakeConfigureCommand)'</_MonoCMakeConfigureCommand>
468470
<_MonoCMakeConfigureCommand Condition="'$(TargetsBrowser)' != 'true' and '$(_MonoRunInitCompiler)' != 'false' and '$(OS)' == 'Windows_NT'">call &quot;$(RepositoryEngineeringDir)native\init-vs-env.cmd&quot; $(_CompilerTargetArch) &amp;&amp; cd /D &quot;$(MonoObjDir)&quot; &amp;&amp; @(_MonoBuildEnv, ' ') $(_MonoCMakeConfigureCommand)</_MonoCMakeConfigureCommand>
469471
<_MonoCMakeConfigureCommand Condition="'$(TargetsBrowser)' != 'true' and '$(_MonoRunInitCompiler)' == 'false'">$(_MonoCCOption) $(_MonoCXXOption) @(_MonoBuildEnv, ' ') $(_MonoCMakeConfigureCommand)</_MonoCMakeConfigureCommand>
470472
<_MonoCMakeConfigureCommand Condition="'$(TargetsBrowser)' == 'true' and '$(OS)' != 'Windows_NT'">bash -c 'source $(EMSDK_PATH)/emsdk_env.sh 2>&amp;1 &amp;&amp; emcmake $(_MonoCMakeConfigureCommand)'</_MonoCMakeConfigureCommand>

0 commit comments

Comments
 (0)