Skip to content

Commit 2d04c91

Browse files
hvadehrabenjaminp
andauthored
[6.2.0] Add version to JavaRuntimeInfo (bazelbuild#17913)
* Add version to JavaRuntimeInfo. 1. As suggested in https://github.com/bazelbuild/bazel/issues/6354#issuecomment-1310489402, add a JDK version attribute to the java_runtime rule. This will allow changing behavior based on the Java runtime's version. 2. As an application of the above, pass -Djava.security.manager=allow to Java tests on JDK 17+. This makes the Bazel Java test runner to work on JDK 19. Fixes bazelbuild#14502. 3. To make the above generally useful, the remote_java_repository and local_java_repository must set the new version attribute in the java_runtime rules that they create. This means the old static jdk.BUILD file no longer suffices. Move the contents of jdk.BUILD into a Starlark file, so the version can be interpolated in by JDK repository rules and macros. (This isn't the nicest, but local_java_repository is not a repository rule, so the template cannot be in a non-Starlark file.) Closes bazelbuild#17775. PiperOrigin-RevId: 518860040 Change-Id: I8223b6407dd09528a4e5a6bf12354e5fc68278c6 (cherry picked from commit 7556e11) * Use string.replace() instead of string.format() to avoid issues with select{} in the template string * Remove test and fix formatting * Fix starlark_repository_test Change the grep pattern to exactly match what the test is looking for. The old pattern would match BUILD file content from the jdk_build_file.bzl template --------- Co-authored-by: Benjamin Peterson <[email protected]>
1 parent 3ea18cc commit 2d04c91

File tree

16 files changed

+83
-20
lines changed

16 files changed

+83
-20
lines changed

src/main/java/com/google/devtools/build/lib/bazel/rules/java/BazelJavaSemantics.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
import com.google.devtools.build.lib.rules.java.JavaConfiguration.OneVersionEnforcementLevel;
5555
import com.google.devtools.build.lib.rules.java.JavaHelper;
5656
import com.google.devtools.build.lib.rules.java.JavaRuleOutputJarsProvider;
57+
import com.google.devtools.build.lib.rules.java.JavaRuntimeInfo;
5758
import com.google.devtools.build.lib.rules.java.JavaSemantics;
5859
import com.google.devtools.build.lib.rules.java.JavaSourceJarsProvider;
5960
import com.google.devtools.build.lib.rules.java.JavaTargetAttributes;
@@ -532,6 +533,9 @@ public Iterable<String> getJvmFlags(
532533
if (testClass == null) {
533534
ruleContext.ruleError("cannot determine test class");
534535
} else {
536+
if (JavaRuntimeInfo.from(ruleContext).version() >= 17) {
537+
jvmFlags.add("-Djava.security.manager=allow");
538+
}
535539
// Always run junit tests with -ea (enable assertion)
536540
jvmFlags.add("-ea");
537541
// "suite" is a misnomer.

src/main/java/com/google/devtools/build/lib/bazel/rules/java/jdk.WORKSPACE.tmpl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
# External dependencies for the java_* rules.
22
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
33
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")
4+
load("@bazel_tools//tools/jdk:jdk_build_file.bzl", "JDK_BUILD_TEMPLATE")
45
load("@bazel_tools//tools/jdk:local_java_repository.bzl", "local_java_repository")
56
load("@bazel_tools//tools/jdk:remote_java_repository.bzl", "remote_java_repository")
67

78
maybe(
89
local_java_repository,
910
name = "local_jdk",
1011
java_home = DEFAULT_SYSTEM_JAVABASE,
11-
build_file = "@bazel_tools//tools/jdk:jdk.BUILD",
12+
build_file_content = JDK_BUILD_TEMPLATE,
1213
)
1314

1415
# OpenJDK distributions that should only be downloaded on demand (e.g. when

src/main/java/com/google/devtools/build/lib/rules/java/JavaRuntime.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package com.google.devtools.build.lib.rules.java;
1616

1717
import static com.google.common.base.Preconditions.checkNotNull;
18+
import static com.google.devtools.build.lib.packages.Type.INTEGER;
1819

1920
import com.google.common.collect.ImmutableList;
2021
import com.google.common.collect.ImmutableMap;
@@ -116,7 +117,8 @@ public ConfiguredTarget create(RuleContext ruleContext)
116117
javaBinaryRunfilesPath,
117118
hermeticInputs,
118119
libModules,
119-
hermeticStaticLibs);
120+
hermeticStaticLibs,
121+
ruleContext.attributes().get("version", INTEGER).toIntUnchecked());
120122

121123
TemplateVariableInfo templateVariableInfo =
122124
new TemplateVariableInfo(

src/main/java/com/google/devtools/build/lib/rules/java/JavaRuntimeInfo.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ public static JavaRuntimeInfo create(
5151
PathFragment javaBinaryRunfilesPath,
5252
NestedSet<Artifact> hermeticInputs,
5353
@Nullable Artifact libModules,
54-
ImmutableList<CcInfo> hermeticStaticLibs) {
54+
ImmutableList<CcInfo> hermeticStaticLibs,
55+
int version) {
5556
return new JavaRuntimeInfo(
5657
javaBaseInputs,
5758
javaHome,
@@ -60,7 +61,8 @@ public static JavaRuntimeInfo create(
6061
javaBinaryRunfilesPath,
6162
hermeticInputs,
6263
libModules,
63-
hermeticStaticLibs);
64+
hermeticStaticLibs,
65+
version);
6466
}
6567

6668
@Override
@@ -124,6 +126,7 @@ private static JavaRuntimeInfo from(RuleContext ruleContext, ToolchainInfo toolc
124126
private final NestedSet<Artifact> hermeticInputs;
125127
@Nullable private final Artifact libModules;
126128
private final ImmutableList<CcInfo> hermeticStaticLibs;
129+
private final int version;
127130

128131
private JavaRuntimeInfo(
129132
NestedSet<Artifact> javaBaseInputs,
@@ -133,7 +136,8 @@ private JavaRuntimeInfo(
133136
PathFragment javaBinaryRunfilesPath,
134137
NestedSet<Artifact> hermeticInputs,
135138
@Nullable Artifact libModules,
136-
ImmutableList<CcInfo> hermeticStaticLibs) {
139+
ImmutableList<CcInfo> hermeticStaticLibs,
140+
int version) {
137141
this.javaBaseInputs = javaBaseInputs;
138142
this.javaHome = javaHome;
139143
this.javaBinaryExecPath = javaBinaryExecPath;
@@ -142,6 +146,7 @@ private JavaRuntimeInfo(
142146
this.hermeticInputs = hermeticInputs;
143147
this.libModules = libModules;
144148
this.hermeticStaticLibs = hermeticStaticLibs;
149+
this.version = version;
145150
}
146151

147152
/** All input artifacts in the javabase. */
@@ -223,6 +228,11 @@ public Depset starlarkJavaBaseInputs() {
223228
return Depset.of(Artifact.TYPE, javaBaseInputs());
224229
}
225230

231+
@Override
232+
public int version() {
233+
return version;
234+
}
235+
226236
@Override
227237
public com.google.devtools.build.lib.packages.Provider getProvider() {
228238
return PROVIDER;

src/main/java/com/google/devtools/build/lib/rules/java/JavaRuntimeRule.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import static com.google.devtools.build.lib.packages.BuildType.LABEL;
1919
import static com.google.devtools.build.lib.packages.BuildType.LABEL_LIST;
2020
import static com.google.devtools.build.lib.packages.BuildType.LICENSE;
21+
import static com.google.devtools.build.lib.packages.Type.INTEGER;
2122
import static com.google.devtools.build.lib.packages.Type.STRING;
2223

2324
import com.google.devtools.build.lib.analysis.BaseRuleClasses;
@@ -72,6 +73,11 @@ public RuleClass build(RuleClass.Builder builder, RuleDefinitionEnvironment env)
7273
<!-- #END_BLAZE_RULE.ATTRIBUTE --> */
7374
.add(attr("java_home", STRING))
7475
.add(attr("output_licenses", LICENSE))
76+
/* <!-- #BLAZE_RULE(java_runtime).ATTRIBUTE(version) -->
77+
The feature version of the Java runtime. I.e., the integer returned by
78+
<code>Runtime.version().feature()</code>.
79+
<!-- #END_BLAZE_RULE.ATTRIBUTE --> */
80+
.add(attr("version", INTEGER))
7581
.build();
7682
}
7783

src/main/java/com/google/devtools/build/lib/starlarkbuildapi/java/JavaRuntimeInfoApi.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,11 @@ public interface JavaRuntimeInfoApi extends StructApi {
9595
doc = "Returns the JDK static libraries.",
9696
structField = true)
9797
Sequence<CcInfo> starlarkHermeticStaticLibs();
98+
99+
/** The Java feature version of the runtime. This is 0 if the version is unknown. */
100+
@StarlarkMethod(
101+
name = "version",
102+
doc = "The Java feature version of the runtime. This is 0 if the version is unknown.",
103+
structField = true)
104+
int version();
98105
}

src/test/java/com/google/devtools/build/lib/analysis/mock/BazelAnalysisMock.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,7 @@ public void setupMockToolsRepository(MockToolsConfig config) throws IOException
578578
"",
579579
"def http_jar(**kwargs):",
580580
" pass");
581+
config.create("embedded_tools/tools/jdk/jdk_build_file.bzl", "JDK_BUILD_TEMPLATE = ''");
581582
config.create(
582583
"embedded_tools/tools/jdk/local_java_repository.bzl",
583584
"def local_java_repository(**kwargs):",

src/test/java/com/google/devtools/build/lib/rules/java/JavaRuntimeInfoTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ public void equalityIsObjectIdentity() {
3737
PathFragment.create(""),
3838
NestedSetBuilder.emptySet(Order.STABLE_ORDER),
3939
null,
40-
ImmutableList.of());
40+
ImmutableList.of(),
41+
17);
4142
JavaRuntimeInfo b =
4243
JavaRuntimeInfo.create(
4344
NestedSetBuilder.emptySet(Order.STABLE_ORDER),
@@ -47,7 +48,8 @@ public void equalityIsObjectIdentity() {
4748
PathFragment.create(""),
4849
NestedSetBuilder.emptySet(Order.STABLE_ORDER),
4950
null,
50-
ImmutableList.of());
51+
ImmutableList.of(),
52+
17);
5153

5254
new EqualsTester().addEqualityGroup(a).addEqualityGroup(b).testEquals();
5355
}

src/test/java/com/google/devtools/build/lib/skyframe/packages/BazelPackageLoaderTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ private static void mockEmbeddedTools(Path embeddedBinaries) throws IOException
106106
" if name not in native.existing_rules():",
107107
" repo_rule(name = name, **kwargs)");
108108
FileSystemUtils.writeIsoLatin1(tools.getRelative("tools/jdk/BUILD"));
109+
FileSystemUtils.writeIsoLatin1(
110+
tools.getRelative("tools/jdk/jdk_build_file.bzl"), "JDK_BUILD_TEMPLATE = ''");
109111
FileSystemUtils.writeIsoLatin1(
110112
tools.getRelative("tools/jdk/local_java_repository.bzl"),
111113
"def local_java_repository(**kwargs):",

src/test/py/bazel/query_test.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,10 @@ def testSimpleQuery(self):
4040

4141
def testQueryFilesUsedByRepositoryRules(self):
4242
self.ScratchFile('WORKSPACE')
43-
self._AssertQueryOutputContains("kind('source file', deps(//external:*))",
44-
'@bazel_tools//tools/jdk:jdk.BUILD')
43+
self._AssertQueryOutputContains(
44+
"kind('source file', deps(//external:*))",
45+
'@bazel_tools//tools/genrule:genrule-setup.sh',
46+
)
4547

4648
def testBuildFilesForExternalRepos_Simple(self):
4749
self.ScratchFile('WORKSPACE', [

0 commit comments

Comments
 (0)