Read $HOME first to determine the home directory (and when not present, fall back to getpwuid())

Also a minor compatibility fix in a sed invocation.

--
MOS_MIGRATED_REVID=106291639
diff --git a/scripts/bootstrap/compile.sh b/scripts/bootstrap/compile.sh
index f9605b5..1634cea 100755
--- a/scripts/bootstrap/compile.sh
+++ b/scripts/bootstrap/compile.sh
@@ -17,6 +17,7 @@
 # Script for building bazel from scratch without bazel
 
 PROTO_FILES=$(ls src/main/protobuf/*.proto)
+# TODO: which other library contains Guava?
 LIBRARY_JARS=$(find third_party -name '*.jar' | tr "\n" " ")
 DIRS=$(echo src/{java_tools/singlejar/java/com/google/devtools/build/zip,main/java,tools/xcode-common/java/com/google/devtools/build/xcode/{common,util}} ${OUTPUT_DIR}/src)
 
@@ -267,7 +268,7 @@
     local OBJ=$(basename "${FILE}").o
     FILES+=("${OUTPUT_DIR}/${OBJDIR}/${OBJ}")
   done
-  run_silent "${CXX}" -o ${OUTPUT} "${FILES[@]}" -lstdc++ ${LDFLAGS}
+  run_silent "${CXX}" -o ${OUTPUT} "${FILES[@]}" ${LDFLAGS}
 }
 
 function cc_build() {
@@ -320,12 +321,12 @@
   done
 
   log "Linking ${JNILIB}..."
-  run_silent "${CXX}" -o ${OUTPUT_DIR}/${JNILIB} $JNI_LD_ARGS -shared ${OUTPUT_DIR}/native/*.o -l stdc++
+  run_silent "${CXX}" -o ${OUTPUT_DIR}/${JNILIB} $JNI_LD_ARGS -shared ${OUTPUT_DIR}/native/*.o -lstdc++
 fi
 
 log "Compiling build-runfiles..."
 # Clang on Linux requires libstdc++
-run_silent "${CXX}" -o ${OUTPUT_DIR}/build-runfiles -std=c++0x src/main/tools/build-runfiles.cc -l stdc++
+run_silent "${CXX}" -o ${OUTPUT_DIR}/build-runfiles -std=c++0x src/main/tools/build-runfiles.cc -lstdc++
 
 log "Compiling process-wrapper..."
 run_silent "${CC}" -o ${OUTPUT_DIR}/process-wrapper -std=c99 src/main/tools/process-wrapper.c src/main/tools/process-tools.c -lm
diff --git a/src/BUILD b/src/BUILD
index adcaf26..21cb1f8 100644
--- a/src/BUILD
+++ b/src/BUILD
@@ -52,7 +52,7 @@
     outs = ["java.version"],
     cmd = """
           VERSION_LINE=$$(cat $< | grep target_version);
-          JAVA_VERSION=$$(echo $${VERSION_LINE} | sed -E 's/.*value="([^"])".*/\\1/');
+          JAVA_VERSION=$$(echo $${VERSION_LINE} | sed 's/.*value="\\([^"]\\)".*/\\1/');
           if [ -z "$${JAVA_VERSION}" ]; then
             echo "1.8" >$@  # Java 8 is the default
           elif [[ "$${JAVA_VERSION}" =~ ^[0-9]+$$ ]]; then
diff --git a/src/main/cpp/blaze_util_linux.cc b/src/main/cpp/blaze_util_linux.cc
index 08bd075..867fbb2 100644
--- a/src/main/cpp/blaze_util_linux.cc
+++ b/src/main/cpp/blaze_util_linux.cc
@@ -36,15 +36,25 @@
 
 string GetOutputRoot() {
   char buf[2048];
-  struct passwd pwbuf;
-  struct passwd *pw = NULL;
-  int uid = getuid();
-  int r = getpwuid_r(uid, &pwbuf, buf, 2048, &pw);
-  if (r != -1 && pw != NULL) {
-    return blaze_util::JoinPath(pw->pw_dir, ".cache/bazel");
+  string base;
+  const char* home = getenv("HOME");
+  if (home != NULL) {
+    base = home;
   } else {
-    return "/tmp";
+    struct passwd pwbuf;
+    struct passwd *pw = NULL;
+    int uid = getuid();
+    int r = getpwuid_r(uid, &pwbuf, buf, 2048, &pw);
+    if (r != -1 && pw != NULL) {
+      base = pw->pw_dir;
+    }
   }
+
+  if (base != "") {
+    return blaze_util::JoinPath(base, ".cache/bazel");
+  }
+
+  return "/tmp";
 }
 
 void WarnFilesystemType(const string& output_base) {