@@ -20,6 +20,7 @@ load("//java/common/rules:java_helper.bzl", "helper")
2020load ("//java/common/rules:java_toolchain.bzl" , "JavaToolchainInfo" )
2121load (
2222 ":java_info.bzl" ,
23+ "JavaInfo" ,
2324 "JavaPluginInfo" ,
2425 "disable_plugin_info_annotation_processing" ,
2526 "java_info_for_compilation" ,
@@ -165,7 +166,8 @@ def compile(
165166 include_compilation_info = True ,
166167 classpath_resources = [],
167168 resource_jars = [],
168- injecting_rule_kind = None ):
169+ injecting_rule_kind = None ,
170+ extra_args = []):
169171 """Compiles Java source files/jars from the implementation of a Starlark rule
170172
171173 The result is a provider that represents the results of the compilation and can be added to the
@@ -215,6 +217,7 @@ def compile(
215217 add_exports: ([str]) Allow this library to access the given <module>/<package>. Optional.
216218 add_opens: ([str]) Allow this library to reflectively access the given <module>/<package>.
217219 Optional.
220+ extra_args: (list[Args]) Additional args to pass to JavaBuilder. Optional.
218221
219222 Returns:
220223 (JavaInfo)
@@ -314,6 +317,32 @@ def compile(
314317 if uses_annotation_processing :
315318 generated_class_jar = _derive_output_file (ctx , output , name_suffix = "-gen" )
316319 generated_source_jar = _derive_output_file (ctx , output , name_suffix = "-gensrc" )
320+
321+ extra_args_list = list (extra_args )
322+ if semantics .create_compilation_action_supports_extra_args :
323+ repo_name = ctx .label .repo_name if hasattr (ctx .label , "repo_name" ) else ctx .label .workspace_name
324+ if not repo_name :
325+ resolved_unused_deps_mode = "off"
326+ for package_config in java_toolchain ._package_configuration :
327+ if package_config .matches (package_config .package_specs , ctx .label ):
328+ if hasattr (package_config , "unused_deps" ):
329+ resolved_unused_deps_mode = package_config .unused_deps
330+
331+ if resolved_unused_deps_mode == "error" and hasattr (ctx .attr , "deps" ):
332+ unused_deps_args = ctx .actions .args ()
333+ for dep in ctx .attr .deps :
334+ if JavaInfo in dep :
335+ if hasattr (dep [JavaInfo ], "java_outputs" ):
336+ for output_info in dep [JavaInfo ].java_outputs :
337+ dep_compile_jar = output_info .compile_jar if output_info .compile_jar else output_info .class_jar
338+ if dep_compile_jar :
339+ unused_deps_args .add ("--declared_dep" , dep_compile_jar , format = "%s::" + str (dep .label ))
340+ extra_args_list .append (unused_deps_args )
341+
342+ extra_compilation_action_args = {}
343+ if extra_args_list and semantics .create_compilation_action_supports_extra_args :
344+ extra_compilation_action_args ["extra_args" ] = extra_args_list
345+
317346 get_internal_java_common ().create_compilation_action (
318347 ctx ,
319348 java_toolchain ,
@@ -343,6 +372,7 @@ def compile(
343372 enable_direct_classpath ,
344373 annotation_processor_additional_inputs ,
345374 annotation_processor_additional_outputs ,
375+ ** extra_compilation_action_args
346376 )
347377
348378 create_output_source_jar = len (source_files ) > 0 or source_jars != [output_source_jar ]
0 commit comments