[Tizen] Add CoreCLR tests build dependencies v3.1.3 for armel, arm64
[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     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 [ "$(uname -m | grep "i[3456]86")" = "i686" ]; then
69     echo "Warning: build not supported on 32 bit Unix"
70 fi
71
72 if [ "$(uname -m)" = "armhf" ] || [ "$(uname -m)" = "arm64" ] || [ "$(uname -m)" = "aarch64" ];  then
73     if [ "$(uname -m)" = "armhf" ]; then
74         __PKG_ARCH=arm
75     fi
76
77     if [ "$(uname -m)" = "arm64" ] || [ "$(uname -m)" = "aarch64" ]; then
78         __PKG_ARCH=arm64
79     fi
80 else
81     __PKG_ARCH=x64
82 fi
83
84 OSName=$(uname -s)
85 case $OSName in
86     Darwin)
87         OS=OSX
88         __PKG_RID=osx
89         ulimit -n 2048
90         # Format x.y.z as single integer with three digits for each part
91         VERSION=`sw_vers -productVersion| sed -e 's/\./ /g' | xargs printf "%03d%03d%03d"`
92         if [ "$VERSION" -lt 010012000 ]; then
93             echo error: macOS version `sw_vers -productVersion` is too old. 10.12 is needed as minimum.
94             exit 1
95         fi
96         ;;
97     FreeBSD)
98         __PKG_RID=freebsd
99         OS=FreeBSD
100         ;;
101     Linux)
102         __PKG_RID=linux
103         OS=Linux
104
105         if [ -e /etc/os-release ]; then
106             source /etc/os-release
107             if [[ $ID == "alpine" ]]; then
108                 __PKG_RID=linux-musl
109             fi
110         elif [ -e /etc/redhat-release ]; then
111             redhatRelease=$(</etc/redhat-release)
112             if [[ $redhatRelease == "CentOS release 6."* || $redhatRelease == "Red Hat Enterprise Linux Server release 6."* ]]; then
113                 __PKG_RID=rhel.6
114             fi
115         fi
116         OSArch=$(uname -m)
117         if [ $OSArch == 'armv7l' ];then
118             __PKG_ARCH=arm
119         elif [ $OSArch == 'aarch64' ]; then
120             __PKG_ARCH=arm64
121         fi
122
123         ;;
124
125     *)
126     echo "Unsupported OS '$OSName' detected. Downloading linux-$__PKG_ARCH tools."
127         OS=Linux
128         __PKG_RID=linux
129         ;;
130 esac
131 __PKG_RID=$__PKG_RID-$__PKG_ARCH
132
133 if [ ! -e "$__DOTNET_CMD" ]; then
134     source $__scriptpath/init-dotnet.sh
135     if [ ! -e "$__DOTNET_CMD" ]; then
136         echo "ERROR: Ensure arcade dotnet install did not install dotnet at $__DOTNET_CMD"
137         exit 1
138     fi
139 fi
140
141 if [ ! -e "$__BUILD_TOOLS_PATH" ]; then
142     echo "Restoring BuildTools version $__BUILD_TOOLS_PACKAGE_VERSION..."
143     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"
144     "$__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"
145     if [ ! -e "$__BUILD_TOOLS_PATH/init-tools.sh" ]; then
146         echo "ERROR: Could not restore build tools correctly." 1>&2
147         display_error_message
148     fi
149 fi
150
151 if [ -z "${__ILASM_RID-}" ]; then
152     __ILASM_RID=$__PKG_RID
153 fi
154
155 echo "Using RID $__ILASM_RID for BuildTools native tools"
156 export NATIVE_TOOLS_RID=$__ILASM_RID
157
158 # Build tools only supported on x64
159 if [ "${__PKG_ARCH}" != "x64" ] &&  [ "${__PKG_ARCH}" != "arm" ]; then
160     echo "Skipped installing build tools."
161 else
162     echo "Initializing BuildTools..."
163     echo "Running: $__BUILD_TOOLS_PATH/init-tools.sh $__scriptpath $__DOTNET_CMD $__TOOLRUNTIME_DIR $__PACKAGES_DIR" >> "$__init_tools_log"
164
165     # Executables restored with .NET Core 2.0 do not have executable permission flags. https://github.com/NuGet/Home/issues/4424
166     chmod +x "$__BUILD_TOOLS_PATH/init-tools.sh"
167     "$__BUILD_TOOLS_PATH/init-tools.sh" "$__scriptpath" "$__DOTNET_CMD" "$__TOOLRUNTIME_DIR" "$__PACKAGES_DIR" >> "$__init_tools_log"
168     if [ "$?" != "0" ]; then
169         echo "ERROR: An error occurred when trying to initialize the tools." 1>&2
170         display_error_message
171         exit 1
172     fi
173
174
175     if [ ! -n "${DotNetBootstrapCliTarPath-}" ]; then
176         # Assume ilasm is not in nuget yet when bootstrapping...
177         # 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.
178         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"
179         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"
180     fi
181
182     echo "Making all .sh files executable under Tools."
183     # Executables restored with .NET Core 2.0 do not have executable permission flags. https://github.com/NuGet/Home/issues/4424
184     ls "$__scriptpath/Tools/"*.sh | xargs chmod +x
185     ls "$__scriptpath/Tools/scripts/docker/"*.sh | xargs chmod +x
186
187     "$__scriptpath/Tools/crossgen.sh" "$__scriptpath/Tools" $__PKG_RID
188
189     mkdir -p "$(dirname "$__BUILD_TOOLS_SEMAPHORE")" && touch "$__BUILD_TOOLS_SEMAPHORE"
190
191     echo "Done initializing tools."
192 fi