From 3bf2ec926f642a859a48f83024f0c2baac712a85 Mon Sep 17 00:00:00 2001 From: Chris Cummins Date: Thu, 2 Apr 2020 16:12:27 +0100 Subject: [PATCH] tools/bazel: Split environment into separate script. 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 --- tools/bazel | 67 +++++----------------------------------------- tools/bazel_env.sh | 63 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 60 deletions(-) create mode 100755 tools/bazel_env.sh diff --git a/tools/bazel b/tools/bazel index b34829a72..fba2d4e12 100755 --- a/tools/bazel +++ b/tools/bazel @@ -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" "$@" diff --git a/tools/bazel_env.sh b/tools/bazel_env.sh new file mode 100755 index 000000000..2198475af --- /dev/null +++ b/tools/bazel_env.sh @@ -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 + +$@