Skip to content
9 changes: 9 additions & 0 deletions java/common/rules/java_package_configuration.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ JavaPackageConfigurationInfo = provider(
"matches",
"package_specs",
"system",
"unused_deps",
],
)

Expand All @@ -50,6 +51,7 @@ def _rule_impl(ctx):
matches = _matches,
package_specs = package_specs,
system = system,
unused_deps = ctx.attr.unused_deps,
),
]

Expand Down Expand Up @@ -116,6 +118,13 @@ The list of files needed by this configuration at runtime.
providers = [BootClassPathInfo],
doc = """
Corresponds to javac's --system flag.
""",
),
"unused_deps": attr.string(
default = "off",
values = ["off", "error"],
doc = """
Unused dependencies checking mode.
""",
),
# buildifier: disable=attr-licenses
Expand Down
31 changes: 29 additions & 2 deletions java/private/java_common_internal.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ load("//java/common/rules:java_helper.bzl", "helper")
load("//java/common/rules:java_toolchain.bzl", "JavaToolchainInfo")
load(
":java_info.bzl",
"JavaInfo",
"JavaPluginInfo",
"disable_plugin_info_annotation_processing",
"java_info_for_compilation",
Expand Down Expand Up @@ -165,7 +166,8 @@ def compile(
include_compilation_info = True,
classpath_resources = [],
resource_jars = [],
injecting_rule_kind = None):
injecting_rule_kind = None,
extra_args = []):
"""Compiles Java source files/jars from the implementation of a Starlark rule

The result is a provider that represents the results of the compilation and can be added to the
Expand Down Expand Up @@ -215,6 +217,7 @@ def compile(
add_exports: ([str]) Allow this library to access the given <module>/<package>. Optional.
add_opens: ([str]) Allow this library to reflectively access the given <module>/<package>.
Optional.
extra_args: (list[Args]) Additional args to pass to JavaBuilder. Optional.

Returns:
(JavaInfo)
Expand Down Expand Up @@ -314,7 +317,30 @@ def compile(
if uses_annotation_processing:
generated_class_jar = _derive_output_file(ctx, output, name_suffix = "-gen")
generated_source_jar = _derive_output_file(ctx, output, name_suffix = "-gensrc")
get_internal_java_common().create_compilation_action(

extra_args_list = list(extra_args)
unused_deps_args = ctx.actions.args()
resolved_unused_deps_mode = "off"
internal_common = get_internal_java_common()
repo_name = ctx.label.repo_name if hasattr(ctx.label, "repo_name") else ctx.label.workspace_name
if not repo_name:
for package_config in java_toolchain._package_configuration:
matched = package_config.matches(package_config.package_specs, ctx.label)
if matched:
if hasattr(package_config, "unused_deps"):
resolved_unused_deps_mode = package_config.unused_deps

if resolved_unused_deps_mode == "error" and hasattr(ctx.attr, "deps"):
for dep in ctx.attr.deps:
if JavaInfo in dep:
if hasattr(dep[JavaInfo], "java_outputs"):
for output_info in dep[JavaInfo].java_outputs:
dep_compile_jar = output_info.compile_jar if output_info.compile_jar else output_info.class_jar
if dep_compile_jar:
unused_deps_args.add("--declared_dep", dep_compile_jar, format = "%s::" + str(dep.label))
extra_args_list.append(unused_deps_args)

internal_common.create_compilation_action(
ctx,
java_toolchain,
output,
Expand Down Expand Up @@ -343,6 +369,7 @@ def compile(
enable_direct_classpath,
annotation_processor_additional_inputs,
annotation_processor_additional_outputs,
extra_args = extra_args_list,
)

create_output_source_jar = len(source_files) > 0 or source_jars != [output_source_jar]
Expand Down