Skip to content

Commit

Permalink
[SNAP FORK][UPSTREAMED] Implement namespaced R class
Browse files Browse the repository at this point in the history
The native version of the rules have this optimization (in our internal
fork). This is that implementationm, but in starlark.

See https://github.sc-corp.net/Snapchat/bazel/pull/126 for more details
  • Loading branch information
Mauricio Galindo committed Jan 18, 2023
1 parent e364356 commit 0d3b435
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 80 deletions.
2 changes: 1 addition & 1 deletion rules/aar_import/impl.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def _process_resources(
exports = ctx.attr.exports,
exports_manifest = getattr(ctx.attr, "exports_manifest", True),
propagate_resources = _acls.in_aar_propagate_resources(str(ctx.label)),

namespaced_r_class = False,
# Tool and Processing related inputs
aapt = _get_android_toolchain(ctx).aapt2.files_to_run,
android_jar = ctx.attr._android_sdk[AndroidSdkInfo].android_jar,
Expand Down
1 change: 0 additions & 1 deletion rules/android_library/rule.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ def _outputs(name, _package_name, _defined_local_resources):
path_prefix = ""
outputs.update(
dict(
resources_src_jar = path_prefix + "%{name}.srcjar",
resources_txt = path_prefix + "%{name}_symbols/R.txt",
resources_jar = path_prefix + "%{name}_resources.jar",
),
Expand Down
26 changes: 24 additions & 2 deletions rules/busybox.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -603,11 +603,15 @@ def _validate_and_link(
def _compile(
ctx,
out_file = None,
out_class_jar = None,
out_r_txt = None,
manifest = None,
assets = [],
assets_dir = None,
resource_files = [],
busybox = None,
aapt = None,
android_jar = None,
host_javabase = None):
"""Compile and store resources in a single archive.
Expand Down Expand Up @@ -641,14 +645,31 @@ def _compile(
)
args.add("--output", out_file)

optional_outputs = []
if out_class_jar:
args.add("--classJarOutput", out_class_jar)
optional_outputs.append(out_class_jar)
if out_r_txt:
args.add("--rTxtOut", out_r_txt)
optional_outputs.append(out_r_txt)

args.add("--targetLabel", ctx.label)
optional_inputs = []
if manifest:
args.add("--manifest", manifest)
optional_inputs = [manifest]
if android_jar:
args.add("--androidJar", android_jar)
optional_inputs.append(android_jar)

_java.run(
ctx = ctx,
host_javabase = host_javabase,
executable = busybox,
tools = [aapt],
arguments = [args],
inputs = resource_files + assets,
outputs = [out_file],
inputs = resource_files + assets + optional_inputs,
outputs = [out_file] + optional_outputs,
mnemonic = "CompileAndroidResources",
progress_message = "Compiling Android Resources in %s" % out_file.short_path,
)
Expand Down Expand Up @@ -1042,6 +1063,7 @@ busybox = struct(
process_databinding = _process_databinding,
generate_binary_r = _generate_binary_r,
make_aar = _make_aar,
ANDROID_RESOURCES_STRICT_DEPS = _ANDROID_RESOURCES_STRICT_DEPS,

# Exposed for testing
mergee_manifests_flag = _mergee_manifests_flag,
Expand Down
188 changes: 112 additions & 76 deletions rules/resources.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -1116,7 +1116,8 @@ def _process_starlark(
host_javabase = None,
instrument_xslt = None,
xsltproc = None,
zip_tool = None):
zip_tool = None,
namespaced_r_class = True):
"""Processes Android Resources.
Args:
Expand Down Expand Up @@ -1370,6 +1371,7 @@ def _process_starlark(
parsed_assets = ctx.actions.declare_file(
"_migrated/" + ctx.label.name + "_symbols/assets.bin",
)

_busybox.parse(
ctx,
out_symbols = parsed_assets,
Expand Down Expand Up @@ -1438,14 +1440,38 @@ def _process_starlark(
compiled_resources = ctx.actions.declare_file(
"_migrated/" + ctx.label.name + "_symbols/symbols.zip",
)
_busybox.compile(
ctx,
out_file = compiled_resources,
resource_files = processed_resources,
aapt = aapt,
busybox = busybox,
host_javabase = host_javabase,

out_class_jar = ctx.actions.declare_file(
"_migrated/" + ctx.label.name + "_resources.jar",
)
processed_manifest = None

# when using namespaced r classes the compile action generated both
# R.txt and the resources.jar without merging with outputs produced
# by transitive deps. Doing this means less changes that invalidate
# the action cache which in turn improves performance significantly.
if namespaced_r_class:
out_aapt2_r_txt = ctx.actions.declare_file(
"_migrated/" + ctx.label.name + "_symbols/R.txt",
)
_busybox.compile(
ctx,
out_file = compiled_resources,
out_class_jar = out_class_jar,
out_r_txt = out_aapt2_r_txt,
manifest = manifest,
android_jar = android_jar,
resource_files = processed_resources,
aapt = aapt,
busybox = busybox,
host_javabase = host_javabase,
)
r_txt = out_aapt2_r_txt
java_info = JavaInfo(
output_jar = out_class_jar,
compile_jar = out_class_jar,
)
processed_manifest = manifest

# TODO(b/160907203): Remove this fix once the native resource processing pipeline is turned off.
if enable_data_binding:
Expand All @@ -1460,76 +1486,85 @@ def _process_starlark(
)
compiled_resources = fixed_compiled_resources

out_class_jar = ctx.actions.declare_file(
"_migrated/" + ctx.label.name + "_resources.jar",
)
processed_manifest = ctx.actions.declare_file(
"_migrated/" + ctx.label.name + "_processed_manifest/AndroidManifest.xml",
)
out_aapt2_r_txt = ctx.actions.declare_file(
"_migrated/" + ctx.label.name + "_symbols/R.aapt2.txt",
)
_busybox.merge_compiled(
ctx,
out_class_jar = out_class_jar,
out_manifest = processed_manifest,
out_aapt2_r_txt = out_aapt2_r_txt,
java_package = java_package,
manifest = manifest,
compiled_resources = compiled_resources,
direct_resources_nodes =
depset(transitive = direct_resources_nodes, order = "preorder"),
transitive_resources_nodes = depset(
transitive = transitive_resources_nodes,
order = "preorder",
),
direct_compiled_resources = depset(
transitive = direct_compiled_resources,
order = "preorder",
),
transitive_compiled_resources = depset(
transitive = transitive_compiled_resources,
order = "preorder",
),
android_jar = android_jar,
busybox = busybox,
host_javabase = host_javabase,
)
resources_ctx[_MERGED_MANIFEST] = processed_manifest
if not namespaced_r_class:
processed_manifest = ctx.actions.declare_file(
"_migrated/" + ctx.label.name + "_processed_manifest/AndroidManifest.xml",
)
out_aapt2_r_txt = ctx.actions.declare_file(
"_migrated/" + ctx.label.name + "_symbols/R.aapt2.txt",
)
_busybox.compile(
ctx,
out_file = compiled_resources,
manifest = manifest,
android_jar = android_jar,
resource_files = processed_resources,
aapt = aapt,
busybox = busybox,
host_javabase = host_javabase,
)

_busybox.merge_compiled(
ctx,
out_class_jar = out_class_jar,
out_manifest = processed_manifest,
out_aapt2_r_txt = out_aapt2_r_txt,
java_package = java_package,
manifest = manifest,
compiled_resources = compiled_resources,
direct_resources_nodes =
depset(transitive = direct_resources_nodes, order = "preorder"),
transitive_resources_nodes = depset(
transitive = transitive_resources_nodes,
order = "preorder",
),
direct_compiled_resources = depset(
transitive = direct_compiled_resources,
order = "preorder",
),
transitive_compiled_resources = depset(
transitive = transitive_compiled_resources,
order = "preorder",
),
android_jar = android_jar,
busybox = busybox,
host_javabase = host_javabase,
)

apk = ctx.actions.declare_file(
"_migrated/" + ctx.label.name + "_files/library.ap_",
)
r_java = ctx.actions.declare_file(
"_migrated/" + ctx.label.name + ".srcjar",
)
r_txt = ctx.actions.declare_file(
"_migrated/" + ctx.label.name + "_symbols/R.txt",
)
_busybox.validate_and_link(
ctx,
out_r_src_jar = r_java,
out_r_txt = r_txt,
out_file = apk,
compiled_resources = compiled_resources,
transitive_compiled_resources = depset(
transitive = transitive_compiled_resources,
order = "preorder",
),
java_package = java_package,
manifest = processed_manifest,
android_jar = android_jar,
aapt = aapt,
busybox = busybox,
host_javabase = host_javabase,
)
resources_ctx[_RESOURCES_APK] = apk
apk = ctx.actions.declare_file(
"_migrated/" + ctx.label.name + "_files/library.ap_",
)
r_java = ctx.actions.declare_file(
"_migrated/" + ctx.label.name + ".srcjar",
)
r_txt = ctx.actions.declare_file(
"_migrated/" + ctx.label.name + "_symbols/R.txt",
)
_busybox.validate_and_link(
ctx,
out_r_src_jar = r_java,
out_r_txt = r_txt,
out_file = apk,
compiled_resources = compiled_resources,
transitive_compiled_resources = depset(
transitive = transitive_compiled_resources,
order = "preorder",
),
java_package = java_package,
manifest = processed_manifest,
android_jar = android_jar,
aapt = aapt,
busybox = busybox,
host_javabase = host_javabase,
)
resources_ctx[_RESOURCES_APK] = apk
java_info = JavaInfo(
output_jar = out_class_jar,
compile_jar = out_class_jar,
source_jar = r_java,
)

java_info = JavaInfo(
output_jar = out_class_jar,
compile_jar = out_class_jar,
source_jar = r_java,
)
resources_ctx[_MERGED_MANIFEST] = processed_manifest

packages_to_r_txts_depset.setdefault(java_package, []).append(depset([out_aapt2_r_txt]))

Expand Down Expand Up @@ -1770,6 +1805,7 @@ def _process(
java_toolchain = java_toolchain,
host_javabase = host_javabase,
zip_tool = zip_tool,
namespaced_r_class = _busybox.ANDROID_RESOURCES_STRICT_DEPS not in ctx.disabled_features,
)


Expand Down

0 comments on commit 0d3b435

Please sign in to comment.