Added /etc/bazel.bazelrc as a global Bazel's master rc.

This will allow system-wide configuration for system-wide
installation of Bazel.

--
Change-Id: I71b7232e648f2690766c3b9184f863dc888524c0
Reviewed-on: https://bazel-review.googlesource.com/#/c/1540/
MOS_MIGRATED_REVID=95994630
diff --git a/compile.sh b/compile.sh
index 163eb5c..6bffcd2 100755
--- a/compile.sh
+++ b/compile.sh
@@ -153,7 +153,7 @@
 if [ $DO_TESTS ]; then
   new_step "Running tests"
   display "."
-  $BAZEL --blazerc=${BAZELRC} test \
+  $BAZEL --blazerc=${BAZELRC} --nomaster_blazerc test \
       --javacopt="-source ${JAVA_VERSION} -target ${JAVA_VERSION}" \
       -k --test_output=errors //src/... //third_party/ijar/... //scripts/... \
       || fail "Tests failed"
diff --git a/scripts/bootstrap/bootstrap.sh b/scripts/bootstrap/bootstrap.sh
index 9d346b7..17a577d 100755
--- a/scripts/bootstrap/bootstrap.sh
+++ b/scripts/bootstrap/bootstrap.sh
@@ -39,11 +39,13 @@
   if [[ ! ${BAZEL_SKIP_TOOL_COMPILATION-} =~ "$2" ]]; then
     log "Building $2"
     if [ -n "${4-}" ]; then
-      ${BAZEL} --blazerc=${BAZELRC} build ${BAZEL_ARGS} \
+      ${BAZEL} --nomaster_blazerc --blazerc=${BAZELRC} \
+          build ${BAZEL_ARGS} \
           --javacopt="-source ${JAVA_VERSION} -target ${JAVA_VERSION}" \
           "${EMBED_LABEL_ARG[@]}" $1
     else
-      run_silent ${BAZEL} --blazerc=${BAZELRC} build ${BAZEL_ARGS} \
+      run_silent ${BAZEL} --nomaster_blazerc --blazerc=${BAZELRC} \
+          build ${BAZEL_ARGS} \
           --javacopt="-source ${JAVA_VERSION} -target ${JAVA_VERSION}" \
           "${EMBED_LABEL_ARG[@]}" $1
     fi
@@ -74,8 +76,10 @@
   local BAZEL_BIN=$1
   local BAZEL_SUM=$2
   [ -x "${BAZEL_BIN}" ] || fail "syntax: bootstrap bazel-binary"
-  run_silent ${BAZEL_BIN} --blazerc=${BAZELRC} clean || return $?
-  run_silent ${BAZEL_BIN} --blazerc=${BAZELRC} build --fetch --nostamp \
+  run_silent ${BAZEL_BIN} --nomaster_blazerc --blazerc=${BAZELRC} clean \
+      || return $?
+  run_silent ${BAZEL_BIN} --nomaster_blazerc --blazerc=${BAZELRC} build \
+      --fetch --nostamp \
       --javacopt="-source ${JAVA_VERSION} -target ${JAVA_VERSION}" \
       //src:bazel //src:tools || return $?
   if [ -n "${BAZEL_SUM}" ]; then
diff --git a/src/main/cpp/blaze_startup_options.cc b/src/main/cpp/blaze_startup_options.cc
index 1af062d..6428e22 100644
--- a/src/main/cpp/blaze_startup_options.cc
+++ b/src/main/cpp/blaze_startup_options.cc
@@ -161,4 +161,8 @@
   candidates->push_back("tools/bazel.rc");
 }
 
+string BlazeStartupOptions::SystemWideRcPath() {
+  return "/etc/bazel.bazelrc";
+}
+
 }  // namespace blaze
diff --git a/src/main/cpp/blaze_startup_options.h b/src/main/cpp/blaze_startup_options.h
index b62878e..dd1850f 100644
--- a/src/main/cpp/blaze_startup_options.h
+++ b/src/main/cpp/blaze_startup_options.h
@@ -189,6 +189,9 @@
   // Returns the basename for the rc file.
   static string RcBasename();
 
+  // Returns the path for the system-wide rc file.
+  static string SystemWideRcPath();
+
   // Returns the search paths for the RC file in the workspace.
   static void WorkspaceRcFileSearchPath(std::vector<string>* candidates);
 
diff --git a/src/main/cpp/option_processor.cc b/src/main/cpp/option_processor.cc
index 18a00e7..c15b57c 100644
--- a/src/main/cpp/option_processor.cc
+++ b/src/main/cpp/option_processor.cc
@@ -183,6 +183,17 @@
 }
 
 
+// Return the path of the bazelrc file that sits in /etc.
+// This allows for installing Bazel on system-wide directory.
+string OptionProcessor::FindSystemWideBlazerc() {
+  string path = BlazeStartupOptions::SystemWideRcPath();
+  if (!path.empty() && !access(path.c_str(), R_OK)) {
+    return path;
+  }
+  return "";
+}
+
+
 // Return the path the the user rc file.  If cmdLineRcFile != NULL,
 // use it, dying if it is not readable.  Otherwise, return the first
 // readable file called rc_basename from [workspace, $HOME]
@@ -279,6 +290,15 @@
         return parse_exit_code;
       }
     }
+    string system_wide_blazerc = FindSystemWideBlazerc();
+    if (!system_wide_blazerc.empty()) {
+      blazercs_.push_back(new RcFile(system_wide_blazerc, blazercs_.size()));
+      blaze_exit_code::ExitCode parse_exit_code =
+          blazercs_.back()->Parse(&blazercs_, &rcoptions_, error);
+      if (parse_exit_code != blaze_exit_code::SUCCESS) {
+        return parse_exit_code;
+      }
+    }
   }
 
   string user_blazerc_path;
diff --git a/src/main/cpp/option_processor.h b/src/main/cpp/option_processor.h
index f5eb27e..37a946f 100644
--- a/src/main/cpp/option_processor.h
+++ b/src/main/cpp/option_processor.h
@@ -64,6 +64,7 @@
   virtual string FindDepotBlazerc(const string& workspace);
   virtual string FindAlongsideBinaryBlazerc(const string& cwd,
                                             const string& arg0);
+  virtual string FindSystemWideBlazerc();
   virtual blaze_exit_code::ExitCode FindUserBlazerc(const char* cmdLineRcFile,
                                                     const string& rc_basename,
                                                     const string& workspace,