| #!/bin/bash |
| # |
| # Copyright 2022 Google LLC |
| # |
| # Licensed under the Apache License, Version 2.0 (the "License"); |
| # you may not use this file except in compliance with the License. |
| # You may obtain a copy of the License at |
| # |
| # https://www.apache.org/licenses/LICENSE-2.0 |
| # |
| # Unless required by applicable law or agreed to in writing, software |
| # distributed under the License is distributed on an "AS IS" BASIS, |
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| # See the License for the specific language governing permissions and |
| # limitations under the License. |
| |
| # Manage RISC-V GCC/LLVM toolchains w.r.t Shodan's GCS service. |
| |
| PUBLIC_ARTIFACTS_PATH="gs://shodan-public-artifacts" |
| |
| function die { |
| echo "$@" >/dev/stderr |
| exit 1 |
| } |
| |
| function try { |
| echo "$@" |
| "$@" || die "Failed to execute '$@': exited with return code $?" |
| } |
| |
| function show-usage { |
| cat >/dev/stderr <<EOF |
| Usage: manage-riscv-toolchain.sh [-c <toolchain>|-l|-u <tarball>|-p <tarball>] |
| |
| Create, uploads, and promotes RISC-V GGC/LLVM/CHERIOT toolchain tarballs from toolchain installs |
| on local disk in cache/. |
| |
| Options: |
| -l | --list List available RISC-V tarballs. |
| -c <toolchain> | --create <toolchain> |
| Create a new toolchain tarball and sha256sum from files in |
| cache/. |
| -u <tarball> | --upload <tarball> |
| Upload the existing toolchain tarball to Google storage. |
| Note: requires the Google Cloud SDK to be installed and |
| logged in to function. |
| -p <tarball> | --promote <tarball> |
| Promote the existing tarball in backup to public. |
| Note: requires the Google Cloud SDK to be installed and |
| logged in to function. |
| EOF |
| exit 1 |
| } |
| |
| function check-toolchain { |
| local toolchain="$1" |
| if [[ ${toolchain} == "toolchain" ]]; then |
| true |
| elif [[ ${toolchain} == "toolchain_iree_rv32imf" ]]; then |
| true |
| elif [[ ${toolchain} == "toolchain_kelvin" ]]; then |
| true |
| elif [[ ${toolchain} == "toolchain_cheriot" ]]; then |
| true |
| else |
| die "Unsupported toolchain ${toolchain}" |
| fi |
| } |
| |
| function get-promoted-tarball { |
| local tarball=$1 |
| if [[ ${tarball} == "toolchain_iree_rv32"* ]]; then |
| echo "toolchain_iree_rv32.tar.gz" |
| elif [[ ${tarball} == "toolchain_kelvin"* ]]; then |
| echo "toolchain_kelvin.tar.gz" |
| elif [[ ${tarball} == "toolchain_cheriot"* ]]; then |
| echo "toolchain_cheriot.tar.gz" |
| elif [[ ${tarball} == "toolchain"* ]]; then |
| echo "toolchain.tar.gz" |
| else |
| die "Malformed toolchain tarball ${tarball}" |
| fi |
| } |
| |
| function generate-tarball-name { |
| local toolchain=$1 |
| check-toolchain "${toolchain}" |
| |
| local datestamp=$(date +%Y-%m-%d) |
| echo "${toolchain}_${datestamp}.tar.gz" |
| } |
| |
| function create-tarball { |
| local toolchain=$1; shift |
| if [[ ! -d "${CACHE}/${toolchain}" ]]; then |
| die "No toolchain installed at ${CACHE}/${toolchain}. Please install it first, and try again." |
| fi |
| |
| local tarball="$(generate-tarball-name ${toolchain})" |
| |
| if [[ -f "${OUT}/${tarball}" ]]; then |
| die "Tarball ${tarball} already exists -- cowardly refusing to overwrite it." |
| fi |
| |
| mkdir -p "${OUT}" |
| |
| echo "Creating tarball in ${OUT}/${tarball}..." |
| tar -C "${ROOTDIR}/cache" -czf "${OUT}/${tarball}" "${toolchain}" |
| |
| if [[ "$?" != 0 ]]; then |
| die "Couldn't create tarball." |
| fi |
| |
| echo "Generating sha256sums..." |
| (cd "${OUT}" && sha256sum "${tarball}") > "${OUT}/${tarball}.sha256sum" |
| |
| if [[ "$?" != 0 ]]; then |
| die "Couldn't create sha256sum checksum file." |
| fi |
| |
| echo "Verifying sha256sum is valid..." |
| (cd "${OUT}" && sha256sum -c "${tarball}.sha256sum") |
| |
| if [[ "$?" != 0 ]]; then |
| die "Couldn't verify sha256sum!" |
| fi |
| } |
| |
| function list-tarballs { |
| echo "Available tarballs:" |
| echo |
| gsutil ls "${PUBLIC_ARTIFACTS_PATH}/toolchain_backups/toolchain*.tar.gz" |
| } |
| |
| function upload-tarball { |
| local tarball=$1; shift |
| |
| echo "Uploading tarball ${tarball}..." |
| try gsutil cp "${OUT}/${tarball}" \ |
| "${PUBLIC_ARTIFACTS_PATH}/toolchain_backups/${tarball}" |
| try gsutil cp "${OUT}/${tarball}.sha256sum" \ |
| "${PUBLIC_ARTIFACTS_PATH}/toolchain_backups/${tarball}.sha256sum" |
| } |
| |
| function promote-tarball { |
| local promote_tarball="$1" |
| local tarball_name="$(get-promoted-tarball ${promote_tarball})" |
| |
| echo "Removing old latest toolchain..." |
| try gsutil rm "${PUBLIC_ARTIFACTS_PATH}/${tarball_name}" |
| try gsutil rm "${PUBLIC_ARTIFACTS_PATH}/${tarball_name}.sha256sum" |
| |
| echo "Promoting tarball ${promote_tarball} to ${tarball_name}" |
| try gsutil cp \ |
| "${PUBLIC_ARTIFACTS_PATH}/toolchain_backups/${promote_tarball}" \ |
| "${PUBLIC_ARTIFACTS_PATH}/${tarball_name}" |
| try gsutil cp \ |
| "${PUBLIC_ARTIFACTS_PATH}/toolchain_backups/${promote_tarball}.sha256sum" \ |
| "${PUBLIC_ARTIFACTS_PATH}/${tarball_name}.sha256sum" |
| } |
| |
| function main { |
| local usage="Usage: manage-riscv-toolchain.sh [-l|-c <toolchain> |-u <date> |-p <tarball>]" |
| local args=$(getopt -o h,l,c:,u:,p: --long help,list,create:,upload:,promote: \ |
| -n manage-rust-toolchain.sh -- "$@") |
| eval set -- "$args" |
| |
| local mode="" |
| local toolchain="" |
| local upload_tarball="" |
| local promote_tarball="" |
| |
| while true; do |
| case "$1" in |
| -l|--list) |
| mode="list" |
| shift |
| ;; |
| |
| -c|--create) |
| mode="create-tarball" |
| toolchain="$2" |
| shift |
| shift |
| ;; |
| |
| -u|--upload) |
| mode="upload" |
| upload_tarball="$2" |
| shift |
| shift |
| ;; |
| |
| -p|--promote) |
| mode="promote" |
| promote_tarball="$2" |
| shift |
| shift |
| ;; |
| |
| -h|--help) |
| show-usage |
| ;; |
| |
| --) |
| shift |
| break |
| ;; |
| |
| *) |
| die "Unknown option '$1'; maybe try --help?" |
| ;; |
| esac |
| done |
| |
| case "${mode}" in |
| list) |
| list-tarballs |
| ;; |
| |
| create-tarball) |
| create-tarball "${toolchain}" |
| ;; |
| |
| upload) |
| upload-tarball "${upload_tarball}" |
| ;; |
| |
| promote) |
| promote-tarball "${promote_tarball}" |
| ;; |
| |
| *) |
| show-usage |
| ;; |
| esac |
| } |
| |
| if [[ "$EUID" == 0 ]]; then |
| die "This script must NOT be run as root." |
| fi |
| |
| if [[ -z "${ROOTDIR}" || -z "${RUSTDIR}" ]]; then |
| die "Source build/setup.sh first" |
| fi |
| |
| if ! hash gsutil 2>/dev/null; then |
| die "This script requires the Google SDK to be installed." |
| fi |
| |
| main "$@" |