Skip to content

Commit

Permalink
Expose used_jars.json in a provider
Browse files Browse the repository at this point in the history
Summary:
- used-jars.json files are used to remove unused dependencies from android/java/kotlin targets
- exposing them in a provider (JavaLibraryIntellijInfo) allows reading and ensuring them in BXL

Reviewed By: IanChilds

Differential Revision: D68282757

fbshipit-source-id: 4a201636a256cf78058df3f6b8132e8e855f4deb
  • Loading branch information
Arsen Tumanyan authored and facebook-github-bot committed Jan 17, 2025
1 parent ac35246 commit 7c2931f
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 5 deletions.
1 change: 1 addition & 0 deletions prelude/java/java_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,7 @@ def build_java_library(
sources_jar = sources_jar,
gwt_module = gwt_output,
preprocessed_library = outputs.preprocessed_library if outputs else None,
used_jars_json = outputs.used_jars_json if outputs else None,
)

default_info = get_default_info(
Expand Down
10 changes: 8 additions & 2 deletions prelude/java/java_providers.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ JavaLibraryIntellijInfo = provider(
# If this library has a jar_postprocessor, this is the jar prior to post-processing.
# Otherwise, it is the same as library_output in JavaLibraryInfo.
"preprocessed_library": provider_field(typing.Any, default = None), # ["artifact", None]
"used_jars_json": provider_field(typing.Any, default = None), # ["artifact"]
},
)

Expand Down Expand Up @@ -237,6 +238,7 @@ JavaCompileOutputs = record(
annotation_processor_output = Artifact | None,
preprocessed_library = Artifact,
incremental_state_dir = Artifact | None,
used_jars_json = Artifact | None,
)

JavaProviders = record(
Expand Down Expand Up @@ -281,7 +283,8 @@ def make_compile_outputs(
required_for_source_only_abi: bool = False,
annotation_processor_output: Artifact | None = None,
incremental_state_dir: Artifact | None = None,
abi_jar_snapshot: Artifact | None = None) -> JavaCompileOutputs:
abi_jar_snapshot: Artifact | None = None,
used_jars_json: Artifact | None = None) -> JavaCompileOutputs:
expect(classpath_abi != None or classpath_abi_dir == None, "A classpath_abi_dir should only be provided if a classpath_abi is provided!")
return JavaCompileOutputs(
full_library = full_library,
Expand All @@ -298,6 +301,7 @@ def make_compile_outputs(
annotation_processor_output = annotation_processor_output,
preprocessed_library = preprocessed_library,
incremental_state_dir = incremental_state_dir,
used_jars_json = used_jars_json,
)

def create_abi(actions: AnalysisActions, class_abi_generator: Dependency, library: Artifact) -> Artifact:
Expand Down Expand Up @@ -636,7 +640,8 @@ def create_java_library_providers(
proguard_config: Artifact | None = None,
gwt_module: Artifact | None = None,
lint_jar: Artifact | None = None,
preprocessed_library: Artifact | None = None) -> (JavaLibraryInfo, JavaPackagingInfo, JavaGlobalCodeInfo, SharedLibraryInfo, ResourceInfo, LinkableGraph, TemplatePlaceholderInfo, JavaLibraryIntellijInfo):
preprocessed_library: Artifact | None = None,
used_jars_json: Artifact | None = None) -> (JavaLibraryInfo, JavaPackagingInfo, JavaGlobalCodeInfo, SharedLibraryInfo, ResourceInfo, LinkableGraph, TemplatePlaceholderInfo, JavaLibraryIntellijInfo):
first_order_classpath_deps = filter(None, [x.get(JavaLibraryInfo) for x in declared_deps + exported_deps + runtime_deps])
first_order_classpath_libs = [dep.output_for_classpath_macro for dep in first_order_classpath_deps]

Expand Down Expand Up @@ -672,6 +677,7 @@ def create_java_library_providers(
annotation_jars_dir = annotation_jars_dir,
lint_jar = lint_jar,
preprocessed_library = preprocessed_library,
used_jars_json = used_jars_json,
)

return (library_info, packaging_info, global_code_info, shared_library_info, cxx_resource_info, linkable_graph, template_info, intellij_info)
7 changes: 6 additions & 1 deletion prelude/java/javacd_jar_creator.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def create_jar_artifact_javacd(
source_only_abi_compiling_deps = [],
track_class_usage = track_class_usage,
)
define_javacd_action(
used_jars_json = define_javacd_action(
category_prefix = "",
actions_identifier = actions_identifier,
encoded_command = command,
Expand Down Expand Up @@ -196,6 +196,7 @@ def create_jar_artifact_javacd(
required_for_source_only_abi = required_for_source_only_abi,
annotation_processor_output = output_paths.annotations,
abi_jar_snapshot = abi_jar_snapshot,
used_jars_json = used_jars_json,
)
else:
full_jar_snapshot = generate_java_classpath_snapshot(ctx.actions, java_toolchain.cp_snapshot_generator, ClasspathSnapshotGranularity("CLASS_MEMBER_LEVEL"), final_jar_output.final_jar, actions_identifier)
Expand All @@ -205,6 +206,7 @@ def create_jar_artifact_javacd(
required_for_source_only_abi = required_for_source_only_abi,
annotation_processor_output = output_paths.annotations,
abi_jar_snapshot = full_jar_snapshot,
used_jars_json = used_jars_json,
)
return result

Expand Down Expand Up @@ -298,6 +300,7 @@ def _define_javacd_action(
post_build_params["abiOutputDir"] = abi_dir.as_output()

dep_files = {}
used_jars_json_output = None
if not is_creating_subtarget and srcs and (java_toolchain.dep_files == DepFiles("per_jar") or java_toolchain.dep_files == DepFiles("per_class")) and track_class_usage:
abi_to_abi_dir_map = None
hidden = []
Expand Down Expand Up @@ -354,3 +357,5 @@ def _define_javacd_action(
weight = 2,
error_handler = java_toolchain.java_error_handler,
)

return used_jars_json_output
1 change: 1 addition & 0 deletions prelude/kotlin/kotlin_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,7 @@ def build_kotlin_library(
has_srcs = bool(srcs),
sources_jar = sources_jar,
preprocessed_library = outputs.preprocessed_library if outputs else None,
used_jars_json = outputs.used_jars_json if outputs else None,
)

default_info = get_default_info(
Expand Down
7 changes: 5 additions & 2 deletions prelude/kotlin/kotlincd_jar_creator.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def create_jar_artifact_kotlincd(
source_only_abi_compiling_deps = [],
track_class_usage = track_class_usage,
)
proto = define_kotlincd_action(
proto, used_jars_json = define_kotlincd_action(
category_prefix = "",
actions_identifier = actions_identifier,
encoded_command = command,
Expand Down Expand Up @@ -249,6 +249,7 @@ def create_jar_artifact_kotlincd(
annotation_processor_output = output_paths.annotations,
incremental_state_dir = incremental_state_dir,
abi_jar_snapshot = abi_jar_snapshot,
used_jars_json = used_jars_json,
), proto
else:
full_jar_snapshot = generate_java_classpath_snapshot(actions, java_toolchain.cp_snapshot_generator, ClasspathSnapshotGranularity("CLASS_MEMBER_LEVEL"), final_jar_output.final_jar, actions_identifier)
Expand All @@ -258,6 +259,7 @@ def create_jar_artifact_kotlincd(
required_for_source_only_abi = required_for_source_only_abi,
annotation_processor_output = output_paths.annotations,
abi_jar_snapshot = full_jar_snapshot,
used_jars_json = used_jars_json,
), proto

def _encode_kotlin_extra_params(
Expand Down Expand Up @@ -458,6 +460,7 @@ def _define_kotlincd_action(
post_build_params["incrementalStateDir"] = incremental_state_dir.as_output()

dep_files = {}
used_jars_json_output = None
if not is_creating_subtarget and srcs and (kotlin_toolchain.dep_files == DepFiles("per_jar") or kotlin_toolchain.dep_files == DepFiles("per_class")) and target_type == TargetType("library") and track_class_usage:
used_classes_json_outputs = [
output_paths.jar_parent.project("used-classes.json"),
Expand Down Expand Up @@ -512,4 +515,4 @@ def _define_kotlincd_action(
error_handler = kotlin_toolchain.kotlin_error_handler,
**incremental_run_params
)
return proto
return proto, used_jars_json_output

0 comments on commit 7c2931f

Please sign in to comment.