signature_patterns.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. #!/usr/bin/env python
  2. #
  3. # Copyright (C) 2021 The Android Open Source Project
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. """Generate a set of signature patterns for a bootclasspath_fragment.
  17. The patterns are generated from the modular flags produced by the
  18. bootclasspath_fragment and are used to select a subset of the monolithic flags
  19. against which the modular flags can be compared.
  20. """
  21. import argparse
  22. import csv
  23. import sys
  24. def dict_reader(csv_file):
  25. return csv.DictReader(
  26. csv_file, delimiter=',', quotechar='|', fieldnames=['signature'])
  27. def dot_package_to_slash_package(pkg):
  28. return pkg.replace('.', '/')
  29. def dot_packages_to_slash_packages(pkgs):
  30. return [dot_package_to_slash_package(p) for p in pkgs]
  31. def slash_package_to_dot_package(pkg):
  32. return pkg.replace('/', '.')
  33. def slash_packages_to_dot_packages(pkgs):
  34. return [slash_package_to_dot_package(p) for p in pkgs]
  35. def is_split_package(split_packages, pkg):
  36. return split_packages and (pkg in split_packages or '*' in split_packages)
  37. def matched_by_package_prefix_pattern(package_prefixes, prefix):
  38. for packagePrefix in package_prefixes:
  39. if prefix == packagePrefix:
  40. return packagePrefix
  41. if (prefix.startswith(packagePrefix) and
  42. prefix[len(packagePrefix)] == '/'):
  43. return packagePrefix
  44. return False
  45. def validate_package_is_not_matched_by_package_prefix(package_type, pkg,
  46. package_prefixes):
  47. package_prefix = matched_by_package_prefix_pattern(package_prefixes, pkg)
  48. if package_prefix:
  49. # A package prefix matches the package.
  50. package_for_output = slash_package_to_dot_package(pkg)
  51. package_prefix_for_output = slash_package_to_dot_package(package_prefix)
  52. return [
  53. f'{package_type} {package_for_output} is matched by '
  54. f'package prefix {package_prefix_for_output}'
  55. ]
  56. return []
  57. def validate_package_prefixes(split_packages, single_packages,
  58. package_prefixes):
  59. # If there are no package prefixes then there is no possible conflict
  60. # between them and the split packages.
  61. if len(package_prefixes) == 0:
  62. return []
  63. # Check to make sure that the split packages and package prefixes do not
  64. # overlap.
  65. errors = []
  66. for split_package in split_packages:
  67. if split_package == '*':
  68. # A package prefix matches a split package.
  69. package_prefixes_for_output = ', '.join(
  70. slash_packages_to_dot_packages(package_prefixes))
  71. errors.append(
  72. "split package '*' conflicts with all package prefixes "
  73. f'{package_prefixes_for_output}\n'
  74. ' add split_packages:[] to fix')
  75. else:
  76. errs = validate_package_is_not_matched_by_package_prefix(
  77. 'split package', split_package, package_prefixes)
  78. errors.extend(errs)
  79. # Check to make sure that the single packages and package prefixes do not
  80. # overlap.
  81. for single_package in single_packages:
  82. errs = validate_package_is_not_matched_by_package_prefix(
  83. 'single package', single_package, package_prefixes)
  84. errors.extend(errs)
  85. return errors
  86. def validate_split_packages(split_packages):
  87. errors = []
  88. if '*' in split_packages and len(split_packages) > 1:
  89. errors.append('split packages are invalid as they contain both the'
  90. ' wildcard (*) and specific packages, use the wildcard or'
  91. ' specific packages, not a mixture')
  92. return errors
  93. def validate_single_packages(split_packages, single_packages):
  94. overlaps = []
  95. for single_package in single_packages:
  96. if single_package in split_packages:
  97. overlaps.append(single_package)
  98. if overlaps:
  99. indented = ''.join([f'\n {o}' for o in overlaps])
  100. return [
  101. f'single_packages and split_packages overlap, please ensure the '
  102. f'following packages are only present in one:{indented}'
  103. ]
  104. return []
  105. def produce_patterns_from_file(file,
  106. split_packages=None,
  107. single_packages=None,
  108. package_prefixes=None):
  109. with open(file, 'r', encoding='utf8') as f:
  110. return produce_patterns_from_stream(f, split_packages, single_packages,
  111. package_prefixes)
  112. def produce_patterns_from_stream(stream,
  113. split_packages=None,
  114. single_packages=None,
  115. package_prefixes=None):
  116. split_packages = set(split_packages or [])
  117. single_packages = set(single_packages or [])
  118. package_prefixes = list(package_prefixes or [])
  119. # Read in all the signatures into a list and remove any unnecessary class
  120. # and member names.
  121. patterns = set()
  122. unmatched_packages = set()
  123. for row in dict_reader(stream):
  124. signature = row['signature']
  125. text = signature.removeprefix('L')
  126. # Remove the class specific member signature
  127. pieces = text.split(';->')
  128. qualified_class_name = pieces[0]
  129. pieces = qualified_class_name.rsplit('/', maxsplit=1)
  130. pkg = pieces[0]
  131. # If the package is split across multiple modules then it cannot be used
  132. # to select the subset of the monolithic flags that this module
  133. # produces. In that case we need to keep the name of the class but can
  134. # discard any nested class names as an outer class cannot be split
  135. # across modules.
  136. #
  137. # If the package is not split then every class in the package must be
  138. # provided by this module so there is no need to list the classes
  139. # explicitly so just use the package name instead.
  140. if is_split_package(split_packages, pkg):
  141. # Remove inner class names.
  142. pieces = qualified_class_name.split('$', maxsplit=1)
  143. pattern = pieces[0]
  144. patterns.add(pattern)
  145. elif pkg in single_packages:
  146. # Add a * to ensure that the pattern matches the classes in that
  147. # package.
  148. pattern = pkg + '/*'
  149. patterns.add(pattern)
  150. else:
  151. unmatched_packages.add(pkg)
  152. # Remove any unmatched packages that would be matched by a package prefix
  153. # pattern.
  154. unmatched_packages = [
  155. p for p in unmatched_packages
  156. if not matched_by_package_prefix_pattern(package_prefixes, p)
  157. ]
  158. errors = []
  159. if unmatched_packages:
  160. unmatched_packages.sort()
  161. indented = ''.join([
  162. f'\n {slash_package_to_dot_package(p)}'
  163. for p in unmatched_packages
  164. ])
  165. errors.append('The following packages were unexpected, please add them '
  166. 'to one of the hidden_api properties, split_packages, '
  167. f'single_packages or package_prefixes:{indented}')
  168. # Remove any patterns that would be matched by a package prefix pattern.
  169. patterns = [
  170. p for p in patterns
  171. if not matched_by_package_prefix_pattern(package_prefixes, p)
  172. ]
  173. # Add the package prefix patterns to the list. Add a ** to ensure that each
  174. # package prefix pattern will match the classes in that package and all
  175. # sub-packages.
  176. patterns = patterns + [f'{p}/**' for p in package_prefixes]
  177. # Sort the patterns.
  178. patterns.sort()
  179. return patterns, errors
  180. def print_and_exit(errors):
  181. for error in errors:
  182. print(error)
  183. sys.exit(1)
  184. def main(args):
  185. args_parser = argparse.ArgumentParser(
  186. description='Generate a set of signature patterns '
  187. 'that select a subset of monolithic hidden API files.')
  188. args_parser.add_argument(
  189. '--flags',
  190. help='The stub flags file which contains an entry for every dex member',
  191. )
  192. args_parser.add_argument(
  193. '--split-package',
  194. action='append',
  195. help='A package that is split across multiple bootclasspath_fragment '
  196. 'modules')
  197. args_parser.add_argument(
  198. '--package-prefix',
  199. action='append',
  200. help='A package prefix unique to this set of flags')
  201. args_parser.add_argument(
  202. '--single-package',
  203. action='append',
  204. help='A single package unique to this set of flags')
  205. args_parser.add_argument('--output', help='Generated signature prefixes')
  206. args = args_parser.parse_args(args)
  207. split_packages = set(
  208. dot_packages_to_slash_packages(args.split_package or []))
  209. errors = validate_split_packages(split_packages)
  210. if errors:
  211. print_and_exit(errors)
  212. single_packages = list(
  213. dot_packages_to_slash_packages(args.single_package or []))
  214. errors = validate_single_packages(split_packages, single_packages)
  215. if errors:
  216. print_and_exit(errors)
  217. package_prefixes = dot_packages_to_slash_packages(args.package_prefix or [])
  218. errors = validate_package_prefixes(split_packages, single_packages,
  219. package_prefixes)
  220. if errors:
  221. print_and_exit(errors)
  222. patterns = []
  223. # Read in all the patterns into a list.
  224. patterns, errors = produce_patterns_from_file(args.flags, split_packages,
  225. single_packages,
  226. package_prefixes)
  227. if errors:
  228. print_and_exit(errors)
  229. # Write out all the patterns.
  230. with open(args.output, 'w', encoding='utf8') as outputFile:
  231. for pattern in patterns:
  232. outputFile.write(pattern)
  233. outputFile.write('\n')
  234. if __name__ == '__main__':
  235. main(sys.argv[1:])