[Tizen] Add method to write coredump of .NET process (#317)
[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="$__scriptpath/.dotnet"
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     if [ "$0" = "$BASH_SOURCE" ]; then
21         # not sourced
22         exit 0
23     else
24         # sourced
25         return #return instead of exit because this script is inlined in other scripts which we don't want to exit
26     fi
27 fi
28
29 if [ -e "$__TOOLRUNTIME_DIR" ]; then rm -rf -- "$__TOOLRUNTIME_DIR"; fi
30
31 if [ -d "${DotNetBuildToolsDir:-}" ]; then
32     echo "Using tools from '$DotNetBuildToolsDir'."
33     ln -s "$DotNetBuildToolsDir" "$__TOOLRUNTIME_DIR"
34
35     if [ ! -e "$__DOTNET_CMD" ]; then
36         echo "ERROR: Ensure that $DotNetBuildToolsDir contains the .NET Core SDK at $__DOTNET_PATH"
37         exit 1
38     fi
39
40     echo "Done initializing tools."
41     mkdir -p "$(dirname "$__BUILD_TOOLS_SEMAPHORE")" && touch "$__BUILD_TOOLS_SEMAPHORE"
42     return #return instead of exit because this script is inlined in other scripts which we don't want to exit
43 fi
44
45 echo "Running: $__scriptpath/init-tools.sh" > "$__init_tools_log"
46
47 display_error_message()
48 {
49     echo "Please check the detailed log that follows." 1>&2
50     cat "$__init_tools_log" 1>&2
51 }
52
53 # Executes a command and retries if it fails.
54 execute_with_retry() {
55     local count=0
56     local retries=${retries:-5}
57     local waitFactor=${waitFactor:-6}
58     until "$@"; do
59         local exit=$?
60         count=$(( $count + 1 ))
61         if [ $count -lt $retries ]; then
62             local wait=$(( waitFactor ** (( count - 1 )) ))
63             echo "Retry $count/$retries exited $exit, retrying in $wait seconds..."
64             sleep $wait
65         else
66             say_err "Retry $count/$retries exited $exit, no more retries left."
67             return $exit
68         fi
69     done
70
71     return 0
72 }
73
74 if [ "$(uname -m | grep "i[3456]86")" = "i686" ]; then
75     echo "Warning: build not supported on 32 bit Unix"
76 fi
77
78 if [ "$(uname -m)" = "armhf" ] || [ "$(uname -m)" = "arm64" ] || [ "$(uname -m)" = "aarch64" ];  then
79     if [ "$(uname -m)" = "armhf" ]; then
80         __PKG_ARCH=arm
81     fi
82
83     if [ "$(uname -m)" = "arm64" ] || [ "$(uname -m)" = "aarch64" ]; then
84         __PKG_ARCH=arm64
85     fi
86 else
87     __PKG_ARCH=x64
88 fi
89
90 OSName=$(uname -s)
91 case $OSName in
92     Darwin)
93         OS=OSX
94         __PKG_RID=osx
95         ulimit -n 2048
96         # Format x.y.z as single integer with three digits for each part
97         VERSION=`sw_vers -productVersion| sed -e 's/\./ /g' | xargs printf "%03d%03d%03d"`
98         if [ "$VERSION" -lt 010012000 ]; then
99             echo error: macOS version `sw_vers -productVersion` is too old. 10.12 is needed as minimum.
100             exit 1
101         fi
102         ;;
103     FreeBSD)
104         __PKG_RID=freebsd
105         OS=FreeBSD
106         ;;
107     Linux)
108         __PKG_RID=linux
109         OS=Linux
110
111         if [ -e /etc/os-release ]; then
112             source /etc/os-release
113             if [[ $ID == "alpine" ]]; then
114                 __PKG_RID=linux-musl
115             fi
116         elif [ -e /etc/redhat-release ]; then
117             redhatRelease=$(</etc/redhat-release)
118             if [[ $redhatRelease == "CentOS release 6."* || $redhatRelease == "Red Hat Enterprise Linux Server release 6."* ]]; then
119                 __PKG_RID=rhel.6
120             fi
121         fi
122         OSArch=$(uname -m)
123         if [ $OSArch == 'armv7l' ];then
124             __PKG_ARCH=arm
125         elif [ $OSArch == 'aarch64' ]; then
126             __PKG_ARCH=arm64
127         fi
128
129         ;;
130
131     *)
132     echo "Unsupported OS '$OSName' detected. Downloading linux-$__PKG_ARCH tools."
133         OS=Linux
134         __PKG_RID=linux
135         ;;
136 esac
137 __PKG_RID=$__PKG_RID-$__PKG_ARCH
138
139 if [ ! -e "$__DOTNET_CMD" ]; then
140     source $__scriptpath/init-dotnet.sh
141     if [ ! -e "$__DOTNET_CMD" ]; then
142         echo "ERROR: Ensure arcade dotnet install did not install dotnet at $__DOTNET_CMD"
143         exit 1
144     fi
145 fi
146
147 if [ ! -e "$__BUILD_TOOLS_PATH" ]; then
148     echo "Restoring BuildTools version $__BUILD_TOOLS_PACKAGE_VERSION..."
149     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"
150     "$__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"
151     if [ ! -e "$__BUILD_TOOLS_PATH/init-tools.sh" ]; then
152         echo "ERROR: Could not restore build tools correctly." 1>&2
153         display_error_message
154     fi
155 fi
156
157 if [ -z "${__ILASM_RID-}" ]; then
158     __ILASM_RID=$__PKG_RID
159 fi
160
161 echo "Using RID $__ILASM_RID for BuildTools native tools"
162 export NATIVE_TOOLS_RID=$__ILASM_RID
163
164 # Build tools only supported on x64
165 if [ "${__PKG_ARCH}" != "x64" ] &&  [ "${__PKG_ARCH}" != "arm" ]; then
166     echo "Skipped installing build tools."
167 else
168     echo "Initializing BuildTools..."
169     echo "Running: $__BUILD_TOOLS_PATH/init-tools.sh $__scriptpath $__DOTNET_CMD $__TOOLRUNTIME_DIR $__PACKAGES_DIR" >> "$__init_tools_log"
170
171     # Executables restored with .NET Core 2.0 do not have executable permission flags. https://github.com/NuGet/Home/issues/4424
172     chmod +x "$__BUILD_TOOLS_PATH/init-tools.sh"
173     "$__BUILD_TOOLS_PATH/init-tools.sh" "$__scriptpath" "$__DOTNET_CMD" "$__TOOLRUNTIME_DIR" "$__PACKAGES_DIR" >> "$__init_tools_log"
174     if [ "$?" != "0" ]; then
175         echo "ERROR: An error occurred when trying to initialize the tools." 1>&2
176         display_error_message
177         exit 1
178     fi
179
180
181     if [ ! -n "${DotNetBootstrapCliTarPath-}" ]; then
182         # Assume ilasm is not in nuget yet when bootstrapping...
183         # ILasm/ILDasm used to be restored by buildtools. The reference there was a netocreapp2.0, which was prior to our support of linux-musl. We initialize it locally as a 3.0 with the new SDK.
184         echo "Running: eval $__DOTNET_CMD build ${__scriptpath}/tests/ilasm-restore/ilasm.depproj --no-cache --packages $__PACKAGES_DIR -r $NATIVE_TOOLS_RID -p:ILAsmPackageVersion=$__ILASM_VERSION  -p:ExpectedILAsmPath=$__TOOLRUNTIME_DIR/ilasm" >> "$__init_tools_log"
185         eval "$__DOTNET_CMD" build "${__scriptpath}/tests/ilasm-restore/ilasm.depproj" --no-cache --packages "$__PACKAGES_DIR" -r "$NATIVE_TOOLS_RID" -p:ILAsmPackageVersion="$__ILASM_VERSION" -p:ExpectedILAsmPath="$__TOOLRUNTIME_DIR/ilasm"
186     fi
187
188     echo "Making all .sh files executable under Tools."
189     # Executables restored with .NET Core 2.0 do not have executable permission flags. https://github.com/NuGet/Home/issues/4424
190     ls "$__scriptpath/Tools/"*.sh | xargs chmod +x
191     ls "$__scriptpath/Tools/scripts/docker/"*.sh | xargs chmod +x
192
193     "$__scriptpath/Tools/crossgen.sh" "$__scriptpath/Tools" $__PKG_RID
194
195     mkdir -p "$(dirname "$__BUILD_TOOLS_SEMAPHORE")" && touch "$__BUILD_TOOLS_SEMAPHORE"
196
197     echo "Done initializing tools."
198 fi