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