blob: f5ad3311ad03234159ec8a4a2b0f0cb58bfd9ba6 [file] [log] [blame]
Robert Iannucci2188fe92016-12-02 11:15:57 -08001#!/bin/bash -e
2
3# Copyright (c) 2016 The Chromium Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
Dan Jacquese82c0de2017-07-05 18:10:23 -07007set -e -o pipefail
8
Brian White361822c2018-04-04 15:21:45 -04009CYGWIN=false
Robert Iannucci2188fe92016-12-02 11:15:57 -080010MYPATH=$(dirname "${BASH_SOURCE[0]}")
11
12: ${CIPD_CLIENT_VER:=`cat $MYPATH/cipd_client_version`}
13: ${CIPD_CLIENT_SRV:='https://chrome-infra-packages.appspot.com'}
14
15UNAME=`uname -s | tr '[:upper:]' '[:lower:]'`
16case $UNAME in
17 linux)
18 PLAT=linux
19 ;;
Brian White361822c2018-04-04 15:21:45 -040020 cygwin*)
21 PLAT=windows
22 CYGWIN=true
23 ;;
24 msys*|mingw*)
Robert Iannucci2188fe92016-12-02 11:15:57 -080025 PLAT=windows
26 ;;
27 darwin)
28 PLAT=mac
29 ;;
30 *)
Vasili Skurydzin179059f2018-06-19 20:00:41 +000031 echo "cipd not supported on $UNAME"
Robert Iannucci2188fe92016-12-02 11:15:57 -080032 exit 1
33esac
34
35UNAME=`uname -m | tr '[:upper:]' '[:lower:]'`
36case $UNAME in
37 x86_64|amd64)
38 ARCH=amd64
39 ;;
Robert Iannucciff2bf092017-09-11 16:27:26 -070040 s390x) # best-effort support for IBM s390x: crbug.com/764087
41 ARCH=s390x
42 ;;
Dan Jacques63421282017-10-16 15:05:59 -040043 ppc64) # best-effort support for 64-bit PowerPC: crbug.com/773857
44 ARCH=ppc64
45 ;;
Dan Jacques1e0b7a52017-10-17 11:19:18 -040046 ppc64le) # best-effort support for 64-bit PowerPC/LE: crbug.com/773857
Dan Jacques63421282017-10-16 15:05:59 -040047 ARCH=ppc64le
48 ;;
William Hesse6fd24842017-10-10 13:55:22 +020049 aarch64)
50 ARCH=arm64
51 ;;
Benjamin Pastenee346e412017-10-20 12:57:43 -070052 armv7l)
53 ARCH=armv6l
54 ;;
Robert Iannucci2188fe92016-12-02 11:15:57 -080055 arm*)
56 ARCH=$UNAME
57 ;;
58 *86)
59 ARCH=386
60 ;;
Wang Qing254538b2018-07-26 02:23:53 +000061 mips*)
Wang Qing82bb7562018-08-03 21:45:46 +000062 # detect mips64le vs mips64.
Wang Qing254538b2018-07-26 02:23:53 +000063 ARCH=$UNAME
Wang Qing82bb7562018-08-03 21:45:46 +000064 if lscpu | grep -q "Little Endian"; then
65 ARCH+=le
66 fi
Wang Qing254538b2018-07-26 02:23:53 +000067 ;;
Robert Iannucci2188fe92016-12-02 11:15:57 -080068 *)
69 echo "UNKNOWN Machine architecture: $UNAME"
70 exit 1
71esac
72
73URL="$CIPD_CLIENT_SRV/client?platform=${PLAT}-${ARCH}&version=$CIPD_CLIENT_VER"
74CLIENT="$MYPATH/.cipd_client"
75
76USER_AGENT="depot_tools/$(git -C $MYPATH rev-parse HEAD 2>/dev/null || echo "???")"
77
Vadim Shtayura7e50ee32018-07-26 17:43:51 +000078# clean_bootstrap bootstraps the client from scratch using 'curl' or 'wget'.
79function clean_bootstrap() {
Dan Jacqueseb1feb92017-07-28 13:04:28 +020080 echo "Bootstrapping cipd client for ${PLAT}-${ARCH} from ${URL}..."
Pawel Hajdan, Jr8c8a0a52017-09-06 19:40:18 +000081
82 # Download the client into a temporary file, then move it into the final
83 # location atomically.
84 #
85 # This wonky tempdir method works on Linux and Mac.
Vadim Shtayura7e50ee32018-07-26 17:43:51 +000086 local CIPD_CLIENT_TMP=$(\
Pawel Hajdan, Jr8c8a0a52017-09-06 19:40:18 +000087 mktemp -p "$MYPATH" 2>/dev/null || \
88 mktemp "$MYPATH/.cipd_client.XXXXXXX")
89
Robert Iannucci2188fe92016-12-02 11:15:57 -080090 if hash curl 2> /dev/null ; then
Dan Jacqueseb1feb92017-07-28 13:04:28 +020091 curl "$URL" -s --show-error -f -A "$USER_AGENT" -L -o "$CIPD_CLIENT_TMP"
Pawel Hajdan, Jr8c8a0a52017-09-06 19:40:18 +000092 elif hash wget 2> /dev/null ; then
93 wget "$URL" -q -U "${USER_AGENT}" -O "${CIPD_CLIENT_TMP}"
Robert Iannucci2188fe92016-12-02 11:15:57 -080094 else
Pawel Hajdan, Jr8c8a0a52017-09-06 19:40:18 +000095 echo Your platform is missing a supported fetch command. Please use your package
96 echo manager to install one before continuing:
97 echo
98 echo curl
99 echo wget
Robert Iannucci2188fe92016-12-02 11:15:57 -0800100 echo
101 echo Alternately, manually download:
102 echo "$URL"
103 echo To $CLIENT, and then re-run this command.
Pawel Hajdan, Jr8c8a0a52017-09-06 19:40:18 +0000104 rm "${CIPD_CLIENT_TMP}"
Robert Iannucci2188fe92016-12-02 11:15:57 -0800105 exit 1
106 fi
Pawel Hajdan, Jr8c8a0a52017-09-06 19:40:18 +0000107
108 chmod +x "$CIPD_CLIENT_TMP"
109
110 set +e
111 mv "$CIPD_CLIENT_TMP" "$CLIENT"
112 set -e
Vadim Shtayura7e50ee32018-07-26 17:43:51 +0000113}
114
115function self_update() {
116 "$CLIENT" selfupdate -version "$CIPD_CLIENT_VER" -service-url "$CIPD_CLIENT_SRV"
117}
118
119if [ ! -x "$CLIENT" ]; then
120 clean_bootstrap
Robert Iannucci2188fe92016-12-02 11:15:57 -0800121fi
122
123export CIPD_HTTP_USER_AGENT_PREFIX=$USER_AGENT
Vadim Shtayura7e50ee32018-07-26 17:43:51 +0000124if ! self_update ; then
125 echo -n "CIPD selfupdate failed. " 1>&2
126 echo "Trying to bootstrap the CIPD client from scratch... " 1>&2
127 clean_bootstrap
128 if ! self_update ; then # need to run it again to setup .cipd_version file
129 echo -n "Bootstrap from scratch failed, something is seriously broken " 1>&2
130 echo "run \`CIPD_HTTP_USER_AGENT_PREFIX=$USER_AGENT/manual $CLIENT selfupdate -version '$CIPD_CLIENT_VER'\` to diagnose if this is repeating." 1>&2
131 echo "" 1>&2
132 fi
Robert Iannucci2188fe92016-12-02 11:15:57 -0800133fi
134
Brian White361822c2018-04-04 15:21:45 -0400135# CygWin requires changing absolute paths to Windows form. Relative paths
136# are typically okay as Windows generally accepts both forward and back
137# slashes. This could possibly be constrained to only /tmp/ and /cygdrive/.
138if $CYGWIN; then
139 args=("$@")
140 for i in `seq 2 $#`; do
141 arg="${@:$i:1}"
142 if [ "${arg:0:1}" == "/" ]; then
143 last=$((i-1))
144 next=$((i+1))
145 set -- "${@:1:$last}" `cygpath -w "$arg"` "${@:$next}"
146 fi
147 done
148 echo "$CLIENT" "${@}"
149fi
150
Robert Iannucci2188fe92016-12-02 11:15:57 -0800151exec "$CLIENT" "${@}"