blob: e7b2c2cad8d11604bb1231c518f64ee08f4c5c92 [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
Tim Windelschmidt5bfdb972023-11-25 06:01:28 +010012 echo "Detected Nix based bazel installation and we are not in a nix-shell, overriding to nix-shell." >&2
13 USE_NIX_SHELL=yes
14fi
Tim Windelschmidt246f2fe2023-10-26 04:39:35 +020015
Tim Windelschmidt5bfdb972023-11-25 06:01:28 +010016# If the wrapper is called directly we check if nix-shell is available
17# to automagically switch into the nix-shell. Otherwise complain and
18# exit.
19if [[ -z "${BAZEL_REAL:-}" ]]; then
20 if [[ -x $(command -v nix-shell) ]]; then
21 echo "BAZEL_REAL is not set and nix-shell is available, overriding to nix-shell" >&2
22 USE_NIX_SHELL=yes
23 else
24 echo "BAZEL_REAL is not set and nix-shell not available. Please check the setup guide." >&2
25 exit 1
26 fi
27fi
28
29if [[ -n "${USE_NIX_SHELL:-}" ]]; then
Tim Windelschmidt246f2fe2023-10-26 04:39:35 +020030 # Jump to project root since bwrap hangs if we aren't there
31 cd "${DIR}/../"
32
33 export COMMAND="bazel $*"
Tim Windelschmidt7f14f912023-12-15 21:49:15 +010034 export PWD="$OLDPWD"
Tim Windelschmidt246f2fe2023-10-26 04:39:35 +020035 exec nix-shell
36fi
37
Leopold7fbf1042023-01-06 19:57:37 +010038# Short circuit if we're rebuilding the sandbox via third_party/sandboxroot/regenerate.sh.
39if [[ -n ${MONOGON_SYSROOT_REBUILD:-} ]]; then
40 echo "Skipping Bazel wrapper" >&2
41 exec -a "$0" "${BAZEL_REAL}" "$@"
Leopold29400282023-01-04 17:12:46 +010042fi
43
Leopold7fbf1042023-01-06 19:57:37 +010044SANDBOX="${DIR}/../.bazeldnf/sandbox/default"
45BAZEL_ARGS="--noworkspace_rc --bazelrc ${DIR}/../.bazelrc.sandboxroot"
Leopold29400282023-01-04 17:12:46 +010046
Leopold7fbf1042023-01-06 19:57:37 +010047prechecks() {
Leopold7fbf1042023-01-06 19:57:37 +010048 # Recommend using Bazelisk instead of Bazel's "bazel.sh" wrapper.
Leopold Schabel9508b122023-07-14 17:54:17 +020049 # Skip if we're inside the Nix shell (which uses a customized Bazel build).
50 if [[ -z "${BAZELISK_SKIP_WRAPPER:-}" && -z "${MONOGON_NIXOS:-}" ]]; then
Leopold7fbf1042023-01-06 19:57:37 +010051 echo "############################################################" >&2
52 echo "# Please use Bazelisk to build the Monorepo. Using Bazel #" >&2
53 echo "# directly may work, but is not recommended or supported. #" >&2
54 echo "############################################################" >&2
55 fi
56
57 # Our local user needs write access to /dev/kvm. Warn if this is not the case.
58 if ! touch /dev/kvm; then
59 echo "###################################################################" >&2
60 echo "# Cannot write to /dev/kvm - please verify permissions. #" >&2
61 echo "# Most tests require KVM and will not work. Builds still work. #" >&2
62 echo "# On most systems, add your user to the kvm group and re-login. #" >&2
63 echo "###################################################################" >&2
64 fi
65}
66
67intellij_patch() {
68 # When IntelliJ's Bazel plugin uses //scripts/bin/bazel to either build targets
69 # or run syncs, it adds a --override_repository flag to the bazel command
70 # line that points @intellij_aspect into a path on the filesystem. This
71 # external repository contains a Bazel Aspect definition which Bazel
72 # executes to provide the IntelliJ Bazel plugin with information about the
73 # workspace / build targets / etc...
74 #
75 # We need to patch the aspect definition to fix a number of bugs
76 # to make it work with the Monogon monorepo.
77
78 # Find all IntelliJ installation/config directories.
79 local ij_home_paths=("${HOME}/.local/share/JetBrains/IntelliJIdea"*)
80 # Get the newest one, if any.
81 local ij_home=""
82 if ! [[ ${#ij_home_paths[@]} -eq 0 ]]; then
83 # Reverse sort paths by name, with the first being the newest IntelliJ
84 # installation.
85 IFS=$'\n'
86 local sorted=($(sort -r <<<"${ij_home_paths[*]}"))
87 unset IFS
88 ij_home="${sorted[0]}"
89 fi
90
Tim Windelschmidt90d40512025-03-25 12:37:06 +010091
Leopold7fbf1042023-01-06 19:57:37 +010092 # If we don't have or can't find ij_home, don't bother with attempting to patch anything.
93 if [[ -d "${ij_home}" ]]; then
94 # aspect_path is the path to the aspect external repository that IntelliJ will
95 # inject into bazel via --override_repository.
96 local aspect_path="${ij_home}/ijwb/aspect"
Tim Windelschmidt90d40512025-03-25 12:37:06 +010097 if [[ -d "${aspect_path}" ]]; then
98 # aspect_path is the path to the aspect external repository that IntelliJ will
99 # inject into bazel via --override_repository.
100 local aspect_path="${ij_home}/ijwb/aspect"
101 # Our copy of it.
102 local patched_path="${ij_home}/ijwb/aspect-monogon"
103 # Checksum of the patch that was used to create patched_path.
104 local checksum_file="${patched_path}/checksum"
105 # The patch
106 local patch_file="${DIR}/../intellij/patches/bazel_intellij_aspect_filter.patch"
107 # The checksum of the patch we're about to apply.
108 local checksum
109 checksum=$(sha256sum "$patch_file" | cut -d' ' -f1)
Leopold7fbf1042023-01-06 19:57:37 +0100110
Tim Windelschmidt90d40512025-03-25 12:37:06 +0100111 # If the patched aspect repository doesn't exist, or the checksum of the patch
112 # we're about to apply doesn't match the checksum of the patch that was used
113 # to create the patched aspect repository, apply the patch.
Leopold7fbf1042023-01-06 19:57:37 +0100114
Tim Windelschmidt90d40512025-03-25 12:37:06 +0100115 if ! [[ -d "${patched_path}" ]] || ! [[ "$(cat "${checksum_file}")" == "${checksum}" ]]; then
116 echo "IntelliJ found at ${ij_home}, patching aspect repository." >&2
117 # Copy the aspect repository to the patched path.
118 rm -rf "${patched_path}"
119 cp -r "${aspect_path}" "${patched_path}"
120 # Apply the patch.
121 patch -d "${patched_path}" -p1 < "${patch_file}"
122 # Write the checksum of the patch to the checksum file.
123 echo "${checksum}" > "${checksum_file}"
124 else
125 echo "IntelliJ found at ${ij_home}, aspect repository already patched." >&2
126 fi
Leopold7fbf1042023-01-06 19:57:37 +0100127 else
Leopold Schabelb8f02062025-06-25 18:28:02 +0200128 echo "IntelliJ found at ${ij_home}, but aspect repository is missing. Skipping..." >&2
Leopold7fbf1042023-01-06 19:57:37 +0100129 fi
130 fi
131}
132
133regenerate_sysroot() {
134 local checksum_file="${SANDBOX}/checksum"
135 local checksum_input=(
136 ${DIR}/../third_party/sandboxroot/{repositories.bzl,BUILD.bazel}
Leopold7fbf1042023-01-06 19:57:37 +0100137 ${DIR}/../.bazelrc.sandboxroot
138 ${DIR}/../tools/bazel
139 )
140 local checksum
141 checksum="$(sha256sum <(cat "${checksum_input[@]}") | cut -d' ' -f1)"
142
143 if [[ -f "${checksum_file}" ]] && [[ "$(cat "${checksum_file}")" == "${checksum}" ]]; then
144 # Sysroot is up to date.
145 return
146 fi
147
148 echo "Regenerating sysroot $SANDBOX ..." >&2
149 rm -rf "$SANDBOX"
150 "$BAZEL_REAL" ${BAZEL_ARGS} run //third_party/sandboxroot:sandboxroot
151
152 # Manually resolve alternatives (https://github.com/rmohr/bazeldnf/issues/28)
153 ln -r -s -f "${SANDBOX}/root/usr/bin/ld.bfd" "${SANDBOX}/root/usr/bin/ld"
154
155 # Write checksum of the sysroot to a file in order to detect changes.
156 echo "$checksum" > "${SANDBOX}/checksum"
157
158 # Write Bazel config
159 ROOT=$(realpath "$DIR/..")
160
161 # We need the host's resolv.conf for some E2E tests which require internet access.
162 cp /etc/resolv.conf "${ROOT}/.bazeldnf/sandbox/default/root/etc/resolv.conf"
163
164 cat > "${DIR}/../.bazelrc.sandbox" <<EOF
Leopoldd2668122023-02-01 15:15:59 +0100165# Autogenerated by tools/bazel. Manual changes can result in stale caches.
166# Modify the generator instead.
167
Leopold Schabel9508b122023-07-14 17:54:17 +0200168# Mount directories from the generated Fedora sandbox root.
Leopold7fbf1042023-01-06 19:57:37 +0100169build --sandbox_add_mount_pair=${ROOT}/.bazeldnf/sandbox/default/root/etc:/etc
170build --sandbox_add_mount_pair=${ROOT}/.bazeldnf/sandbox/default/root/usr:/usr
171build --sandbox_add_mount_pair=${ROOT}/.bazeldnf/sandbox/default/root/var:/var
172build --sandbox_add_mount_pair=${ROOT}/.bazeldnf/sandbox/default/root/run:/run
173build --sandbox_add_mount_pair=${ROOT}/.bazeldnf/sandbox/default/root/tmp:/tmp
174build --sandbox_add_mount_pair=${ROOT}/.bazeldnf/sandbox/default/root/lib64:/lib64
175build --sandbox_add_mount_pair=${ROOT}/.bazeldnf/sandbox/default/root/lib:/lib
176build --sandbox_add_mount_pair=${ROOT}/.bazeldnf/sandbox/default/root/bin:/bin
177build --sandbox_add_mount_pair=${ROOT}/.bazeldnf/sandbox/default/root/sbin:/sbin
Leopold Schabel9508b122023-07-14 17:54:17 +0200178
179# Needed for the Go SDK shipped by rules_go to resolve its own GOROOT via /proc/self/exe.
180build --sandbox_add_mount_pair=/proc
181
182# Needed for python's multiprocessing lock implementation
183# (_multiprocessing.SemLock for eg. mp.Queue), as used in EDK2's build system.
184build --sandbox_add_mount_pair=/dev/shm
185
186# Needed for qemu for tests.
187build --sandbox_add_mount_pair=/dev/kvm
188
189# Put a tmpfs on /tmp for better performance.
190build --sandbox_tmpfs_path=/tmp
Leopold7fbf1042023-01-06 19:57:37 +0100191EOF
192
193 echo "Done regenerating sysroot." >&2
194}
195
196prechecks
197intellij_patch
198regenerate_sysroot
199
200# Find the --override_repository=intellij_aspect=[path]
201# argument in $@ and replace the path with the patched version.
202# This is surprisingly tricky - bash special-cases "$@" to expand
203# as "$1" "$2" ... "$n" so that argv is preserved, so we need to
204# modify the real $@ array.
205
206for i in $(seq 1 $#); do
207 if [[ "${!i}" == "--override_repository=intellij_aspect="* ]]; then
208 new_arg="${!i/\/aspect/\/aspect-monogon}"
209 set -- "${@:1:$((i-1))}" "${new_arg}" "${@:$((i+1))}"
210 fi
211done
212
213# Bazel does not track the ambient environment, so we need to invalidate
214# the entire build via an --action_env whenever the sandbox digest changes.
215# This is strictly necessary to guarantee correctness.
216export MONOGON_SANDBOX_DIGEST="$(cat "${SANDBOX}/checksum")"
217
Leopold Schabel9508b122023-07-14 17:54:17 +0200218# Ignore the host TMPDIR - it might be something funny like /run/user/1000,
219# we want it to be /tmp inside the sandbox.
220export TMPDIR=/tmp
221
Leopold7fbf1042023-01-06 19:57:37 +0100222exec -a "$0" "${BAZEL_REAL}" "$@"