forked from bazel-xcode/xchammer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bazelwrapper
executable file
·70 lines (57 loc) · 2.14 KB
/
bazelwrapper
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/bin/bash
set -e
# Make sure we're in the project root directory.
SCRIPTPATH="$( cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 ; pwd -P )"
pushd "$SCRIPTPATH/.." > /dev/null
BAZEL="${SCRIPTPATH}/bazel"
function clean() {
echo "INFO: Bazel force cleaning"
$BAZEL clean --expunge
# The Bazel server can have issues across Bazel versions
killall -9 bazel || true
}
XCODE_SELECT_ENV_PATH="${PWD}/.xcode_select_env"
CURRENT_XCODE_PATH="$(/usr/bin/xcode-select -p)"
XCODE_VERSION=$(/usr/bin/xcodebuild -version | grep Xcode | cut -d ' ' -f2)
CURRENT_XCODE_HASH="${CURRENT_XCODE_PATH}-${XCODE_VERSION}-${BAZEL_VERSION}"
if [[ -f "${XCODE_SELECT_ENV_PATH}" ]]; then
EXISTING_XCODE_HASH="$(cat "${XCODE_SELECT_ENV_PATH}")"
if [[ $EXISTING_XCODE_HASH != "${CURRENT_XCODE_HASH}" ]]; then
echo "Xcode select path or Bazel version has changed, must clear cached data"
clean
fi
fi
echo "${CURRENT_XCODE_HASH}" > "${XCODE_SELECT_ENV_PATH}"
if [[ -n $BAZEL_FORCE_CLEAN ]]; then
clean
fi
# Make variable support
# In the context of Xcode builds, variables are defined as "Make variable"
# strings.
# In practice, the variables are stored as strings, and then later assigned to
# the value of the current environment.
ARGS=()
HAS_XCODE_VERSION_FLAG=false
for ARG in "$@"; do
if [[ "$ARG" =~ \$(.*) ]]; then
# Get the name of the make variable
MAKEVAR=$(echo $ARG | sed 's,.*\$(\(.*\)).*,\1,g')
# Next, parameter expansion of the variable by name
VALUE="${!MAKEVAR}"
REPLACED="$(echo $ARG | sed "s,\$(\(.*\)),$VALUE,g")"
ARGS+=(${REPLACED})
else
ARGS+=("${ARG}")
fi
if [[ "$ARG" =~ "--xcode_version" ]]; then
HAS_XCODE_VERSION_FLAG=true
fi
done
# If we don't select an Xcode then use the default Xcode
if [[ "$1" == "build" ]] || [[ "$1" == "test" ]]; then
if [[ ! $HAS_XCODE_VERSION_FLAG == true ]]; then
ARGS+=("--xcode_version=$(/usr/bin/xcodebuild -version | grep Xcode | cut -d ' ' -f2)")
fi
fi
# Exec in a subshell in order to trap
(exec -a "$BAZEL" /usr/bin/env - TERM="${TERM}" SHELL="${SHELL}" PATH="${PATH}" HOME="${HOME}" "${BAZEL}" "${ARGS[@]}")