Skip to content

Commit

Permalink
Set Java toolchain source and target version from target Java runtime.
Browse files Browse the repository at this point in the history
This functionality will enable Bazel to automatically produce target code for detected version of local JDK.

PiperOrigin-RevId: 347577778
  • Loading branch information
comius authored and copybara-github committed Dec 15, 2020
1 parent 909bec5 commit b1e9884
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.collect.nestedset.NestedSet;
import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
import com.google.devtools.build.lib.packages.Type;
import com.google.devtools.build.lib.util.OsUtils;
import com.google.devtools.build.lib.vfs.PathFragment;

Expand Down Expand Up @@ -102,6 +103,7 @@ public ConfiguredTarget create(RuleContext ruleContext)

JavaRuntimeInfo javaRuntime =
JavaRuntimeInfo.create(
ruleContext.attributes().get("version", Type.STRING),
filesToBuild,
middleman,
javaHome,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@
public final class JavaRuntimeInfo extends ToolchainInfo implements JavaRuntimeInfoApi {

public static JavaRuntimeInfo create(
String version,
NestedSet<Artifact> javaBaseInputs,
NestedSet<Artifact> javaBaseInputsMiddleman,
PathFragment javaHome,
PathFragment javaBinaryExecPath,
PathFragment javaHomeRunfilesPath,
PathFragment javaBinaryRunfilesPath) {
return new JavaRuntimeInfo(
version,
javaBaseInputs,
javaBaseInputsMiddleman,
javaHome,
Expand Down Expand Up @@ -81,6 +83,7 @@ private static JavaRuntimeInfo from(RuleContext ruleContext, String attributeNam
return (JavaRuntimeInfo) prerequisite.get(ToolchainInfo.PROVIDER);
}

private final String version;
private final NestedSet<Artifact> javaBaseInputs;
private final NestedSet<Artifact> javaBaseInputsMiddleman;
private final PathFragment javaHome;
Expand All @@ -91,13 +94,15 @@ private static JavaRuntimeInfo from(RuleContext ruleContext, String attributeNam
@AutoCodec.Instantiator
@VisibleForSerialization
JavaRuntimeInfo(
String version,
NestedSet<Artifact> javaBaseInputs,
NestedSet<Artifact> javaBaseInputsMiddleman,
PathFragment javaHome,
PathFragment javaBinaryExecPath,
PathFragment javaHomeRunfilesPath,
PathFragment javaBinaryRunfilesPath) {
super(ImmutableMap.of(), Location.BUILTIN);
this.version = version;
this.javaBaseInputs = javaBaseInputs;
this.javaBaseInputsMiddleman = javaBaseInputsMiddleman;
this.javaHome = javaHome;
Expand All @@ -106,6 +111,11 @@ private static JavaRuntimeInfo from(RuleContext ruleContext, String attributeNam
this.javaBinaryRunfilesPath = javaBinaryRunfilesPath;
}

/** Release version of the Java runtiem. */
public String version() {
return version;
}

/** All input artifacts in the javabase. */
public NestedSet<Artifact> javaBaseInputs() {
return javaBaseInputs;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ public RuleClass build(RuleClass.Builder builder, RuleDefinitionEnvironment env)
path. In that case, the <code>srcs</code> and <code>java</code> attributes must be empty.
<!-- #END_BLAZE_RULE.ATTRIBUTE --> */
.add(attr("java_home", STRING))
/* <!-- #BLAZE_RULE(java_runtime).ATTRIBUTE(version) -->
The release version of JDK, this is for example 8 for JDK 1.8.0, and 11 for JDK 11.
<!-- #END_BLAZE_RULE.ATTRIBUTE --> */
.add(attr("version", STRING))
.add(attr("output_licenses", LICENSE))
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,18 @@ public ConfiguredTarget create(RuleContext ruleContext)
private ImmutableList<String> getJavacOpts(RuleContext ruleContext) {
ImmutableList.Builder<String> javacopts = ImmutableList.builder();
String source = ruleContext.attributes().get("source_version", Type.STRING);
String target = ruleContext.attributes().get("target_version", Type.STRING);
if (!ruleContext.attributes().isAttributeValueExplicitlySpecified("source_version")
&& !ruleContext.attributes().isAttributeValueExplicitlySpecified("target_version")) {
JavaRuntimeInfo targetJavaRuntime =
(JavaRuntimeInfo)
ruleContext.getPrerequisite("$target_java_runtime", JavaRuntimeInfo.PROVIDER);
source = targetJavaRuntime.version();
target = source;
}
if (!isNullOrEmpty(source)) {
javacopts.add("-source").add(source);
}
String target = ruleContext.attributes().get("target_version", Type.STRING);
if (!isNullOrEmpty(target)) {
javacopts.add("-target").add(target);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import com.google.devtools.build.lib.analysis.config.ExecutionTransitionFactory;
import com.google.devtools.build.lib.analysis.config.transitions.NoTransition;
import com.google.devtools.build.lib.analysis.platform.ToolchainInfo;
import com.google.devtools.build.lib.packages.Attribute;
import com.google.devtools.build.lib.packages.AttributeMap;
import com.google.devtools.build.lib.packages.RuleClass;
import com.google.devtools.build.lib.util.FileTypeSet;
import java.util.List;
Expand Down Expand Up @@ -295,6 +297,22 @@ The Java target version (e.g., '6' or '7'). It specifies for which Java runtime
.mandatoryProviders(ToolchainInfo.PROVIDER.id())
.allowedFileTypes(FileTypeSet.ANY_FILE)
.useOutputLicenses())
.add(
attr("$target_java_runtime", LABEL)
.value(
new Attribute.ComputedDefault("source_version", "target_version") {
@Override
public Object getDefault(AttributeMap rule) {
if (!rule.isAttributeValueExplicitlySpecified("source_version")
&& !rule.isAttributeValueExplicitlySpecified("target_version")) {
return JavaSemantics.jvmAttribute(env);
}
return null;
}
})
.mandatoryProviders(ToolchainInfo.PROVIDER.id())
.allowedFileTypes(FileTypeSet.ANY_FILE)
.useOutputLicenses())
/* <!-- #BLAZE_RULE(java_toolchain).ATTRIBUTE(android_lint_runner) -->
Label of the Android Lint runner, if any.
<!-- #END_BLAZE_RULE.ATTRIBUTE --> */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class JavaRuntimeInfoTest {
public void equalityIsObjectIdentity() {
JavaRuntimeInfo a =
JavaRuntimeInfo.create(
/* version = */ "",
NestedSetBuilder.emptySet(Order.STABLE_ORDER),
NestedSetBuilder.emptySet(Order.STABLE_ORDER),
PathFragment.create(""),
Expand All @@ -37,6 +38,7 @@ public void equalityIsObjectIdentity() {
PathFragment.create(""));
JavaRuntimeInfo b =
JavaRuntimeInfo.create(
/* version = */ "",
NestedSetBuilder.emptySet(Order.STABLE_ORDER),
NestedSetBuilder.emptySet(Order.STABLE_ORDER),
PathFragment.create(""),
Expand Down

0 comments on commit b1e9884

Please sign in to comment.