Support arm64 jiri binary

Change upload and bootstrap scripts to support it

Change-Id: I4819f22dcdaf3dd88855b7b8f45f8d2b690d41a7
diff --git a/BUILD.md b/BUILD.md
index 3fc6b42..539d6e3 100644
--- a/BUILD.md
+++ b/BUILD.md
@@ -9,7 +9,7 @@
 ## Get source
 
 ### Using jiri prebuilt
-This method only works with linux and darwin `x86_64` systems.
+This method only works with linux (x86\_64 and aarch64 ) and darwin (x86\_64) systems.
 The bootstrap procedure requires that you have Go 1.6 or newer and Git installed and on your `PATH`. Below command will create checkout in new folder called `fuchsia`.
 ```
 curl -s https://raw.githubusercontent.com/fuchsia-mirror/jiri/master/scripts/bootstrap_jiri | bash -s fuchsia
diff --git a/scripts/bootstrap_jiri b/scripts/bootstrap_jiri
index 90a7523..a7e16ec 100755
--- a/scripts/bootstrap_jiri
+++ b/scripts/bootstrap_jiri
@@ -39,6 +39,16 @@
   exit 1
 }
 
+readonly HOST_ARCH=$(uname -m)
+if [ "$HOST_ARCH" == "aarch64" ]; then
+  readonly ARCH="arm64"
+elif [ "$HOST_ARCH" == "x86_64" ]; then
+  readonly ARCH="amd64"
+else
+  echo "Arch not supported: $HOST_ARCH"
+  exit 1
+fi
+
 # toabs converts the possibly relative argument into an absolute path.  Run in a
 # subshell to avoid changing the caller's working directory.
 toabs() (
@@ -70,7 +80,7 @@
 
 # Determine and validate the version of jiri.
 readonly HOST_OS=$(uname | tr '[:upper:]' '[:lower:]')
-readonly TARGET="${HOST_OS}-amd64"
+readonly TARGET="${HOST_OS}-${ARCH}"
 readonly COMMIT_URL="${JIRI_REPO_URL}/+refs/heads/master?format=JSON"
 readonly LOG_URL="${JIRI_REPO_URL}/+log/refs/heads/master?format=JSON"
 readonly VERSION=$(curl -sSf "${COMMIT_URL}" | sed -n 's/.*"value": "\([0-9a-f]\{40\}\)"/\1/p')
@@ -95,6 +105,9 @@
   exit 1
 fi
 chmod 755 "${BIN_DIR}/jiri"
+if [ "$ARCH" == "arm64" ]; then
+  echo "WARNING: Jiri doesn't support timely updates for arch '$HOST_ARCH'. This or future binaries of Jiri might be out of date."
+fi
 echo "Please add ${BIN_DIR} to your PATH"
 
 trap - EXIT
diff --git a/scripts/upload_cipd_package b/scripts/upload_cipd_package
index 983e773..c974429 100755
--- a/scripts/upload_cipd_package
+++ b/scripts/upload_cipd_package
@@ -29,18 +29,37 @@
 readonly TEMP_DIR="$(mktemp -d)"
 trap "rm -rf -- "${TEMP_DIR}"" EXIT
 
+failed=false
+RED='\033[0;31m'
+NC='\033[0m'
+readonly ARCHS="amd64 arm64"
 for target in ${TARGETS}; do
-  os=$(echo ${target} | sed 's/darwin/mac/')
-  mkdir -p "${TEMP_DIR}/jiri-${os}"
+  for arch in ${ARCHS}; do
+    if [[ "$target" == "darwin" && "$arch" == "arm64" ]]; then
+      continue
+    fi
+    os=$(echo ${target} | sed 's/darwin/mac/')
+    tmpdir="${TEMP_DIR}/jiri-${os}-${arch}"
+    mkdir -p ${tmpdir}
 
-  # Download the jiri binary.
-  if ! curl -sf -o "${TEMP_DIR}/jiri-${os}/jiri" "${GS_BUCKET_URL}/${target}-amd64/${VERSION}"; then
-    echo "Failed downloading prebuilt jiri binary." 1>&2
-    echo "${GS_BUCKET_URL}/${os}/${VERSION}"
-    exit 1
-  fi
-  chmod 755 "${TEMP_DIR}/jiri-${os}/jiri"
+    # Download the jiri binary.
+    if ! curl -sSf -o "${tmpdir}/jiri" "${GS_BUCKET_URL}/${target}-${arch}/${VERSION}"; then
+      echo -e "${RED}Failed downloading prebuilt jiri binary.${NC}" 1>&2
+      echo -e "${RED}${GS_BUCKET_URL}/${target}-${arch}/${VERSION}${NC}"
+      if [[ "$arch" != "arm64" ]]; then
+        failed=true
+      fi
+      continue
+    fi
+    chmod 755 "${tmpdir}/jiri"
 
-  # Upload the jiri binary to CIPD
-  cipd create -name "fuchsia/tools/jiri/${os}-amd64" -in "${TEMP_DIR}/jiri-${os}" -ref latest -tag "git_repository:https://fuchsia.googlesource.com/jiri" -tag "git_revision:${VERSION}"
+    # Upload the jiri binary to CIPD
+    cipd create -name "fuchsia/tools/jiri/${os}-${arch}" -in "${tmpdir}" -ref latest -tag "git_repository:https://fuchsia.googlesource.com/jiri" -tag "git_revision:${VERSION}"
+  done
 done
+
+# fail if any os-arch failed
+if [[ "$failed" = true ]]; then
+  echo -e "${RED}Failure${NC}"
+  exit 1
+fi
diff --git a/scripts/upload_jiri b/scripts/upload_jiri
index 0ad1f99..0707c2d 100755
--- a/scripts/upload_jiri
+++ b/scripts/upload_jiri
@@ -18,6 +18,14 @@
 readonly GIT_DIR="$(dirname "${SCRIPT_DIR}")"
 
 readonly HOST_ARCH=$(uname -m)
+if [ "$HOST_ARCH" == "aarch64" ]; then
+  readonly ARCH="arm64"
+elif [ "$HOST_ARCH" == "x86_64" ]; then
+  readonly ARCH="amd64"
+else
+  echo "Arch not supported: $HOST_ARCH"
+  exit 1
+fi
 readonly HOST_OS="$(uname | tr '[:upper:]' '[:lower:]')"
 
 readonly PKG_PATH="fuchsia.googlesource.com/jiri"
@@ -27,4 +35,4 @@
 readonly VERSION="$(git --git-dir="${GIT_DIR}/.git" --work-tree="${GIT_DIR}" log -1 --format=%H)"
 
 # Upload the binary
-gsutil cp "jiri" "${GS_BUCKET}/${HOST_OS}-amd64/${VERSION}"
+gsutil cp "jiri" "${GS_BUCKET}/${HOST_OS}-${ARCH}/${VERSION}"