Merge pull request #6297 from CarolEidt/MorphGenTreeRefactors
[platform/upstream/coreclr.git] / init-tools.sh
1 #!/usr/bin/env bash
2
3 initDistroName()
4 {
5     if [ "$1" == "Linux" ]; then
6         if [ ! -e /etc/os-release ]; then
7             echo "WARNING: Can not determine runtime id for current distro."
8             export __DistroRid=""
9         else
10             source /etc/os-release
11             export __DistroRid="$ID.$VERSION_ID-x64"
12         fi
13     fi
14 }
15
16 __scriptpath=$(cd "$(dirname "$0")"; pwd -P)
17
18 # CI_SPECIFIC - On CI machines, $HOME may not be set. In such a case, create a subfolder and set the variable to set.
19 # This is needed by CLI to function.
20 if [ -z "$HOME" ]; then
21     if [ ! -d "$__scriptpath/temp_home" ]; then
22         mkdir temp_home
23     fi
24     export HOME=$__scriptpath/temp_home
25     echo "HOME not defined; setting it to $HOME"
26 fi
27
28 __PACKAGES_DIR=$__scriptpath/packages
29 __TOOLRUNTIME_DIR=$__scriptpath/Tools
30 __DOTNET_PATH=$__TOOLRUNTIME_DIR/dotnetcli
31 __DOTNET_CMD=$__DOTNET_PATH/dotnet
32 if [ -z "$__BUILDTOOLS_SOURCE" ]; then __BUILDTOOLS_SOURCE=https://www.myget.org/F/dotnet-buildtools/; fi
33 __BUILD_TOOLS_PACKAGE_VERSION=$(cat $__scriptpath/BuildToolsVersion.txt)
34 __DOTNET_TOOLS_VERSION=$(cat $__scriptpath/DotnetCLIVersion.txt)
35 __BUILD_TOOLS_PATH=$__PACKAGES_DIR/Microsoft.DotNet.BuildTools/$__BUILD_TOOLS_PACKAGE_VERSION/lib
36 __PROJECT_JSON_PATH=$__TOOLRUNTIME_DIR/$__BUILD_TOOLS_PACKAGE_VERSION
37 __PROJECT_JSON_FILE=$__PROJECT_JSON_PATH/project.json
38 __PROJECT_JSON_CONTENTS="{ \"dependencies\": { \"Microsoft.DotNet.BuildTools\": \"$__BUILD_TOOLS_PACKAGE_VERSION\" }, \"frameworks\": { \"dnxcore50\": { } } }"
39 __DistroRid=""
40
41 OSName=$(uname -s)
42 case $OSName in
43     Darwin)
44         OS=OSX
45         __DOTNET_PKG=dotnet-dev-osx-x64
46         ;;
47
48     Linux)
49         OS=Linux
50
51         if [ ! -e /etc/os-release ]; then
52             echo "Cannot determine Linux distribution, asuming Ubuntu 14.04."
53             __DOTNET_PKG=dotnet-dev-ubuntu.14.04-x64
54         else
55             source /etc/os-release
56             __DOTNET_PKG="dotnet-dev-$ID.$VERSION_ID-x64"
57         fi
58         ;;
59
60     *)
61         echo "Unsupported OS $OSName detected. Downloading ubuntu-x64 tools"
62         OS=Linux
63         __DOTNET_PKG=dotnet-dev-ubuntu.14.04-x64
64         ;;
65 esac
66
67 # Initialize Linux Distribution name and .NET CLI package name.
68
69 initDistroName $OS
70
71 # Work around mac build issue 
72 if [ "$OS" == "OSX" ]; then
73   echo Raising file open limit - otherwise Mac build may fail
74   echo ulimit -n 2048
75   ulimit -n 2048
76 fi
77
78 __CLIDownloadURL=https://dotnetcli.blob.core.windows.net/dotnet/preview/Binaries/${__DOTNET_TOOLS_VERSION}/${__DOTNET_PKG}.${__DOTNET_TOOLS_VERSION}.tar.gz
79 echo ".NET CLI will be downloaded from $__CLIDownloadURL"
80 echo "Locating $__PROJECT_JSON_FILE to see if we already downloaded .NET CLI tools..." 
81
82 if [ ! -e $__PROJECT_JSON_FILE ]; then
83     echo "$__PROJECT_JSON_FILE not found. Proceeding to download .NET CLI tools. " 
84     if [ -e $__TOOLRUNTIME_DIR ]; then rm -rf -- $__TOOLRUNTIME_DIR; fi
85
86     if [ ! -e $__DOTNET_PATH ]; then
87         # curl has HTTPS CA trust-issues less often than wget, so lets try that first.
88         which curl > /dev/null 2> /dev/null
89         if [ $? -ne 0 ]; then
90           mkdir -p "$__DOTNET_PATH"
91           wget -q -O $__DOTNET_PATH/dotnet.tar $__CLIDownloadURL
92           echo "wget -q -O $__DOTNET_PATH/dotnet.tar $__CLIDownloadURL"
93         else
94           curl --retry 10 -sSL --create-dirs -o $__DOTNET_PATH/dotnet.tar $__CLIDownloadURL
95           echo "curl --retry 10 -sSL --create-dirs -o $__DOTNET_PATH/dotnet.tar $__CLIDownloadURL"
96         fi
97         cd $__DOTNET_PATH
98         tar -xf $__DOTNET_PATH/dotnet.tar
99         if [ -n "$BUILDTOOLS_OVERRIDE_RUNTIME" ]; then
100             find $__DOTNET_PATH -name *.ni.* | xargs rm 2>/dev/null
101             cp -R $BUILDTOOLS_OVERRIDE_RUNTIME/* $__DOTNET_PATH/bin
102             cp -R $BUILDTOOLS_OVERRIDE_RUNTIME/* $__DOTNET_PATH/bin/dnx
103             cp -R $BUILDTOOLS_OVERRIDE_RUNTIME/* $__DOTNET_PATH/runtime/coreclr
104         fi
105
106         cd $__scriptpath
107     fi
108
109     mkdir "$__PROJECT_JSON_PATH"
110     echo $__PROJECT_JSON_CONTENTS > "$__PROJECT_JSON_FILE"
111
112     if [ ! -e $__BUILD_TOOLS_PATH ]; then
113         $__DOTNET_CMD restore "$__PROJECT_JSON_FILE" --packages $__PACKAGES_DIR --source $__BUILDTOOLS_SOURCE
114     fi
115
116     # On ubuntu 14.04, /bin/sh (symbolic link) calls /bin/dash by default.
117     $__BUILD_TOOLS_PATH/init-tools.sh $__scriptpath $__DOTNET_CMD $__TOOLRUNTIME_DIR
118
119 else
120     echo "$__PROJECT_JSON_FILE found. Skipping .NET CLI installation."   
121 fi