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