Skip to content

Commit

Permalink
Use the local JDK as the default target javabase
Browse files Browse the repository at this point in the history
and continue to use the embedded JDK as the default host_javabase.

PiperOrigin-RevId: 196471714
  • Loading branch information
cushon authored and Copybara-Service committed May 14, 2018
1 parent 5c3f5c9 commit 849df36
Show file tree
Hide file tree
Showing 51 changed files with 179 additions and 67 deletions.
1 change: 1 addition & 0 deletions scripts/bootstrap/compile.sh
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ function run_bazel_jar() {
--output_base=${OUTPUT_DIR}/out \
--output_user_root=${OUTPUT_DIR}/user_root \
--install_md5= \
--default_system_javabase="${JAVA_HOME}" \
--workspace_directory="$(get_cwd)" \
--nofatal_event_bus_exceptions \
${BAZEL_DIR_STARTUP_OPTIONS} \
Expand Down
5 changes: 1 addition & 4 deletions src/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -358,10 +358,7 @@ genrule(
],
outs = ["derived_java_srcs.zip"],
cmd = "$(location :combine_derived_java_srcs.sh) $(JAVABASE) $@ $(SRCS)",
# Technically, this should be prefixed with @bazel_tools, but in order to
# avoid having to wait a Bazel release which has this target under
# @bazel_tools, we use it from the main repository. It's the same anyway.
toolchains = ["@bazel_tools//tools/jdk:current_java_runtime"],
toolchains = ["@bazel_tools//tools/jdk:current_host_java_runtime"],
tools = ["combine_derived_java_srcs.sh"],
visibility = ["//:__pkg__"],
)
Expand Down
1 change: 1 addition & 0 deletions src/main/cpp/blaze.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 4 additions & 8 deletions src/main/cpp/blaze_util_darwin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -159,31 +159,27 @@ 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;
}

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.
Expand Down
2 changes: 1 addition & 1 deletion src/main/cpp/blaze_util_freebsd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
11 changes: 4 additions & 7 deletions src/main/cpp/blaze_util_linux.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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, ':');
Expand All @@ -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()) {
Expand All @@ -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;

Expand Down
7 changes: 4 additions & 3 deletions src/main/cpp/blaze_util_platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
5 changes: 2 additions & 3 deletions src/main/cpp/blaze_util_windows.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
38 changes: 21 additions & 17 deletions src/main/cpp/startup_options.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 4 additions & 3 deletions src/main/cpp/startup_options.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ public final class BlazeDirectories {
private final ServerDirectories serverDirectories;
/** Workspace root and server CWD. */
private final Path workspace;
/**
* The root of 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.
*/
private final Path defaultSystemJavabase;
/** The root of all build actions. */
private final Path execRoot;

Expand All @@ -62,9 +67,14 @@ public final class BlazeDirectories {
private final String productName;

@AutoCodec.Instantiator
public BlazeDirectories(ServerDirectories serverDirectories, Path workspace, String productName) {
public BlazeDirectories(
ServerDirectories serverDirectories,
Path workspace,
Path defaultSystemJavabase,
String productName) {
this.serverDirectories = serverDirectories;
this.workspace = workspace;
this.defaultSystemJavabase = defaultSystemJavabase;
this.productName = productName;
Path outputBase = serverDirectories.getOutputBase();
Path execRootBase = outputBase.getChild("execroot");
Expand Down Expand Up @@ -99,6 +109,11 @@ public Path getWorkspace() {
return workspace;
}

/** Returns the root of the user's local JDK install (not the embedded JDK). */
public Path getLocalJavabase() {
return defaultSystemJavabase;
}

/** Returns if the workspace directory is a valid workspace. */
public boolean inWorkspace() {
return this.workspace != null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

new_local_repository(
name = "local_jdk",
path = DEFAULT_SYSTEM_JAVABASE,
build_file = __embedded_dir__ + "/jdk.BUILD",
)

new_local_repository(
name = "embedded_jdk",
path = DEFAULT_SERVER_JAVABASE,
build_file = __embedded_dir__ + "/jdk.BUILD",
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,14 @@ public class WorkspaceFactory {
"__embedded_dir__", // serializable so optional
"__workspace_dir__", // serializable so optional
"DEFAULT_SERVER_JAVABASE", // serializable so optional
"DEFAULT_SYSTEM_JAVABASE", // serializable so optional
PackageFactory.PKG_CONTEXT);

private final Package.Builder builder;

private final Path installDir;
private final Path workspaceDir;
private final Path defaultSystemJavabaseDir;
private final Mutability mutability;

private final boolean allowOverride;
Expand Down Expand Up @@ -107,7 +109,7 @@ public WorkspaceFactory(
RuleClassProvider ruleClassProvider,
ImmutableList<EnvironmentExtension> environmentExtensions,
Mutability mutability) {
this(builder, ruleClassProvider, environmentExtensions, mutability, true, null, null);
this(builder, ruleClassProvider, environmentExtensions, mutability, true, null, null, null);
}

// TODO(bazel-team): document installDir
Expand All @@ -118,6 +120,7 @@ public WorkspaceFactory(
* @param mutability the Mutability for the current evaluation context
* @param installDir the install directory
* @param workspaceDir the workspace directory
* @param defaultSystemJavabaseDir the local JDK directory
*/
public WorkspaceFactory(
Package.Builder builder,
Expand All @@ -126,11 +129,13 @@ public WorkspaceFactory(
Mutability mutability,
boolean allowOverride,
@Nullable Path installDir,
@Nullable Path workspaceDir) {
@Nullable Path workspaceDir,
@Nullable Path defaultSystemJavabaseDir) {
this.builder = builder;
this.mutability = mutability;
this.installDir = installDir;
this.workspaceDir = workspaceDir;
this.defaultSystemJavabaseDir = defaultSystemJavabaseDir;
this.allowOverride = allowOverride;
this.environmentExtensions = environmentExtensions;
this.ruleFactory = new RuleFactory(ruleClassProvider, AttributeContainer::new);
Expand Down Expand Up @@ -534,6 +539,11 @@ private void addWorkspaceFunctions(Environment workspaceEnv, StoredEventHandler
javaHome = javaHome.getParentFile();
}
workspaceEnv.update("DEFAULT_SERVER_JAVABASE", javaHome.toString());
workspaceEnv.update(
"DEFAULT_SYSTEM_JAVABASE",
defaultSystemJavabaseDir != null
? defaultSystemJavabaseDir.toString()
: javaHome.toString());

for (EnvironmentExtension extension : environmentExtensions) {
extension.updateWorkspace(workspaceEnv);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1016,6 +1016,7 @@ private static BlazeRuntime newRuntime(Iterable<BlazeModule> blazeModules, List<
String productName = startupOptions.productName.toLowerCase(Locale.US);

PathFragment workspaceDirectory = startupOptions.workspaceDirectory;
PathFragment defaultSystemJavabase = startupOptions.defaultSystemJavabase;
PathFragment outputUserRoot = startupOptions.outputUserRoot;
PathFragment installBase = startupOptions.installBase;
PathFragment outputBase = startupOptions.outputBase;
Expand Down Expand Up @@ -1060,6 +1061,10 @@ private static BlazeRuntime newRuntime(Iterable<BlazeModule> blazeModules, List<
if (!workspaceDirectory.equals(PathFragment.EMPTY_FRAGMENT)) {
workspaceDirectoryPath = fs.getPath(workspaceDirectory);
}
Path defaultSystemJavabasePath = null;
if (!defaultSystemJavabase.equals(PathFragment.EMPTY_FRAGMENT)) {
defaultSystemJavabasePath = fs.getPath(defaultSystemJavabase);
}

ServerDirectories serverDirectories =
new ServerDirectories(
Expand Down Expand Up @@ -1097,7 +1102,8 @@ private static BlazeRuntime newRuntime(Iterable<BlazeModule> blazeModules, List<
BlazeRuntime runtime = runtimeBuilder.build();

BlazeDirectories directories =
new BlazeDirectories(serverDirectories, workspaceDirectoryPath, productName);
new BlazeDirectories(
serverDirectories, workspaceDirectoryPath, defaultSystemJavabasePath, productName);
BinTools binTools;
try {
binTools = BinTools.forProduction(directories);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,18 @@ public String getTypeDescription() {
)
public PathFragment workspaceDirectory;

@Option(
name = "default_system_javabase",
defaultValue = "", // NOTE: only for documentation, value is always passed by the client.
documentationCategory = OptionDocumentationCategory.UNDOCUMENTED,
effectTags = {OptionEffectTag.CHANGES_INPUTS, OptionEffectTag.LOSES_INCREMENTAL_STATE},
metadataTags = {OptionMetadataTag.HIDDEN},
converter = OptionsUtils.PathFragmentConverter.class,
help =
"The root of 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.")
public PathFragment defaultSystemJavabase;

@Option(
name = "max_idle_secs",
// NOTE: default value only used for documentation, value is always passed by the client when
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ public SkyValue compute(SkyKey skyKey, Environment env)
mutability,
key.getIndex() == 0,
directories.getEmbeddedBinariesRoot(),
directories.getWorkspace());
directories.getWorkspace(),
directories.getLocalJavabase());
if (key.getIndex() > 0) {
WorkspaceFileValue prevValue = (WorkspaceFileValue) env.getValue(
WorkspaceFileValue.key(key.getPath(), key.getIndex() - 1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,10 @@ protected Builder(Path workspaceDir, Path installBase, Path outputBase) {
Path devNull = workspaceDir.getFileSystem().getPath("/dev/null");
directories =
new BlazeDirectories(
new ServerDirectories(installBase, outputBase, devNull), workspaceDir, "blaze");
new ServerDirectories(installBase, outputBase, devNull),
workspaceDir,
/* defaultSystemJavabase= */ null,
"blaze");

this.pkgLocator =
new PathPackageLocator(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ public class FakeDirectories {

public static final BlazeDirectories BLAZE_DIRECTORIES =
new BlazeDirectories(
SERVER_DIRECTORIES, OUTPUT_BASE.getRelative("execroot/io_bazel"), "bazel");
SERVER_DIRECTORIES,
OUTPUT_BASE.getRelative("execroot/io_bazel"),
/* defaultSystemJavabase= */ null,
"bazel");

private FakeDirectories() {}
}
Loading

0 comments on commit 849df36

Please sign in to comment.