Merge pull request #7191 from erozenfeld/SSAStackOverflow
[platform/upstream/coreclr.git] / init-tools.sh
1 #!/usr/bin/env bash
2
3 __scriptpath=$(cd "$(dirname "$0")"; pwd -P)
4
5 # CI_SPECIFIC - On CI machines, $HOME may not be set. In such a case, create a subfolder and set the variable to set.
6 # This is needed by CLI to function.
7 if [ -z "$HOME" ]; then
8     if [ ! -d "$__scriptpath/temp_home" ]; then
9         mkdir temp_home
10     fi
11     export HOME=$__scriptpath/temp_home
12     echo "HOME not defined; setting it to $HOME"
13 fi
14
15 __PACKAGES_DIR=$__scriptpath/packages
16 __TOOLRUNTIME_DIR=$__scriptpath/Tools
17 __DOTNET_PATH=$__TOOLRUNTIME_DIR/dotnetcli
18 __DOTNET_CMD=$__DOTNET_PATH/dotnet
19 if [ -z "$__BUILDTOOLS_SOURCE" ]; then __BUILDTOOLS_SOURCE=https://dotnet.myget.org/F/dotnet-buildtools/api/v3/index.json; fi
20 __BUILD_TOOLS_PACKAGE_VERSION=$(cat $__scriptpath/BuildToolsVersion.txt)
21 __DOTNET_TOOLS_VERSION=$(cat $__scriptpath/DotnetCLIVersion.txt)
22 __BUILD_TOOLS_PATH=$__PACKAGES_DIR/Microsoft.DotNet.BuildTools/$__BUILD_TOOLS_PACKAGE_VERSION/lib
23 __PROJECT_JSON_PATH=$__TOOLRUNTIME_DIR/$__BUILD_TOOLS_PACKAGE_VERSION
24 __PROJECT_JSON_FILE=$__PROJECT_JSON_PATH/project.json
25 __PROJECT_JSON_CONTENTS="{ \"dependencies\": { \"Microsoft.DotNet.BuildTools\": \"$__BUILD_TOOLS_PACKAGE_VERSION\" }, \"frameworks\": { \"dnxcore50\": { } } }"
26
27 # Extended version of platform detection logic from dotnet/cli/scripts/obtain/dotnet-install.sh 16692fc
28 get_current_linux_name() {
29     # Detect Distro
30     if [ "$(cat /etc/*-release | grep -cim1 ubuntu)" -eq 1 ]; then
31         if [ "$(cat /etc/*-release | grep -cim1 16.04)" -eq 1 ]; then
32             echo "ubuntu.16.04"
33             return 0
34         fi
35         if [ "$(cat /etc/*-release | grep -cim1 16.10)" -eq 1 ]; then
36             echo "ubuntu.16.10"
37             return 0
38         fi
39
40         echo "ubuntu"
41         return 0
42     elif [ "$(cat /etc/*-release | grep -cim1 centos)" -eq 1 ]; then
43         echo "centos"
44         return 0
45     elif [ "$(cat /etc/*-release | grep -cim1 rhel)" -eq 1 ]; then
46         echo "rhel"
47         return 0
48     elif [ "$(cat /etc/*-release | grep -cim1 debian)" -eq 1 ]; then
49         echo "debian"
50         return 0
51     elif [ "$(cat /etc/*-release | grep -cim1 fedora)" -eq 1 ]; then
52         if [ "$(cat /etc/*-release | grep -cim1 23)" -eq 1 ]; then
53             echo "fedora.23"
54             return 0
55         fi
56         if [ "$(cat /etc/*-release | grep -cim1 24)" -eq 1 ]; then
57             echo "fedora.24"
58             return 0
59         fi
60     elif [ "$(cat /etc/*-release | grep -cim1 opensuse)" -eq 1 ]; then
61         if [ "$(cat /etc/*-release | grep -cim1 13.2)" -eq 1 ]; then
62             echo "opensuse.13.2"
63             return 0
64         fi
65         if [ "$(cat /etc/*-release | grep -cim1 42.1)" -eq 1 ]; then
66             echo "opensuse.42.1"
67             return 0
68         fi
69     fi
70
71     # Cannot determine Linux distribution, assuming Ubuntu 14.04.
72     echo "ubuntu"
73     return 0
74 }
75
76 if [ -z "$__DOTNET_PKG" ]; then
77 OSName=$(uname -s)
78     case $OSName in
79         Darwin)
80             OS=OSX
81             __DOTNET_PKG=dotnet-dev-osx-x64
82             ulimit -n 2048
83             ;;
84
85         Linux)
86             __DOTNET_PKG="dotnet-dev-$(get_current_linux_name)-x64"
87             OS=Linux
88             ;;
89
90         *)
91             echo "Unsupported OS '$OSName' detected. Downloading ubuntu-x64 tools."
92             OS=Linux
93             __DOTNET_PKG=dotnet-dev-ubuntu-x64
94             ;;
95   esac
96 fi
97
98 __CLIDownloadURL=https://dotnetcli.blob.core.windows.net/dotnet/Sdk/${__DOTNET_TOOLS_VERSION}/${__DOTNET_PKG}.${__DOTNET_TOOLS_VERSION}.tar.gz
99 echo ".NET CLI will be downloaded from $__CLIDownloadURL"
100 echo "Locating $__PROJECT_JSON_FILE to see if we already downloaded .NET CLI tools..." 
101
102 if [ ! -e $__PROJECT_JSON_FILE ]; then
103     echo "$__PROJECT_JSON_FILE not found. Proceeding to download .NET CLI tools. " 
104     if [ -e $__TOOLRUNTIME_DIR ]; then rm -rf -- $__TOOLRUNTIME_DIR; fi
105
106     if [ ! -e $__DOTNET_PATH ]; then
107         # curl has HTTPS CA trust-issues less often than wget, so lets try that first.
108         which curl > /dev/null 2> /dev/null
109         if [ $? -ne 0 ]; then
110           mkdir -p "$__DOTNET_PATH"
111           wget -q -O $__DOTNET_PATH/dotnet.tar $__CLIDownloadURL
112           echo "wget -q -O $__DOTNET_PATH/dotnet.tar $__CLIDownloadURL"
113         else
114           curl --retry 10 -sSL --create-dirs -o $__DOTNET_PATH/dotnet.tar $__CLIDownloadURL
115           echo "curl --retry 10 -sSL --create-dirs -o $__DOTNET_PATH/dotnet.tar $__CLIDownloadURL"
116         fi
117         cd $__DOTNET_PATH
118         tar -xf $__DOTNET_PATH/dotnet.tar
119         if [ -n "$BUILDTOOLS_OVERRIDE_RUNTIME" ]; then
120             find $__DOTNET_PATH -name *.ni.* | xargs rm 2>/dev/null
121             cp -R $BUILDTOOLS_OVERRIDE_RUNTIME/* $__DOTNET_PATH/bin
122             cp -R $BUILDTOOLS_OVERRIDE_RUNTIME/* $__DOTNET_PATH/bin/dnx
123             cp -R $BUILDTOOLS_OVERRIDE_RUNTIME/* $__DOTNET_PATH/runtime/coreclr
124         fi
125
126         cd $__scriptpath
127     fi
128
129     mkdir "$__PROJECT_JSON_PATH"
130     echo $__PROJECT_JSON_CONTENTS > "$__PROJECT_JSON_FILE"
131
132     if [ ! -e $__BUILD_TOOLS_PATH ]; then
133         $__DOTNET_CMD restore "$__PROJECT_JSON_FILE" --packages $__PACKAGES_DIR --source $__BUILDTOOLS_SOURCE
134     fi
135
136     # On ubuntu 14.04, /bin/sh (symbolic link) calls /bin/dash by default.
137     $__BUILD_TOOLS_PATH/init-tools.sh $__scriptpath $__DOTNET_CMD $__TOOLRUNTIME_DIR
138
139 else
140     echo "$__PROJECT_JSON_FILE found. Skipping .NET CLI installation."   
141 fi