blob: 03e02c3f5efad357ee477900789141615532b24d [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*)
62 ARCH=$UNAME
63 ;;
Robert Iannucci2188fe92016-12-02 11:15:57 -080064 *)
65 echo "UNKNOWN Machine architecture: $UNAME"
66 exit 1
67esac
68
69URL="$CIPD_CLIENT_SRV/client?platform=${PLAT}-${ARCH}&version=$CIPD_CLIENT_VER"
70CLIENT="$MYPATH/.cipd_client"
71
72USER_AGENT="depot_tools/$(git -C $MYPATH rev-parse HEAD 2>/dev/null || echo "???")"
73
Vadim Shtayura7e50ee32018-07-26 17:43:51 +000074# clean_bootstrap bootstraps the client from scratch using 'curl' or 'wget'.
75function clean_bootstrap() {
Dan Jacqueseb1feb92017-07-28 13:04:28 +020076 echo "Bootstrapping cipd client for ${PLAT}-${ARCH} from ${URL}..."
Pawel Hajdan, Jr8c8a0a52017-09-06 19:40:18 +000077
78 # Download the client into a temporary file, then move it into the final
79 # location atomically.
80 #
81 # This wonky tempdir method works on Linux and Mac.
Vadim Shtayura7e50ee32018-07-26 17:43:51 +000082 local CIPD_CLIENT_TMP=$(\
Pawel Hajdan, Jr8c8a0a52017-09-06 19:40:18 +000083 mktemp -p "$MYPATH" 2>/dev/null || \
84 mktemp "$MYPATH/.cipd_client.XXXXXXX")
85
Robert Iannucci2188fe92016-12-02 11:15:57 -080086 if hash curl 2> /dev/null ; then
Dan Jacqueseb1feb92017-07-28 13:04:28 +020087 curl "$URL" -s --show-error -f -A "$USER_AGENT" -L -o "$CIPD_CLIENT_TMP"
Pawel Hajdan, Jr8c8a0a52017-09-06 19:40:18 +000088 elif hash wget 2> /dev/null ; then
89 wget "$URL" -q -U "${USER_AGENT}" -O "${CIPD_CLIENT_TMP}"
Robert Iannucci2188fe92016-12-02 11:15:57 -080090 else
Pawel Hajdan, Jr8c8a0a52017-09-06 19:40:18 +000091 echo Your platform is missing a supported fetch command. Please use your package
92 echo manager to install one before continuing:
93 echo
94 echo curl
95 echo wget
Robert Iannucci2188fe92016-12-02 11:15:57 -080096 echo
97 echo Alternately, manually download:
98 echo "$URL"
99 echo To $CLIENT, and then re-run this command.
Pawel Hajdan, Jr8c8a0a52017-09-06 19:40:18 +0000100 rm "${CIPD_CLIENT_TMP}"
Robert Iannucci2188fe92016-12-02 11:15:57 -0800101 exit 1
102 fi
Pawel Hajdan, Jr8c8a0a52017-09-06 19:40:18 +0000103
104 chmod +x "$CIPD_CLIENT_TMP"
105
106 set +e
107 mv "$CIPD_CLIENT_TMP" "$CLIENT"
108 set -e
Vadim Shtayura7e50ee32018-07-26 17:43:51 +0000109}
110
111function self_update() {
112 "$CLIENT" selfupdate -version "$CIPD_CLIENT_VER" -service-url "$CIPD_CLIENT_SRV"
113}
114
115if [ ! -x "$CLIENT" ]; then
116 clean_bootstrap
Robert Iannucci2188fe92016-12-02 11:15:57 -0800117fi
118
119export CIPD_HTTP_USER_AGENT_PREFIX=$USER_AGENT
Vadim Shtayura7e50ee32018-07-26 17:43:51 +0000120if ! self_update ; then
121 echo -n "CIPD selfupdate failed. " 1>&2
122 echo "Trying to bootstrap the CIPD client from scratch... " 1>&2
123 clean_bootstrap
124 if ! self_update ; then # need to run it again to setup .cipd_version file
125 echo -n "Bootstrap from scratch failed, something is seriously broken " 1>&2
126 echo "run \`CIPD_HTTP_USER_AGENT_PREFIX=$USER_AGENT/manual $CLIENT selfupdate -version '$CIPD_CLIENT_VER'\` to diagnose if this is repeating." 1>&2
127 echo "" 1>&2
128 fi
Robert Iannucci2188fe92016-12-02 11:15:57 -0800129fi
130
Brian White361822c2018-04-04 15:21:45 -0400131# CygWin requires changing absolute paths to Windows form. Relative paths
132# are typically okay as Windows generally accepts both forward and back
133# slashes. This could possibly be constrained to only /tmp/ and /cygdrive/.
134if $CYGWIN; then
135 args=("$@")
136 for i in `seq 2 $#`; do
137 arg="${@:$i:1}"
138 if [ "${arg:0:1}" == "/" ]; then
139 last=$((i-1))
140 next=$((i+1))
141 set -- "${@:1:$last}" `cygpath -w "$arg"` "${@:$next}"
142 fi
143 done
144 echo "$CLIENT" "${@}"
145fi
146
Robert Iannucci2188fe92016-12-02 11:15:57 -0800147exec "$CLIENT" "${@}"