Kaido Kert | 5ac52c4 | 2021-05-14 12:23:37 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # Copyright 2019 The Chromium Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | import argparse |
| 7 | import os |
| 8 | import re |
| 9 | import sys |
| 10 | import zipfile |
| 11 | |
| 12 | sys.path.append(os.path.join(os.path.dirname(__file__), os.pardir)) |
| 13 | from pylib.dex import dex_parser |
| 14 | from util import build_utils |
| 15 | |
| 16 | _FLAGS_PATH = ( |
| 17 | '//chrome/android/java/static_library_dex_reference_workarounds.flags') |
| 18 | |
| 19 | |
| 20 | def _FindIllegalStaticLibraryReferences(static_lib_dex_files, |
| 21 | main_apk_dex_files): |
| 22 | main_apk_defined_types = set() |
| 23 | for dex_file in main_apk_dex_files: |
| 24 | for class_def_item in dex_file.class_def_item_list: |
| 25 | main_apk_defined_types.add( |
| 26 | dex_file.GetTypeString(class_def_item.class_idx)) |
| 27 | |
| 28 | static_lib_referenced_types = set() |
| 29 | for dex_file in static_lib_dex_files: |
| 30 | for type_item in dex_file.type_item_list: |
| 31 | static_lib_referenced_types.add( |
| 32 | dex_file.GetString(type_item.descriptor_idx)) |
| 33 | |
| 34 | return main_apk_defined_types.intersection(static_lib_referenced_types) |
| 35 | |
| 36 | |
| 37 | def _DexFilesFromPath(path): |
| 38 | if zipfile.is_zipfile(path): |
| 39 | with zipfile.ZipFile(path) as z: |
| 40 | return [ |
| 41 | dex_parser.DexFile(bytearray(z.read(name))) for name in z.namelist() |
| 42 | if re.match(r'.*classes[0-9]*\.dex$', name) |
| 43 | ] |
| 44 | else: |
| 45 | with open(path) as f: |
| 46 | return dex_parser.DexFile(bytearray(f.read())) |
| 47 | |
| 48 | |
| 49 | def main(args): |
| 50 | args = build_utils.ExpandFileArgs(args) |
| 51 | parser = argparse.ArgumentParser() |
| 52 | parser.add_argument( |
| 53 | '--depfile', required=True, help='Path to output depfile.') |
| 54 | parser.add_argument( |
| 55 | '--stamp', required=True, help='Path to file to touch upon success.') |
| 56 | parser.add_argument( |
| 57 | '--static-library-dex', |
| 58 | required=True, |
| 59 | help='classes.dex or classes.zip for the static library APK that was ' |
| 60 | 'proguarded with other dependent APKs') |
| 61 | parser.add_argument( |
| 62 | '--static-library-dependent-dex', |
| 63 | required=True, |
| 64 | action='append', |
| 65 | dest='static_library_dependent_dexes', |
| 66 | help='classes.dex or classes.zip for the APKs that use the static ' |
| 67 | 'library APK') |
| 68 | args = parser.parse_args(args) |
| 69 | |
| 70 | static_library_dexfiles = _DexFilesFromPath(args.static_library_dex) |
| 71 | for path in args.static_library_dependent_dexes: |
| 72 | dependent_dexfiles = _DexFilesFromPath(path) |
| 73 | illegal_references = _FindIllegalStaticLibraryReferences( |
| 74 | static_library_dexfiles, dependent_dexfiles) |
| 75 | |
| 76 | if illegal_references: |
| 77 | msg = 'Found illegal references from {} to {}\n'.format( |
| 78 | args.static_library_dex, path) |
| 79 | msg += 'Add a -keep rule to avoid this. ' |
| 80 | msg += 'See {} for an example and why this is necessary.\n'.format( |
| 81 | _FLAGS_PATH) |
| 82 | msg += 'The illegal references are:\n' |
| 83 | msg += '\n'.join(illegal_references) |
| 84 | sys.stderr.write(msg) |
| 85 | sys.exit(1) |
| 86 | |
| 87 | input_paths = [args.static_library_dex] + args.static_library_dependent_dexes |
| 88 | build_utils.Touch(args.stamp) |
| 89 | build_utils.WriteDepfile(args.depfile, args.stamp, inputs=input_paths) |
| 90 | |
| 91 | |
| 92 | if __name__ == '__main__': |
| 93 | sys.exit(main(sys.argv[1:])) |