blob: 849bb9bc20a8d572d70d001aee4ae0e4cc769b6f [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.
25 if [[ -z "${BAZELISK_SKIP_WRAPPER:-}" ]]; then
26 echo "############################################################" >&2
27 echo "# Please use Bazelisk to build the Monorepo. Using Bazel #" >&2
28 echo "# directly may work, but is not recommended or supported. #" >&2
29 echo "############################################################" >&2
30 fi
31
32 # Our local user needs write access to /dev/kvm. Warn if this is not the case.
33 if ! touch /dev/kvm; then
34 echo "###################################################################" >&2
35 echo "# Cannot write to /dev/kvm - please verify permissions. #" >&2
36 echo "# Most tests require KVM and will not work. Builds still work. #" >&2
37 echo "# On most systems, add your user to the kvm group and re-login. #" >&2
38 echo "###################################################################" >&2
39 fi
40}
41
42intellij_patch() {
43 # When IntelliJ's Bazel plugin uses //scripts/bin/bazel to either build targets
44 # or run syncs, it adds a --override_repository flag to the bazel command
45 # line that points @intellij_aspect into a path on the filesystem. This
46 # external repository contains a Bazel Aspect definition which Bazel
47 # executes to provide the IntelliJ Bazel plugin with information about the
48 # workspace / build targets / etc...
49 #
50 # We need to patch the aspect definition to fix a number of bugs
51 # to make it work with the Monogon monorepo.
52
53 # Find all IntelliJ installation/config directories.
54 local ij_home_paths=("${HOME}/.local/share/JetBrains/IntelliJIdea"*)
55 # Get the newest one, if any.
56 local ij_home=""
57 if ! [[ ${#ij_home_paths[@]} -eq 0 ]]; then
58 # Reverse sort paths by name, with the first being the newest IntelliJ
59 # installation.
60 IFS=$'\n'
61 local sorted=($(sort -r <<<"${ij_home_paths[*]}"))
62 unset IFS
63 ij_home="${sorted[0]}"
64 fi
65
66 # If we don't have or can't find ij_home, don't bother with attempting to patch anything.
67 if [[ -d "${ij_home}" ]]; then
68 # aspect_path is the path to the aspect external repository that IntelliJ will
69 # inject into bazel via --override_repository.
70 local aspect_path="${ij_home}/ijwb/aspect"
71 # Our copy of it.
72 local patched_path="${ij_home}/ijwb/aspect-monogon"
73 # Checksum of the patch that was used to create patched_path.
74 local checksum_file="${patched_path}/checksum"
75 # The patch
76 local patch_file="${DIR}/../intellij/patches/bazel_intellij_aspect_filter.patch"
77 # The checksum of the patch we're about to apply.
78 local checksum
79 checksum=$(sha256sum "$patch_file" | cut -d' ' -f1)
80
81 # If the patched aspect repository doesn't exist, or the checksum of the patch
82 # we're about to apply doesn't match the checksum of the patch that was used
83 # to create the patched aspect repository, apply the patch.
84
85 if ! [[ -d "${patched_path}" ]] || ! [[ "$(cat "${checksum_file}")" == "${checksum}" ]]; then
86 echo "IntelliJ found at ${ij_home}, patching aspect repository." >&2
87 # Copy the aspect repository to the patched path.
88 rm -rf "${patched_path}"
89 cp -r "${aspect_path}" "${patched_path}"
90 # Apply the patch.
91 patch -d "${patched_path}" -p1 < "${patch_file}"
92 # Write the checksum of the patch to the checksum file.
93 echo "${checksum}" > "${checksum_file}"
94 else
95 echo "IntelliJ found at ${ij_home}, aspect repository already patched." >&2
96 fi
97 fi
98}
99
100regenerate_sysroot() {
101 local checksum_file="${SANDBOX}/checksum"
102 local checksum_input=(
103 ${DIR}/../third_party/sandboxroot/{repositories.bzl,BUILD.bazel}
Leopold7fbf1042023-01-06 19:57:37 +0100104 ${DIR}/../.bazelrc.sandboxroot
105 ${DIR}/../tools/bazel
106 )
107 local checksum
108 checksum="$(sha256sum <(cat "${checksum_input[@]}") | cut -d' ' -f1)"
109
110 if [[ -f "${checksum_file}" ]] && [[ "$(cat "${checksum_file}")" == "${checksum}" ]]; then
111 # Sysroot is up to date.
112 return
113 fi
114
115 echo "Regenerating sysroot $SANDBOX ..." >&2
116 rm -rf "$SANDBOX"
117 "$BAZEL_REAL" ${BAZEL_ARGS} run //third_party/sandboxroot:sandboxroot
118
119 # Manually resolve alternatives (https://github.com/rmohr/bazeldnf/issues/28)
120 ln -r -s -f "${SANDBOX}/root/usr/bin/ld.bfd" "${SANDBOX}/root/usr/bin/ld"
121
122 # Write checksum of the sysroot to a file in order to detect changes.
123 echo "$checksum" > "${SANDBOX}/checksum"
124
125 # Write Bazel config
126 ROOT=$(realpath "$DIR/..")
127
128 # We need the host's resolv.conf for some E2E tests which require internet access.
129 cp /etc/resolv.conf "${ROOT}/.bazeldnf/sandbox/default/root/etc/resolv.conf"
130
131 cat > "${DIR}/../.bazelrc.sandbox" <<EOF
Leopoldd2668122023-02-01 15:15:59 +0100132# Autogenerated by tools/bazel. Manual changes can result in stale caches.
133# Modify the generator instead.
134
Leopold7fbf1042023-01-06 19:57:37 +0100135build --sandbox_add_mount_pair=${ROOT}/.bazeldnf/sandbox/default/root/etc:/etc
136build --sandbox_add_mount_pair=${ROOT}/.bazeldnf/sandbox/default/root/usr:/usr
137build --sandbox_add_mount_pair=${ROOT}/.bazeldnf/sandbox/default/root/var:/var
138build --sandbox_add_mount_pair=${ROOT}/.bazeldnf/sandbox/default/root/run:/run
139build --sandbox_add_mount_pair=${ROOT}/.bazeldnf/sandbox/default/root/tmp:/tmp
140build --sandbox_add_mount_pair=${ROOT}/.bazeldnf/sandbox/default/root/lib64:/lib64
141build --sandbox_add_mount_pair=${ROOT}/.bazeldnf/sandbox/default/root/lib:/lib
142build --sandbox_add_mount_pair=${ROOT}/.bazeldnf/sandbox/default/root/bin:/bin
143build --sandbox_add_mount_pair=${ROOT}/.bazeldnf/sandbox/default/root/sbin:/sbin
144EOF
145
146 echo "Done regenerating sysroot." >&2
147}
148
149prechecks
150intellij_patch
151regenerate_sysroot
152
153# Find the --override_repository=intellij_aspect=[path]
154# argument in $@ and replace the path with the patched version.
155# This is surprisingly tricky - bash special-cases "$@" to expand
156# as "$1" "$2" ... "$n" so that argv is preserved, so we need to
157# modify the real $@ array.
158
159for i in $(seq 1 $#); do
160 if [[ "${!i}" == "--override_repository=intellij_aspect="* ]]; then
161 new_arg="${!i/\/aspect/\/aspect-monogon}"
162 set -- "${@:1:$((i-1))}" "${new_arg}" "${@:$((i+1))}"
163 fi
164done
165
166# Bazel does not track the ambient environment, so we need to invalidate
167# the entire build via an --action_env whenever the sandbox digest changes.
168# This is strictly necessary to guarantee correctness.
169export MONOGON_SANDBOX_DIGEST="$(cat "${SANDBOX}/checksum")"
170
171exec -a "$0" "${BAZEL_REAL}" "$@"