Skip to content

Commit

Permalink
tools/bazel: Split environment into separate script.
Browse files Browse the repository at this point in the history
This splits the initialization of build environment into a separate
build_env.sh script, allowing for more complex control flow than when
setting everything up in a single `env -i` invocation, and allowing
the environment variables to sourced by other, non-bazel scripts.

Signed-off-by: format 20.02.05 <github.com/ChrisCummins/format>
  • Loading branch information
ChrisCummins committed Apr 2, 2020
1 parent b2c9267 commit 3bf2ec9
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 60 deletions.
67 changes: 7 additions & 60 deletions tools/bazel
Original file line number Diff line number Diff line change
Expand Up @@ -10,63 +10,10 @@
# environments such as terminals, IDEs, editors, etc. This is especially
# frustrating as it invalidates the results of (possibly expensive) test runs.
#
# This bazel wrapper script forwards only the minimum environment variables
# required for bazel to complete it's task: a ${TERM} environment (so it knows
# how to format build output), and a ${PATH} which has the necessary tools
# required to complete the build (e.g. the compiler toolchains and python
# runtimes).
#
# This assumes that /bin, /usr/bin, and /usr/local/bin contain the necessary
# build tools, which is true for the standard Linux / macOS environments I use.
# Your mileage may vary.
set -eu

build_path() {
local _dir=""
local _path=""

for _dir in "$@"
do
if [ -d $_dir ]; then
_path=$_path:$_dir
fi
done

_path=${_path:1}
echo $_path
}

# Accepts an array of directories and returns a colon separated path
# of all of the directories that exist, in order. Example usage:
#
# dirs=("/usr/local/bin" /usr/bin "/not a real path")
# unset FOO
# FOO=$(build_path "${dirs[@]}")
# echo $FOO
# # Outputs: /usr/local/bin:/usr/bin
path_dirs=( \
/usr/local/opt/llvm/bin \
/usr/local/opt/gnu-sed/libexec/gnubin \
/usr/bin \
/usr/local/bin \
/bin \
)

if [[ -f "/usr/local/opt/llvm/bin/clang" ]]; then
CC=/usr/local/opt/llvm/bin/clang
CXX=/usr/local/opt/llvm/bin/clang++
else
CC="$(which gcc)"
CXX="$(which g++)"
fi

# PULLET_TIMEOUT to increase the timeout on docker image pulls from the default
# 600s. See: https://github.com/bazelbuild/rules_docker
set +u
env -i \
TERM="$TERM" \
PATH="$(build_path ${path_dirs[@]})" \
CC=$CC \
CXX=$CXX \
PULLER_TIMEOUT=3600 \
"$BAZEL_REAL" "$@"
# Instead, the goal of this script is to create consistent environment which
# can be run from anywhere and always makes sure that bazel gets the same
# variables to work with. The way of achieving that is to start from a blank
# slate empty environment and call a script which sets up the necessary
# variables.
set -e
env -i TERM="$TERM" HOME="$HOME" tools/bazel_env.sh "$BAZEL_REAL" "$@"
63 changes: 63 additions & 0 deletions tools/bazel_env.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/usr/bin/env bash
#
# This script exports the environment variables which are used for bazel
# invocations. Use it as a wrapper around a bazel invocation, optionally
# using `exec -i` to produce a clean environment:
#
# $ exec -i tools/bazel_env.sh bazel build //...
#
# This script assumes that /bin, /usr/bin, and /usr/local/bin contain the
# necessary build tools, which is true for the standard Linux / macOS
# environments I use. Your mileage may vary.
set -eu

# Accepts an array of directories and returns a colon separated path
# of all of the directories that exist, in order. Example usage:
#
# dirs=("/usr/local/bin" /usr/bin "/not a real path")
# unset FOO
# FOO=$(build_path "${dirs[@]}")
# echo $FOO
# # Outputs: /usr/local/bin:/usr/bin
build_path() {
local _dir=""
local _path=""

for _dir in "$@"; do
if [ -d $_dir ]; then
_path=$_path:$_dir
fi
done

_path=${_path:1}
echo $_path
}

# Build our path.
path_dirs=(
/usr/local/opt/llvm/bin
/usr/local/opt/gnu-sed/libexec/gnubin
/Library/TeX/Distributions/.DefaultTeX/Contents/Programs/texbin
/usr/bin
/usr/local/bin
/bin
)
export PATH="$(build_path ${path_dirs[@]})"

# Note(github.com/ChrisCummins/phd/issues/55): On macOS, custom LDFLAGS and
# CPPFLAGS are required to pip build MySQLdb:
if [[ -d /usr/local/opt/openssl ]]; then
export LDFLAGS="-L/usr/local/opt/openssl/lib"
export CPPFLAGS="-I/usr/local/opt/openssl/include"
fi

# Increase the timeout on docker image pulls from the default 600s.
# See: https://github.com/bazelbuild/rules_docker
export PULLER_TIMEOUT=3600

if [[ -f "/usr/local/opt/llvm/bin/clang" ]]; then
export CC=/usr/local/opt/llvm/bin/clang
export CXX=/usr/local/opt/llvm/bin/clang++
fi

$@

0 comments on commit 3bf2ec9

Please sign in to comment.