| Leopold | 2940028 | 2023-01-04 17:12:46 +0100 | [diff] [blame] | 1 | #!/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. |
| 4 | set -euo pipefail |
| Leopold | 7fbf104 | 2023-01-06 19:57:37 +0100 | [diff] [blame] | 5 | shopt -s nullglob |
| Leopold | 2940028 | 2023-01-04 17:12:46 +0100 | [diff] [blame] | 6 | |
| Tim Windelschmidt | 246f2fe | 2023-10-26 04:39:35 +0200 | [diff] [blame] | 7 | DIR="$( 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. |
| 11 | if [[ "${BAZEL_REAL:-}" == /nix/store/* && -z "${MONOGON_NIXOS:-}" ]]; then |
| Tim Windelschmidt | 5bfdb97 | 2023-11-25 06:01:28 +0100 | [diff] [blame] | 12 | echo "Detected Nix based bazel installation and we are not in a nix-shell, overriding to nix-shell." >&2 |
| 13 | USE_NIX_SHELL=yes |
| 14 | fi |
| Tim Windelschmidt | 246f2fe | 2023-10-26 04:39:35 +0200 | [diff] [blame] | 15 | |
| Tim Windelschmidt | 5bfdb97 | 2023-11-25 06:01:28 +0100 | [diff] [blame] | 16 | # 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. |
| 19 | if [[ -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 |
| 27 | fi |
| 28 | |
| 29 | if [[ -n "${USE_NIX_SHELL:-}" ]]; then |
| Tim Windelschmidt | 246f2fe | 2023-10-26 04:39:35 +0200 | [diff] [blame] | 30 | # Jump to project root since bwrap hangs if we aren't there |
| 31 | cd "${DIR}/../" |
| 32 | |
| 33 | export COMMAND="bazel $*" |
| Tim Windelschmidt | 7f14f91 | 2023-12-15 21:49:15 +0100 | [diff] [blame] | 34 | export PWD="$OLDPWD" |
| Tim Windelschmidt | 246f2fe | 2023-10-26 04:39:35 +0200 | [diff] [blame] | 35 | exec nix-shell |
| 36 | fi |
| 37 | |
| Leopold | 7fbf104 | 2023-01-06 19:57:37 +0100 | [diff] [blame] | 38 | prechecks() { |
| Leopold | 7fbf104 | 2023-01-06 19:57:37 +0100 | [diff] [blame] | 39 | # Recommend using Bazelisk instead of Bazel's "bazel.sh" wrapper. |
| Leopold Schabel | 9508b12 | 2023-07-14 17:54:17 +0200 | [diff] [blame] | 40 | # Skip if we're inside the Nix shell (which uses a customized Bazel build). |
| 41 | if [[ -z "${BAZELISK_SKIP_WRAPPER:-}" && -z "${MONOGON_NIXOS:-}" ]]; then |
| Leopold | 7fbf104 | 2023-01-06 19:57:37 +0100 | [diff] [blame] | 42 | echo "############################################################" >&2 |
| 43 | echo "# Please use Bazelisk to build the Monorepo. Using Bazel #" >&2 |
| 44 | echo "# directly may work, but is not recommended or supported. #" >&2 |
| 45 | echo "############################################################" >&2 |
| 46 | fi |
| 47 | |
| 48 | # Our local user needs write access to /dev/kvm. Warn if this is not the case. |
| 49 | if ! touch /dev/kvm; then |
| 50 | echo "###################################################################" >&2 |
| 51 | echo "# Cannot write to /dev/kvm - please verify permissions. #" >&2 |
| 52 | echo "# Most tests require KVM and will not work. Builds still work. #" >&2 |
| 53 | echo "# On most systems, add your user to the kvm group and re-login. #" >&2 |
| 54 | echo "###################################################################" >&2 |
| 55 | fi |
| 56 | } |
| 57 | |
| 58 | intellij_patch() { |
| 59 | # When IntelliJ's Bazel plugin uses //scripts/bin/bazel to either build targets |
| 60 | # or run syncs, it adds a --override_repository flag to the bazel command |
| 61 | # line that points @intellij_aspect into a path on the filesystem. This |
| 62 | # external repository contains a Bazel Aspect definition which Bazel |
| 63 | # executes to provide the IntelliJ Bazel plugin with information about the |
| 64 | # workspace / build targets / etc... |
| 65 | # |
| 66 | # We need to patch the aspect definition to fix a number of bugs |
| 67 | # to make it work with the Monogon monorepo. |
| 68 | |
| 69 | # Find all IntelliJ installation/config directories. |
| 70 | local ij_home_paths=("${HOME}/.local/share/JetBrains/IntelliJIdea"*) |
| 71 | # Get the newest one, if any. |
| 72 | local ij_home="" |
| 73 | if ! [[ ${#ij_home_paths[@]} -eq 0 ]]; then |
| 74 | # Reverse sort paths by name, with the first being the newest IntelliJ |
| 75 | # installation. |
| 76 | IFS=$'\n' |
| 77 | local sorted=($(sort -r <<<"${ij_home_paths[*]}")) |
| 78 | unset IFS |
| 79 | ij_home="${sorted[0]}" |
| 80 | fi |
| 81 | |
| Tim Windelschmidt | 90d4051 | 2025-03-25 12:37:06 +0100 | [diff] [blame] | 82 | |
| Leopold | 7fbf104 | 2023-01-06 19:57:37 +0100 | [diff] [blame] | 83 | # If we don't have or can't find ij_home, don't bother with attempting to patch anything. |
| 84 | if [[ -d "${ij_home}" ]]; then |
| 85 | # aspect_path is the path to the aspect external repository that IntelliJ will |
| 86 | # inject into bazel via --override_repository. |
| 87 | local aspect_path="${ij_home}/ijwb/aspect" |
| Tim Windelschmidt | 90d4051 | 2025-03-25 12:37:06 +0100 | [diff] [blame] | 88 | if [[ -d "${aspect_path}" ]]; then |
| 89 | # aspect_path is the path to the aspect external repository that IntelliJ will |
| 90 | # inject into bazel via --override_repository. |
| 91 | local aspect_path="${ij_home}/ijwb/aspect" |
| 92 | # Our copy of it. |
| 93 | local patched_path="${ij_home}/ijwb/aspect-monogon" |
| 94 | # Checksum of the patch that was used to create patched_path. |
| 95 | local checksum_file="${patched_path}/checksum" |
| 96 | # The patch |
| 97 | local patch_file="${DIR}/../intellij/patches/bazel_intellij_aspect_filter.patch" |
| 98 | # The checksum of the patch we're about to apply. |
| 99 | local checksum |
| 100 | checksum=$(sha256sum "$patch_file" | cut -d' ' -f1) |
| Leopold | 7fbf104 | 2023-01-06 19:57:37 +0100 | [diff] [blame] | 101 | |
| Tim Windelschmidt | 90d4051 | 2025-03-25 12:37:06 +0100 | [diff] [blame] | 102 | # If the patched aspect repository doesn't exist, or the checksum of the patch |
| 103 | # we're about to apply doesn't match the checksum of the patch that was used |
| 104 | # to create the patched aspect repository, apply the patch. |
| Leopold | 7fbf104 | 2023-01-06 19:57:37 +0100 | [diff] [blame] | 105 | |
| Tim Windelschmidt | 90d4051 | 2025-03-25 12:37:06 +0100 | [diff] [blame] | 106 | if ! [[ -d "${patched_path}" ]] || ! [[ "$(cat "${checksum_file}")" == "${checksum}" ]]; then |
| 107 | echo "IntelliJ found at ${ij_home}, patching aspect repository." >&2 |
| 108 | # Copy the aspect repository to the patched path. |
| 109 | rm -rf "${patched_path}" |
| 110 | cp -r "${aspect_path}" "${patched_path}" |
| 111 | # Apply the patch. |
| 112 | patch -d "${patched_path}" -p1 < "${patch_file}" |
| 113 | # Write the checksum of the patch to the checksum file. |
| 114 | echo "${checksum}" > "${checksum_file}" |
| 115 | else |
| 116 | echo "IntelliJ found at ${ij_home}, aspect repository already patched." >&2 |
| 117 | fi |
| Leopold | 7fbf104 | 2023-01-06 19:57:37 +0100 | [diff] [blame] | 118 | else |
| Leopold Schabel | b8f0206 | 2025-06-25 18:28:02 +0200 | [diff] [blame] | 119 | echo "IntelliJ found at ${ij_home}, but aspect repository is missing. Skipping..." >&2 |
| Leopold | 7fbf104 | 2023-01-06 19:57:37 +0100 | [diff] [blame] | 120 | fi |
| 121 | fi |
| 122 | } |
| 123 | |
| Leopold | 7fbf104 | 2023-01-06 19:57:37 +0100 | [diff] [blame] | 124 | prechecks |
| 125 | intellij_patch |
| Leopold | 7fbf104 | 2023-01-06 19:57:37 +0100 | [diff] [blame] | 126 | |
| 127 | # Find the --override_repository=intellij_aspect=[path] |
| 128 | # argument in $@ and replace the path with the patched version. |
| 129 | # This is surprisingly tricky - bash special-cases "$@" to expand |
| 130 | # as "$1" "$2" ... "$n" so that argv is preserved, so we need to |
| 131 | # modify the real $@ array. |
| 132 | |
| 133 | for i in $(seq 1 $#); do |
| 134 | if [[ "${!i}" == "--override_repository=intellij_aspect="* ]]; then |
| 135 | new_arg="${!i/\/aspect/\/aspect-monogon}" |
| 136 | set -- "${@:1:$((i-1))}" "${new_arg}" "${@:$((i+1))}" |
| 137 | fi |
| 138 | done |
| 139 | |
| Leopold | 7fbf104 | 2023-01-06 19:57:37 +0100 | [diff] [blame] | 140 | exec -a "$0" "${BAZEL_REAL}" "$@" |