Skip to content

Commit

Permalink
def argfile
Browse files Browse the repository at this point in the history
Summary:
Wrapper for `ctx.actions.write` which attaches dependencies.

I think `ctx.actions.write(with_inputs=True)` could be used, but it does not work properly, a comment says "This will only work for bound artifacts". I don't know if we can fix it, if we do, we can inline this function back.

This function is slightly more expensive than using `write` directly (we create extra `cmd_args`), but
- how much, depends on what you are doing (see benchmark below)
- we will win it back when we make `cmd_args` immutable

Used it in `prelude/android`, because this is what I migrate now from `cmd_args.hidden()` calls.

Reviewed By: blackm00n

Differential Revision: D58041951

fbshipit-source-id: 219664061bef3f145ccc719a4e839ddf3376d627
  • Loading branch information
stepancheg authored and facebook-github-bot committed Jun 5, 2024
1 parent f7bc2f6 commit bd99fc8
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 62 deletions.
19 changes: 9 additions & 10 deletions android/android_aar.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ load("@prelude//android:cpu_filters.bzl", "CPU_FILTER_FOR_DEFAULT_PLATFORM", "CP
load("@prelude//android:util.bzl", "create_enhancement_context")
load("@prelude//java:java_providers.bzl", "create_java_packaging_dep", "get_all_java_packaging_deps", "get_all_java_packaging_deps_from_packaging_infos")
load("@prelude//java:java_toolchain.bzl", "JavaToolchainInfo")
load("@prelude//utils:argfile.bzl", "argfile")
load("@prelude//utils:set.bzl", "set")

def android_aar_impl(ctx: AnalysisContext) -> list[Provider]:
Expand Down Expand Up @@ -50,10 +51,10 @@ def android_aar_impl(ctx: AnalysisContext) -> list[Provider]:
classes_jar_cmd = cmd_args([
java_toolchain.jar_builder,
"--entries-to-jar",
ctx.actions.write("classes_jar_entries.txt", jars),
argfile(actions = ctx.actions, name = "classes_jar_entries.txt", args = jars),
"--output",
classes_jar.as_output(),
], hidden = jars)
])

if ctx.attrs.remove_classes:
remove_classes_file = ctx.actions.write("remove_classes.txt", ctx.attrs.remove_classes)
Expand All @@ -74,10 +75,10 @@ def android_aar_impl(ctx: AnalysisContext) -> list[Provider]:
combined_sources_jar_cmd = cmd_args([
java_toolchain.jar_builder,
"--entries-to-jar",
ctx.actions.write("combined_sources_jar_entries.txt", dependency_sources_jars),
argfile(actions = ctx.actions, name = "combined_sources_jar_entries.txt", args = dependency_sources_jars),
"--output",
combined_sources_jar.as_output(),
], hidden = dependency_sources_jars)
])

if ctx.attrs.remove_classes:
remove_classes_file = ctx.actions.write("sources_remove_classes.txt", ctx.attrs.remove_classes)
Expand All @@ -103,10 +104,10 @@ def android_aar_impl(ctx: AnalysisContext) -> list[Provider]:
merge_resource_sources_cmd = cmd_args([
android_toolchain.merge_android_resource_sources[RunInfo],
"--resource-paths",
ctx.actions.write("resource_paths.txt", res_dirs),
argfile(actions = ctx.actions, name = "resource_paths.txt", args = res_dirs),
"--output",
merged_resource_sources_dir.as_output(),
], hidden = res_dirs)
])

ctx.actions.run(merge_resource_sources_cmd, category = "merge_android_resource_sources")

Expand All @@ -120,8 +121,8 @@ def android_aar_impl(ctx: AnalysisContext) -> list[Provider]:
if cxx_resources:
entries.append(cxx_resources)

native_libs_file = ctx.actions.write("native_libs_entries.txt", android_binary_native_library_info.native_libs_for_primary_apk)
native_libs_assets_file = ctx.actions.write("native_libs_assets_entries.txt", android_binary_native_library_info.root_module_native_lib_assets)
native_libs_file = argfile(actions = ctx.actions, name = "native_libs_entries.txt", args = android_binary_native_library_info.native_libs_for_primary_apk)
native_libs_assets_file = argfile(actions = ctx.actions, name = "native_libs_assets_entries.txt", args = android_binary_native_library_info.root_module_native_lib_assets)

entries_file = ctx.actions.write("entries.txt", entries)

Expand All @@ -142,8 +143,6 @@ def android_aar_impl(ctx: AnalysisContext) -> list[Provider]:
],
hidden = [
entries,
android_binary_native_library_info.native_libs_for_primary_apk,
android_binary_native_library_info.root_module_native_lib_assets,
],
)

Expand Down
13 changes: 5 additions & 8 deletions android/android_apk.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ load("@prelude//java:java_providers.bzl", "KeystoreInfo")
load("@prelude//java:java_toolchain.bzl", "JavaToolchainInfo")
load("@prelude//java/utils:java_more_utils.bzl", "get_path_separator_for_exec_os")
load("@prelude//java/utils:java_utils.bzl", "get_class_to_source_map_info")
load("@prelude//utils:argfile.bzl", "argfile")
load("@prelude//utils:set.bzl", "set")

def android_apk_impl(ctx: AnalysisContext) -> list[Provider]:
Expand Down Expand Up @@ -147,15 +148,11 @@ def build_apk(
dex_files_info.non_root_module_secondary_dex_dirs +
resources_info.module_manifests
)
asset_directories_file = actions.write("asset_directories.txt", asset_directories)
apk_builder_args.hidden(asset_directories)
native_library_directories = actions.write("native_library_directories", native_library_info.native_libs_for_primary_apk)
apk_builder_args.hidden(native_library_info.native_libs_for_primary_apk)
asset_directories_file = argfile(actions = actions, name = "asset_directories.txt", args = asset_directories)
native_library_directories = argfile(actions = actions, name = "native_library_directories", args = native_library_info.native_libs_for_primary_apk)
all_zip_files = [resources_info.packaged_string_assets] if resources_info.packaged_string_assets else []
zip_files = actions.write("zip_files", all_zip_files)
apk_builder_args.hidden(all_zip_files)
jar_files_that_may_contain_resources = actions.write("jar_files_that_may_contain_resources", resources_info.jar_files_that_may_contain_resources)
apk_builder_args.hidden(resources_info.jar_files_that_may_contain_resources)
zip_files = argfile(actions = actions, name = "zip_files", args = all_zip_files)
jar_files_that_may_contain_resources = argfile(actions = actions, name = "jar_files_that_may_contain_resources", args = resources_info.jar_files_that_may_contain_resources)

apk_builder_args.add([
"--asset-directories-list",
Expand Down
14 changes: 9 additions & 5 deletions android/android_binary_native_library_rules.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ load(
)
load("@prelude//linking:strip.bzl", "strip_object")
load("@prelude//linking:types.bzl", "Linkage")
load("@prelude//utils:argfile.bzl", "argfile")
load("@prelude//utils:expect.bzl", "expect")
load("@prelude//utils:graph_utils.bzl", "GraphTraversal", "depth_first_traversal_by", "post_order_traversal", "pre_order_traversal")
load("@prelude//utils:set.bzl", "set", "set_type") # @unused Used as a type
Expand Down Expand Up @@ -688,8 +689,7 @@ def _filter_prebuilt_native_library_dir(
cpu_filters = ctx.attrs.cpu_filters or CPU_FILTER_TO_ABI_DIRECTORY.keys()
abis = [CPU_FILTER_TO_ABI_DIRECTORY[cpu] for cpu in cpu_filters]
filter_tool = ctx.attrs._android_toolchain[AndroidToolchainInfo].filter_prebuilt_native_library_dir[RunInfo]
native_libs_dirs = [native_lib.dir for native_lib in native_libs]
native_libs_dirs_file = ctx.actions.write("{}_list.txt".format(identifier), native_libs_dirs)
native_libs_dirs_file = argfile(actions = ctx.actions, name = "{}_list.txt".format(identifier), args = [native_lib.dir for native_lib in native_libs])
base_output_dir = ctx.actions.declare_output(identifier, dir = True)
if module == ROOT_MODULE:
output_dir = base_output_dir.project(_get_native_libs_as_assets_dir(module)) if package_as_assets else base_output_dir
Expand All @@ -698,7 +698,7 @@ def _filter_prebuilt_native_library_dir(
else:
output_dir = base_output_dir.project(paths.join(_get_native_libs_as_assets_dir(module), "lib"))
ctx.actions.run(
cmd_args([filter_tool, native_libs_dirs_file, output_dir.as_output(), "--abis"] + abis, hidden = native_libs_dirs),
cmd_args([filter_tool, native_libs_dirs_file, output_dir.as_output(), "--abis"] + abis),
category = "filter_prebuilt_native_library_dir",
identifier = identifier,
)
Expand Down Expand Up @@ -789,15 +789,19 @@ def _get_native_libs_as_assets_metadata(
ctx: AnalysisContext,
native_lib_assets: list[Artifact],
module: str) -> Artifact:
native_lib_assets_file = ctx.actions.write("{}/native_lib_assets".format(module), [cmd_args([native_lib_asset, _get_native_libs_as_assets_dir(module)], delimiter = "/") for native_lib_asset in native_lib_assets])
native_lib_assets_file = argfile(
actions = ctx.actions,
name = "{}/native_lib_assets".format(module),
args = [cmd_args([native_lib_asset, _get_native_libs_as_assets_dir(module)], delimiter = "/") for native_lib_asset in native_lib_assets],
)
metadata_output = ctx.actions.declare_output("{}/native_libs_as_assets_metadata.txt".format(module))
metadata_cmd = cmd_args([
ctx.attrs._android_toolchain[AndroidToolchainInfo].native_libs_as_assets_metadata[RunInfo],
"--native-library-dirs",
native_lib_assets_file,
"--metadata-output",
metadata_output.as_output(),
], hidden = native_lib_assets)
])
ctx.actions.run(metadata_cmd, category = "get_native_libs_as_assets_metadata", identifier = module)
return metadata_output

Expand Down
19 changes: 7 additions & 12 deletions android/android_bundle.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ load("@prelude//android:android_toolchain.bzl", "AndroidToolchainInfo")
load("@prelude//android:bundletool_util.bzl", "derive_universal_apk")
load("@prelude//java:java_providers.bzl", "KeystoreInfo")
load("@prelude//java/utils:java_more_utils.bzl", "get_path_separator_for_exec_os")
load("@prelude//utils:argfile.bzl", "argfile")

def android_bundle_impl(ctx: AnalysisContext) -> list[Provider]:
android_binary_info = get_binary_info(ctx, use_proto_format = True)
Expand Down Expand Up @@ -90,22 +91,16 @@ def build_bundle(
bundle_builder_args.add("--package-meta-inf-version-files")

root_module_asset_directories = native_library_info.root_module_native_lib_assets + dex_files_info.root_module_secondary_dex_dirs
root_module_asset_directories_file = actions.write("root_module_asset_directories.txt", root_module_asset_directories)
bundle_builder_args.hidden(root_module_asset_directories)
root_module_asset_directories_file = argfile(actions = actions, name = "root_module_asset_directories.txt", args = root_module_asset_directories)

non_root_module_asset_directories = resources_info.module_manifests + dex_files_info.non_root_module_secondary_dex_dirs
non_root_module_asset_directories_file = actions.write("non_root_module_asset_directories.txt", non_root_module_asset_directories)
bundle_builder_args.hidden(non_root_module_asset_directories)
non_root_module_asset_native_lib_directories = actions.write("non_root_module_asset_native_lib_directories.txt", native_library_info.non_root_module_native_lib_assets)
bundle_builder_args.hidden(native_library_info.non_root_module_native_lib_assets)
non_root_module_asset_directories_file = argfile(actions = actions, name = "non_root_module_asset_directories.txt", args = non_root_module_asset_directories)
non_root_module_asset_native_lib_directories = argfile(actions = actions, name = "non_root_module_asset_native_lib_directories.txt", args = native_library_info.non_root_module_native_lib_assets)

native_library_directories = actions.write("native_library_directories", native_library_info.native_libs_for_primary_apk)
bundle_builder_args.hidden(native_library_info.native_libs_for_primary_apk)
native_library_directories = argfile(actions = actions, name = "native_library_directories", args = native_library_info.native_libs_for_primary_apk)
all_zip_files = [resources_info.packaged_string_assets] if resources_info.packaged_string_assets else []
zip_files = actions.write("zip_files", all_zip_files)
bundle_builder_args.hidden(all_zip_files)
jar_files_that_may_contain_resources = actions.write("jar_files_that_may_contain_resources", resources_info.jar_files_that_may_contain_resources)
bundle_builder_args.hidden(resources_info.jar_files_that_may_contain_resources)
zip_files = argfile(actions = actions, name = "zip_files", args = all_zip_files)
jar_files_that_may_contain_resources = argfile(actions = actions, name = "jar_files_that_may_contain_resources", args = resources_info.jar_files_that_may_contain_resources)

if resources_info.module_assets:
bundle_builder_args.add(["--module-assets-dir", resources_info.module_assets])
Expand Down
4 changes: 2 additions & 2 deletions android/android_manifest.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ load(
)
load("@prelude//android:android_toolchain.bzl", "AndroidToolchainInfo")
load("@prelude//android:voltron.bzl", "ROOT_MODULE")
load("@prelude//utils:argfile.bzl", "argfile")

def android_manifest_impl(ctx: AnalysisContext) -> list[Provider]:
output, merge_report = generate_android_manifest(
Expand Down Expand Up @@ -49,10 +50,9 @@ def generate_android_manifest(
elif type(manifests) == "transitive_set":
manifests = manifests.project_as_args("artifacts", ordering = "bfs")

library_manifest_paths_file = ctx.actions.write("{}/library_manifest_paths_file".format(module_name), manifests)
library_manifest_paths_file = argfile(actions = ctx.actions, name = "{}/library_manifest_paths_file".format(module_name), args = manifests)

generate_manifest_cmd.add(["--library-manifests-list", library_manifest_paths_file])
generate_manifest_cmd.hidden(manifests)

placeholder_entries_args = cmd_args()
for key, val in placeholder_entries.items():
Expand Down
4 changes: 2 additions & 2 deletions android/android_resource.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
# of this source tree.

load("@prelude//java:java_providers.bzl", "get_java_packaging_info")
load("@prelude//utils:argfile.bzl", "argfile")
load("@prelude//utils:expect.bzl", "expect")
load(":android_providers.bzl", "AndroidResourceInfo", "ExportedAndroidResourceInfo", "RESOURCE_PRIORITY_NORMAL", "merge_android_packageable_info")
load(":android_toolchain.bzl", "AndroidToolchainInfo")
Expand Down Expand Up @@ -129,10 +130,9 @@ def get_text_symbols(
dep_symbols = _get_dep_symbols(deps)
dep_symbol_paths.add(dep_symbols)

dep_symbol_paths_file, _ = ctx.actions.write("{}_dep_symbol_paths_file".format(identifier) if identifier else "dep_symbol_paths_file", dep_symbol_paths, allow_args = True)
dep_symbol_paths_file = argfile(actions = ctx.actions, name = "{}_dep_symbol_paths_file".format(identifier) if identifier else "dep_symbol_paths_file", args = dep_symbol_paths, allow_args = True)

mini_aapt_cmd.add(["--dep-symbol-paths", dep_symbol_paths_file])
mini_aapt_cmd.hidden(dep_symbols)

text_symbols = ctx.actions.declare_output("{}_R.txt".format(identifier) if identifier else "R.txt")
mini_aapt_cmd.add(["--output-path", text_symbols.as_output()])
Expand Down
17 changes: 6 additions & 11 deletions android/dex_rules.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ load("@prelude//android:voltron.bzl", "ROOT_MODULE", "get_apk_module_graph_info"
load("@prelude//java:dex.bzl", "DexLibraryInfo", "get_dex_produced_from_java_library")
load("@prelude//java:dex_toolchain.bzl", "DexToolchainInfo")
load("@prelude//java:java_library.bzl", "compile_to_jar")
load("@prelude//utils:argfile.bzl", "at_argfile")
load("@prelude//utils:argfile.bzl", "argfile", "at_argfile")
load("@prelude//utils:expect.bzl", "expect")
load("@prelude//utils:utils.bzl", "flatten")
load("@prelude//paths.bzl", "paths")
Expand Down Expand Up @@ -103,9 +103,8 @@ def get_single_primary_dex(
output_dex_file = ctx.actions.declare_output("classes.dex")
d8_cmd.add(["--output-dex-file", output_dex_file.as_output()])

jar_to_dex_file = ctx.actions.write("jar_to_dex_file.txt", java_library_jars)
jar_to_dex_file = argfile(actions = ctx.actions, name = "jar_to_dex_file.txt", args = java_library_jars)
d8_cmd.add(["--files-to-dex-list", jar_to_dex_file])
d8_cmd.hidden(java_library_jars)

d8_cmd.add(["--android-jar", android_toolchain.android_jar])
if not is_optimized:
Expand Down Expand Up @@ -155,7 +154,7 @@ def get_multi_dex(

secondary_dex_dir_srcs = {}
all_jars = flatten(module_to_jars.values())
all_jars_list = ctx.actions.write("all_jars_classpath.txt", all_jars)
all_jars_list = argfile(actions = ctx.actions, name = "all_jars_classpath.txt", args = all_jars)
for module, jars in module_to_jars.items():
multi_dex_cmd = cmd_args(android_toolchain.multi_dex_command[RunInfo])
secondary_dex_compression_cmd = cmd_args(android_toolchain.secondary_dex_compression_command[RunInfo])
Expand All @@ -176,9 +175,8 @@ def get_multi_dex(
android_toolchain,
)

primary_dex_jar_to_dex_file = ctx.actions.write("primary_dex_jars_to_dex_file_for_root_module.txt", primary_dex_jars)
primary_dex_jar_to_dex_file = argfile(actions = ctx.actions, name = "primary_dex_jars_to_dex_file_for_root_module.txt", args = primary_dex_jars)
multi_dex_cmd.add("--primary-dex-files-to-dex-list", primary_dex_jar_to_dex_file)
multi_dex_cmd.hidden(primary_dex_jars)
multi_dex_cmd.add("--minimize-primary-dex")
else:
jars_to_dex = jars
Expand All @@ -195,16 +193,14 @@ def get_multi_dex(
secondary_dex_compression_cmd.add("--secondary-dex-output-dir", secondary_dex_dir_for_module.as_output())
jars_to_dex = jars
multi_dex_cmd.add("--classpath-files", all_jars_list)
multi_dex_cmd.hidden(all_jars)

multi_dex_cmd.add("--module", module)
multi_dex_cmd.add("--canary-class-name", apk_module_graph_info.module_to_canary_class_name_function(module))
secondary_dex_compression_cmd.add("--module", module)
secondary_dex_compression_cmd.add("--canary-class-name", apk_module_graph_info.module_to_canary_class_name_function(module))

jar_to_dex_file = ctx.actions.write("jars_to_dex_file_for_module_{}.txt".format(module), jars_to_dex)
jar_to_dex_file = argfile(actions = ctx.actions, name = "jars_to_dex_file_for_module_{}.txt".format(module), args = jars_to_dex)
multi_dex_cmd.add("--files-to-dex-list", jar_to_dex_file)
multi_dex_cmd.hidden(jars_to_dex)

multi_dex_cmd.add("--android-jar", android_toolchain.android_jar)
if not is_optimized:
Expand Down Expand Up @@ -602,9 +598,8 @@ def _merge_dexes(
d8_cmd = cmd_args(android_toolchain.d8_command[RunInfo])
d8_cmd.add(["--output-dex-file", output_dex_file.as_output()])

pre_dexed_artifacts_to_dex_file = ctx.actions.write(pre_dexed_artifacts_file.as_output(), pre_dexed_artifacts)
pre_dexed_artifacts_to_dex_file = argfile(actions = ctx.actions, name = pre_dexed_artifacts_file, args = pre_dexed_artifacts)
d8_cmd.add(["--files-to-dex-list", pre_dexed_artifacts_to_dex_file])
d8_cmd.hidden(pre_dexed_artifacts)

d8_cmd.add(["--android-jar", android_toolchain.android_jar])
d8_cmd.add(_DEX_MERGE_OPTIONS)
Expand Down
Loading

0 comments on commit bd99fc8

Please sign in to comment.