blob: 1ff2694ea61a0607677ba3306468336ab797fa54 [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 +010038prechecks() {
Leopold7fbf1042023-01-06 19:57:37 +010039 # Recommend using Bazelisk instead of Bazel's "bazel.sh" wrapper.
Leopold Schabel9508b122023-07-14 17:54:17 +020040 # Skip if we're inside the Nix shell (which uses a customized Bazel build).
41 if [[ -z "${BAZELISK_SKIP_WRAPPER:-}" && -z "${MONOGON_NIXOS:-}" ]]; then
Leopold7fbf1042023-01-06 19:57:37 +010042 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
Leopold7fbf1042023-01-06 19:57:37 +010058prechecks
Leopold7fbf1042023-01-06 19:57:37 +010059
Leopold7fbf1042023-01-06 19:57:37 +010060exec -a "$0" "${BAZEL_REAL}" "$@"