Skip to content

Commit

Permalink
Merge java_plugin_info.bzl into java_info.bzl
Browse files Browse the repository at this point in the history
This is rather unfortunate but avoiding a `load` cycle has become impossible. Implementing `java_common.merge()` in Starlark will need access to the `_new_javainfo` raw constructor which can only be returned by the `provider()` call for `JavaInfo`. `JavaPluginInfo` needs _some_ function for merging `JavaInfo`s which will require said raw constructor. So if `JavaInfo` depends on `JavaPluginInfo`, we get the `JavaPluginInfo`->`JavaInfo`->`JavaPluginInfo` cycle.

If you tilt your head to the left and squint in just the right way, you can see this change captures the class relationship in Java-land where `JavaInfoApi extends JavaPluginInfoApi`.

PiperOrigin-RevId: 545993901
Change-Id: I1ad9c0f94564acb991205757afdd39c9e617f4dd
  • Loading branch information
hvadehra authored and copybara-github committed Jul 6, 2023
1 parent eadb984 commit 6a23ac4
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 154 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,7 @@ public Provider getProvider() {
public static class Provider extends StarlarkProviderWrapper<JavaPluginInfo>
implements com.google.devtools.build.lib.packages.Provider {
private Provider() {
super(
Label.parseCanonicalUnchecked("@_builtins//:common/java/java_plugin_info.bzl"),
PROVIDER_NAME);
super(Label.parseCanonicalUnchecked("@_builtins//:common/java/java_info.bzl"), PROVIDER_NAME);
}

@Override
Expand Down
3 changes: 1 addition & 2 deletions src/main/starlark/builtins_bzl/common/exports.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ load("@_builtins//:common/cc/cc_toolchain_alias.bzl", "cc_toolchain_alias")
load("@_builtins//:common/cc/cc_common.bzl", "cc_common")
load("@_builtins//:common/cc/cc_helper.bzl", "cc_helper")
load(":common/objc/objc_common.bzl", "objc_common")
load(":common/java/java_plugin_info.bzl", "JavaPluginInfo")
load(":common/java/java_info.bzl", "JavaInfo")
load(":common/java/java_info.bzl", "JavaInfo", "JavaPluginInfo")
load(":common/java/java_common.bzl", "java_common")

exported_toplevels = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ load(":common/java/compile_action.bzl", "compile_action")
load(":common/java/java_semantics.bzl", "semantics")
load(":common/java/proguard_validation.bzl", "validate_proguard_specs")
load(":common/cc/cc_info.bzl", "CcInfo")
load(":common/java/java_info.bzl", "JavaInfo")
load(":common/java/java_plugin_info.bzl", "JavaPluginInfo")
load(":common/java/java_info.bzl", "JavaInfo", "JavaPluginInfo")
load(":common/java/java_common.bzl", "java_common")
load(":common/java/java_common_internal_for_builtins.bzl", "target_kind")

Expand Down
3 changes: 1 addition & 2 deletions src/main/starlark/builtins_bzl/common/java/java_binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ load(":common/cc/semantics.bzl", cc_semantics = "semantics")
load(":common/proto/proto_info.bzl", "ProtoInfo")
load(":common/cc/cc_info.bzl", "CcInfo")
load(":common/paths.bzl", "paths")
load(":common/java/java_info.bzl", "JavaInfo", "to_java_binary_info")
load(":common/java/java_plugin_info.bzl", "JavaPluginInfo")
load(":common/java/java_info.bzl", "JavaInfo", "JavaPluginInfo", "to_java_binary_info")
load(":common/java/java_common.bzl", "java_common")
load(
":common/java/java_common_internal_for_builtins.bzl",
Expand Down
2 changes: 1 addition & 1 deletion src/main/starlark/builtins_bzl/common/java/java_common.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
load(
":common/java/java_info.bzl",
"JavaInfo",
"JavaPluginInfo",
_java_info_add_constraints = "add_constraints",
_java_info_make_non_strict = "make_non_strict",
_java_info_set_annotation_processing = "set_annotation_processing",
)
load(":common/java/message_bundle_info.bzl", "MessageBundleInfo")
load(":common/java/java_common_internal_for_builtins.bzl", "compile", "merge", "run_ijar")
load(":common/java/java_plugin_info.bzl", "JavaPluginInfo")
load(":common/java/java_semantics.bzl", "semantics")
load(":common/paths.bzl", "paths")
load(":common/java/java_helper.bzl", "helper")
Expand Down
127 changes: 122 additions & 5 deletions src/main/starlark/builtins_bzl/common/java/java_info.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
# limitations under the License.

"""
Definition of JavaInfo provider.
Definition of JavaInfo and JavaPluginInfo provider.
"""

load(":common/cc/cc_common.bzl", "cc_common")
load(":common/java/java_plugin_info.bzl", "EMPTY_PLUGIN_DATA", "merge_without_outputs")
load(":common/cc/cc_info.bzl", "CcInfo")
load(":common/java/java_common_internal_for_builtins.bzl", _merge_private_for_builtins = "merge")

# TODO(hvd): remove this when:
# - we have a general provider-type checking API
Expand Down Expand Up @@ -107,8 +107,8 @@ def to_java_binary_info(java_info):
"_transitive_full_compile_time_jars": depset(),
"_compile_time_java_dependencies": depset(),
"runtime_output_jars": [],
"plugins": EMPTY_PLUGIN_DATA,
"api_generating_plugins": EMPTY_PLUGIN_DATA,
"plugins": _EMPTY_PLUGIN_DATA,
"api_generating_plugins": _EMPTY_PLUGIN_DATA,
"module_flags_info": _ModuleFlagsInfo(add_exports = depset(), add_opens = depset()),
"_neverlink": False,
"_constraints": [],
Expand Down Expand Up @@ -305,7 +305,7 @@ def _javainfo_init(
_validate_provider_list(native_libraries, "native_libraries", CcInfo)

source_jars = [source_jar] if source_jar else []
plugin_info = merge_without_outputs(exported_plugins + exports)
plugin_info = _merge_plugin_info_without_outputs(exported_plugins + exports)
if neverlink:
transitive_runtime_jars = depset()
else:
Expand Down Expand Up @@ -489,3 +489,120 @@ JavaInfo, _new_javainfo = provider(
},
init = _javainfo_init,
)

_JavaPluginDataInfo = provider(
doc = "Provider encapsulating information about a Java compatible plugin.",
fields = {
"processor_classes": "depset(str) The fully qualified classnames of entry points for the compiler",
"processor_jars": "depset(file) Deps containing an annotation processor",
"processor_data": "depset(file) Files needed during execution",
},
)

_EMPTY_PLUGIN_DATA = _JavaPluginDataInfo(
processor_classes = depset(),
processor_jars = depset(),
processor_data = depset(),
)

def _merge_plugin_info_without_outputs(infos):
""" Merge plugin information from a list of JavaPluginInfo or JavaInfo
Args:
infos: ([JavaPluginInfo|JavaInfo]) list of providers to merge
Returns:
(JavaPluginInfo)
"""
plugins = []
api_generating_plugins = []
for info in infos:
if _has_plugin_data(info.plugins):
plugins.append(info.plugins)
if _has_plugin_data(info.api_generating_plugins):
api_generating_plugins.append(info.api_generating_plugins)
return _new_javaplugininfo(
plugins = _merge_plugin_data(plugins),
api_generating_plugins = _merge_plugin_data(api_generating_plugins),
java_outputs = [],
)

def _has_plugin_data(plugin_data):
return plugin_data and (
plugin_data.processor_classes or
plugin_data.processor_jars or
plugin_data.processor_data
)

def _merge_plugin_data(datas):
return _JavaPluginDataInfo(
processor_classes = depset(transitive = [p.processor_classes for p in datas]),
processor_jars = depset(transitive = [p.processor_jars for p in datas]),
processor_data = depset(transitive = [p.processor_data for p in datas]),
)

def _javaplugininfo_init(
runtime_deps,
processor_class,
data = [],
generates_api = False):
""" Constructs JavaPluginInfo
Args:
runtime_deps: ([JavaInfo]) list of deps containing an annotation
processor.
processor_class: (String) The fully qualified class name that the Java
compiler uses as an entry point to the annotation processor.
data: (depset[File]) The files needed by this annotation
processor during execution.
generates_api: (boolean) Set to true when this annotation processor
generates API code. Such an annotation processor is applied to a
Java target before producing its header jars (which contains method
signatures). When no API plugins are present, header jars are
generated from the sources, reducing the critical path.
WARNING: This parameter affects build performance, use it only if
necessary.
Returns:
(JavaPluginInfo)
"""

# we don't need the private API but java_common needs JavaPluginInfo which would be a cycle
java_infos = _merge_private_for_builtins(runtime_deps)
processor_data = data if type(data) == "depset" else depset(data)
plugins = _JavaPluginDataInfo(
processor_classes = depset([processor_class]) if processor_class else depset(),
processor_jars = java_infos.transitive_runtime_jars,
processor_data = processor_data,
)
return {
"plugins": plugins,
"api_generating_plugins": plugins if generates_api else _EMPTY_PLUGIN_DATA,
"java_outputs": java_infos.java_outputs,
}

JavaPluginInfo, _new_javaplugininfo = provider(
doc = "Provider encapsulating information about Java plugins.",
fields = {
"plugins": """
Returns data about all plugins that a consuming target should apply.
This is typically either a <code>java_plugin</code> itself or a
<code>java_library</code> exporting one or more plugins.
A <code>java_library</code> runs annotation processing with all
plugins from this field appearing in <code>deps</code> and
<code>plugins</code> attributes.""",
"api_generating_plugins": """
Returns data about API generating plugins defined or exported by
this target.
Those annotation processors are applied to a Java target before
producing its header jars (which contain method signatures). When
no API plugins are present, header jars are generated from the
sources, reducing critical path.
The <code>api_generating_plugins</code> is a subset of
<code>plugins</code>.""",
"java_outputs": """
Returns information about outputs of this Java/Java-like target.
""",
},
init = _javaplugininfo_init,
)
3 changes: 1 addition & 2 deletions src/main/starlark/builtins_bzl/common/java/java_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ load(":common/java/basic_java_library.bzl", "BASIC_JAVA_LIBRARY_IMPLICIT_ATTRS",
load(":common/rule_util.bzl", "merge_attrs")
load(":common/java/java_semantics.bzl", "semantics")
load(":common/cc/cc_info.bzl", "CcInfo")
load(":common/java/java_info.bzl", "JavaInfo")
load(":common/java/java_plugin_info.bzl", "JavaPluginInfo")
load(":common/java/java_info.bzl", "JavaInfo", "JavaPluginInfo")

def bazel_java_library_rule(
ctx,
Expand Down
2 changes: 1 addition & 1 deletion src/main/starlark/builtins_bzl/common/java/java_plugin.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ load(":common/java/basic_java_library.bzl", "basic_java_library", "construct_def
load(":common/java/java_library.bzl", "JAVA_LIBRARY_ATTRS", "JAVA_LIBRARY_IMPLICIT_ATTRS")
load(":common/rule_util.bzl", "merge_attrs")
load(":common/java/java_semantics.bzl", "semantics")
load(":common/java/java_plugin_info.bzl", "JavaPluginInfo")
load(":common/java/java_info.bzl", "JavaPluginInfo")

def bazel_java_plugin_rule(
ctx,
Expand Down
136 changes: 0 additions & 136 deletions src/main/starlark/builtins_bzl/common/java/java_plugin_info.bzl

This file was deleted.

0 comments on commit 6a23ac4

Please sign in to comment.