mongo/tools/bazel
Daniel Moody a40280e4cd SERVER-100099 make compiledb configurable (#31743)
GitOrigin-RevId: 7ac6ff4e5104227ebb73618cc770d000fcaf541f
2025-01-29 23:45:47 +00:00

182 lines
6.5 KiB
Bash
Executable File

#!/usr/bin/env bash
# Whenever Bazel is invoked, it first calls this script setting "BAZEL_REAL" to the path of the real Bazel binary.
# Use this file as a wrapper for any logic that should run before bazel itself is executed.
# WARNING : If you run //:compiledb target, you can not print to stdout in this file as it will fail with
# "Bazel aquery failed." because it is reading this files stdout as aquery output
REPO_ROOT=$(dirname $(dirname $(realpath "$0")))
bazel_real="$BAZEL_REAL"
bazelrc_xcode_lines=()
# If disk space becomes an issue, this block can be used to clean up configs
# when disk space is getting low. It is commented out because "bazel info output_path"
# takes .5 seconds, and no need to add that extra time unless disk space becomes a large
# problem
#if [ $# -eq 2 ] || [ $# -eq 3 ]; then
# if [ "$1" = "build" ]; then
# output_path=$($bazel_real info output_path)
# remaining_kb=$(df --output=avail $output_path | tail -n 1)
# remaining_gb=$(($remaining_kb / 1024 / 1024))
# fastbuild_dir=${output_path}/aarch64-fastbuild
# dbg_dir=${output_path}/aarch64-dbg
# opt_dir=${output_path}/aarch64-opt
# if [ "$remaining_gb" -lt 40 ]; then
# echo "Disk space is getting low (under 40GB) - cleaning up other config outputs"
# if [ $# -eq 2 ]; then
# rm -rf "$dbg_dir" &>/dev/null
# rm -rf "$opt_dir" &>/dev/null
# elif [[ "$2" == "--config=fastbuild"* ]]; then
# rm -rf "$dbg_dir" &>/dev/null
# rm -rf "$opt_dir" &>/dev/null
# elif [[ "$2" == "--config=dbg"* ]]; then
# rm -rf "$fastbuild_dir" &>/dev/null
# rm -rf "$opt_dir" &>/dev/null
# elif [[ "$2" == "--config=opt"* ]]; then
# rm -rf "$fastbuild_dir" &>/dev/null
# rm -rf "$dbg_dir" &>/dev/null
# fi
# fi
# fi
#fi
if [[ -z "${BAZELISK_SKIP_WRAPPER}" ]]; then
echo "You're not using Bazelisk, which is recommended for a consistent build environment." >&2
echo "Your version of Bazel may be mismatched with the version intended to be used to build MongoDB." >&2
echo "Please run the following command to install Bazelisk and make sure to add ~/.local/bin to your PATH:" >&2
echo "" >&2
echo "python buildscripts/install_bazel.py" >&2
exit 0
fi
if [[ $OSTYPE == darwin* ]]; then
echo "Running on Apple (darwin), creating .bazelrc for xcode settings." >&2
xcode_path=$(xcode-select -p) >&2
xcode_version=$(xcodebuild -version | tail -1 | cut -d " " -f3) >&2
xcode_build_number=$(/usr/bin/xcodebuild -version 2>/dev/null | tail -1 | cut -d " " -f3) >&2
bazelrc_lines+=("startup --host_jvm_args=-Xdock:name=$xcode_path") >&2
bazelrc_lines+=("common --xcode_version=$xcode_version") >&2
bazelrc_lines+=("common --repo_env=USE_CLANG_CL=$xcode_version") >&2
bazelrc_lines+=("common --repo_env=DEVELOPER_DIR=$xcode_path") >&2
fi
printf '%s\n' "${bazelrc_xcode_lines[@]}" > $REPO_ROOT/.bazelrc.xcode
echo "common --//bazel/config:running_through_bazelisk" > $REPO_ROOT/.bazelrc.bazelisk
if [[ $MONGO_BAZEL_WRAPPER_DEBUG == 1 ]]; then
wrapper_start_time="$(date -u +%s.%N)"
fi
if [[ "$OSTYPE" == "linux"* ]]; then
os="linux"
elif [[ "$OSTYPE" == "darwin"* ]]; then
os="macos"
else
os="unknown"
fi
ARCH=$(uname -m)
if [[ "$ARCH" == "arm64" || "$ARCH" == "aarch64" ]]; then
ARCH="arm64"
elif [[ "$ARCH" == "ppc64le" || "$ARCH" == "ppc64" || "$ARCH" == "ppc" || "$ARCH" == "ppcle" ]]; then
ARCH="ppc64le"
elif [[ "$ARCH" == "s390x" || "$ARCH" == "s390" ]]; then
ARCH="s390x"
else
ARCH="x86_64"
fi
declare -a cert_locs=()
cert_locs+=("/etc/ssl/certs/ca-certificates.crt") # Debian/Ubuntu/Gentoo etc.
cert_locs+=("/etc/pki/tls/certs/ca-bundle.crt") # Fedora/RHEL 6
cert_locs+=("/etc/ssl/ca-bundle.pem") # OpenSUSE
cert_locs+=("/etc/pki/tls/cacert.pem") # OpenELEC
cert_locs+=("/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem") # CentOS/RHEL 7
cert_locs+=("/etc/ssl/cert.pem") # Alpine Linux
for cert in ${cert_locs[@]}; do
if [ -f $cert ]; then
export SSL_CERT_DI=R$(dirname $cert)
export SSL_CERT_FILE=$cert
break
fi
done
cur_dir=$(basename $REPO_ROOT)
bazel_python="$REPO_ROOT/bazel-$cur_dir/external/py_${os}_${ARCH}/dist/bin/python3"
compdb_python="$REPO_ROOT/.compiledb/compiledb-$cur_dir/external/py_${os}_${ARCH}/dist/bin/python3"
python=$bazel_python
if [ ! -f $python ]; then
python=$compdb_python
fi
if [ ! -f $python ]; then
>&2 echo "python prereq missing, using bazel to install python..."
>&2 $bazel_real build --config=local @py_${os}_${ARCH}//:all
if [[ $? != 0 ]]; then
if [[ ! -z "$CI" ]] || [[ $MONGO_BAZEL_WRAPPER_FALLBACK == 1 ]]; then
>&2 echo "wrapper script failed to install python! falling back to normal bazel call..."
exec "$bazel_real" $@
else
exit $?
fi
fi
fi
python=$bazel_python
if [ ! -f $python ]; then
python=$compdb_python
fi
autocomplete_query=0
# bash autocomplete detection
if [[ $* =~ ^.*--output_base=/tmp/.*-completion-$USER.*$ ]]; then
autocomplete_query=1
fi
# zsh autocomplete detection
if [[ $* == *"--noblock_for_lock query kind(\".*_test\", //:all)"* ]] || [[ $* == *"--noblock_for_lock query kind(\".*_test\", :all)"* ]]; then
autocomplete_query=1
fi
rm -f /tmp/mongo_autocomplete_plus_targets
MONGO_BAZEL_WRAPPER_ARGS=$(mktemp)
MONGO_BAZEL_WRAPPER_ARGS=$MONGO_BAZEL_WRAPPER_ARGS \
MONGO_AUTOCOMPLETE_QUERY=$autocomplete_query \
>&2 $python $REPO_ROOT/bazel/wrapper_hook/wrapper_hook.py $bazel_real "$@"
if [[ $? != 0 ]]; then
if [[ ! -z "$CI" ]] || [[ $MONGO_BAZEL_WRAPPER_FALLBACK == 1 ]]; then
>&2 echo "wrapper script failed! falling back to normal bazel call..."
exec "$bazel_real" $@
else
exit $?
fi
fi
new_args=()
while IFS= read -r line; do
new_args+=("$line")
done < $MONGO_BAZEL_WRAPPER_ARGS
if [[ $MONGO_BAZEL_WRAPPER_DEBUG == 1 ]] && [[ $autocomplete_query == 0 ]]; then
wrapper_end_time="$(date -u +%s.%N)"
runtime=$(bc <<< "$wrapper_end_time - $wrapper_start_time")
runtime=$(printf "%0.3f" $runtime)
>&2 echo "[WRAPPER_HOOK_DEBUG]: wrapper hook script input args: $@"
>&2 echo "[WRAPPER_HOOK_DEBUG]: wrapper hook script new args: ${new_args[@]@Q}"
>&2 echo "[WRAPPER_HOOK_DEBUG]: wrapper hook script took $runtime seconds"
fi
if [[ $autocomplete_query == 1 ]]; then
plus_targets=$(</tmp/mongo_autocomplete_plus_targets)
query_output=$("${new_args[@]@Q}")
echo $query_output $plus_targets | tr " " "\n"
else
exec "$bazel_real" "${new_args[@]}"
fi