1#!/usr/bin/env bash
2
3__scriptpath=$(cd "$(dirname "$0")"; pwd -P)
4
5if [ "$BUILDVARS_DONE" != 1 ]; then
6    . $__scriptpath/buildscripts/hostvars-setup.sh
7fi
8
9__init_tools_log=$__scriptpath/init-tools.log
10__PACKAGES_DIR=$__scriptpath/packages
11__TOOLRUNTIME_DIR=$__scriptpath/Tools
12__DOTNET_PATH=$__TOOLRUNTIME_DIR/dotnetcli
13__DOTNET_CMD=$__DOTNET_PATH/dotnet
14if [ -z "$__BUILDTOOLS_SOURCE" ]; then __BUILDTOOLS_SOURCE=https://dotnet.myget.org/F/dotnet-buildtools/api/v3/index.json; fi
15export __BUILDTOOLS_USE_CSPROJ=true
16__BUILD_TOOLS_PACKAGE_VERSION=$(cat $__scriptpath/BuildToolsVersion.txt)
17__DOTNET_TOOLS_VERSION=$(cat $__scriptpath/DotnetCLIVersion.txt)
18__BUILD_TOOLS_PATH=$__PACKAGES_DIR/microsoft.dotnet.buildtools/$__BUILD_TOOLS_PACKAGE_VERSION/lib
19__INIT_TOOLS_RESTORE_PROJECT=$__scriptpath/init-tools.msbuild
20__INIT_TOOLS_DONE_MARKER_DIR=$__TOOLRUNTIME_DIR/$__BUILD_TOOLS_PACKAGE_VERSION
21__INIT_TOOLS_DONE_MARKER=$__INIT_TOOLS_DONE_MARKER_DIR/done
22
23if [ -z "$__DOTNET_PKG" ]; then
24    OSName=$(uname -s)
25    case $OSName in
26        Darwin)
27            OS=OSX
28            __PKG_RID=osx
29            ulimit -n 2048
30            # Format x.y.z as single integer with three digits for each part
31            VERSION=`sw_vers -productVersion| sed -e 's/\./ /g' | xargs printf "%03d%03d%03d"`
32            if [ "$VERSION" -lt 010012000 ]; then
33                echo error: macOS version `sw_vers -productVersion` is too old. 10.12 is needed as minimum.
34                exit 1
35            fi
36            ;;
37
38        Linux)
39            OS=Linux
40            __PKG_RID=linux
41
42            if [ -e /etc/os-release ]; then
43                source /etc/os-release
44                if [[ $ID == "alpine" ]]; then
45                    # remove the last version digit
46                    VERSION_ID=${VERSION_ID%.*}
47                    __PKG_RID=alpine.$VERSION_ID
48                fi
49
50            elif [ -e /etc/redhat-release ]; then
51                redhatRelease=$(</etc/redhat-release)
52                if [[ $redhatRelease == "CentOS release 6."* || $redhatRelease == "Red Hat Enterprise Linux Server release 6."* ]]; then
53                    __PKG_RID=rhel.6
54                fi
55            fi
56
57            ;;
58
59        *)
60            echo "Unsupported OS '$OSName' detected. Downloading linux-$__HostArch tools."
61            OS=Linux
62            __PKG_RID=linux
63            ;;
64  esac
65  __DOTNET_PKG=dotnet-sdk-${__DOTNET_TOOLS_VERSION}-$__PKG_RID-$__HostArch
66fi
67
68display_error_message()
69{
70    echo "Please check the detailed log that follows." 1>&2
71    cat "$__init_tools_log" 1>&2
72}
73
74if [ ! -e $__INIT_TOOLS_DONE_MARKER ]; then
75    __PATCH_CLI_NUGET_FRAMEWORKS=0
76
77    if [ -e $__TOOLRUNTIME_DIR ]; then rm -rf -- $__TOOLRUNTIME_DIR; fi
78    echo "Running: $__scriptpath/init-tools.sh" > $__init_tools_log
79
80    if [ ! -e $__DOTNET_PATH ]; then
81
82        mkdir -p "$__DOTNET_PATH"
83
84        if [ -n "$DOTNET_TOOLSET_DIR" ] && [ -d "$DOTNET_TOOLSET_DIR/$__DOTNET_TOOLS_VERSION" ]; then
85            echo "Copying $DOTNET_TOOLSET_DIR/$__DOTNET_TOOLS_VERSION to $__DOTNET_PATH" >> $__init_tools_log
86            cp -r $DOTNET_TOOLSET_DIR/$__DOTNET_TOOLS_VERSION/* $__DOTNET_PATH
87        elif [ -n "$DOTNET_TOOL_DIR" ] && [ -d "$DOTNET_TOOL_DIR" ]; then
88            echo "Copying $DOTNET_TOOL_DIR to $__DOTNET_PATH" >> $__init_tools_log
89            cp -r $DOTNET_TOOL_DIR/* $__DOTNET_PATH
90        else
91            echo "Installing dotnet cli..."
92            __DOTNET_LOCATION="https://dotnetcli.azureedge.net/dotnet/Sdk/${__DOTNET_TOOLS_VERSION}/${__DOTNET_PKG}.tar.gz"
93            # curl has HTTPS CA trust-issues less often than wget, so lets try that first.
94            echo "Installing '${__DOTNET_LOCATION}' to '$__DOTNET_PATH/dotnet.tar'" >> $__init_tools_log
95            if command -v curl > /dev/null; then
96                curl --retry 10 -sSL --create-dirs -o $__DOTNET_PATH/dotnet.tar ${__DOTNET_LOCATION}
97            else
98                wget -q -O $__DOTNET_PATH/dotnet.tar ${__DOTNET_LOCATION}
99            fi
100            cd $__DOTNET_PATH
101            tar -xf $__DOTNET_PATH/dotnet.tar
102            if [ "$?" != "0" ]; then
103                echo "ERROR: Could not download dotnet cli." 1>&2
104                display_error_message
105                exit 1
106            fi
107
108            cd $__scriptpath
109
110            __PATCH_CLI_NUGET_FRAMEWORKS=1
111        fi
112    fi
113
114
115    if [ -n "$BUILD_TOOLS_TOOLSET_DIR" ] && [ -d "$BUILD_TOOLS_TOOLSET_DIR/$__BUILD_TOOLS_PACKAGE_VERSION" ]; then
116        echo "Copying $BUILD_TOOLS_TOOLSET_DIR/$__BUILD_TOOLS_PACKAGE_VERSION to $__TOOLRUNTIME_DIR" >> $__init_tools_log
117        cp -r $BUILD_TOOLS_TOOLSET_DIR/$__BUILD_TOOLS_PACKAGE_VERSION/* $__TOOLRUNTIME_DIR
118    elif [ -n "$BUILD_TOOLS_TOOL_DIR" ] && [ -d "$BUILD_TOOLS_TOOL_DIR" ]; then
119        echo "Copying $BUILD_TOOLS_TOOL_DIR to $__TOOLRUNTIME_DIR" >> $__init_tools_log
120        cp -r $BUILD_TOOLS_TOOL_DIR/* $__TOOLRUNTIME_DIR
121    else
122        if [ ! -e $__BUILD_TOOLS_PATH ]; then
123            echo "Restoring BuildTools version $__BUILD_TOOLS_PACKAGE_VERSION..."
124            echo "Running: $__DOTNET_CMD restore \"$__INIT_TOOLS_RESTORE_PROJECT\" --no-cache --packages $__PACKAGES_DIR --source $__BUILDTOOLS_SOURCE /p:BuildToolsPackageVersion=$__BUILD_TOOLS_PACKAGE_VERSION" >> $__init_tools_log
125            $__DOTNET_CMD restore "$__INIT_TOOLS_RESTORE_PROJECT" --no-cache --packages $__PACKAGES_DIR --source $__BUILDTOOLS_SOURCE /p:BuildToolsPackageVersion=$__BUILD_TOOLS_PACKAGE_VERSION >> $__init_tools_log
126            if [ ! -e "$__BUILD_TOOLS_PATH/init-tools.sh" ]; then
127                echo "ERROR: Could not restore build tools correctly." 1>&2
128                display_error_message
129            fi
130        fi
131
132        echo "Initializing BuildTools..."
133        echo "Running: $__BUILD_TOOLS_PATH/init-tools.sh $__scriptpath $__DOTNET_CMD $__TOOLRUNTIME_DIR" >> $__init_tools_log
134
135        # Executables restored with .NET Core 2.0 do not have executable permission flags. https://github.com/NuGet/Home/issues/4424
136        chmod +x $__BUILD_TOOLS_PATH/init-tools.sh
137        $__BUILD_TOOLS_PATH/init-tools.sh $__scriptpath $__DOTNET_CMD $__TOOLRUNTIME_DIR >> $__init_tools_log
138        if [ "$?" != "0" ]; then
139            echo "ERROR: An error occurred when trying to initialize the tools." 1>&2
140            display_error_message
141            exit 1
142        fi
143    fi
144
145    echo "Making all .sh files executable under Tools."
146    # Executables restored with .NET Core 2.0 do not have executable permission flags. https://github.com/NuGet/Home/issues/4424
147    ls $__scriptpath/Tools/*.sh | xargs chmod +x
148    ls $__scriptpath/Tools/scripts/docker/*.sh | xargs chmod +x
149
150    Tools/crossgen.sh $__scriptpath/Tools
151
152    # CoreRT does not use special copy of the shared runtime for testing
153    cp $__TOOLRUNTIME_DIR/csc.runtimeconfig.json $__TOOLRUNTIME_DIR/xunit.console.netcore.runtimeconfig.json
154
155    mkdir -p $__INIT_TOOLS_DONE_MARKER_DIR
156    touch $__INIT_TOOLS_DONE_MARKER
157
158    echo "Done initializing tools."
159else
160    echo "Tools are already initialized"
161fi
162