Skip to content

Commit 5fdc188

Browse files
authored
Remove bash dependency from init-compiler.sh (#77304)
1 parent 4d0fcf2 commit 5fdc188

File tree

8 files changed

+98
-65
lines changed

8 files changed

+98
-65
lines changed

eng/common/native/init-compiler.sh

Lines changed: 37 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,25 @@
1-
#!/usr/bin/env bash
1+
#!/bin/sh
22
#
33
# This file detects the C/C++ compiler and exports it to the CC/CXX environment variables
44
#
55
# NOTE: some scripts source this file and rely on stdout being empty, make sure to not output anything here!
66

7-
if [[ "$#" -lt 3 ]]; then
7+
if [ -z "$build_arch" ] || [ -z "$compiler" ]; then
88
echo "Usage..."
9-
echo "init-compiler.sh <script directory> <Architecture> <compiler>"
10-
echo "Specify the script directory."
9+
echo "build_arch=<ARCH> compiler=<NAME> init-compiler.sh"
1110
echo "Specify the target architecture."
1211
echo "Specify the name of compiler (clang or gcc)."
1312
exit 1
1413
fi
1514

16-
nativescriptroot="$1"
17-
build_arch="$2"
18-
compiler="$3"
19-
2015
case "$compiler" in
2116
clang*|-clang*|--clang*)
2217
# clangx.y or clang-x.y
2318
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
19+
majorVersion="${version%%.*}"
20+
[ -z "${version##*.*}" ] && minorVersion="${version#*.}"
21+
22+
if [ -z "$minorVersion" ] && [ -n "$majorVersion" ] && [ "$majorVersion" -le 6 ]; then
2823
minorVersion=0;
2924
fi
3025
compiler=clang
@@ -33,23 +28,20 @@ case "$compiler" in
3328
gcc*|-gcc*|--gcc*)
3429
# gccx.y or gcc-x.y
3530
version="$(echo "$compiler" | tr -d '[:alpha:]-=')"
36-
parts=(${version//./ })
37-
majorVersion="${parts[0]}"
38-
minorVersion="${parts[1]}"
31+
majorVersion="${version%%.*}"
32+
[ -z "${version##*.*}" ] && minorVersion="${version#*.}"
3933
compiler=gcc
4034
;;
4135
esac
4236

4337
cxxCompiler="$compiler++"
4438

45-
. "$nativescriptroot"/../pipeline-logging-functions.sh
46-
4739
# clear the existing CC and CXX from environment
4840
CC=
4941
CXX=
5042
LDFLAGS=
5143

52-
if [[ "$compiler" == "gcc" ]]; then cxxCompiler="g++"; fi
44+
if [ "$compiler" = "gcc" ]; then cxxCompiler="g++"; fi
5345

5446
check_version_exists() {
5547
desired_version=-1
@@ -66,74 +58,75 @@ check_version_exists() {
6658
echo "$desired_version"
6759
}
6860

69-
if [[ -z "$CLR_CC" ]]; then
61+
if [ -z "$CLR_CC" ]; then
7062

7163
# Set default versions
72-
if [[ -z "$majorVersion" ]]; then
64+
if [ -z "$majorVersion" ]; then
7365
# note: gcc (all versions) and clang versions higher than 6 do not have minor version in file name, if it is zero.
74-
if [[ "$compiler" == "clang" ]]; then versions=( 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 )
75-
elif [[ "$compiler" == "gcc" ]]; then versions=( 12 11 10 9 8 7 6 5 4.9 ); fi
76-
77-
for version in "${versions[@]}"; do
78-
parts=(${version//./ })
79-
desired_version="$(check_version_exists "${parts[0]}" "${parts[1]}")"
80-
if [[ "$desired_version" != "-1" ]]; then majorVersion="${parts[0]}"; break; fi
66+
if [ "$compiler" = "clang" ]; then versions="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"
67+
elif [ "$compiler" = "gcc" ]; then versions="12 11 10 9 8 7 6 5 4.9"; fi
68+
69+
for version in $versions; do
70+
_major="${version%%.*}"
71+
[ -z "${version##*.*}" ] && _minor="${version#*.}"
72+
desired_version="$(check_version_exists "$_major" "$_minor")"
73+
if [ "$desired_version" != "-1" ]; then majorVersion="$_major"; break; fi
8174
done
8275

83-
if [[ -z "$majorVersion" ]]; then
76+
if [ -z "$majorVersion" ]; then
8477
if command -v "$compiler" > /dev/null; then
85-
if [[ "$(uname)" != "Darwin" ]]; then
86-
Write-PipelineTelemetryError -category "Build" -type "warning" "Specific version of $compiler not found, falling back to use the one in PATH."
78+
if [ "$(uname)" != "Darwin" ]; then
79+
echo "Warning: Specific version of $compiler not found, falling back to use the one in PATH."
8780
fi
8881
CC="$(command -v "$compiler")"
8982
CXX="$(command -v "$cxxCompiler")"
9083
else
91-
Write-PipelineTelemetryError -category "Build" "No usable version of $compiler found."
84+
echo "No usable version of $compiler found."
9285
exit 1
9386
fi
9487
else
95-
if [[ "$compiler" == "clang" && "$majorVersion" -lt 5 ]]; then
96-
if [[ "$build_arch" == "arm" || "$build_arch" == "armel" ]]; then
88+
if [ "$compiler" = "clang" ] && [ "$majorVersion" -lt 5 ]; then
89+
if [ "$build_arch" = "arm" ] || [ "$build_arch" = "armel" ]; then
9790
if command -v "$compiler" > /dev/null; then
98-
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."
91+
echo "Warning: Found clang version $majorVersion which is not supported on arm/armel architectures, falling back to use clang from PATH."
9992
CC="$(command -v "$compiler")"
10093
CXX="$(command -v "$cxxCompiler")"
10194
else
102-
Write-PipelineTelemetryError -category "Build" "Found clang version $majorVersion which is not supported on arm/armel architectures, and there is no clang in PATH."
95+
echo "Found clang version $majorVersion which is not supported on arm/armel architectures, and there is no clang in PATH."
10396
exit 1
10497
fi
10598
fi
10699
fi
107100
fi
108101
else
109102
desired_version="$(check_version_exists "$majorVersion" "$minorVersion")"
110-
if [[ "$desired_version" == "-1" ]]; then
111-
Write-PipelineTelemetryError -category "Build" "Could not find specific version of $compiler: $majorVersion $minorVersion."
103+
if [ "$desired_version" = "-1" ]; then
104+
echo "Could not find specific version of $compiler: $majorVersion $minorVersion."
112105
exit 1
113106
fi
114107
fi
115108

116-
if [[ -z "$CC" ]]; then
109+
if [ -z "$CC" ]; then
117110
CC="$(command -v "$compiler$desired_version")"
118111
CXX="$(command -v "$cxxCompiler$desired_version")"
119-
if [[ -z "$CXX" ]]; then CXX="$(command -v "$cxxCompiler")"; fi
112+
if [ -z "$CXX" ]; then CXX="$(command -v "$cxxCompiler")"; fi
120113
fi
121114
else
122-
if [[ ! -f "$CLR_CC" ]]; then
123-
Write-PipelineTelemetryError -category "Build" "CLR_CC is set but path '$CLR_CC' does not exist"
115+
if [ ! -f "$CLR_CC" ]; then
116+
echo "CLR_CC is set but path '$CLR_CC' does not exist"
124117
exit 1
125118
fi
126119
CC="$CLR_CC"
127120
CXX="$CLR_CXX"
128121
fi
129122

130-
if [[ -z "$CC" ]]; then
131-
Write-PipelineTelemetryError -category "Build" "Unable to find $compiler."
123+
if [ -z "$CC" ]; then
124+
echo "Unable to find $compiler."
132125
exit 1
133126
fi
134127

135128
# Only lld version >= 9 can be considered stable. lld doesn't support s390x.
136-
if [[ "$compiler" == "clang" && "$majorVersion" -ge 9 && "$build_arch" != "s390x" ]]; then
129+
if [ "$compiler" = "clang" ] && [ -n "$majorVersion" ] && [ "$majorVersion" -ge 9 ] && [ "$build_arch" != "s390x" ]; then
137130
if "$CC" -fuse-ld=lld -Wl,--version >/dev/null 2>&1; then
138131
LDFLAGS="-fuse-ld=lld"
139132
fi

eng/native/gen-buildsys.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ compiler="$5"
2525

2626
if [[ "$compiler" != "default" ]]; then
2727
nativescriptroot="$( cd -P "$scriptroot/../common/native" && pwd )"
28-
source "$nativescriptroot/init-compiler.sh" "$nativescriptroot" "$host_arch" "$compiler"
28+
build_arch="$host_arch" compiler="$compiler" . "$nativescriptroot/init-compiler.sh"
2929

3030
CCC_CC="$CC"
3131
CCC_CXX="$CXX"

eng/testing/tests.singlefile.targets

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
<PropertyGroup Condition="'$(TestNativeAot)' == 'true'">
2222
<IlcToolsPath>$(CoreCLRILCompilerDir)</IlcToolsPath>
2323
<IlcToolsPath Condition="'$(TargetArchitecture)' != '$(BuildArchitecture)'">$(CoreCLRCrossILCompilerDir)</IlcToolsPath>
24-
<CppCompilerAndLinker Condition="'$(TargetArchitecture)' != '$(BuildArchitecture)' and '$(HostOS)' == 'linux' and '$(RuntimeIdentifier)' == 'linux-musl-arm64'">clang-15</CppCompilerAndLinker>
25-
<CppCompilerAndLinker Condition="'$(TargetArchitecture)' != '$(BuildArchitecture)' and '$(HostOS)' == 'linux' and '$(RuntimeIdentifier)' != 'linux-musl-arm64'">clang-9</CppCompilerAndLinker>
2624
<SysRoot Condition="'$(TargetArchitecture)' != '$(BuildArchitecture)' and '$(HostOS)' != 'windows'">$(ROOTFS_DIR)</SysRoot>
2725
<IlcBuildTasksPath>$(CoreCLRILCompilerDir)netstandard/ILCompiler.Build.Tasks.dll</IlcBuildTasksPath>
2826
<IlcSdkPath>$(CoreCLRAotSdkDir)</IlcSdkPath>
@@ -84,6 +82,28 @@
8482
</ItemGroup>
8583
</Target>
8684

85+
<!--
86+
Use init-compiler.sh to locate the compiler toolchain which was resolved for rest of the build.
87+
This target is essentially an override hook which is called before `SetupOSSpecificProps` in
88+
`Microsoft.NETCore.Native.Unix.targets`. Note that the input is `CppCompilerAndLinker`
89+
and the output is `CppLinker`, because from `SetupOSSpecificProps` onwards, we only use `CppLinker`
90+
(when alternative compiler, i.e. gcc, is not selected)
91+
-->
92+
<Target Name="LocateNativeCompiler"
93+
Condition="'$(TestNativeAot)' == 'true' and '$(HostOS)' != 'windows'"
94+
BeforeTargets="SetupOSSpecificProps">
95+
<PropertyGroup>
96+
<CppCompilerAndLinker Condition="'$(CppCompilerAndLinker)' == ''">clang</CppCompilerAndLinker>
97+
</PropertyGroup>
98+
99+
<Exec Command="sh -c 'build_arch=&quot;$(TargetArchitecture)&quot; compiler=&quot;$(CppCompilerAndLinker)&quot; . &quot;$(RepositoryEngineeringDir)/common/native/init-compiler.sh&quot; &amp;&amp; echo $CC' 2>/dev/null"
100+
EchoOff="true"
101+
ConsoleToMsBuild="true"
102+
StandardOutputImportance="Low">
103+
<Output TaskParameter="ConsoleOutput" PropertyName="CppLinker" />
104+
</Exec>
105+
</Target>
106+
87107
<Target Name="PublishTestAsSingleFile"
88108
Condition="'$(IsCrossTargetingBuild)' != 'true'"
89109
AfterTargets="Build"

src/coreclr/nativeaot/BuildIntegration/Microsoft.NETCore.Native.Unix.targets

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,10 @@ The .NET Foundation licenses this file to you under the MIT license.
140140
</PropertyGroup>
141141

142142
<PropertyGroup Condition="'$(ObjCopyName)' == '' and '$(TargetOS)' != 'OSX'">
143-
<ObjCopyName Condition="'$(CppCompilerAndLinker)' != 'clang'">objcopy</ObjCopyName>
144-
<ObjCopyName Condition="'$(CppCompilerAndLinker)' == 'clang'">llvm-objcopy</ObjCopyName>
143+
<ObjCopyName Condition="!$(CppCompilerAndLinker.Contains('clang'))">objcopy</ObjCopyName>
144+
<ObjCopyName Condition="$(CppCompilerAndLinker.Contains('clang'))">llvm-objcopy</ObjCopyName>
145145
<ObjCopyNameAlternative />
146-
<ObjCopyNameAlternative Condition="'$(CppCompilerAndLinker)' == 'clang'">objcopy</ObjCopyNameAlternative>
146+
<ObjCopyNameAlternative Condition="$(CppCompilerAndLinker.Contains('clang'))">objcopy</ObjCopyNameAlternative>
147147
</PropertyGroup>
148148

149149
<Error Condition="'$(_WhereLinker)' != '0' and '$(TargetOS)' == 'OSX'" Text="Platform linker ('$(CppLinker)') not found in PATH. Try installing Xcode to resolve the problem." />

src/coreclr/tools/aot/crossgen2/crossgen2.csproj

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@
3131
<PropertyGroup Condition="'$(NativeAotSupported)' == 'true'">
3232
<IlcToolsPath>$(CoreCLRILCompilerDir)</IlcToolsPath>
3333
<IlcToolsPath Condition="'$(TargetArchitecture)' != '$(BuildArchitecture)'">$(CoreCLRCrossILCompilerDir)</IlcToolsPath>
34-
<CppCompilerAndLinker Condition="'$(TargetArchitecture)' != '$(BuildArchitecture)' and '$(HostOS)' == 'linux' and '$(RuntimeIdentifier)' == 'linux-musl-arm64'">clang-15</CppCompilerAndLinker>
35-
<CppCompilerAndLinker Condition="'$(TargetArchitecture)' != '$(BuildArchitecture)' and '$(HostOS)' == 'linux' and '$(RuntimeIdentifier)' != 'linux-musl-arm64'">clang-9</CppCompilerAndLinker>
3634
<SysRoot Condition="'$(TargetArchitecture)' != '$(BuildArchitecture)' and '$(HostOS)' != 'windows'">$(ROOTFS_DIR)</SysRoot>
3735
<IlcBuildTasksPath>$(CoreCLRILCompilerDir)netstandard/ILCompiler.Build.Tasks.dll</IlcBuildTasksPath>
3836
<IlcSdkPath>$(CoreCLRAotSdkDir)</IlcSdkPath>
@@ -59,4 +57,19 @@
5957
</ItemGroup>
6058
</Target>
6159

60+
<Target Name="LocateNativeCompiler"
61+
Condition="'$(NativeAotSupported)' == 'true' and '$(_IsPublishing)' == 'true' and '$(HostOS)' != 'windows'"
62+
BeforeTargets="SetupOSSpecificProps">
63+
<PropertyGroup>
64+
<CppCompilerAndLinker Condition="'$(CppCompilerAndLinker)' == ''">clang</CppCompilerAndLinker>
65+
</PropertyGroup>
66+
67+
<Exec Command="sh -c 'build_arch=&quot;$(TargetArchitecture)&quot; compiler=&quot;$(CppCompilerAndLinker)&quot; . &quot;$(RepositoryEngineeringDir)/common/native/init-compiler.sh&quot; &amp;&amp; echo $CC' 2>/dev/null"
68+
EchoOff="true"
69+
ConsoleToMsBuild="true"
70+
StandardOutputImportance="Low">
71+
<Output TaskParameter="ConsoleOutput" PropertyName="CppLinker" />
72+
</Exec>
73+
</Target>
74+
6275
</Project>

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,14 @@
4444
<NativeCompiler Condition="'$(NativeCompiler)' == ''">clang</NativeCompiler>
4545
</PropertyGroup>
4646

47-
<Exec Command="bash -c 'source &quot;$(RepositoryEngineeringDir)/common/native/init-compiler.sh&quot; &quot;$(RepositoryEngineeringDir)/common/native&quot; $(TargetArchitecture) $(NativeCompiler) &amp;&amp; echo $CC' 2>/dev/null"
47+
<Exec Command="sh -c 'build_arch=&quot;$(TargetArchitecture)&quot; compiler=&quot;$(NativeCompiler)&quot; . &quot;$(RepositoryEngineeringDir)/common/native/init-compiler.sh&quot; &amp;&amp; echo $CC' 2>/dev/null"
4848
EchoOff="true"
4949
ConsoleToMsBuild="true"
5050
StandardOutputImportance="Low">
5151
<Output TaskParameter="ConsoleOutput" PropertyName="DnneCompilerCommand" />
5252
</Exec>
5353

54-
<Exec Command="bash -c 'source &quot;$(RepositoryEngineeringDir)/common/native/init-compiler.sh&quot; &quot;$(RepositoryEngineeringDir)/common/native&quot; $(TargetArchitecture) $(NativeCompiler) &amp;&amp; echo $LDFLAGS' 2>/dev/null"
54+
<Exec Command="sh -c 'build_arch=&quot;$(TargetArchitecture)&quot; compiler=&quot;$(NativeCompiler)&quot; . &quot;$(RepositoryEngineeringDir)/common/native/init-compiler.sh&quot; &amp;&amp; echo $LDFLAGS' 2>/dev/null"
5555
EchoOff="true"
5656
ConsoleToMsBuild="true"
5757
StandardOutputImportance="Low">

src/mono/mono.proj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@
513513
<PropertyGroup>
514514
<EMSDK_PATH>$([MSBuild]::EnsureTrailingSlash('$(EMSDK_PATH)'))</EMSDK_PATH>
515515
<_MonoCMakeConfigureCommand>cmake @(_MonoCMakeArgs, ' ') $(MonoCMakeExtraArgs) &quot;$(MonoProjectRoot.TrimEnd('\/'))&quot;</_MonoCMakeConfigureCommand>
516-
<_MonoCMakeConfigureCommand Condition="'$(TargetsBrowser)' != 'true' and '$(_MonoRunInitCompiler)' != 'false' and '$(HostOS)' != 'windows'">bash -c 'source $(RepositoryEngineeringCommonDir)native/init-compiler.sh &quot;$(RepositoryEngineeringCommonDir)native&quot; &quot;$(_CompilerTargetArch)&quot; &quot;$(MonoCCompiler)&quot; &amp;&amp; @(_MonoBuildEnv, ' ') $(_MonoCMakeConfigureCommand)'</_MonoCMakeConfigureCommand>
516+
<_MonoCMakeConfigureCommand Condition="'$(TargetsBrowser)' != 'true' and '$(_MonoRunInitCompiler)' != 'false' and '$(HostOS)' != 'windows'">sh -c 'build_arch=&quot;$(_CompilerTargetArch)&quot; compiler=&quot;$(MonoCCompiler)&quot; . &quot;$(RepositoryEngineeringCommonDir)native/init-compiler.sh&quot; &amp;&amp; @(_MonoBuildEnv, ' ') $(_MonoCMakeConfigureCommand)'</_MonoCMakeConfigureCommand>
517517
<_MonoCMakeConfigureCommand Condition="'$(TargetsBrowser)' != 'true' and '$(_MonoRunInitCompiler)' != 'false' and '$(HostOS)' == 'windows'">call &quot;$(RepositoryEngineeringDir)native\init-vs-env.cmd&quot; $(_CompilerTargetArch) &amp;&amp; cd /D &quot;$(MonoObjDir)&quot; &amp;&amp; @(_MonoBuildEnv, ' ') $(_MonoCMakeConfigureCommand)</_MonoCMakeConfigureCommand>
518518
<_MonoCMakeConfigureCommand Condition="'$(TargetsBrowser)' != 'true' and '$(_MonoRunInitCompiler)' == 'false'">$(_MonoCCOption) $(_MonoCXXOption) @(_MonoBuildEnv, ' ') $(_MonoCMakeConfigureCommand)</_MonoCMakeConfigureCommand>
519519
<_MonoCMakeConfigureCommand Condition="'$(TargetsBrowser)' == 'true' and '$(HostOS)' != 'windows'">bash -c 'source $(EMSDK_PATH)/emsdk_env.sh 2>&amp;1 &amp;&amp; emcmake $(_MonoCMakeConfigureCommand)'</_MonoCMakeConfigureCommand>

src/native/libs/System.Globalization.Native/local_build.sh

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,24 @@
1010

1111
# Currently, only Linux is supported
1212

13-
SHIM_SOURCE_DIR=$1/native/src
14-
INTERMEDIATE_OUTPUT_PATH=$2
13+
SHIM_SOURCE_DIR="$1"/native/src
14+
INTERMEDIATE_OUTPUT_PATH="$2"
1515

1616
if [ -d "$SHIM_SOURCE_DIR" ]; then
1717
LOCAL_SHIM_DIR="$INTERMEDIATE_OUTPUT_PATH"/libs/System.Globalization.Native/build
18-
mkdir -p "$LOCAL_SHIM_DIR" && cd "$LOCAL_SHIM_DIR"
19-
if [ $? -ne 0 ]; then echo "local_build.sh::ERROR: Cannot use local build directory"; exit 1; fi
20-
cmake -S "$SHIM_SOURCE_DIR/libs/System.Globalization.Native/" -DLOCAL_BUILD:STRING=1 -DCLR_CMAKE_TARGET_UNIX:STRING=1
21-
if [ $? -ne 0 ]; then echo "local_build.sh::ERROR: cmake failed"; exit 1; fi
22-
make -j
23-
if [ $? -ne 0 ]; then echo "local_build.sh::ERROR: Build failed"; exit 1; fi
24-
fi
2518

26-
exit 0
19+
if ! { mkdir -p "$LOCAL_SHIM_DIR" && cd "$LOCAL_SHIM_DIR"; }; then
20+
echo "local_build.sh::ERROR: Cannot use local build directory"
21+
exit 1
22+
fi
23+
24+
if ! cmake -S "$SHIM_SOURCE_DIR/libs/System.Globalization.Native/" -DLOCAL_BUILD:STRING=1 -DCLR_CMAKE_TARGET_UNIX:STRING=1; then
25+
echo "local_build.sh::ERROR: cmake failed"
26+
exit 1
27+
fi
28+
29+
if ! make -j; then
30+
echo "local_build.sh::ERROR: Build failed"
31+
exit 1
32+
fi
33+
fi

0 commit comments

Comments
 (0)