Skip to content

Commit

Permalink
Preserve correct JavaInfo.compilation_info for java_binary/`java_…
Browse files Browse the repository at this point in the history
…test` targets

Using `java_common.merge()` to include the generated extension registry outputs causes us to lose `compilation_info`. So we retain a reference before merge, create the right provider instance if needed and pass it to `to_java_binary_info`.

PiperOrigin-RevId: 580169649
Change-Id: Ifd06ddc1f833cc35a4a96308aa88710773f427c4
  • Loading branch information
hvadehra authored and copybara-github committed Nov 7, 2023
1 parent 804c503 commit 2052c63
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 25 deletions.
12 changes: 10 additions & 2 deletions src/main/starlark/builtins_bzl/common/java/java_binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ load(
"get_runtime_classpath_for_archive",
)
load(":common/java/java_helper.bzl", "helper")
load(":common/java/java_info.bzl", "JavaInfo", "JavaPluginInfo", "to_java_binary_info")
load(":common/java/java_info.bzl", "JavaCompilationInfo", "JavaInfo", "JavaPluginInfo", "to_java_binary_info")
load(":common/java/java_semantics.bzl", "semantics")
load(":common/paths.bzl", "paths")
load(":common/proto/proto_info.bzl", "ProtoInfo")
Expand Down Expand Up @@ -127,6 +127,7 @@ def basic_java_binary(
add_opens = ctx.attr.add_opens,
)
java_info = target["JavaInfo"]
compilation_info = java_info.compilation_info
runtime_classpath = depset(
order = "preorder",
transitive = [
Expand All @@ -149,6 +150,13 @@ def basic_java_binary(
),
],
)
compilation_info = JavaCompilationInfo(
compilation_classpath = compilation_info.compilation_classpath,
runtime_classpath = runtime_classpath,
boot_classpath = compilation_info.boot_classpath,
javac_options = compilation_info.javac_options,
javac_options_list = compilation_info.javac_options_list,
)

java_attrs = _collect_attrs(ctx, runtime_classpath, classpath_resources)

Expand Down Expand Up @@ -264,7 +272,7 @@ def basic_java_binary(

_filter_validation_output_group(ctx, output_groups)

java_binary_info = to_java_binary_info(java_info)
java_binary_info = to_java_binary_info(java_info, compilation_info)

default_info = struct(
files = files,
Expand Down
27 changes: 4 additions & 23 deletions src/main/starlark/builtins_bzl/common/java/java_info.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ _JavaGenJarsInfo = provider(
},
)

_JavaCompilationInfo = provider(
JavaCompilationInfo = provider(
doc = "Compilation information in Java rules, for perusal of aspects and tools.",
fields = {
"boot_classpath": "Boot classpath for this Java target.",
Expand All @@ -91,14 +91,6 @@ _JavaCompilationInfo = provider(
},
)

_EMPTY_COMPILATION_INFO = _JavaCompilationInfo(
compilation_classpath = depset(),
runtime_classpath = depset(),
boot_classpath = None,
javac_options = [],
javac_options_list = [],
)

def merge(
providers,
# private to @_builtins:
Expand Down Expand Up @@ -196,12 +188,12 @@ def merge(
)
return _java_common_internal.wrap_java_info(_new_javainfo(**result))

def to_java_binary_info(java_info):
def to_java_binary_info(java_info, compilation_info):
"""Get a copy of the given JavaInfo with minimal info returned by a java_binary
Args:
java_info: (JavaInfo) A JavaInfo provider instance
compilation_info: (JavaCompilationInfo)
Returns:
(JavaInfo) A JavaInfo instance representing a java_binary target
"""
Expand All @@ -226,17 +218,6 @@ def to_java_binary_info(java_info):
if hasattr(java_info, "cc_link_params_info"):
result.update(cc_link_params_info = java_info.cc_link_params_info)

compilation_info = _EMPTY_COMPILATION_INFO
if java_info.compilation_info:
compilation_info = java_info.compilation_info
elif java_info.transitive_compile_time_jars or java_info.transitive_runtime_jars:
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,
)
result["compilation_info"] = compilation_info

java_outputs = [
Expand Down Expand Up @@ -471,7 +452,7 @@ def java_info_for_compilation(
)
if compilation_info:
result.update(
compilation_info = _JavaCompilationInfo(
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,
Expand Down

0 comments on commit 2052c63

Please sign in to comment.