Enable BSTR Field Marshaller for x-plat (#20264)
[platform/upstream/coreclr.git] / init-tools.sh
1 #!/usr/bin/env bash
2
3 __scriptpath=$(cd "$(dirname "$0")"; pwd -P)
4 __init_tools_log="$__scriptpath/init-tools.log"
5 __PACKAGES_DIR="$__scriptpath/packages"
6 __TOOLRUNTIME_DIR="$__scriptpath/Tools"
7 __DOTNET_PATH="$__TOOLRUNTIME_DIR/dotnetcli"
8 __DOTNET_CMD="$__DOTNET_PATH/dotnet"
9 if [ -z "${__BUILDTOOLS_SOURCE:-}" ]; then __BUILDTOOLS_SOURCE=https://dotnet.myget.org/F/dotnet-buildtools/api/v3/index.json; fi
10 export __BUILDTOOLS_USE_CSPROJ=true
11 __BUILD_TOOLS_PACKAGE_VERSION=$(cat "$__scriptpath/BuildToolsVersion.txt" | sed 's/\r$//') # remove CR if mounted repo on Windows drive
12 __DOTNET_TOOLS_VERSION=$(cat "$__scriptpath/DotnetCLIVersion.txt" | sed 's/\r$//') # remove CR if mounted repo on Windows drive
13 __ILASM_VERSION=$(cat "$__scriptpath/ILAsmVersion.txt" | sed 's/\r$//') # remove CR if mounted repo on Windows drive
14 __BUILD_TOOLS_PATH="$__PACKAGES_DIR/microsoft.dotnet.buildtools/$__BUILD_TOOLS_PACKAGE_VERSION/lib"
15 __INIT_TOOLS_RESTORE_PROJECT="$__scriptpath/init-tools.msbuild"
16 __BUILD_TOOLS_SEMAPHORE="$__TOOLRUNTIME_DIR/$__BUILD_TOOLS_PACKAGE_VERSION/init-tools.complete"
17
18 if [ -e "$__BUILD_TOOLS_SEMAPHORE" ]; then
19     echo "Tools are already initialized"
20     return #return instead of exit because this script is inlined in other scripts which we don't want to exit
21 fi
22
23 if [ -e "$__TOOLRUNTIME_DIR" ]; then rm -rf -- "$__TOOLRUNTIME_DIR"; fi
24
25 if [ -d "${DotNetBuildToolsDir:-}" ]; then
26     echo "Using tools from '$DotNetBuildToolsDir'."
27     ln -s "$DotNetBuildToolsDir" "$__TOOLRUNTIME_DIR"
28
29     if [ ! -e "$__DOTNET_CMD" ]; then
30         echo "ERROR: Ensure that $DotNetBuildToolsDir contains the .NET Core SDK at $__DOTNET_PATH"
31         exit 1
32     fi
33
34     echo "Done initializing tools."
35     mkdir -p "$(dirname "$__BUILD_TOOLS_SEMAPHORE")" && touch "$__BUILD_TOOLS_SEMAPHORE"
36     return #return instead of exit because this script is inlined in other scripts which we don't want to exit
37 fi
38
39 echo "Running: $__scriptpath/init-tools.sh" > "$__init_tools_log"
40
41 display_error_message()
42 {
43     echo "Please check the detailed log that follows." 1>&2
44     cat "$__init_tools_log" 1>&2
45 }
46
47 # Executes a command and retries if it fails.
48 execute_with_retry() {
49     local count=0
50     local retries=${retries:-5}
51     local waitFactor=${waitFactor:-6}
52     until "$@"; do
53         local exit=$?
54         count=$(( $count + 1 ))
55         if [ $count -lt $retries ]; then
56             local wait=$(( waitFactor ** (( count - 1 )) ))
57             echo "Retry $count/$retries exited $exit, retrying in $wait seconds..."
58             sleep $wait
59         else
60             say_err "Retry $count/$retries exited $exit, no more retries left."
61             return $exit
62         fi
63     done
64
65     return 0
66 }
67
68 if [ ! -e "$__DOTNET_PATH" ]; then
69     if [ -z "${__DOTNET_PKG:-}" ]; then
70         if [ "$(uname -m | grep "i[3456]86")" = "i686" ]; then
71             echo "Warning: build not supported on 32 bit Unix"
72         fi
73
74         if [ "$(uname -m)" = "armhf" ] || [ "$(uname -m)" = "arm64" ] || [ "$(uname -m)" = "aarch64" ];  then
75             if [ "$(uname -m)" = "armhf" ]; then
76                 __PKG_ARCH=arm
77             fi
78
79             if [ "$(uname -m)" = "arm64" ] || [ "$(uname -m)" = "aarch64" ]; then
80                 __PKG_ARCH=arm64
81             fi
82         else
83             __PKG_ARCH=x64
84         fi
85
86         OSName=$(uname -s)
87         case $OSName in
88             Darwin)
89                 OS=OSX
90                 __PKG_RID=osx
91                 ulimit -n 2048
92                 # Format x.y.z as single integer with three digits for each part
93                 VERSION=`sw_vers -productVersion| sed -e 's/\./ /g' | xargs printf "%03d%03d%03d"`
94                 if [ "$VERSION" -lt 010012000 ]; then
95                     echo error: macOS version `sw_vers -productVersion` is too old. 10.12 is needed as minimum.
96                     exit 1
97                 fi
98                 ;;
99
100             Linux)
101                 __PKG_RID=linux
102                 OS=Linux
103
104                 if [ -e /etc/os-release ]; then
105                     source /etc/os-release
106                     if [[ $ID == "alpine" ]]; then
107                         __PKG_RID=linux-musl
108                     fi
109                 elif [ -e /etc/redhat-release ]; then
110                     redhatRelease=$(</etc/redhat-release)
111                     if [[ $redhatRelease == "CentOS release 6."* || $redhatRelease == "Red Hat Enterprise Linux Server release 6."* ]]; then
112                         __PKG_RID=rhel.6
113                     fi
114                 fi
115                 OSArch=$(uname -m)
116                 if [ $OSArch == 'armv7l' ];then
117                     __PKG_ARCH=arm
118                 elif [ $OSArch == 'aarch64' ]; then
119                     __PKG_ARCH=arm64
120                 fi
121
122                 ;;
123
124             *)
125             echo "Unsupported OS '$OSName' detected. Downloading linux-$__PKG_ARCH tools."
126                 OS=Linux
127                 __PKG_RID=linux
128                 ;;
129         esac
130         __PKG_RID=$__PKG_RID-$__PKG_ARCH
131         __DOTNET_PKG=dotnet-sdk-${__DOTNET_TOOLS_VERSION}-$__PKG_RID
132     fi
133     mkdir -p "$__DOTNET_PATH"
134
135     echo "Installing dotnet cli..."
136     __DOTNET_LOCATION="https://dotnetcli.azureedge.net/dotnet/Sdk/${__DOTNET_TOOLS_VERSION}/${__DOTNET_PKG}.tar.gz"
137
138     install_dotnet_cli() {
139         if [[ -z "${DotNetBootstrapCliTarPath-}" ]]; then
140             echo "Installing '${__DOTNET_LOCATION}' to '$__DOTNET_PATH/dotnet.tar'"
141             rm -rf -- "$__DOTNET_PATH/*"
142             # curl has HTTPS CA trust-issues less often than wget, so lets try that first.
143             if command -v curl > /dev/null; then
144                 curl --retry 10 -sSL --create-dirs -o $__DOTNET_PATH/dotnet.tar ${__DOTNET_LOCATION}
145             else
146                 wget -q -O $__DOTNET_PATH/dotnet.tar ${__DOTNET_LOCATION}
147             fi
148         else
149             echo "Copying '$DotNetBootstrapCliTarPath' to '$__DOTNET_PATH/dotnet.tar'"
150             cp $DotNetBootstrapCliTarPath $__DOTNET_PATH/dotnet.tar
151         fi
152         cd "$__DOTNET_PATH"
153         tar -xf "$__DOTNET_PATH/dotnet.tar"
154     }
155     execute_with_retry install_dotnet_cli >> "$__init_tools_log" 2>&1
156
157     cd "$__scriptpath"
158 fi
159
160 if [ ! -e "$__BUILD_TOOLS_PATH" ]; then
161     echo "Restoring BuildTools version $__BUILD_TOOLS_PACKAGE_VERSION..."
162     echo "Running: $__DOTNET_CMD restore \"$__INIT_TOOLS_RESTORE_PROJECT\" --no-cache --packages $__PACKAGES_DIR --source $__BUILDTOOLS_SOURCE /p:BuildToolsPackageVersion=$__BUILD_TOOLS_PACKAGE_VERSION /p:ToolsDir=$__TOOLRUNTIME_DIR" >> "$__init_tools_log"
163     "$__DOTNET_CMD" restore "$__INIT_TOOLS_RESTORE_PROJECT" --no-cache --packages "$__PACKAGES_DIR" --source "$__BUILDTOOLS_SOURCE" /p:BuildToolsPackageVersion=$__BUILD_TOOLS_PACKAGE_VERSION /p:ToolsDir="$__TOOLRUNTIME_DIR" >> "$__init_tools_log"
164     if [ ! -e "$__BUILD_TOOLS_PATH/init-tools.sh" ]; then
165         echo "ERROR: Could not restore build tools correctly." 1>&2
166         display_error_message
167     fi
168 fi
169
170 if [ -z "${__ILASM_RID-}" ]; then
171     __ILASM_RID=$__PKG_RID
172 fi
173
174 echo "Using RID $__ILASM_RID for BuildTools native tools"
175
176 export ILASMCOMPILER_VERSION=$__ILASM_VERSION
177 export NATIVE_TOOLS_RID=$__ILASM_RID
178
179 if [ -n "${DotNetBootstrapCliTarPath-}" ]; then
180     # Assume ilasm is not in nuget yet when bootstrapping...
181     unset ILASMCOMPILER_VERSION
182 fi
183
184 # Build tools only supported on x64
185 if [ "${__PKG_ARCH}" != "x64" ] &&  [ "${__PKG_ARCH}" != "arm" ]; then
186     echo "Skipped installing build tools."
187 else
188     echo "Initializing BuildTools..."
189     echo "Running: $__BUILD_TOOLS_PATH/init-tools.sh $__scriptpath $__DOTNET_CMD $__TOOLRUNTIME_DIR $__PACKAGES_DIR" >> "$__init_tools_log"
190
191     # Executables restored with .NET Core 2.0 do not have executable permission flags. https://github.com/NuGet/Home/issues/4424
192     chmod +x "$__BUILD_TOOLS_PATH/init-tools.sh"
193     "$__BUILD_TOOLS_PATH/init-tools.sh" "$__scriptpath" "$__DOTNET_CMD" "$__TOOLRUNTIME_DIR" "$__PACKAGES_DIR" >> "$__init_tools_log"
194     if [ "$?" != "0" ]; then
195         echo "ERROR: An error occurred when trying to initialize the tools." 1>&2
196         display_error_message
197         exit 1
198     fi
199
200     echo "Making all .sh files executable under Tools."
201     # Executables restored with .NET Core 2.0 do not have executable permission flags. https://github.com/NuGet/Home/issues/4424
202     ls "$__scriptpath/Tools/"*.sh | xargs chmod +x
203     ls "$__scriptpath/Tools/scripts/docker/"*.sh | xargs chmod +x
204
205     "$__scriptpath/Tools/crossgen.sh" "$__scriptpath/Tools" $__PKG_RID
206
207     mkdir -p "$(dirname "$__BUILD_TOOLS_SEMAPHORE")" && touch "$__BUILD_TOOLS_SEMAPHORE"
208
209     echo "Done initializing tools."
210 fi