Skip to content

Commit

Permalink
Migrate ZipFilter action to Starlark used in proguard and android_ins…
Browse files Browse the repository at this point in the history
…trumentation test.

PiperOrigin-RevId: 534132305
Change-Id: Id1c8b7bb60d3a4ba4e33a5b91037158823f62819
  • Loading branch information
Zhaoqing Xu authored and JiaYan Lin committed Jan 23, 2024
1 parent adf3815 commit 012fe60
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
53 changes: 53 additions & 0 deletions rules/common.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,58 @@ def _filter_zip_include(ctx, in_zip, out_zip, filters = []):
progress_message = "Filtering %s" % in_zip.short_path,
)

def _filter_zip_exclude(
ctx,
output = None,
input = None,
filter_zips = [],
filter_types = [],
filters = [],
check_hash_mismatch = False,
compression_mode = "DONT_CARE"):
"""Filter out entries from a zip file based on the filter types and filter zips.
Args:
ctx: The Context.
output: File. The output zip.
input: File. The input zip.
filter_zips: List of Files. The zips used as filters. Contents in these files will be omitted from the output zip.
filter_types: List of strings. Only contents in the filter Zip files with these extensions will be filtered out.
filters: List of strings. The regex to the set of filters to always check for and remove.
check_hash_mismatch: Boolean. Whether to enable checking of hash mismatches for files with the same name.
compression_mode: String. The compression mode for the output zip. There are 3 modes:
* FORCE_DEFLATE: Force the output zip to be compressed.
* FORCE_STORED: Force the output zip to be stored.
* DONT_CARE: The output zip will have the same compression mode with the input zip.
"""
args = ctx.actions.args()

args.add("inputZip", input.path)
args.add("--outputZip", output.path)

if filter_zips:
args.add("--filterZips", ",".join([z.path for z in filter_zips]))
if filter_types:
args.add("--filterTypes", ",".join(filter_types))
if filters:
args.add("--explicitFilters", ",".join(filters))

if check_hash_mismatch:
args.add("--checkHashMismatch", "ERROR")
else:
args.add("--checkHashMisMatch", "IGNORE")

args.add("--outputMode", compression_mode)

ctx.actions.run(
executable = get_android_toolchain(ctx).zip_filter.files_to_run,
arguments = [args],
inputs = [input] + filter_zips,
outputs = [output],
mnemonic = "FilterZipExclude",
progress_message = "Filtering %s" % input.short_path,
)

def _create_signer_properties(ctx, oldest_key):
properties = ctx.actions.declare_file("%s/keystore.properties" % ctx.label.name)
ctx.actions.expand_template(
Expand All @@ -77,6 +129,7 @@ common = struct(
get_host_javabase = _get_host_javabase,
get_java_toolchain = _get_java_toolchain,
filter_zip_include = _filter_zip_include,
filter_zip_exclude = _filter_zip_exclude,
)

android_common = _native_android_common
5 changes: 5 additions & 0 deletions toolchains/android/toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,11 @@ _ATTRS = dict(
default = "//toolchains/android:zip",
executable = True,
),
zip_filter = attr.label(
cfg = "exec",
default = "@bazel_tools//tools/android:zip_filter",
executable = True,
),
)

def _impl(ctx):
Expand Down

0 comments on commit 012fe60

Please sign in to comment.