aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main
diff options
context:
space:
mode:
authorGravatar László Csomor <laszlocsomor@google.com>2017-03-23 15:09:34 +0000
committerGravatar Yue Gan <yueg@google.com>2017-03-23 17:53:39 +0000
commitfa27b5045df54394a07e22e247767e9ecca57b38 (patch)
tree69387e319dc5b55cb30dde3ea9303d0615028007 /src/main
parentf3991e880468047269e3c6cf213ad693cef679b6 (diff)
Bazel client, Windows: implement GetHomeDir
Create a method in blaze_util_<platform> to retrieve the path to the home dir ($HOME on Linux/macOS, %USERPROFILE% on Windows), where we look for the user's bazelrc file (".bazelrc"). -- Change-Id: I86be1dbe1f992ad55eb09b496024754099d54912 Reviewed-on: https://cr.bazel.build/9513 PiperOrigin-RevId: 151004759 MOS_MIGRATED_REVID=151004759
Diffstat (limited to 'src/main')
-rw-r--r--src/main/cpp/BUILD2
-rw-r--r--src/main/cpp/blaze_util_platform.h4
-rw-r--r--src/main/cpp/blaze_util_posix.cc2
-rw-r--r--src/main/cpp/blaze_util_windows.cc26
-rw-r--r--src/main/cpp/option_processor.cc2
5 files changed, 30 insertions, 6 deletions
diff --git a/src/main/cpp/BUILD b/src/main/cpp/BUILD
index 05a53c5ff4..632fd791a3 100644
--- a/src/main/cpp/BUILD
+++ b/src/main/cpp/BUILD
@@ -52,6 +52,8 @@ cc_library(
],
"//src:windows_msvc": [
"-Wl,advapi32.lib", # GetUserNameW
+ "-Wl,ole32.lib", # CoTaskMemFree
+ "-Wl,shell32.lib", # SHGetKnownFolderPath
"-Wl,ws2_32.lib", # grpc
],
"//conditions:default": [
diff --git a/src/main/cpp/blaze_util_platform.h b/src/main/cpp/blaze_util_platform.h
index 2f313a4eeb..f158b58a30 100644
--- a/src/main/cpp/blaze_util_platform.h
+++ b/src/main/cpp/blaze_util_platform.h
@@ -56,6 +56,10 @@ std::string GetSelfPath();
// Returns the directory Bazel can use to store output.
std::string GetOutputRoot();
+// Returns the current user's home directory, or the empty string if unknown.
+// On Linux/macOS, this is $HOME. On Windows this is %USERPROFILE%.
+std::string GetHomeDir();
+
// Returns the location of the global bazelrc file if it exists, otherwise "".
std::string FindSystemWideBlazerc();
diff --git a/src/main/cpp/blaze_util_posix.cc b/src/main/cpp/blaze_util_posix.cc
index 99070d47f8..dab5f0ab64 100644
--- a/src/main/cpp/blaze_util_posix.cc
+++ b/src/main/cpp/blaze_util_posix.cc
@@ -126,6 +126,8 @@ string GetProcessIdAsString() {
return ToString(getpid());
}
+string GetHomeDir() { return GetEnv("HOME"); }
+
string FindSystemWideBlazerc() {
string path = "/etc/bazel.bazelrc";
if (blaze_util::CanReadFile(path)) {
diff --git a/src/main/cpp/blaze_util_windows.cc b/src/main/cpp/blaze_util_windows.cc
index e859da31bf..dbc5e32a6e 100644
--- a/src/main/cpp/blaze_util_windows.cc
+++ b/src/main/cpp/blaze_util_windows.cc
@@ -24,14 +24,17 @@
#include <sys/stat.h>
#include <sys/statfs.h>
#include <unistd.h>
-#endif // not COMPILER_MSVC
+#endif // COMPILER_MSVC
-#include <windows.h>
#include <lmcons.h> // UNLEN
+#include <windows.h>
#ifdef COMPILER_MSVC
-#include <io.h> // _open
-#endif // COMPILER_MSVC
+#include <io.h> // _open
+#include <knownfolders.h> // FOLDERID_Profile
+#include <objbase.h> // CoTaskMemFree
+#include <shlobj.h> // SHGetKnownFolderPath
+#endif
#include <algorithm>
#include <cstdio>
@@ -334,9 +337,22 @@ string GetOutputRoot() {
#endif // COMPILER_MSVC
}
+string GetHomeDir() {
+#ifdef COMPILER_MSVC
+ PWSTR wpath;
+ if (SUCCEEDED(::SHGetKnownFolderPath(FOLDERID_Profile, KF_FLAG_DEFAULT, NULL,
+ &wpath))) {
+ string result = string(blaze_util::WstringToCstring(wpath).get());
+ ::CoTaskMemFree(wpath);
+ return result;
+ }
+#endif
+ return GetEnv("HOME"); // only defined in MSYS/Cygwin
+}
+
string FindSystemWideBlazerc() {
#ifdef COMPILER_MSVC
- // TODO(bazel-team): implement this.
+ // TODO(bazel-team): figure out a good path to return here.
return "";
#else // not COMPILER_MSVC
string path = "/etc/bazel.bazelrc";
diff --git a/src/main/cpp/option_processor.cc b/src/main/cpp/option_processor.cc
index f025c3069d..66a09e3266 100644
--- a/src/main/cpp/option_processor.cc
+++ b/src/main/cpp/option_processor.cc
@@ -283,7 +283,7 @@ blaze_exit_code::ExitCode OptionProcessor::FindUserBlazerc(
return blaze_exit_code::SUCCESS;
}
- string home = blaze::GetEnv("HOME");
+ string home = blaze::GetHomeDir();
if (home.empty()) {
*blaze_rc_file = "";
return blaze_exit_code::SUCCESS;