Skip to content

Commit a54816a

Browse files
cushoncopybara-github
authored andcommitted
Allow tuning JavaBuilder JVM flags for individual targets
PiperOrigin-RevId: 587033579 Change-Id: I31b3022e94fce8115909e80c6c2a6b34363c42b3
1 parent de87119 commit a54816a

File tree

8 files changed

+42
-4
lines changed

8 files changed

+42
-4
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ public final class JavaCompilationHelper {
7373
private final JavaTargetAttributes.Builder attributes;
7474
private JavaTargetAttributes builtAttributes;
7575
private final ImmutableList<String> customJavacOpts;
76+
private NestedSet<String> javaBuilderJvmFlags = NestedSetBuilder.emptySet(Order.STABLE_ORDER);
7677
private final JavaSemantics semantics;
7778
private final ImmutableList<Artifact> additionalInputsForDatabinding;
7879
private final StrictDepsMode strictJavaDeps;
@@ -139,6 +140,10 @@ static ImmutableList<String> internJavacOpts(@Nullable ImmutableList<String> jav
139140
return javacOptsInterner.intern(javacOpts);
140141
}
141142

143+
public void javaBuilderJvmFlags(NestedSet<String> javaBuilderJvmFlags) {
144+
this.javaBuilderJvmFlags = javaBuilderJvmFlags;
145+
}
146+
142147
public void enableJspecify(boolean enableJspecify) {
143148
this.enableJspecify = enableJspecify;
144149
}
@@ -346,7 +351,8 @@ && getJavaConfiguration().experimentalEnableJspecify()
346351
}
347352
builder.setSourcePathEntries(attributes.getSourcePath());
348353
builder.setToolsJars(javaToolchain.getTools());
349-
builder.setJavaBuilder(javaToolchain.getJavaBuilder());
354+
builder.setJavaBuilder(
355+
javaToolchain.getJavaBuilder().withAdditionalJvmFlags(javaBuilderJvmFlags));
350356
if (!turbineAnnotationProcessing) {
351357
builder.setGenSourceOutput(outputs.genSource());
352358
builder.setAdditionalOutputs(attributes.getAdditionalOutputs());

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ public void createCompilationAction(
179179
Depset compileTimeClasspath,
180180
Depset directJars,
181181
Object bootClassPathUnchecked,
182+
Depset javaBuilderJvmFlags,
182183
Depset compileTimeJavaDeps,
183184
Depset javacOpts,
184185
String strictDepsMode,
@@ -249,6 +250,8 @@ public void createCompilationAction(
249250
JavaToolchainProvider.PROVIDER.wrap(javaToolchain),
250251
Sequence.cast(additionalInputs, Artifact.class, "additional_inputs")
251252
.getImmutableList());
253+
compilationHelper.javaBuilderJvmFlags(
254+
Depset.cast(javaBuilderJvmFlags, String.class, "javabuilder_jvm_flags"));
252255
compilationHelper.enableJspecify(enableJSpecify);
253256
compilationHelper.enableDirectClasspath(enableDirectClasspath);
254257
compilationHelper.createCompileAction(outputs);

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,17 @@ void addInputs(JavaToolchainProvider toolchain, NestedSetBuilder<Artifact> input
132132
inputs.addTransitive(toolchain.getJavaRuntime().javaBaseInputs());
133133
}
134134
}
135+
136+
public JavaToolchainTool withAdditionalJvmFlags(NestedSet<String> additionalJvmFlags) {
137+
if (additionalJvmFlags.isEmpty()) {
138+
return this;
139+
}
140+
return create(
141+
tool(),
142+
data(),
143+
NestedSetBuilder.<String>stableOrder()
144+
.addTransitive(jvmOpts())
145+
.addTransitive(additionalJvmFlags)
146+
.build());
147+
}
135148
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,7 @@ void createHeaderCompilationAction(
537537
@Param(name = "compilation_classpath"),
538538
@Param(name = "direct_jars"),
539539
@Param(name = "bootclasspath"),
540+
@Param(name = "javabuilder_jvm_flags"),
540541
@Param(name = "compile_time_java_deps"),
541542
@Param(name = "javac_opts"),
542543
@Param(name = "strict_deps_mode"),
@@ -566,6 +567,7 @@ void createCompilationAction(
566567
Depset compileTimeClasspath,
567568
Depset directJars,
568569
Object bootClassPath,
570+
Depset javabuilderJvmFlags,
569571
Depset compileTimeJavaDeps,
570572
Depset javacOpts,
571573
String strictDepsMode,

src/main/starlark/builtins_bzl/common/java/basic_java_library.bzl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ def basic_java_library(
7676
proguard_specs = None,
7777
add_exports = [],
7878
add_opens = [],
79-
bootclasspath = None):
79+
bootclasspath = None,
80+
javabuilder_jvm_flags = None):
8081
"""
8182
Creates actions that compile and lint Java sources, sets up coverage and returns JavaInfo, InstrumentedFilesInfo and output groups.
8283
@@ -111,6 +112,7 @@ def basic_java_library(
111112
add_exports: (list[str]) Allow this library to access the given <module>/<package>.
112113
add_opens: (list[str]) Allow this library to reflectively access the given <module>/<package>.
113114
bootclasspath: (Target) The JDK APIs to compile this library against.
115+
javabuilder_jvm_flags: (list[str]) Additional JVM flags to pass to JavaBuilder.
114116
Returns:
115117
(dict[str, Provider],
116118
{files_to_build: list[File],
@@ -150,6 +152,7 @@ def basic_java_library(
150152
add_exports = add_exports,
151153
add_opens = add_opens,
152154
bootclasspath = bootclasspath[BootClassPathInfo] if bootclasspath else None,
155+
javabuilder_jvm_flags = javabuilder_jvm_flags,
153156
)
154157
target = {"JavaInfo": java_info}
155158

src/main/starlark/builtins_bzl/common/java/compile_action.bzl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ def compile_action(
5757
enable_compile_jar_action = True,
5858
add_exports = [],
5959
add_opens = [],
60-
bootclasspath = None):
60+
bootclasspath = None,
61+
javabuilder_jvm_flags = None):
6162
"""
6263
Creates actions that compile Java sources, produce source jar, and produce header jar and returns JavaInfo.
6364
@@ -119,6 +120,7 @@ def compile_action(
119120
add_exports: (list[str]) Allow this library to access the given <module>/<package>.
120121
add_opens: (list[str]) Allow this library to reflectively access the given <module>/<package>.
121122
bootclasspath: (BootClassPathInfo) The set of JDK APIs to compile this library against.
123+
javabuilder_jvm_flags: (list[str]) Additional JVM flags to pass to JavaBuilder.
122124
123125
Returns:
124126
((JavaInfo, {files_to_build: list[File],
@@ -155,6 +157,7 @@ def compile_action(
155157
add_exports = add_exports,
156158
add_opens = add_opens,
157159
bootclasspath = bootclasspath,
160+
javabuilder_jvm_flags = javabuilder_jvm_flags,
158161
)
159162

160163
compilation_info = struct(

src/main/starlark/builtins_bzl/common/java/java_common_internal_for_builtins.bzl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ def compile(
4646
annotation_processor_additional_outputs = [],
4747
strict_deps = "ERROR",
4848
bootclasspath = None,
49+
javabuilder_jvm_flags = None,
4950
sourcepath = [],
5051
resources = [],
5152
add_exports = [],
@@ -89,6 +90,7 @@ def compile(
8990
'OFF', 'ERROR', 'WARN' and 'DEFAULT'.
9091
bootclasspath: (BootClassPathInfo) If present, overrides the bootclasspath associated with
9192
the provided java_toolchain. Optional.
93+
javabuilder_jvm_flags: (list[str]) Additional JVM flags to pass to JavaBuilder.
9294
sourcepath: ([File])
9395
resources: ([File])
9496
resource_jars: ([File])
@@ -241,6 +243,7 @@ def compile(
241243
compilation_classpath,
242244
direct_jars,
243245
bootclasspath,
246+
depset(javabuilder_jvm_flags),
244247
compile_time_java_deps,
245248
all_javac_opts,
246249
strict_deps,

src/main/starlark/builtins_bzl/common/java/java_library.bzl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ def bazel_java_library_rule(
3838
proguard_specs = [],
3939
add_exports = [],
4040
add_opens = [],
41-
bootclasspath = None):
41+
bootclasspath = None,
42+
javabuilder_jvm_flags = None):
4243
"""Implements java_library.
4344
4445
Use this call when you need to produce a fully fledged java_library from
@@ -60,6 +61,7 @@ def bazel_java_library_rule(
6061
add_exports: (list[str]) Allow this library to access the given <module>/<package>.
6162
add_opens: (list[str]) Allow this library to reflectively access the given <module>/<package>.
6263
bootclasspath: (Target) The JDK APIs to compile this library against.
64+
javabuilder_jvm_flags: (list[str]) Additional JVM flags to pass to JavaBuilder.
6365
Returns:
6466
(dict[str, provider]) A list containing DefaultInfo, JavaInfo,
6567
InstrumentedFilesInfo, OutputGroupsInfo, ProguardSpecProvider providers.
@@ -84,6 +86,7 @@ def bazel_java_library_rule(
8486
add_exports = add_exports,
8587
add_opens = add_opens,
8688
bootclasspath = bootclasspath,
89+
javabuilder_jvm_flags = javabuilder_jvm_flags,
8790
)
8891

8992
target["DefaultInfo"] = construct_defaultinfo(
@@ -114,6 +117,7 @@ def _proxy(ctx):
114117
ctx.attr.add_exports,
115118
ctx.attr.add_opens,
116119
ctx.attr.bootclasspath,
120+
ctx.attr.javabuilder_jvm_flags,
117121
).values()
118122

119123
JAVA_LIBRARY_IMPLICIT_ATTRS = BASIC_JAVA_LIBRARY_IMPLICIT_ATTRS
@@ -165,6 +169,7 @@ JAVA_LIBRARY_ATTRS = merge_attrs(
165169
providers = [BootClassPathInfo],
166170
flags = ["SKIP_CONSTRAINTS_OVERRIDE"],
167171
),
172+
"javabuilder_jvm_flags": attr.string_list(),
168173
"javacopts": attr.string_list(),
169174
"neverlink": attr.bool(),
170175
"resource_strip_prefix": attr.string(),

0 commit comments

Comments
 (0)