blob: 583fc8a86e5867388b9c5587784817f05ddbb9f5 [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}"
11: "${BAZEL_VERSION:=$(cat ${REPO_TOP}/.bazelversion)}"
12
13: "${BAZEL_AIRGAPPED_DIR:=bazel-airgapped}"
14: "${BAZEL_DISTDIR:=bazel-distdir}"
15: "${BAZEL_CACHEDIR:=bazel-cache}"
Timothy Trippelabfcfb02022-05-04 14:29:21 -070016: "${BAZEL_BITSTREAMS_CACHEDIR:=bitstreams-cache}"
Timothy Trippelb54abfa2022-04-12 18:04:10 -070017: "${BAZEL_PYTHON_WHEEL_REPO:=ot_python_wheels}"
Timothy Trippelabfcfb02022-05-04 14:29:21 -070018: "${BAZEL_BITSTREAMS_REPO:=bitstreams}"
Timothy Trippelfa70bcb2022-04-06 15:13:41 -070019
20LINE_SEP="====================================================================="
21
22################################################################################
23# Process cmd line args.
24################################################################################
25function usage() {
26 cat << USAGE
27Utility script to prepare a directory with all bazel dependencies needed to
28build project artifacts with bazel in an airgapped environment.
29
Timothy Trippel43e59a92022-05-06 13:40:32 -070030Usage: $0 [-c ALL | DISTDIR | CACHE]
Timothy Trippelfa70bcb2022-04-06 15:13:41 -070031
32 - c: airgapped directory contents, set to either ALL or DISTDIR or CACHE.
33 - f: force rebuild of airgapped directory, overwriting any existing one.
34
35Airgapped directory contents (-b):
36 - ALL: both the distdir and cache will be added. (Default)
37 - DISTDIR: only the distdir, containing bazel and its dependencies will be added.
38 - CACHE: only the OpenTitan bazel workspace dependencies will be added.
39
40USAGE
41}
42
43AIRGAPPED_DIR_CONTENTS="ALL"
44FORCE_REBUILD=false
45
46while getopts ':c:f' flag; do
47 case "${flag}" in
48 c) AIRGAPPED_DIR_CONTENTS="${OPTARG}";;
49 f) FORCE_REBUILD=true;;
50 \?) echo "Unexpected option: -${OPTARG}" >&2
51 usage
52 exit 1
53 ;;
54 :) echo "Option -${OPTARG} requires an argument" >&2
55 usage
56 exit 1
57 ;;
58 *) echo "Internal Error: Unhandled option: -${flag}" >&2
59 exit 1
60 ;;
61 esac
62done
63shift $((OPTIND - 1))
64
65# We do not accept additional arguments.
66if [[ "$#" -gt 0 ]]; then
67 echo "Unexpected arguments:" "$@" >&2
68 exit 1
69fi
70
71if [[ ${AIRGAPPED_DIR_CONTENTS} != "ALL" && \
72 ${AIRGAPPED_DIR_CONTENTS} != "DISTDIR" && \
73 ${AIRGAPPED_DIR_CONTENTS} != "CACHE" ]]; then
74 echo "Invalid -c option: ${AIRGAPPED_DIR_CONTENTS}." >&2
75 echo "Expected ALL, DISTDIR, or CACHE." >&2
76 exit 1
77fi
78
79
80################################################################################
81# Check if a previous airgapped directory has been built.
82################################################################################
83if [[ -d ${BAZEL_AIRGAPPED_DIR} ]]; then
84 if [[ ${FORCE_REBUILD} = false ]]; then
85 while true; do
86 read -p "Airgapped directory exists, rebuild? [Y/n]" yn
87 case $yn in
88 "") rm -rf ${BAZEL_AIRGAPPED_DIR}; break;;
89 [Yy]*) rm -rf ${BAZEL_AIRGAPPED_DIR}; break;;
90 [Nn]*) exit;;
91 *) echo "Please enter [Yy] or [Nn]."
92 esac
93 done
94 else
95 rm -rf ${BAZEL_AIRGAPPED_DIR}
96 fi
97fi
98
99################################################################################
100# Setup the airgapped directory.
101################################################################################
102mkdir -p ${BAZEL_AIRGAPPED_DIR}
103
104################################################################################
105# Prepare the distdir.
106################################################################################
107if [[ ${AIRGAPPED_DIR_CONTENTS} == "ALL" || \
108 ${AIRGAPPED_DIR_CONTENTS} == "DISTDIR" ]]; then
109 echo $LINE_SEP
110 echo "Preparing bazel offline distdir ..."
111 mkdir -p ${BAZEL_AIRGAPPED_DIR}/${BAZEL_DISTDIR}
112 cd ${BAZEL_AIRGAPPED_DIR}
113 curl --silent --location \
114 https://github.com/bazelbuild/bazel/releases/download/${BAZEL_VERSION}/bazel-${BAZEL_VERSION}-linux-x86_64 \
115 --output bazel
116 chmod +x bazel
117 git clone https://github.com/bazelbuild/bazel bazel-repo
118 cd bazel-repo
119 git checkout "$(cat ${REPO_TOP}/.bazelversion)"
120 ../bazel build @additional_distfiles//:archives.tar
121 tar xvf bazel-bin/external/additional_distfiles/archives.tar \
122 -C "../${BAZEL_DISTDIR}" \
123 --strip-components=3
124 cd ..
125 rm -rf bazel-repo
126 echo "Done."
127fi
128
129################################################################################
130# Prepare the cache.
131################################################################################
132if [[ ${AIRGAPPED_DIR_CONTENTS} == "ALL" || \
133 ${AIRGAPPED_DIR_CONTENTS} == "CACHE" ]]; then
134 echo $LINE_SEP
135 echo "Preparing bazel offline cachedir ..."
136 cd ${REPO_TOP}
137 mkdir -p ${BAZEL_AIRGAPPED_DIR}/${BAZEL_CACHEDIR}
138 # Make bazel forget everything it knows, then download everything.
139 ${BAZELISK} clean --expunge
140 ${BAZELISK} fetch \
141 --repository_cache=${BAZEL_AIRGAPPED_DIR}/${BAZEL_CACHEDIR} \
142 //... \
143 @bazel_embedded_upstream_toolchain//... \
144 @local_config_cc_toolchains//... \
145 @local_config_platform//... \
146 @local_config_sh//... \
Timothy Trippel98017492022-04-07 18:46:31 -0700147 @python3_toolchains//... \
Timothy Trippel43e59a92022-05-06 13:40:32 -0700148 @remotejdk11_linux//... \
Timothy Trippelfa70bcb2022-04-06 15:13:41 -0700149 @riscv-compliance//... \
Timothy Trippel43e59a92022-05-06 13:40:32 -0700150 @rust_linux_x86_64//... \
Timothy Trippelfa70bcb2022-04-06 15:13:41 -0700151 @rust_darwin_aarch64_toolchains//... \
152 @rust_darwin_x86_64_toolchains//... \
153 @rust_freebsd_x86_64_toolchains//... \
154 @rust_linux_aarch64_toolchains//... \
155 @rust_linux_x86_64_toolchains//... \
156 @rust_windows_x86_64_toolchains//...
Timothy Trippelb54abfa2022-04-12 18:04:10 -0700157 cp -R $(${BAZELISK} info output_base)/external/${BAZEL_PYTHON_WHEEL_REPO} \
158 ${BAZEL_AIRGAPPED_DIR}/
Timothy Trippelabfcfb02022-05-04 14:29:21 -0700159 cp -R $(dirname $(readlink -f $(bazel info output_base)/external/${BAZEL_BITSTREAMS_REPO}/cache)) \
160 ${BAZEL_AIRGAPPED_DIR}/${BAZEL_BITSTREAMS_CACHEDIR}
Timothy Trippelfa70bcb2022-04-06 15:13:41 -0700161 echo "Done."
162fi
163
164################################################################################
165# Print some usage instructions.
166################################################################################
167if [[ ${AIRGAPPED_DIR_CONTENTS} == "ALL" ]]; then
168 echo $LINE_SEP
169 echo "To perform an airgapped build, ship the contents of ${BAZEL_AIRGAPPED_DIR} to your airgapped environment and then:"
170 echo ""
171 echo "bazel build --distdir=${BAZEL_AIRGAPPED_DIR}/${BAZEL_DISTDIR} --repository_cache=${BAZEL_AIRGAPPED_DIR}/${BAZEL_DISTDIR} <label>"
172fi