blob: 6b28ecc99a34c6dfded0ed8cc23b38efad3f88d6 [file] [log] [blame]
Timothy Trippelfa70bcb2022-04-06 15:13:41 -07001#!/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
6set -euo pipefail
7
8: "${REPO_TOP:=$(git rev-parse --show-toplevel)}"
9
10: "${BAZELISK:=${REPO_TOP}/bazelisk.sh}"
Dan McArdle0306ab22022-09-23 11:50:38 -040011: "${BAZEL_VERSION:=$(cat "${REPO_TOP}/.bazelversion")}"
Timothy Trippelfa70bcb2022-04-06 15:13:41 -070012
13: "${BAZEL_AIRGAPPED_DIR:=bazel-airgapped}"
14: "${BAZEL_DISTDIR:=bazel-distdir}"
15: "${BAZEL_CACHEDIR:=bazel-cache}"
Timothy Trippel2abd7862022-09-14 19:13:19 -070016: "${BAZEL_BITSTREAMS_CACHE:=bitstreams-cache}"
17: "${BAZEL_BITSTREAMS_CACHEDIR:=${BAZEL_BITSTREAMS_CACHE}/cache}"
Timothy Trippelb54abfa2022-04-12 18:04:10 -070018: "${BAZEL_PYTHON_WHEEL_REPO:=ot_python_wheels}"
Timothy Trippelabfcfb02022-05-04 14:29:21 -070019: "${BAZEL_BITSTREAMS_REPO:=bitstreams}"
Timothy Trippelfa70bcb2022-04-06 15:13:41 -070020
21LINE_SEP="====================================================================="
22
23################################################################################
24# Process cmd line args.
25################################################################################
26function usage() {
27 cat << USAGE
28Utility script to prepare a directory with all bazel dependencies needed to
29build project artifacts with bazel in an airgapped environment.
30
Timothy Trippel43e59a92022-05-06 13:40:32 -070031Usage: $0 [-c ALL | DISTDIR | CACHE]
Timothy Trippelfa70bcb2022-04-06 15:13:41 -070032
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
36Airgapped 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
41USAGE
42}
43
44AIRGAPPED_DIR_CONTENTS="ALL"
45FORCE_REBUILD=false
46
47while 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
63done
64shift $((OPTIND - 1))
65
66# We do not accept additional arguments.
67if [[ "$#" -gt 0 ]]; then
68 echo "Unexpected arguments:" "$@" >&2
69 exit 1
70fi
71
72if [[ ${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
78fi
79
80
81################################################################################
82# Check if a previous airgapped directory has been built.
83################################################################################
84if [[ -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
98fi
99
100################################################################################
101# Setup the airgapped directory.
102################################################################################
103mkdir -p ${BAZEL_AIRGAPPED_DIR}
104
105################################################################################
106# Prepare the distdir.
107################################################################################
108if [[ ${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 McArdle0306ab22022-09-23 11:50:38 -0400118 git clone -b "${BAZEL_VERSION}" --depth 1 https://github.com/bazelbuild/bazel bazel-repo
Timothy Trippelfa70bcb2022-04-06 15:13:41 -0700119 cd bazel-repo
Dan McArdle0306ab22022-09-23 11:50:38 -0400120 echo "Cloned bazel repo @ \"${BAZEL_VERSION}\" (commit $(git rev-parse HEAD))"
Timothy Trippelfa70bcb2022-04-06 15:13:41 -0700121 ../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."
128fi
129
130################################################################################
131# Prepare the cache.
132################################################################################
133if [[ ${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 Trippeld120ffa2022-10-12 11:24:53 -0700144 @bindgen_clang_linux//... \
145 @rules_rust_bindgen__bindgen-0.60.1//... \
Timothy Trippelb99dd9c2022-09-26 20:14:00 +0000146 @go_sdk//... \
Timothy Trippel41b46fe2022-08-17 23:01:25 -0700147 @lowrisc_rv32imcb_files//... \
Timothy Trippelfa70bcb2022-04-06 15:13:41 -0700148 @local_config_cc_toolchains//... \
149 @local_config_platform//... \
150 @local_config_sh//... \
Timothy Trippel98017492022-04-07 18:46:31 -0700151 @python3_toolchains//... \
Timothy Trippel43e59a92022-05-06 13:40:32 -0700152 @remotejdk11_linux//... \
Timothy Trippelfa70bcb2022-04-06 15:13:41 -0700153 @riscv-compliance//... \
Alexander Williams4dd6d2d2022-12-22 12:41:39 -0800154 @rules_foreign_cc//toolchains/... \
Chris Frantz95594022023-02-13 17:59:54 +0000155 @rust_analyzer_1.67.0_tools//... \
156 @rust_linux_x86_64__x86_64-unknown-linux-gnu__nightly_tools//...
Miles Dai762b9ae2022-07-07 15:29:11 -0400157 cp -R "$(${BAZELISK} info output_base)"/external/${BAZEL_PYTHON_WHEEL_REPO} \
Timothy Trippelb54abfa2022-04-12 18:04:10 -0700158 ${BAZEL_AIRGAPPED_DIR}/
Timothy Trippel2abd7862022-09-14 19:13:19 -0700159 # 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 McArdleefb483b2022-09-22 16:50:57 -0400165 # The revision named in latest.txt is not necessarily on disk. Induce the
166 # cache backend to fetch the latest bitstreams.
Alexander Williams34bc3202022-12-13 18:09:21 -0800167 BITSTREAM=latest ${BAZELISK} fetch @bitstreams//...
Timothy Trippel2abd7862022-09-14 19:13:19 -0700168 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 Trippelfa70bcb2022-04-06 15:13:41 -0700173 echo "Done."
174fi
175
176################################################################################
177# Print some usage instructions.
178################################################################################
179if [[ ${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 McArdle7ee42082022-09-23 12:39:17 -0400183 echo "bazel build --distdir=${BAZEL_AIRGAPPED_DIR}/${BAZEL_DISTDIR} --repository_cache=${BAZEL_AIRGAPPED_DIR}/${BAZEL_CACHEDIR} <label>"
Timothy Trippelfa70bcb2022-04-06 15:13:41 -0700184fi