Skip to content

Commit ea154dc

Browse files
hvadehracopybara-github
authored andcommitted
Expose compilation_info.javac_options_list for migration
Once usages are migrated, `compilation_info.javac_options` can be changed to a `Depset`. PiperOrigin-RevId: 560953670 Change-Id: I92c7a872358845d16a15ceeeb17bc7911edede54
1 parent 3faab8c commit ea154dc

File tree

5 files changed

+50
-2
lines changed

5 files changed

+50
-2
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,17 @@ public interface JavaCompilationInfoProviderApi<FileT extends FileApi> extends S
3232
@StarlarkMethod(name = "javac_options", structField = true, doc = "Options to java compiler.")
3333
ImmutableList<String> getJavacOpts();
3434

35+
@StarlarkMethod(
36+
name = "javac_options_list",
37+
structField = true,
38+
doc =
39+
"A list of options to java compiler. This exists temporarily for migration purposes. "
40+
+ "javac_options will return a depset in the future, and this method will be dropped "
41+
+ "once all usages have been updated to handle depsets.")
42+
default ImmutableList<String> getJavacOptsList() {
43+
return getJavacOpts();
44+
}
45+
3546
@StarlarkMethod(
3647
name = "runtime_classpath",
3748
structField = true,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ def compile_action(
159159
runfiles = [output_class_jar] if source_files or source_jars or resources else [],
160160
# TODO(ilist): collect compile_jars from JavaInfo in deps & exports
161161
compilation_classpath = java_info.compilation_info.compilation_classpath,
162-
javac_options = java_info.compilation_info.javac_options,
162+
javac_options = java_info.compilation_info.javac_options_list,
163163
plugins = _collect_plugins(deps, plugins),
164164
)
165165

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,7 @@ def compile(
314314

315315
compilation_info = struct(
316316
javac_options = all_javac_opts_list,
317+
javac_options_list = all_javac_opts_list,
317318
# needs to be flattened because the public API is a list
318319
boot_classpath = (bootclasspath.bootclasspath if bootclasspath else java_toolchain.bootclasspath).to_list(),
319320
# we only add compile time jars from deps, and not exports

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ _JavaCompilationInfo = provider(
8282
fields = {
8383
"boot_classpath": "Boot classpath for this Java target.",
8484
"javac_options": "Options to the java compiler.",
85+
"javac_options_list": """A list of options to java compiler. This exists
86+
temporarily for migration purposes. javac_options will return a depset
87+
in the future, and this method will be dropped once all usages have
88+
been updated to handle depsets.""",
8589
"compilation_classpath": "Compilation classpath for this Java target.",
8690
"runtime_classpath": "Run-time classpath for this Java target.",
8791
},
@@ -92,6 +96,7 @@ _EMPTY_COMPILATION_INFO = _JavaCompilationInfo(
9296
runtime_classpath = depset(),
9397
boot_classpath = None,
9498
javac_options = [],
99+
javac_options_list = [],
95100
)
96101

97102
def merge(
@@ -228,6 +233,7 @@ def to_java_binary_info(java_info):
228233
compilation_info = _JavaCompilationInfo(
229234
boot_classpath = None,
230235
javac_options = [],
236+
javac_options_list = [],
231237
compilation_classpath = java_info.transitive_compile_time_jars,
232238
runtime_classpath = java_info.transitive_runtime_jars,
233239
)
@@ -467,6 +473,7 @@ def java_info_for_compilation(
467473
result.update(
468474
compilation_info = _JavaCompilationInfo(
469475
javac_options = _java_common_internal.intern_javac_opts(compilation_info.javac_options),
476+
javac_options_list = _java_common_internal.intern_javac_opts(compilation_info.javac_options_list),
470477
boot_classpath = compilation_info.boot_classpath,
471478
compilation_classpath = compilation_info.compilation_classpath,
472479
runtime_classpath = compilation_info.runtime_classpath,

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

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2166,7 +2166,7 @@ public void javaInfoGetCompilationInfoProvider() throws Exception {
21662166
scratch.file(
21672167
"foo/BUILD",
21682168
"load(':extension.bzl', 'my_rule')",
2169-
"java_library(name = 'my_java_lib_a', srcs = ['java/A.java'])",
2169+
"java_library(name = 'my_java_lib_a', srcs = ['java/A.java'], javacopts = ['opt1'])",
21702170
"my_rule(name = 'my_starlark_rule', dep = ':my_java_lib_a')");
21712171
assertNoEvents();
21722172
ConfiguredTarget myRuleTarget = getConfiguredTarget("//foo:my_starlark_rule");
@@ -2182,6 +2182,35 @@ public void javaInfoGetCompilationInfoProvider() throws Exception {
21822182
prettyArtifactNames(
21832183
javaCompilationInfoProvider.getRuntimeClasspath().getSet(Artifact.class)))
21842184
.containsExactly("foo/libmy_java_lib_a.jar");
2185+
assertThat(javaCompilationInfoProvider.getJavacOpts()).contains("opt1");
2186+
assertThat(javaCompilationInfoProvider.getJavacOptsList()).contains("opt1");
2187+
}
2188+
2189+
@Test
2190+
public void javaInfoStarlarkCompilationInfoJavacOpts() throws Exception {
2191+
scratch.file(
2192+
"foo/extension.bzl",
2193+
"result = provider()",
2194+
"def _impl(ctx):",
2195+
" return [result(property = ctx.attr.dep[JavaInfo].compilation_info.javac_options_list)]",
2196+
"my_rule = rule(_impl, attrs = { 'dep' : attr.label() })");
2197+
scratch.file(
2198+
"foo/BUILD",
2199+
"load(':extension.bzl', 'my_rule')",
2200+
"java_library(name = 'my_java_lib_a', srcs = ['java/A.java'], javacopts = ['opt1',"
2201+
+ " 'opt2'])",
2202+
"my_rule(name = 'my_starlark_rule', dep = ':my_java_lib_a')");
2203+
assertNoEvents();
2204+
ConfiguredTarget myRuleTarget = getConfiguredTarget("//foo:my_starlark_rule");
2205+
2206+
StructImpl info =
2207+
(StructImpl)
2208+
myRuleTarget.get(
2209+
new StarlarkProvider.Key(Label.parseCanonical("//foo:extension.bzl"), "result"));
2210+
Sequence<String> javacOptionsList =
2211+
Sequence.cast(info.getValue("property"), String.class, "javac_options_list");
2212+
2213+
assertThat(javacOptionsList).containsAtLeast("opt1", "opt2").inOrder();
21852214
}
21862215

21872216
/* Test inspired by {@link AbstractJavaLibraryConfiguredTargetTest#testNeverlink}.*/

0 commit comments

Comments
 (0)