Timothy Trippel | fa70bcb | 2022-04-06 15:13:41 -0700 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # Copyright lowRISC contributors. |
| 3 | # Licensed under the Apache License, Version 2.0, see LICENSE for details. |
| 4 | # SPDX-License-Identifier: Apache-2.0 |
| 5 | |
| 6 | set -euo pipefail |
| 7 | |
| 8 | : "${REPO_TOP:=$(git rev-parse --show-toplevel)}" |
| 9 | |
| 10 | : "${BAZELISK:=${REPO_TOP}/bazelisk.sh}" |
Dan McArdle | 0306ab2 | 2022-09-23 11:50:38 -0400 | [diff] [blame] | 11 | : "${BAZEL_VERSION:=$(cat "${REPO_TOP}/.bazelversion")}" |
Timothy Trippel | fa70bcb | 2022-04-06 15:13:41 -0700 | [diff] [blame] | 12 | |
| 13 | : "${BAZEL_AIRGAPPED_DIR:=bazel-airgapped}" |
| 14 | : "${BAZEL_DISTDIR:=bazel-distdir}" |
| 15 | : "${BAZEL_CACHEDIR:=bazel-cache}" |
Timothy Trippel | 2abd786 | 2022-09-14 19:13:19 -0700 | [diff] [blame] | 16 | : "${BAZEL_BITSTREAMS_CACHE:=bitstreams-cache}" |
| 17 | : "${BAZEL_BITSTREAMS_CACHEDIR:=${BAZEL_BITSTREAMS_CACHE}/cache}" |
Timothy Trippel | b54abfa | 2022-04-12 18:04:10 -0700 | [diff] [blame] | 18 | : "${BAZEL_PYTHON_WHEEL_REPO:=ot_python_wheels}" |
Timothy Trippel | abfcfb0 | 2022-05-04 14:29:21 -0700 | [diff] [blame] | 19 | : "${BAZEL_BITSTREAMS_REPO:=bitstreams}" |
Timothy Trippel | fa70bcb | 2022-04-06 15:13:41 -0700 | [diff] [blame] | 20 | |
| 21 | LINE_SEP="=====================================================================" |
| 22 | |
| 23 | ################################################################################ |
| 24 | # Process cmd line args. |
| 25 | ################################################################################ |
| 26 | function usage() { |
| 27 | cat << USAGE |
| 28 | Utility script to prepare a directory with all bazel dependencies needed to |
| 29 | build project artifacts with bazel in an airgapped environment. |
| 30 | |
Timothy Trippel | 43e59a9 | 2022-05-06 13:40:32 -0700 | [diff] [blame] | 31 | Usage: $0 [-c ALL | DISTDIR | CACHE] |
Timothy Trippel | fa70bcb | 2022-04-06 15:13:41 -0700 | [diff] [blame] | 32 | |
| 33 | - c: airgapped directory contents, set to either ALL or DISTDIR or CACHE. |
| 34 | - f: force rebuild of airgapped directory, overwriting any existing one. |
| 35 | |
| 36 | Airgapped directory contents (-b): |
| 37 | - ALL: both the distdir and cache will be added. (Default) |
| 38 | - DISTDIR: only the distdir, containing bazel and its dependencies will be added. |
| 39 | - CACHE: only the OpenTitan bazel workspace dependencies will be added. |
| 40 | |
| 41 | USAGE |
| 42 | } |
| 43 | |
| 44 | AIRGAPPED_DIR_CONTENTS="ALL" |
| 45 | FORCE_REBUILD=false |
| 46 | |
| 47 | while getopts ':c:f' flag; do |
| 48 | case "${flag}" in |
| 49 | c) AIRGAPPED_DIR_CONTENTS="${OPTARG}";; |
| 50 | f) FORCE_REBUILD=true;; |
| 51 | \?) echo "Unexpected option: -${OPTARG}" >&2 |
| 52 | usage |
| 53 | exit 1 |
| 54 | ;; |
| 55 | :) echo "Option -${OPTARG} requires an argument" >&2 |
| 56 | usage |
| 57 | exit 1 |
| 58 | ;; |
| 59 | *) echo "Internal Error: Unhandled option: -${flag}" >&2 |
| 60 | exit 1 |
| 61 | ;; |
| 62 | esac |
| 63 | done |
| 64 | shift $((OPTIND - 1)) |
| 65 | |
| 66 | # We do not accept additional arguments. |
| 67 | if [[ "$#" -gt 0 ]]; then |
| 68 | echo "Unexpected arguments:" "$@" >&2 |
| 69 | exit 1 |
| 70 | fi |
| 71 | |
| 72 | if [[ ${AIRGAPPED_DIR_CONTENTS} != "ALL" && \ |
| 73 | ${AIRGAPPED_DIR_CONTENTS} != "DISTDIR" && \ |
| 74 | ${AIRGAPPED_DIR_CONTENTS} != "CACHE" ]]; then |
| 75 | echo "Invalid -c option: ${AIRGAPPED_DIR_CONTENTS}." >&2 |
| 76 | echo "Expected ALL, DISTDIR, or CACHE." >&2 |
| 77 | exit 1 |
| 78 | fi |
| 79 | |
| 80 | |
| 81 | ################################################################################ |
| 82 | # Check if a previous airgapped directory has been built. |
| 83 | ################################################################################ |
| 84 | if [[ -d ${BAZEL_AIRGAPPED_DIR} ]]; then |
| 85 | if [[ ${FORCE_REBUILD} = false ]]; then |
| 86 | while true; do |
| 87 | read -p "Airgapped directory exists, rebuild? [Y/n]" yn |
| 88 | case $yn in |
| 89 | "") rm -rf ${BAZEL_AIRGAPPED_DIR}; break;; |
| 90 | [Yy]*) rm -rf ${BAZEL_AIRGAPPED_DIR}; break;; |
| 91 | [Nn]*) exit;; |
| 92 | *) echo "Please enter [Yy] or [Nn]." |
| 93 | esac |
| 94 | done |
| 95 | else |
| 96 | rm -rf ${BAZEL_AIRGAPPED_DIR} |
| 97 | fi |
| 98 | fi |
| 99 | |
| 100 | ################################################################################ |
| 101 | # Setup the airgapped directory. |
| 102 | ################################################################################ |
| 103 | mkdir -p ${BAZEL_AIRGAPPED_DIR} |
| 104 | |
| 105 | ################################################################################ |
| 106 | # Prepare the distdir. |
| 107 | ################################################################################ |
| 108 | if [[ ${AIRGAPPED_DIR_CONTENTS} == "ALL" || \ |
| 109 | ${AIRGAPPED_DIR_CONTENTS} == "DISTDIR" ]]; then |
| 110 | echo $LINE_SEP |
| 111 | echo "Preparing bazel offline distdir ..." |
| 112 | mkdir -p ${BAZEL_AIRGAPPED_DIR}/${BAZEL_DISTDIR} |
| 113 | cd ${BAZEL_AIRGAPPED_DIR} |
| 114 | curl --silent --location \ |
| 115 | https://github.com/bazelbuild/bazel/releases/download/${BAZEL_VERSION}/bazel-${BAZEL_VERSION}-linux-x86_64 \ |
| 116 | --output bazel |
| 117 | chmod +x bazel |
Dan McArdle | 0306ab2 | 2022-09-23 11:50:38 -0400 | [diff] [blame] | 118 | git clone -b "${BAZEL_VERSION}" --depth 1 https://github.com/bazelbuild/bazel bazel-repo |
Timothy Trippel | fa70bcb | 2022-04-06 15:13:41 -0700 | [diff] [blame] | 119 | cd bazel-repo |
Dan McArdle | 0306ab2 | 2022-09-23 11:50:38 -0400 | [diff] [blame] | 120 | echo "Cloned bazel repo @ \"${BAZEL_VERSION}\" (commit $(git rev-parse HEAD))" |
Timothy Trippel | fa70bcb | 2022-04-06 15:13:41 -0700 | [diff] [blame] | 121 | ../bazel build @additional_distfiles//:archives.tar |
| 122 | tar xvf bazel-bin/external/additional_distfiles/archives.tar \ |
| 123 | -C "../${BAZEL_DISTDIR}" \ |
| 124 | --strip-components=3 |
| 125 | cd .. |
| 126 | rm -rf bazel-repo |
| 127 | echo "Done." |
| 128 | fi |
| 129 | |
| 130 | ################################################################################ |
| 131 | # Prepare the cache. |
| 132 | ################################################################################ |
| 133 | if [[ ${AIRGAPPED_DIR_CONTENTS} == "ALL" || \ |
| 134 | ${AIRGAPPED_DIR_CONTENTS} == "CACHE" ]]; then |
| 135 | echo $LINE_SEP |
| 136 | echo "Preparing bazel offline cachedir ..." |
| 137 | cd ${REPO_TOP} |
| 138 | mkdir -p ${BAZEL_AIRGAPPED_DIR}/${BAZEL_CACHEDIR} |
| 139 | # Make bazel forget everything it knows, then download everything. |
| 140 | ${BAZELISK} clean --expunge |
| 141 | ${BAZELISK} fetch \ |
| 142 | --repository_cache=${BAZEL_AIRGAPPED_DIR}/${BAZEL_CACHEDIR} \ |
| 143 | //... \ |
Timothy Trippel | d120ffa | 2022-10-12 11:24:53 -0700 | [diff] [blame] | 144 | @bindgen_clang_linux//... \ |
| 145 | @rules_rust_bindgen__bindgen-0.60.1//... \ |
Timothy Trippel | b99dd9c | 2022-09-26 20:14:00 +0000 | [diff] [blame] | 146 | @go_sdk//... \ |
Timothy Trippel | 41b46fe | 2022-08-17 23:01:25 -0700 | [diff] [blame] | 147 | @lowrisc_rv32imcb_files//... \ |
Timothy Trippel | fa70bcb | 2022-04-06 15:13:41 -0700 | [diff] [blame] | 148 | @local_config_cc_toolchains//... \ |
| 149 | @local_config_platform//... \ |
| 150 | @local_config_sh//... \ |
Timothy Trippel | 9801749 | 2022-04-07 18:46:31 -0700 | [diff] [blame] | 151 | @python3_toolchains//... \ |
Timothy Trippel | 43e59a9 | 2022-05-06 13:40:32 -0700 | [diff] [blame] | 152 | @remotejdk11_linux//... \ |
Timothy Trippel | fa70bcb | 2022-04-06 15:13:41 -0700 | [diff] [blame] | 153 | @riscv-compliance//... \ |
Alexander Williams | 4dd6d2d | 2022-12-22 12:41:39 -0800 | [diff] [blame] | 154 | @rules_foreign_cc//toolchains/... \ |
Chris Frantz | 9559402 | 2023-02-13 17:59:54 +0000 | [diff] [blame] | 155 | @rust_analyzer_1.67.0_tools//... \ |
| 156 | @rust_linux_x86_64__x86_64-unknown-linux-gnu__nightly_tools//... |
Miles Dai | 762b9ae | 2022-07-07 15:29:11 -0400 | [diff] [blame] | 157 | cp -R "$(${BAZELISK} info output_base)"/external/${BAZEL_PYTHON_WHEEL_REPO} \ |
Timothy Trippel | b54abfa | 2022-04-12 18:04:10 -0700 | [diff] [blame] | 158 | ${BAZEL_AIRGAPPED_DIR}/ |
Timothy Trippel | 2abd786 | 2022-09-14 19:13:19 -0700 | [diff] [blame] | 159 | # We don't need all bitstreams in the cache, we just need the latest one so |
| 160 | # that the cache is "initialized" and "offline" mode will work correctly. |
| 161 | mkdir -p ${BAZEL_AIRGAPPED_DIR}/${BAZEL_BITSTREAMS_CACHEDIR} |
| 162 | readonly SYSTEM_BITSTREAM_CACHE="${HOME}/.cache/opentitan-bitstreams" |
| 163 | readonly SYSTEM_BITSTREAM_CACHEDIR="${SYSTEM_BITSTREAM_CACHE}/cache" |
| 164 | readonly LATEST_BISTREAM_HASH_FILE="${SYSTEM_BITSTREAM_CACHE}/latest.txt" |
Dan McArdle | efb483b | 2022-09-22 16:50:57 -0400 | [diff] [blame] | 165 | # The revision named in latest.txt is not necessarily on disk. Induce the |
| 166 | # cache backend to fetch the latest bitstreams. |
Alexander Williams | 34bc320 | 2022-12-13 18:09:21 -0800 | [diff] [blame] | 167 | BITSTREAM=latest ${BAZELISK} fetch @bitstreams//... |
Timothy Trippel | 2abd786 | 2022-09-14 19:13:19 -0700 | [diff] [blame] | 168 | cp "${LATEST_BISTREAM_HASH_FILE}" \ |
| 169 | "${BAZEL_AIRGAPPED_DIR}/${BAZEL_BITSTREAMS_CACHE}/" |
| 170 | LATEST_BISTREAM_HASH=$(cat "${LATEST_BISTREAM_HASH_FILE}") |
| 171 | cp -r "${SYSTEM_BITSTREAM_CACHEDIR}/${LATEST_BISTREAM_HASH}" \ |
| 172 | "${BAZEL_AIRGAPPED_DIR}/${BAZEL_BITSTREAMS_CACHEDIR}" |
Timothy Trippel | fa70bcb | 2022-04-06 15:13:41 -0700 | [diff] [blame] | 173 | echo "Done." |
| 174 | fi |
| 175 | |
| 176 | ################################################################################ |
| 177 | # Print some usage instructions. |
| 178 | ################################################################################ |
| 179 | if [[ ${AIRGAPPED_DIR_CONTENTS} == "ALL" ]]; then |
| 180 | echo $LINE_SEP |
| 181 | echo "To perform an airgapped build, ship the contents of ${BAZEL_AIRGAPPED_DIR} to your airgapped environment and then:" |
| 182 | echo "" |
Dan McArdle | 7ee4208 | 2022-09-23 12:39:17 -0400 | [diff] [blame] | 183 | echo "bazel build --distdir=${BAZEL_AIRGAPPED_DIR}/${BAZEL_DISTDIR} --repository_cache=${BAZEL_AIRGAPPED_DIR}/${BAZEL_CACHEDIR} <label>" |
Timothy Trippel | fa70bcb | 2022-04-06 15:13:41 -0700 | [diff] [blame] | 184 | fi |