Uppercase some hand-selected environment variable names on Windows/MSVC.

--
Change-Id: Ie1f3be6258f024d352ff4571a5355660f409f70d
Reviewed-on: https://cr.bazel.build/9291
PiperOrigin-RevId: 149470780
MOS_MIGRATED_REVID=149470780
diff --git a/src/main/cpp/option_processor.cc b/src/main/cpp/option_processor.cc
index d61587d..f025c30 100644
--- a/src/main/cpp/option_processor.cc
+++ b/src/main/cpp/option_processor.cc
@@ -459,6 +459,44 @@
   return blaze_exit_code::SUCCESS;
 }
 
+#if defined(COMPILER_MSVC)
+static void PreprocessEnvString(string* env_str) {
+  static std::set<string> vars_to_uppercase = {"PATH", "TMP", "TEMP", "TEMPDIR",
+                                               "SYSTEMROOT"};
+
+  int pos = env_str->find_first_of('=');
+  if (pos == string::npos) return;
+
+  string name = env_str->substr(0, pos);
+  // We do not care about locale. All variable names are ASCII.
+  std::transform(name.begin(), name.end(), name.begin(), ::toupper);
+  if (vars_to_uppercase.find(name) != vars_to_uppercase.end()) {
+    env_str->assign(name + "=" + env_str->substr(pos + 1));
+  }
+}
+
+#elif defined(__CYGWIN__)  // not defined(COMPILER_MSVC)
+
+static void PreprocessEnvString(string* env_str) {
+  int pos = env_str->find_first_of('=');
+  if (pos == string::npos) return;
+  string name = env_str->substr(0, pos);
+  if (name == "PATH") {
+    env_str->assign("PATH=" + ConvertPathList(env_str->substr(pos + 1)));
+  } else if (name == "TMP") {
+    // A valid Windows path "c:/foo" is also a valid Unix path list of
+    // ["c", "/foo"] so must use ConvertPath here. See GitHub issue #1684.
+    env_str->assign("TMP=" + ConvertPath(env_str->substr(pos + 1)));
+  }
+}
+
+#else  // Non-Windows platforms.
+
+static void PreprocessEnvString(const string* env_str) {
+  // do nothing.
+}
+#endif  // defined(COMPILER_MSVC)
+
 // Appends the command and arguments from argc/argv to the end of arg_vector,
 // and also splices in some additional terminal and environment options between
 // the command and the arguments. NB: Keep the options added here in sync with
@@ -498,17 +536,7 @@
   // Pass the client environment to the server.
   for (char** env = environ; *env != NULL; env++) {
     string env_str(*env);
-    int pos = env_str.find("=");
-    if (pos != string::npos) {
-      string name = env_str.substr(0, pos);
-      if (name == "PATH") {
-        env_str = "PATH=" + ConvertPathList(env_str.substr(pos + 1));
-      } else if (name == "TMP") {
-        // A valid Windows path "c:/foo" is also a valid Unix path list of
-        // ["c", "/foo"] so must use ConvertPath here. See GitHub issue #1684.
-        env_str = "TMP=" + ConvertPath(env_str.substr(pos + 1));
-      }
-    }
+    PreprocessEnvString(&env_str);
     command_arguments_.push_back("--client_env=" + env_str);
   }
   command_arguments_.push_back("--client_cwd=" + blaze::ConvertPath(cwd));