Merge pull request #7730 from dotnet-bot/from-tfs
[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 __BUILD_TOOLS_PACKAGE_VERSION=$(cat $__scriptpath/BuildToolsVersion.txt)
11 __DOTNET_TOOLS_VERSION=$(cat $__scriptpath/DotnetCLIVersion.txt)
12 __BUILD_TOOLS_PATH=$__PACKAGES_DIR/Microsoft.DotNet.BuildTools/$__BUILD_TOOLS_PACKAGE_VERSION/lib
13 __PROJECT_JSON_PATH=$__TOOLRUNTIME_DIR/$__BUILD_TOOLS_PACKAGE_VERSION
14 __PROJECT_JSON_FILE=$__PROJECT_JSON_PATH/project.json
15 __PROJECT_JSON_CONTENTS="{ \"dependencies\": { \"Microsoft.DotNet.BuildTools\": \"$__BUILD_TOOLS_PACKAGE_VERSION\" }, \"frameworks\": { \"netcoreapp1.0\": { } } }"
16 __INIT_TOOLS_DONE_MARKER=$__PROJECT_JSON_PATH/done
17
18 # Extended version of platform detection logic from dotnet/cli/scripts/obtain/dotnet-install.sh 16692fc
19 get_current_linux_name() {
20     # Detect Distro
21     if [ "$(cat /etc/*-release | grep -cim1 ubuntu)" -eq 1 ]; then
22         if [ "$(cat /etc/*-release | grep -cim1 16.04)" -eq 1 ]; then
23             echo "ubuntu.16.04"
24             return 0
25         fi
26         if [ "$(cat /etc/*-release | grep -cim1 16.10)" -eq 1 ]; then
27             echo "ubuntu.16.10"
28             return 0
29         fi
30
31         echo "ubuntu"
32         return 0
33     elif [ "$(cat /etc/*-release | grep -cim1 centos)" -eq 1 ]; then
34         echo "centos"
35         return 0
36     elif [ "$(cat /etc/*-release | grep -cim1 rhel)" -eq 1 ]; then
37         echo "rhel"
38         return 0
39     elif [ "$(cat /etc/*-release | grep -cim1 debian)" -eq 1 ]; then
40         echo "debian"
41         return 0
42     elif [ "$(cat /etc/*-release | grep -cim1 alpine)" -eq 1 ]; then
43         echo "alpine"
44         return 0
45     elif [ "$(cat /etc/*-release | grep -cim1 fedora)" -eq 1 ]; then
46         if [ "$(cat /etc/*-release | grep -cim1 23)" -eq 1 ]; then
47             echo "fedora.23"
48             return 0
49         fi
50         if [ "$(cat /etc/*-release | grep -cim1 24)" -eq 1 ]; then
51             echo "fedora.24"
52             return 0
53         fi
54     elif [ "$(cat /etc/*-release | grep -cim1 opensuse)" -eq 1 ]; then
55         if [ "$(cat /etc/*-release | grep -cim1 13.2)" -eq 1 ]; then
56             echo "opensuse.13.2"
57             return 0
58         fi
59         if [ "$(cat /etc/*-release | grep -cim1 42.1)" -eq 1 ]; then
60             echo "opensuse.42.1"
61             return 0
62         fi
63     fi
64
65     # Cannot determine Linux distribution, assuming Ubuntu 14.04.
66     echo "ubuntu"
67     return 0
68 }
69
70 if [ -z "$__DOTNET_PKG" ]; then
71 OSName=$(uname -s)
72     case $OSName in
73         Darwin)
74             OS=OSX
75             __DOTNET_PKG=dotnet-dev-osx-x64
76             ulimit -n 2048
77             ;;
78
79         Linux)
80             __DOTNET_PKG="dotnet-dev-$(get_current_linux_name)-x64"
81             OS=Linux
82             ;;
83
84         *)
85             echo "Unsupported OS '$OSName' detected. Downloading ubuntu-x64 tools."
86             OS=Linux
87             __DOTNET_PKG=dotnet-dev-ubuntu-x64
88             ;;
89   esac
90 fi
91
92 if [ ! -e $__INIT_TOOLS_DONE_MARKER ]; then
93     __PATCH_CLI_NUGET_FRAMEWORKS=0
94
95     if [ -e $__TOOLRUNTIME_DIR ]; then rm -rf -- $__TOOLRUNTIME_DIR; fi
96     echo "Running: $__scriptpath/init-tools.sh" > $__init_tools_log
97
98     if [ ! -e $__DOTNET_PATH ]; then
99
100         mkdir -p "$__DOTNET_PATH"
101
102         if [ -n "$DOTNET_TOOLSET_DIR" ] && [ -d "$DOTNET_TOOLSET_DIR/$__DOTNET_TOOLS_VERSION" ]; then
103             echo "Copying $DOTNET_TOOLSET_DIR/$__DOTNET_TOOLS_VERSION to $__DOTNET_PATH" >> $__init_tools_log
104             cp -r $DOTNET_TOOLSET_DIR/$__DOTNET_TOOLS_VERSION/* $__DOTNET_PATH
105         elif [ -n "$DOTNET_TOOL_DIR" ] && [ -d "$DOTNET_TOOL_DIR" ]; then
106             echo "Copying $DOTNET_TOOL_DIR to $__DOTNET_PATH" >> $__init_tools_log
107             cp -r $DOTNET_TOOL_DIR/* $__DOTNET_PATH
108         else
109             echo "Installing dotnet cli..."
110             __DOTNET_LOCATION="https://dotnetcli.blob.core.windows.net/dotnet/Sdk/${__DOTNET_TOOLS_VERSION}/${__DOTNET_PKG}.${__DOTNET_TOOLS_VERSION}.tar.gz"
111             # curl has HTTPS CA trust-issues less often than wget, so lets try that first.
112             echo "Installing '${__DOTNET_LOCATION}' to '$__DOTNET_PATH/dotnet.tar'" >> $__init_tools_log
113             which curl > /dev/null 2> /dev/null
114             if [ $? -ne 0 ]; then
115                 wget -q -O $__DOTNET_PATH/dotnet.tar ${__DOTNET_LOCATION}
116             else
117                 curl --retry 10 -sSL --create-dirs -o $__DOTNET_PATH/dotnet.tar ${__DOTNET_LOCATION}
118             fi
119             cd $__DOTNET_PATH
120             tar -xf $__DOTNET_PATH/dotnet.tar
121
122             cd $__scriptpath
123
124             __PATCH_CLI_NUGET_FRAMEWORKS=1
125         fi
126     fi
127
128
129     if [ -n "$BUILD_TOOLS_TOOLSET_DIR" ] && [ -d "$BUILD_TOOLS_TOOLSET_DIR/$__BUILD_TOOLS_PACKAGE_VERSION" ]; then
130         echo "Copying $BUILD_TOOLS_TOOLSET_DIR/$__BUILD_TOOLS_PACKAGE_VERSION to $__TOOLRUNTIME_DIR" >> $__init_tools_log
131         cp -r $BUILD_TOOLS_TOOLSET_DIR/$__BUILD_TOOLS_PACKAGE_VERSION/* $__TOOLRUNTIME_DIR
132     elif [ -n "$BUILD_TOOLS_TOOL_DIR" ] && [ -d "$BUILD_TOOLS_TOOL_DIR" ]; then
133         echo "Copying $BUILD_TOOLS_TOOL_DIR to $__TOOLRUNTIME_DIR" >> $__init_tools_log
134         cp -r $BUILD_TOOLS_TOOL_DIR/* $__TOOLRUNTIME_DIR
135     else
136         if [ ! -d "$__PROJECT_JSON_PATH" ]; then mkdir "$__PROJECT_JSON_PATH"; fi
137         echo $__PROJECT_JSON_CONTENTS > "$__PROJECT_JSON_FILE"
138
139         if [ ! -e $__BUILD_TOOLS_PATH ]; then
140             echo "Restoring BuildTools version $__BUILD_TOOLS_PACKAGE_VERSION..."
141             echo "Running: $__DOTNET_CMD restore \"$__PROJECT_JSON_FILE\" --no-cache --packages $__PACKAGES_DIR --source $__BUILDTOOLS_SOURCE" >> $__init_tools_log
142             $__DOTNET_CMD restore "$__PROJECT_JSON_FILE" --no-cache --packages $__PACKAGES_DIR --source $__BUILDTOOLS_SOURCE >> $__init_tools_log
143             if [ ! -e "$__BUILD_TOOLS_PATH/init-tools.sh" ]; then echo "ERROR: Could not restore build tools correctly. See '$__init_tools_log' for more details."1>&2; fi
144         fi
145
146         echo "Initializing BuildTools..."
147         echo "Running: $__BUILD_TOOLS_PATH/init-tools.sh $__scriptpath $__DOTNET_CMD $__TOOLRUNTIME_DIR" >> $__init_tools_log
148         $__BUILD_TOOLS_PATH/init-tools.sh $__scriptpath $__DOTNET_CMD $__TOOLRUNTIME_DIR >> $__init_tools_log
149         if [ "$?" != "0" ]; then
150             echo "ERROR: An error occured when trying to initialize the tools. Please check '$__init_tools_log' for more details."1>&2
151             exit 1
152         fi
153     fi
154
155     if [ $__PATCH_CLI_NUGET_FRAMEWORKS -eq 1 ]; then
156         echo "Updating CLI NuGet Frameworks map..."
157         cp $__TOOLRUNTIME_DIR/NuGet.Frameworks.dll $__TOOLRUNTIME_DIR/dotnetcli/sdk/$__DOTNET_TOOLS_VERSION >> $__init_tools_log
158         if [ "$?" != "0" ]; then
159             echo "ERROR: An error occured when updating Nuget for CLI . Please check '$__init_tools_log' for more details."1>&2
160             exit 1
161         fi
162     fi
163
164     touch $__INIT_TOOLS_DONE_MARKER
165
166     echo "Done initializing tools."
167 else
168     echo "Tools are already initialized"
169 fi