blob: 7711189da1a6e1e33fe2c989d6dc6df7e8e35e65 [file] [log] [blame]
Leopold29400282023-01-04 17:12:46 +01001#!/usr/bin/env bash
2# Both bazelisk and bazel's native wrapper scripts will attempt to use the well-known executable
3# named "tools/bazel" to run Bazel. The path of the original executable is stored in BAZEL_REAL.
4set -euo pipefail
Leopold7fbf1042023-01-06 19:57:37 +01005shopt -s nullglob
Leopold29400282023-01-04 17:12:46 +01006
Tim Windelschmidt246f2fe2023-10-26 04:39:35 +02007DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
8
9# Jump into nix-shell if BAZEL_REAL is set to a /nix/store path and we aren't
10# inside our shell yet.
11if [[ "${BAZEL_REAL:-}" == /nix/store/* && -z "${MONOGON_NIXOS:-}" ]]; then
12 echo "BAZEL_REAL is pointing to a /nix/store path, overriding to nix-shell." >&2
13
14 # Jump to project root since bwrap hangs if we aren't there
15 cd "${DIR}/../"
16
17 export COMMAND="bazel $*"
18 exec nix-shell
19fi
20
Leopold7fbf1042023-01-06 19:57:37 +010021# Short circuit if we're rebuilding the sandbox via third_party/sandboxroot/regenerate.sh.
22if [[ -n ${MONOGON_SYSROOT_REBUILD:-} ]]; then
23 echo "Skipping Bazel wrapper" >&2
24 exec -a "$0" "${BAZEL_REAL}" "$@"
Leopold29400282023-01-04 17:12:46 +010025fi
26
Leopold7fbf1042023-01-06 19:57:37 +010027SANDBOX="${DIR}/../.bazeldnf/sandbox/default"
28BAZEL_ARGS="--noworkspace_rc --bazelrc ${DIR}/../.bazelrc.sandboxroot"
Leopold29400282023-01-04 17:12:46 +010029
Leopold7fbf1042023-01-06 19:57:37 +010030prechecks() {
31 # Complain if script invoked directly.
32 if [[ -z "${BAZEL_REAL:-}" ]]; then
33 echo "BAZEL_REAL is not set - do not run directly, instead, use bazelisk" >&2
34 exit 1
35 fi
36
37 # Recommend using Bazelisk instead of Bazel's "bazel.sh" wrapper.
Leopold Schabel9508b122023-07-14 17:54:17 +020038 # Skip if we're inside the Nix shell (which uses a customized Bazel build).
39 if [[ -z "${BAZELISK_SKIP_WRAPPER:-}" && -z "${MONOGON_NIXOS:-}" ]]; then
Leopold7fbf1042023-01-06 19:57:37 +010040 echo "############################################################" >&2
41 echo "# Please use Bazelisk to build the Monorepo. Using Bazel #" >&2
42 echo "# directly may work, but is not recommended or supported. #" >&2
43 echo "############################################################" >&2
44 fi
45
46 # Our local user needs write access to /dev/kvm. Warn if this is not the case.
47 if ! touch /dev/kvm; then
48 echo "###################################################################" >&2
49 echo "# Cannot write to /dev/kvm - please verify permissions. #" >&2
50 echo "# Most tests require KVM and will not work. Builds still work. #" >&2
51 echo "# On most systems, add your user to the kvm group and re-login. #" >&2
52 echo "###################################################################" >&2
53 fi
54}
55
56intellij_patch() {
57 # When IntelliJ's Bazel plugin uses //scripts/bin/bazel to either build targets
58 # or run syncs, it adds a --override_repository flag to the bazel command
59 # line that points @intellij_aspect into a path on the filesystem. This
60 # external repository contains a Bazel Aspect definition which Bazel
61 # executes to provide the IntelliJ Bazel plugin with information about the
62 # workspace / build targets / etc...
63 #
64 # We need to patch the aspect definition to fix a number of bugs
65 # to make it work with the Monogon monorepo.
66
67 # Find all IntelliJ installation/config directories.
68 local ij_home_paths=("${HOME}/.local/share/JetBrains/IntelliJIdea"*)
69 # Get the newest one, if any.
70 local ij_home=""
71 if ! [[ ${#ij_home_paths[@]} -eq 0 ]]; then
72 # Reverse sort paths by name, with the first being the newest IntelliJ
73 # installation.
74 IFS=$'\n'
75 local sorted=($(sort -r <<<"${ij_home_paths[*]}"))
76 unset IFS
77 ij_home="${sorted[0]}"
78 fi
79
80 # If we don't have or can't find ij_home, don't bother with attempting to patch anything.
81 if [[ -d "${ij_home}" ]]; then
82 # aspect_path is the path to the aspect external repository that IntelliJ will
83 # inject into bazel via --override_repository.
84 local aspect_path="${ij_home}/ijwb/aspect"
85 # Our copy of it.
86 local patched_path="${ij_home}/ijwb/aspect-monogon"
87 # Checksum of the patch that was used to create patched_path.
88 local checksum_file="${patched_path}/checksum"
89 # The patch
90 local patch_file="${DIR}/../intellij/patches/bazel_intellij_aspect_filter.patch"
91 # The checksum of the patch we're about to apply.
92 local checksum
93 checksum=$(sha256sum "$patch_file" | cut -d' ' -f1)
94
95 # If the patched aspect repository doesn't exist, or the checksum of the patch
96 # we're about to apply doesn't match the checksum of the patch that was used
97 # to create the patched aspect repository, apply the patch.
98
99 if ! [[ -d "${patched_path}" ]] || ! [[ "$(cat "${checksum_file}")" == "${checksum}" ]]; then
100 echo "IntelliJ found at ${ij_home}, patching aspect repository." >&2
101 # Copy the aspect repository to the patched path.
102 rm -rf "${patched_path}"
103 cp -r "${aspect_path}" "${patched_path}"
104 # Apply the patch.
105 patch -d "${patched_path}" -p1 < "${patch_file}"
106 # Write the checksum of the patch to the checksum file.
107 echo "${checksum}" > "${checksum_file}"
108 else
109 echo "IntelliJ found at ${ij_home}, aspect repository already patched." >&2
110 fi
111 fi
112}
113
114regenerate_sysroot() {
115 local checksum_file="${SANDBOX}/checksum"
116 local checksum_input=(
117 ${DIR}/../third_party/sandboxroot/{repositories.bzl,BUILD.bazel}
Leopold7fbf1042023-01-06 19:57:37 +0100118 ${DIR}/../.bazelrc.sandboxroot
119 ${DIR}/../tools/bazel
120 )
121 local checksum
122 checksum="$(sha256sum <(cat "${checksum_input[@]}") | cut -d' ' -f1)"
123
124 if [[ -f "${checksum_file}" ]] && [[ "$(cat "${checksum_file}")" == "${checksum}" ]]; then
125 # Sysroot is up to date.
126 return
127 fi
128
129 echo "Regenerating sysroot $SANDBOX ..." >&2
130 rm -rf "$SANDBOX"
131 "$BAZEL_REAL" ${BAZEL_ARGS} run //third_party/sandboxroot:sandboxroot
132
133 # Manually resolve alternatives (https://github.com/rmohr/bazeldnf/issues/28)
134 ln -r -s -f "${SANDBOX}/root/usr/bin/ld.bfd" "${SANDBOX}/root/usr/bin/ld"
135
136 # Write checksum of the sysroot to a file in order to detect changes.
137 echo "$checksum" > "${SANDBOX}/checksum"
138
139 # Write Bazel config
140 ROOT=$(realpath "$DIR/..")
141
142 # We need the host's resolv.conf for some E2E tests which require internet access.
143 cp /etc/resolv.conf "${ROOT}/.bazeldnf/sandbox/default/root/etc/resolv.conf"
144
145 cat > "${DIR}/../.bazelrc.sandbox" <<EOF
Leopoldd2668122023-02-01 15:15:59 +0100146# Autogenerated by tools/bazel. Manual changes can result in stale caches.
147# Modify the generator instead.
148
Leopold Schabel9508b122023-07-14 17:54:17 +0200149# Mount directories from the generated Fedora sandbox root.
Leopold7fbf1042023-01-06 19:57:37 +0100150build --sandbox_add_mount_pair=${ROOT}/.bazeldnf/sandbox/default/root/etc:/etc
151build --sandbox_add_mount_pair=${ROOT}/.bazeldnf/sandbox/default/root/usr:/usr
152build --sandbox_add_mount_pair=${ROOT}/.bazeldnf/sandbox/default/root/var:/var
153build --sandbox_add_mount_pair=${ROOT}/.bazeldnf/sandbox/default/root/run:/run
154build --sandbox_add_mount_pair=${ROOT}/.bazeldnf/sandbox/default/root/tmp:/tmp
155build --sandbox_add_mount_pair=${ROOT}/.bazeldnf/sandbox/default/root/lib64:/lib64
156build --sandbox_add_mount_pair=${ROOT}/.bazeldnf/sandbox/default/root/lib:/lib
157build --sandbox_add_mount_pair=${ROOT}/.bazeldnf/sandbox/default/root/bin:/bin
158build --sandbox_add_mount_pair=${ROOT}/.bazeldnf/sandbox/default/root/sbin:/sbin
Leopold Schabel9508b122023-07-14 17:54:17 +0200159
160# Needed for the Go SDK shipped by rules_go to resolve its own GOROOT via /proc/self/exe.
161build --sandbox_add_mount_pair=/proc
162
163# Needed for python's multiprocessing lock implementation
164# (_multiprocessing.SemLock for eg. mp.Queue), as used in EDK2's build system.
165build --sandbox_add_mount_pair=/dev/shm
166
167# Needed for qemu for tests.
168build --sandbox_add_mount_pair=/dev/kvm
169
170# Put a tmpfs on /tmp for better performance.
171build --sandbox_tmpfs_path=/tmp
Leopold7fbf1042023-01-06 19:57:37 +0100172EOF
173
174 echo "Done regenerating sysroot." >&2
175}
176
177prechecks
178intellij_patch
179regenerate_sysroot
180
181# Find the --override_repository=intellij_aspect=[path]
182# argument in $@ and replace the path with the patched version.
183# This is surprisingly tricky - bash special-cases "$@" to expand
184# as "$1" "$2" ... "$n" so that argv is preserved, so we need to
185# modify the real $@ array.
186
187for i in $(seq 1 $#); do
188 if [[ "${!i}" == "--override_repository=intellij_aspect="* ]]; then
189 new_arg="${!i/\/aspect/\/aspect-monogon}"
190 set -- "${@:1:$((i-1))}" "${new_arg}" "${@:$((i+1))}"
191 fi
192done
193
194# Bazel does not track the ambient environment, so we need to invalidate
195# the entire build via an --action_env whenever the sandbox digest changes.
196# This is strictly necessary to guarantee correctness.
197export MONOGON_SANDBOX_DIGEST="$(cat "${SANDBOX}/checksum")"
198
Leopold Schabel9508b122023-07-14 17:54:17 +0200199# Ignore the host TMPDIR - it might be something funny like /run/user/1000,
200# we want it to be /tmp inside the sandbox.
201export TMPDIR=/tmp
202
Leopold7fbf1042023-01-06 19:57:37 +0100203exec -a "$0" "${BAZEL_REAL}" "$@"