Make bootstrapping support a designated work dir

Change the bootstrapping process so that setting the BAZEL_WRKDIR
environment variable will tell the bootstrap process to mostly write
to that directory---apart from adding the symlinks next to the WORKSPACE
file. So setting this variable will avoid the usual writes to random
places in the file system (like /tmp and the user's home directory).

--
Change-Id: I9d1af747e75cc2a7bb1af08308acc9dfd927e920
Reviewed-on: https://bazel-review.googlesource.com/#/c/3963
MOS_MIGRATED_REVID=127075535
diff --git a/compile.sh b/compile.sh
index 4daeb3a..53fc412 100755
--- a/compile.sh
+++ b/compile.sh
@@ -189,7 +189,9 @@
       fi
     fi
   fi
-  $BAZEL --bazelrc=${BAZELRC} --nomaster_bazelrc test \
+  $BAZEL --bazelrc=${BAZELRC} --nomaster_bazelrc \
+      ${BAZEL_DIR_STARTUP_OPTIONS} \
+      test \
       --test_tag_filters="${BAZEL_TEST_FILTERS-}" \
       --build_tests_only \
       --nolegacy_bazel_java_test \
diff --git a/scripts/bootstrap/bootstrap.sh b/scripts/bootstrap/bootstrap.sh
index d11b6c5..e6143f7 100755
--- a/scripts/bootstrap/bootstrap.sh
+++ b/scripts/bootstrap/bootstrap.sh
@@ -54,7 +54,7 @@
   }
 else
   function bazel_build() {
-    ${BAZEL} --bazelrc=${BAZELRC} build \
+    ${BAZEL} --bazelrc=${BAZELRC} ${BAZEL_DIR_STARTUP_OPTIONS} build \
            ${BAZEL_ARGS-} \
            --verbose_failures \
            --javacopt="-source ${JAVA_VERSION} -target ${JAVA_VERSION}" \
@@ -89,9 +89,13 @@
     STRATEGY=
   fi
   [ -x "${BAZEL_BIN}" ] || fail "syntax: bootstrap bazel-binary"
-  run ${BAZEL_BIN} --nomaster_bazelrc --bazelrc=${BAZELRC} clean \
+  run ${BAZEL_BIN} --nomaster_bazelrc --bazelrc=${BAZELRC} \
+      ${BAZEL_DIR_STARTUP_OPTIONS} \
+      clean \
       --expunge || return $?
-  run ${BAZEL_BIN} --nomaster_bazelrc --bazelrc=${BAZELRC} build \
+  run ${BAZEL_BIN} --nomaster_bazelrc --bazelrc=${BAZELRC} \
+      ${BAZEL_DIR_STARTUP_OPTIONS} \
+      build \
       ${EXTRA_BAZEL_ARGS-} ${STRATEGY} \
       --fetch --nostamp \
       --define "JAVA_VERSION=${JAVA_VERSION}" \
diff --git a/scripts/bootstrap/buildenv.sh b/scripts/bootstrap/buildenv.sh
index 6efa355..6c851b0 100755
--- a/scripts/bootstrap/buildenv.sh
+++ b/scripts/bootstrap/buildenv.sh
@@ -18,6 +18,19 @@
 
 set -o errexit
 
+# If BAZEL_WRKDIR is set, default all variables to point into
+# that directory
+
+if [ -n "${BAZEL_WRKDIR}" ] ; then
+ mkdir -p "${BAZEL_WRKDIR}/tmp"
+ mkdir -p "${BAZEL_WRKDIR}/user_root"
+ : ${TMPDIR:=${BAZEL_WRKDIR}/tmp}
+ export TMPDIR
+ : ${BAZEL_DIR_STARTUP_OPTIONS:="--output_user_root=${BAZEL_WRKDIR}/user_root"}
+fi
+
+
+# Set standard variables
 DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
 WORKSPACE_DIR="$(dirname $(dirname ${DIR}))"
 
@@ -245,6 +258,6 @@
 
 # Return the target that a bind point to, using Bazel query.
 function get_bind_target() {
-  $BAZEL --bazelrc=${BAZELRC} --nomaster_bazelrc \
+  $BAZEL --bazelrc=${BAZELRC} --nomaster_bazelrc ${BAZEL_DIR_STARTUP_OPTIONS} \
     query "deps($1, 1) - $1"
 }
diff --git a/scripts/bootstrap/compile.sh b/scripts/bootstrap/compile.sh
index 2cb8e58..ff81f27 100755
--- a/scripts/bootstrap/compile.sh
+++ b/scripts/bootstrap/compile.sh
@@ -277,6 +277,7 @@
       --install_md5= \
       --workspace_directory=${PWD} \
       --nofatal_event_bus_exceptions \
+      ${BAZEL_DIR_STARTUP_OPTIONS} \
       build \
       --ignore_unsupported_sandboxing \
       --startup_time=329 --extract_data_time=523 \