diff options
author | cushon <cushon@google.com> | 2018-05-14 01:51:45 -0700 |
---|---|---|
committer | Copybara-Service <copybara-piper@google.com> | 2018-05-14 01:53:21 -0700 |
commit | 849df36c5ad31ebe8791c4228321c38c6d0ae56c (patch) | |
tree | cc39afc4e667214b4aac69217fb697166c96ddd4 /src/main/cpp | |
parent | 5c3f5c9be7fa40d4fb3c35756891fab8483ca406 (diff) |
Use the local JDK as the default target javabase
and continue to use the embedded JDK as the default host_javabase.
PiperOrigin-RevId: 196471714
Diffstat (limited to 'src/main/cpp')
-rw-r--r-- | src/main/cpp/blaze.cc | 1 | ||||
-rw-r--r-- | src/main/cpp/blaze_util_darwin.cc | 12 | ||||
-rw-r--r-- | src/main/cpp/blaze_util_freebsd.cc | 2 | ||||
-rw-r--r-- | src/main/cpp/blaze_util_linux.cc | 11 | ||||
-rw-r--r-- | src/main/cpp/blaze_util_platform.h | 7 | ||||
-rw-r--r-- | src/main/cpp/blaze_util_windows.cc | 5 | ||||
-rw-r--r-- | src/main/cpp/startup_options.cc | 38 | ||||
-rw-r--r-- | src/main/cpp/startup_options.h | 7 |
8 files changed, 41 insertions, 42 deletions
diff --git a/src/main/cpp/blaze.cc b/src/main/cpp/blaze.cc index 6c9aea3895..331c4c212b 100644 --- a/src/main/cpp/blaze.cc +++ b/src/main/cpp/blaze.cc @@ -505,6 +505,7 @@ static vector<string> GetArgumentArray( blaze::ConvertPath(globals->options->output_base)); result.push_back("--workspace_directory=" + blaze::ConvertPath(globals->workspace)); + result.push_back("--default_system_javabase=" + GetSystemJavabase()); if (!globals->options->server_jvm_out.empty()) { result.push_back("--server_jvm_out=" + globals->options->server_jvm_out); diff --git a/src/main/cpp/blaze_util_darwin.cc b/src/main/cpp/blaze_util_darwin.cc index 4bc3990eab..c925e2e3fb 100644 --- a/src/main/cpp/blaze_util_darwin.cc +++ b/src/main/cpp/blaze_util_darwin.cc @@ -159,7 +159,7 @@ bool IsSharedLibrary(const string &filename) { return blaze_util::ends_with(filename, ".dylib"); } -string GetDefaultHostJavabase() { +string GetSystemJavabase() { string java_home = GetEnv("JAVA_HOME"); if (!java_home.empty()) { return java_home; @@ -167,23 +167,19 @@ string GetDefaultHostJavabase() { FILE *output = popen("/usr/libexec/java_home -v 1.7+", "r"); if (output == NULL) { - BAZEL_DIE(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR) - << "Could not run /usr/libexec/java_home: " << GetLastErrorString(); + return ""; } char buf[512]; char *result = fgets(buf, sizeof(buf), output); pclose(output); if (result == NULL) { - BAZEL_DIE(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR) - << "No output from /usr/libexec/java_home"; + return ""; } string javabase = buf; if (javabase.empty()) { - BAZEL_DIE(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR) - << "Empty output from /usr/libexec/java_home - install a JDK, or " - "install a JRE and point your JAVA_HOME to it"; + return ""; } // The output ends with a \n, trim it off. diff --git a/src/main/cpp/blaze_util_freebsd.cc b/src/main/cpp/blaze_util_freebsd.cc index 4b27b4287d..0d196827d8 100644 --- a/src/main/cpp/blaze_util_freebsd.cc +++ b/src/main/cpp/blaze_util_freebsd.cc @@ -143,7 +143,7 @@ bool IsSharedLibrary(const string &filename) { return blaze_util::ends_with(filename, ".so"); } -string GetDefaultHostJavabase() { +string GetSystemJavabase() { // if JAVA_HOME is defined, then use it as default. string javahome = GetEnv("JAVA_HOME"); return !javahome.empty() ? javahome : "/usr/local/openjdk8"; diff --git a/src/main/cpp/blaze_util_linux.cc b/src/main/cpp/blaze_util_linux.cc index ba0320d1c2..4f01ba833b 100644 --- a/src/main/cpp/blaze_util_linux.cc +++ b/src/main/cpp/blaze_util_linux.cc @@ -146,8 +146,7 @@ bool IsSharedLibrary(const string &filename) { static string Which(const string &executable) { string path(GetEnv("PATH")); if (path.empty()) { - BAZEL_DIE(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR) - << "Could not get PATH to find " << executable; + return ""; } vector<string> pieces = blaze_util::Split(path, ':'); @@ -167,7 +166,7 @@ static string Which(const string &executable) { return ""; } -string GetDefaultHostJavabase() { +string GetSystemJavabase() { // if JAVA_HOME is defined, then use it as default. string javahome = GetEnv("JAVA_HOME"); if (!javahome.empty()) { @@ -177,15 +176,13 @@ string GetDefaultHostJavabase() { // which javac string javac_dir = Which("javac"); if (javac_dir.empty()) { - BAZEL_DIE(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR) - << "Could not find javac"; + return ""; } // Resolve all symlinks. char resolved_path[PATH_MAX]; if (realpath(javac_dir.c_str(), resolved_path) == NULL) { - BAZEL_DIE(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR) - << "Could not resolve javac directory: " << GetLastErrorString(); + return ""; } javac_dir = resolved_path; diff --git a/src/main/cpp/blaze_util_platform.h b/src/main/cpp/blaze_util_platform.h index 50ac21933f..6de5eb154a 100644 --- a/src/main/cpp/blaze_util_platform.h +++ b/src/main/cpp/blaze_util_platform.h @@ -84,9 +84,10 @@ std::string GetProcessCWD(int pid); bool IsSharedLibrary(const std::string& filename); -// Return the default path to the JDK used to run Blaze itself -// (must be an absolute directory). -std::string GetDefaultHostJavabase(); +// Returns the absolute path to the user's local JDK install, to be used as +// the default target javabase and as a fall-back host_javabase. This is not +// the embedded JDK. +std::string GetSystemJavabase(); // Return the path to the JVM binary relative to a javabase, e.g. "bin/java". std::string GetJavaBinaryUnderJavabase(); diff --git a/src/main/cpp/blaze_util_windows.cc b/src/main/cpp/blaze_util_windows.cc index efd0d72c59..565e8876c2 100644 --- a/src/main/cpp/blaze_util_windows.cc +++ b/src/main/cpp/blaze_util_windows.cc @@ -250,11 +250,10 @@ bool IsSharedLibrary(const string &filename) { return blaze_util::ends_with(filename, ".dll"); } -string GetDefaultHostJavabase() { +string GetSystemJavabase() { string javahome(GetEnv("JAVA_HOME")); if (javahome.empty()) { - BAZEL_DIE(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR) - << "Error: JAVA_HOME not set."; + return ""; } return javahome; } diff --git a/src/main/cpp/startup_options.cc b/src/main/cpp/startup_options.cc index 1f9bc8b324..f2cc25dea0 100644 --- a/src/main/cpp/startup_options.cc +++ b/src/main/cpp/startup_options.cc @@ -392,30 +392,34 @@ blaze_exit_code::ExitCode StartupOptions::ProcessArgs( return blaze_exit_code::SUCCESS; } -string StartupOptions::GetDefaultHostJavabase() const { - return blaze::GetDefaultHostJavabase(); +string StartupOptions::GetSystemJavabase() const { + return blaze::GetSystemJavabase(); } string StartupOptions::GetHostJavabase() { // 1) Allow overriding the host_javabase via --host_javabase. - if (host_javabase.empty()) { - if (default_host_javabase.empty()) { - string bundled_jre_path = blaze_util::JoinPath( - install_base, "_embedded_binaries/embedded_tools/jdk"); - if (blaze_util::CanExecuteFile(blaze_util::JoinPath( - bundled_jre_path, GetJavaBinaryUnderJavabase()))) { - // 2) Use a bundled JVM if we have one. - default_host_javabase = bundled_jre_path; - } else { - // 3) Otherwise fall back to using the default system JVM. - default_host_javabase = GetDefaultHostJavabase(); + if (!host_javabase.empty()) { + return host_javabase; + } + if (default_host_javabase.empty()) { + string bundled_jre_path = blaze_util::JoinPath( + install_base, "_embedded_binaries/embedded_tools/jdk"); + if (blaze_util::CanExecuteFile(blaze_util::JoinPath( + bundled_jre_path, GetJavaBinaryUnderJavabase()))) { + // 2) Use a bundled JVM if we have one. + default_host_javabase = bundled_jre_path; + } else { + // 3) Otherwise fall back to using the default system JVM. + string system_javabase = GetSystemJavabase(); + if (system_javabase.empty()) { + BAZEL_DIE(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR) + << "Could not find system javabase. Ensure JAVA_HOME is set, or " + "javac is on your PATH."; } + default_host_javabase = system_javabase; } - - return default_host_javabase; - } else { - return host_javabase; } + return default_host_javabase; } string StartupOptions::GetExplicitHostJavabase() const { diff --git a/src/main/cpp/startup_options.h b/src/main/cpp/startup_options.h index 91a4ab7fd8..242a383fa3 100644 --- a/src/main/cpp/startup_options.h +++ b/src/main/cpp/startup_options.h @@ -133,9 +133,10 @@ class StartupOptions { const char *arg, const char *next_arg, const std::string &rcfile, const char **value, bool *is_processed, std::string *error) = 0; - // Return the default path to the JDK used to run Blaze itself - // (must be an absolute directory). - virtual std::string GetDefaultHostJavabase() const; + // Returns the absolute path to the user's local JDK install, to be used as + // the default target javabase and as a fall-back host_javabase. This is not + // the embedded JDK. + virtual std::string GetSystemJavabase() const; // Returns the path to the JVM. This should be called after parsing // the startup options. |