Skip to content

Commit 79e739d

Browse files
authored
Remove bash dependency from init-compiler (#11359)
1 parent 57ba56d commit 79e739d

File tree

3 files changed

+39
-45
lines changed

3 files changed

+39
-45
lines changed

eng/common/native/init-compiler.sh

100755100644
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/configure-toolset.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ function Test-FilesUseTelemetryOutput {
44
require_telemetry_exclude_files=(
55
'eng/common/build.sh'
66
'eng/common/cibuild.sh'
7+
'eng/common/native/init-compiler.sh'
78
'eng/common/cross/tizen-build-rootfs.sh'
89
'eng/common/cross/tizen-fetch.sh'
910
'eng/common/cross/build-android-rootfs.sh'

src/Microsoft.DotNet.CMake.Sdk/build/Microsoft.DotNet.CMake.Sdk.targets

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@
123123
<PropertyGroup>
124124
<_NativeScriptsDir>$([MSBuild]::NormalizeDirectory('$(RepositoryEngineeringDir)', 'common', 'native'))</_NativeScriptsDir>
125125
<CMakeCompilerSearchScript>
126-
. &quot;$([MSBuild]::MakeRelative('$(MSBuildProjectDirectory)', '$(_NativeScriptsDir)/init-compiler.sh'))&quot; &quot;$(_NativeScriptsDir)&quot; &quot;$(Platform)&quot; &quot;$(CMakeCompilerToolchain)&quot;
126+
build_arch=&quot;$(Platform)&quot; compiler=&quot;$(CMakeCompilerToolchain)&quot; . &quot;$([MSBuild]::MakeRelative('$(MSBuildProjectDirectory)', '$(_NativeScriptsDir)/init-compiler.sh'))&quot;
127127
</CMakeCompilerSearchScript>
128128
</PropertyGroup>
129129
</Target>

0 commit comments

Comments
 (0)