aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/rules/java/JavaRuntime.java
diff options
context:
space:
mode:
authorGravatar lberki <lberki@google.com>2017-07-07 03:53:36 -0400
committerGravatar John Cater <jcater@google.com>2017-07-07 07:08:50 -0400
commitb7584447cbc7fbea3afb9ae42baf78f262be1041 (patch)
treee68084619db8e0a77fa4e8a2f47ded50b16a002b /src/main/java/com/google/devtools/build/lib/rules/java/JavaRuntime.java
parent692f763fe7bfb36610a711a4ca05194db721a34b (diff)
Plumb some information about the location of the JVM through JavaRuntimeProvider instead of Jvm if a java_runtime rule is used.
There are a few things standing in the way of removing package loading from JvmConfigurationLoader: - The JAVABASE/JAVA Make variables, which are a function of the contents of the package with the JVM. The plan is to make JavaRuntime a MakeVariableProvider and add an attribute to rules that do Make variable evaluation to tell which Make variables they need - The path to the Java binary is exported to Skylark through the configuration fragment. We'll need to export an attribute to Skylark that can be used to depend on the JVM, then do the three-step dance to use that instead of the configuration and figure out what to do if --javabase is not a label. - Jvm#getJavaExecutable() has a bunch of call sites. They need to be adjusted individually. RELNOTES: None. PiperOrigin-RevId: 161176462
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/rules/java/JavaRuntime.java')
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/java/JavaRuntime.java43
1 files changed, 42 insertions, 1 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/JavaRuntime.java b/src/main/java/com/google/devtools/build/lib/rules/java/JavaRuntime.java
index abe3c9ac00..0d15e60e9f 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/java/JavaRuntime.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/java/JavaRuntime.java
@@ -25,16 +25,40 @@ import com.google.devtools.build.lib.analysis.RuleConfiguredTargetBuilder;
import com.google.devtools.build.lib.analysis.RuleContext;
import com.google.devtools.build.lib.analysis.Runfiles;
import com.google.devtools.build.lib.analysis.RunfilesProvider;
+import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.collect.nestedset.NestedSet;
import com.google.devtools.build.lib.rules.RuleConfiguredTargetFactory;
+import com.google.devtools.build.lib.syntax.Type;
+import com.google.devtools.build.lib.util.OsUtils;
+import com.google.devtools.build.lib.vfs.PathFragment;
/** Implementation for the {@code java_runtime} rule. */
public class JavaRuntime implements RuleConfiguredTargetFactory {
+ // TODO(lberki): This is incorrect but that what the Jvm configuration fragment did. We'd have the
+ // the ability to do better if we knew what OS the BuildConfiguration refers to.
+ private static final String BIN_JAVA = "bin/java" + OsUtils.executableExtension();
+
@Override
public ConfiguredTarget create(RuleContext ruleContext)
throws InterruptedException, RuleErrorException {
NestedSet<Artifact> filesToBuild =
PrerequisiteArtifacts.nestedSet(ruleContext, "srcs", Mode.TARGET);
+ PathFragment javaHome = defaultJavaHome(ruleContext.getLabel());
+ if (ruleContext.attributes().isAttributeValueExplicitlySpecified("java_home")) {
+ PathFragment javaHomeAttribute = PathFragment.create(
+ ruleContext.attributes().get("java_home", Type.STRING));
+ if (!filesToBuild.isEmpty() && javaHomeAttribute.isAbsolute()) {
+ ruleContext.ruleError("'java_home' with an absolute path requires 'srcs' to be empty.");
+ return null;
+ }
+
+ javaHome = javaHome.getRelative(javaHomeAttribute);
+ }
+
+ PathFragment javaBinaryExecPath = javaHome.getRelative(BIN_JAVA);
+ PathFragment javaBinaryRunfilesPath = getRunfilesJavaExecutable(
+ javaHome, ruleContext.getLabel());
+
NestedSet<Artifact> middleman =
CompilationHelper.getAggregatingMiddleman(
ruleContext, Actions.escapeLabel(ruleContext.getLabel()), filesToBuild);
@@ -46,8 +70,25 @@ public class JavaRuntime implements RuleConfiguredTargetFactory {
return new RuleConfiguredTargetBuilder(ruleContext)
.addProvider(RunfilesProvider.class, RunfilesProvider.simple(runfiles))
.setFilesToBuild(filesToBuild)
- .addProvider(JavaRuntimeProvider.class, JavaRuntimeProvider.create(filesToBuild))
+ .addProvider(JavaRuntimeProvider.class, JavaRuntimeProvider.create(
+ filesToBuild, javaHome, javaBinaryExecPath, javaBinaryRunfilesPath))
.addProvider(MiddlemanProvider.class, new MiddlemanProvider(middleman))
.build();
}
+
+ static PathFragment defaultJavaHome(Label javabase) {
+ if (javabase.getPackageIdentifier().getRepository().isDefault()) {
+ return javabase.getPackageFragment();
+ }
+ return javabase.getPackageIdentifier().getSourceRoot();
+ }
+
+ private static PathFragment getRunfilesJavaExecutable(PathFragment javaHome, Label javabase) {
+ if (javaHome.isAbsolute() || javabase.getPackageIdentifier().getRepository().isMain()) {
+ return javaHome.getRelative(BIN_JAVA);
+ } else {
+ return javabase.getPackageIdentifier().getRepository().getRunfilesPath()
+ .getRelative(BIN_JAVA);
+ }
+ }
}