| 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 |
| Leopold Schabel | bb53d28 | 2025-07-08 17:13:31 +0200 | [diff] [blame] | 20 | # If we are already in the nix shell, we just set BAZEL_REAL and continue |
| 21 | # regular execution. Bazel will be in PATH. |
| 22 | if [[ -n "${MONOGON_NIXOS:-}" ]]; then |
| 23 | BAZEL_REAL="$(command -v bazel)" |
| 24 | elif [[ -x $(command -v nix-shell) ]]; then |
| Tim Windelschmidt | 5bfdb97 | 2023-11-25 06:01:28 +0100 | [diff] [blame] | 25 | echo "BAZEL_REAL is not set and nix-shell is available, overriding to nix-shell" >&2 |
| 26 | USE_NIX_SHELL=yes |
| 27 | else |
| 28 | echo "BAZEL_REAL is not set and nix-shell not available. Please check the setup guide." >&2 |
| 29 | exit 1 |
| 30 | fi |
| 31 | fi |
| 32 | |
| 33 | if [[ -n "${USE_NIX_SHELL:-}" ]]; then |
| Tim Windelschmidt | 246f2fe | 2023-10-26 04:39:35 +0200 | [diff] [blame] | 34 | # Jump to project root since bwrap hangs if we aren't there |
| 35 | cd "${DIR}/../" |
| 36 | |
| Tim Windelschmidt | 7f14f91 | 2023-12-15 21:49:15 +0100 | [diff] [blame] | 37 | export PWD="$OLDPWD" |
| Tim Windelschmidt | 5d357d8 | 2025-07-10 18:47:15 +0200 | [diff] [blame^] | 38 | exec nix-shell --command "bazel $*" |
| Tim Windelschmidt | 246f2fe | 2023-10-26 04:39:35 +0200 | [diff] [blame] | 39 | fi |
| 40 | |
| Leopold | 7fbf104 | 2023-01-06 19:57:37 +0100 | [diff] [blame] | 41 | prechecks() { |
| Leopold | 7fbf104 | 2023-01-06 19:57:37 +0100 | [diff] [blame] | 42 | # Recommend using Bazelisk instead of Bazel's "bazel.sh" wrapper. |
| Leopold Schabel | 9508b12 | 2023-07-14 17:54:17 +0200 | [diff] [blame] | 43 | # Skip if we're inside the Nix shell (which uses a customized Bazel build). |
| 44 | if [[ -z "${BAZELISK_SKIP_WRAPPER:-}" && -z "${MONOGON_NIXOS:-}" ]]; then |
| Leopold | 7fbf104 | 2023-01-06 19:57:37 +0100 | [diff] [blame] | 45 | echo "############################################################" >&2 |
| 46 | echo "# Please use Bazelisk to build the Monorepo. Using Bazel #" >&2 |
| 47 | echo "# directly may work, but is not recommended or supported. #" >&2 |
| 48 | echo "############################################################" >&2 |
| 49 | fi |
| 50 | |
| 51 | # Our local user needs write access to /dev/kvm. Warn if this is not the case. |
| 52 | if ! touch /dev/kvm; then |
| 53 | echo "###################################################################" >&2 |
| 54 | echo "# Cannot write to /dev/kvm - please verify permissions. #" >&2 |
| 55 | echo "# Most tests require KVM and will not work. Builds still work. #" >&2 |
| 56 | echo "# On most systems, add your user to the kvm group and re-login. #" >&2 |
| 57 | echo "###################################################################" >&2 |
| 58 | fi |
| 59 | } |
| 60 | |
| Leopold | 7fbf104 | 2023-01-06 19:57:37 +0100 | [diff] [blame] | 61 | prechecks |
| Leopold | 7fbf104 | 2023-01-06 19:57:37 +0100 | [diff] [blame] | 62 | |
| Leopold | 7fbf104 | 2023-01-06 19:57:37 +0100 | [diff] [blame] | 63 | exec -a "$0" "${BAZEL_REAL}" "$@" |