Skip to content

Commit

Permalink
Expose compilation_info.javac_options_list for migration
Browse files Browse the repository at this point in the history
Once usages are migrated, `compilation_info.javac_options` can be changed to a `Depset`.

PiperOrigin-RevId: 560953670
Change-Id: I92c7a872358845d16a15ceeeb17bc7911edede54
  • Loading branch information
hvadehra authored and Copybara-Service committed Aug 29, 2023
1 parent 3faab8c commit ea154dc
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 2 deletions.
Expand Up @@ -32,6 +32,17 @@ public interface JavaCompilationInfoProviderApi<FileT extends FileApi> extends S
@StarlarkMethod(name = "javac_options", structField = true, doc = "Options to java compiler.")
ImmutableList<String> getJavacOpts();

@StarlarkMethod(
name = "javac_options_list",
structField = true,
doc =
"A list of options to java compiler. This exists temporarily for migration purposes. "
+ "javac_options will return a depset in the future, and this method will be dropped "
+ "once all usages have been updated to handle depsets.")
default ImmutableList<String> getJavacOptsList() {
return getJavacOpts();
}

@StarlarkMethod(
name = "runtime_classpath",
structField = true,
Expand Down
Expand Up @@ -159,7 +159,7 @@ def compile_action(
runfiles = [output_class_jar] if source_files or source_jars or resources else [],
# TODO(ilist): collect compile_jars from JavaInfo in deps & exports
compilation_classpath = java_info.compilation_info.compilation_classpath,
javac_options = java_info.compilation_info.javac_options,
javac_options = java_info.compilation_info.javac_options_list,
plugins = _collect_plugins(deps, plugins),
)

Expand Down
Expand Up @@ -314,6 +314,7 @@ def compile(

compilation_info = struct(
javac_options = all_javac_opts_list,
javac_options_list = all_javac_opts_list,
# needs to be flattened because the public API is a list
boot_classpath = (bootclasspath.bootclasspath if bootclasspath else java_toolchain.bootclasspath).to_list(),
# we only add compile time jars from deps, and not exports
Expand Down
7 changes: 7 additions & 0 deletions src/main/starlark/builtins_bzl/common/java/java_info.bzl
Expand Up @@ -82,6 +82,10 @@ _JavaCompilationInfo = provider(
fields = {
"boot_classpath": "Boot classpath for this Java target.",
"javac_options": "Options to the java compiler.",
"javac_options_list": """A list of options to java compiler. This exists
temporarily for migration purposes. javac_options will return a depset
in the future, and this method will be dropped once all usages have
been updated to handle depsets.""",
"compilation_classpath": "Compilation classpath for this Java target.",
"runtime_classpath": "Run-time classpath for this Java target.",
},
Expand All @@ -92,6 +96,7 @@ _EMPTY_COMPILATION_INFO = _JavaCompilationInfo(
runtime_classpath = depset(),
boot_classpath = None,
javac_options = [],
javac_options_list = [],
)

def merge(
Expand Down Expand Up @@ -228,6 +233,7 @@ def to_java_binary_info(java_info):
compilation_info = _JavaCompilationInfo(
boot_classpath = None,
javac_options = [],
javac_options_list = [],
compilation_classpath = java_info.transitive_compile_time_jars,
runtime_classpath = java_info.transitive_runtime_jars,
)
Expand Down Expand Up @@ -467,6 +473,7 @@ def java_info_for_compilation(
result.update(
compilation_info = _JavaCompilationInfo(
javac_options = _java_common_internal.intern_javac_opts(compilation_info.javac_options),
javac_options_list = _java_common_internal.intern_javac_opts(compilation_info.javac_options_list),
boot_classpath = compilation_info.boot_classpath,
compilation_classpath = compilation_info.compilation_classpath,
runtime_classpath = compilation_info.runtime_classpath,
Expand Down
Expand Up @@ -2166,7 +2166,7 @@ public void javaInfoGetCompilationInfoProvider() throws Exception {
scratch.file(
"foo/BUILD",
"load(':extension.bzl', 'my_rule')",
"java_library(name = 'my_java_lib_a', srcs = ['java/A.java'])",
"java_library(name = 'my_java_lib_a', srcs = ['java/A.java'], javacopts = ['opt1'])",
"my_rule(name = 'my_starlark_rule', dep = ':my_java_lib_a')");
assertNoEvents();
ConfiguredTarget myRuleTarget = getConfiguredTarget("//foo:my_starlark_rule");
Expand All @@ -2182,6 +2182,35 @@ public void javaInfoGetCompilationInfoProvider() throws Exception {
prettyArtifactNames(
javaCompilationInfoProvider.getRuntimeClasspath().getSet(Artifact.class)))
.containsExactly("foo/libmy_java_lib_a.jar");
assertThat(javaCompilationInfoProvider.getJavacOpts()).contains("opt1");
assertThat(javaCompilationInfoProvider.getJavacOptsList()).contains("opt1");
}

@Test
public void javaInfoStarlarkCompilationInfoJavacOpts() throws Exception {
scratch.file(
"foo/extension.bzl",
"result = provider()",
"def _impl(ctx):",
" return [result(property = ctx.attr.dep[JavaInfo].compilation_info.javac_options_list)]",
"my_rule = rule(_impl, attrs = { 'dep' : attr.label() })");
scratch.file(
"foo/BUILD",
"load(':extension.bzl', 'my_rule')",
"java_library(name = 'my_java_lib_a', srcs = ['java/A.java'], javacopts = ['opt1',"
+ " 'opt2'])",
"my_rule(name = 'my_starlark_rule', dep = ':my_java_lib_a')");
assertNoEvents();
ConfiguredTarget myRuleTarget = getConfiguredTarget("//foo:my_starlark_rule");

StructImpl info =
(StructImpl)
myRuleTarget.get(
new StarlarkProvider.Key(Label.parseCanonical("//foo:extension.bzl"), "result"));
Sequence<String> javacOptionsList =
Sequence.cast(info.getValue("property"), String.class, "javac_options_list");

assertThat(javacOptionsList).containsAtLeast("opt1", "opt2").inOrder();
}

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

0 comments on commit ea154dc

Please sign in to comment.