blob: 1abdef1146d9c2eebf5746e57b6c8bd209676d83 [file] [log] [blame]
Paul Duffin67b9d612021-07-21 17:38:47 +01001#!/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.
Paul Duffin1e18e982021-08-03 15:42:27 +010016"""Generate a set of signature patterns for a bootclasspath_fragment.
17
18The patterns are generated from the modular flags produced by the
19bootclasspath_fragment and are used to select a subset of the monolithic flags
Paul Duffin67b9d612021-07-21 17:38:47 +010020against which the modular flags can be compared.
21"""
22
23import argparse
24import csv
Spandan Das2c2219b2021-08-25 17:47:43 +000025import sys
Paul Duffin67b9d612021-07-21 17:38:47 +010026
Paul Duffin1e18e982021-08-03 15:42:27 +010027
Paul Duffin1024f1d2022-03-15 17:45:57 +000028def dict_reader(csv_file):
Spandan Das2c2219b2021-08-25 17:47:43 +000029 return csv.DictReader(
Paul Duffin1024f1d2022-03-15 17:45:57 +000030 csv_file, delimiter=',', quotechar='|', fieldnames=['signature'])
Spandan Das2c2219b2021-08-25 17:47:43 +000031
Paul Duffin67b9d612021-07-21 17:38:47 +010032
Paul Duffin1024f1d2022-03-15 17:45:57 +000033def dot_package_to_slash_package(pkg):
Paul Duffin1e18e982021-08-03 15:42:27 +010034 return pkg.replace('.', '/')
35
36
Paul Duffin1024f1d2022-03-15 17:45:57 +000037def dot_packages_to_slash_packages(pkgs):
38 return [dot_package_to_slash_package(p) for p in pkgs]
39
40
41def slash_package_to_dot_package(pkg):
Paul Duffin1e18e982021-08-03 15:42:27 +010042 return pkg.replace('/', '.')
43
44
Paul Duffin1024f1d2022-03-15 17:45:57 +000045def slash_packages_to_dot_packages(pkgs):
46 return [slash_package_to_dot_package(p) for p in pkgs]
Paul Duffin1e18e982021-08-03 15:42:27 +010047
48
Paul Duffin1024f1d2022-03-15 17:45:57 +000049def is_split_package(split_packages, pkg):
50 return split_packages and (pkg in split_packages or '*' in split_packages)
51
52
53def matched_by_package_prefix_pattern(package_prefixes, prefix):
54 for packagePrefix in package_prefixes:
Paul Duffin1e18e982021-08-03 15:42:27 +010055 if prefix == packagePrefix:
56 return packagePrefix
Paul Duffin1024f1d2022-03-15 17:45:57 +000057 if (prefix.startswith(packagePrefix) and
58 prefix[len(packagePrefix)] == '/'):
Paul Duffin1e18e982021-08-03 15:42:27 +010059 return packagePrefix
60 return False
61
62
Paul Duffin1024f1d2022-03-15 17:45:57 +000063def validate_package_prefixes(split_packages, package_prefixes):
Paul Duffin1e18e982021-08-03 15:42:27 +010064 # If there are no package prefixes then there is no possible conflict
65 # between them and the split packages.
Paul Duffin1024f1d2022-03-15 17:45:57 +000066 if len(package_prefixes) == 0:
67 return []
Paul Duffin1e18e982021-08-03 15:42:27 +010068
69 # Check to make sure that the split packages and package prefixes do not
70 # overlap.
71 errors = []
Paul Duffin1024f1d2022-03-15 17:45:57 +000072 for split_package in split_packages:
73 if split_package == '*':
Paul Duffin1e18e982021-08-03 15:42:27 +010074 # A package prefix matches a split package.
Paul Duffin1024f1d2022-03-15 17:45:57 +000075 package_prefixes_for_output = ', '.join(
76 slash_packages_to_dot_packages(package_prefixes))
Paul Duffin1e18e982021-08-03 15:42:27 +010077 errors.append(
Paul Duffin1024f1d2022-03-15 17:45:57 +000078 "split package '*' conflicts with all package prefixes "
79 f'{package_prefixes_for_output}\n'
80 ' add split_packages:[] to fix')
Paul Duffin1e18e982021-08-03 15:42:27 +010081 else:
Paul Duffin1024f1d2022-03-15 17:45:57 +000082 package_prefix = matched_by_package_prefix_pattern(
83 package_prefixes, split_package)
84 if package_prefix:
Paul Duffin1e18e982021-08-03 15:42:27 +010085 # A package prefix matches a split package.
Paul Duffin1024f1d2022-03-15 17:45:57 +000086 split_package_for_output = slash_package_to_dot_package(
87 split_package)
88 package_prefix_for_output = slash_package_to_dot_package(
89 package_prefix)
Paul Duffin1e18e982021-08-03 15:42:27 +010090 errors.append(
Paul Duffin1024f1d2022-03-15 17:45:57 +000091 f'split package {split_package_for_output} is matched by '
92 f'package prefix {package_prefix_for_output}')
Paul Duffin1e18e982021-08-03 15:42:27 +010093 return errors
94
95
Paul Duffin1024f1d2022-03-15 17:45:57 +000096def validate_split_packages(split_packages):
Paul Duffin1e18e982021-08-03 15:42:27 +010097 errors = []
Paul Duffin1024f1d2022-03-15 17:45:57 +000098 if '*' in split_packages and len(split_packages) > 1:
Paul Duffin1e18e982021-08-03 15:42:27 +010099 errors.append('split packages are invalid as they contain both the'
100 ' wildcard (*) and specific packages, use the wildcard or'
101 ' specific packages, not a mixture')
102 return errors
103
104
Paul Duffin1024f1d2022-03-15 17:45:57 +0000105def produce_patterns_from_file(file,
106 split_packages=None,
107 package_prefixes=None):
108 with open(file, 'r', encoding='utf8') as f:
109 return produce_patterns_from_stream(f, split_packages, package_prefixes)
Paul Duffin67b9d612021-07-21 17:38:47 +0100110
Spandan Das2c2219b2021-08-25 17:47:43 +0000111
Paul Duffin1e18e982021-08-03 15:42:27 +0100112def produce_patterns_from_stream(stream,
Paul Duffin1024f1d2022-03-15 17:45:57 +0000113 split_packages=None,
114 package_prefixes=None):
115 split_packages = set(split_packages or [])
116 package_prefixes = list(package_prefixes or [])
Paul Duffin1e18e982021-08-03 15:42:27 +0100117 # Read in all the signatures into a list and remove any unnecessary class
118 # and member names.
Paul Duffin6ffdff82021-08-09 13:47:19 +0100119 patterns = set()
120 for row in dict_reader(stream):
Paul Duffin67b9d612021-07-21 17:38:47 +0100121 signature = row['signature']
Paul Duffin1e18e982021-08-03 15:42:27 +0100122 text = signature.removeprefix('L')
Paul Duffin6ffdff82021-08-09 13:47:19 +0100123 # Remove the class specific member signature
Paul Duffin1e18e982021-08-03 15:42:27 +0100124 pieces = text.split(';->')
Paul Duffin1024f1d2022-03-15 17:45:57 +0000125 qualified_class_name = pieces[0]
126 pieces = qualified_class_name.rsplit('/', maxsplit=1)
Paul Duffin1e18e982021-08-03 15:42:27 +0100127 pkg = pieces[0]
128 # If the package is split across multiple modules then it cannot be used
129 # to select the subset of the monolithic flags that this module
130 # produces. In that case we need to keep the name of the class but can
131 # discard any nested class names as an outer class cannot be split
132 # across modules.
133 #
134 # If the package is not split then every class in the package must be
135 # provided by this module so there is no need to list the classes
136 # explicitly so just use the package name instead.
Paul Duffin1024f1d2022-03-15 17:45:57 +0000137 if is_split_package(split_packages, pkg):
Paul Duffin1e18e982021-08-03 15:42:27 +0100138 # Remove inner class names.
Paul Duffin1024f1d2022-03-15 17:45:57 +0000139 pieces = qualified_class_name.split('$', maxsplit=1)
Paul Duffin1e18e982021-08-03 15:42:27 +0100140 pattern = pieces[0]
141 else:
142 # Add a * to ensure that the pattern matches the classes in that
143 # package.
144 pattern = pkg + '/*'
Paul Duffin6ffdff82021-08-09 13:47:19 +0100145 patterns.add(pattern)
146
Paul Duffin1e18e982021-08-03 15:42:27 +0100147 # Remove any patterns that would be matched by a package prefix pattern.
Paul Duffin1024f1d2022-03-15 17:45:57 +0000148 patterns = [
149 p for p in patterns
150 if not matched_by_package_prefix_pattern(package_prefixes, p)
151 ]
Paul Duffin1e18e982021-08-03 15:42:27 +0100152 # Add the package prefix patterns to the list. Add a ** to ensure that each
153 # package prefix pattern will match the classes in that package and all
154 # sub-packages.
Paul Duffin1024f1d2022-03-15 17:45:57 +0000155 patterns = patterns + [f'{p}/**' for p in package_prefixes]
Paul Duffin1e18e982021-08-03 15:42:27 +0100156 # Sort the patterns.
Paul Duffin6ffdff82021-08-09 13:47:19 +0100157 patterns.sort()
Paul Duffin67b9d612021-07-21 17:38:47 +0100158 return patterns
159
Spandan Das2c2219b2021-08-25 17:47:43 +0000160
Paul Duffin67b9d612021-07-21 17:38:47 +0100161def main(args):
Spandan Das2c2219b2021-08-25 17:47:43 +0000162 args_parser = argparse.ArgumentParser(
163 description='Generate a set of signature patterns '
Paul Duffin1e18e982021-08-03 15:42:27 +0100164 'that select a subset of monolithic hidden API files.')
Spandan Das2c2219b2021-08-25 17:47:43 +0000165 args_parser.add_argument(
166 '--flags',
167 help='The stub flags file which contains an entry for every dex member',
168 )
Paul Duffin1e18e982021-08-03 15:42:27 +0100169 args_parser.add_argument(
170 '--split-package',
171 action='append',
Paul Duffin1024f1d2022-03-15 17:45:57 +0000172 help='A package that is split across multiple bootclasspath_fragment '
173 'modules')
Paul Duffin1e18e982021-08-03 15:42:27 +0100174 args_parser.add_argument(
175 '--package-prefix',
176 action='append',
177 help='A package prefix unique to this set of flags')
Paul Duffin67b9d612021-07-21 17:38:47 +0100178 args_parser.add_argument('--output', help='Generated signature prefixes')
179 args = args_parser.parse_args(args)
180
Paul Duffin1024f1d2022-03-15 17:45:57 +0000181 split_packages = set(
182 dot_packages_to_slash_packages(args.split_package or []))
183 errors = validate_split_packages(split_packages)
Paul Duffin1e18e982021-08-03 15:42:27 +0100184
Paul Duffin1024f1d2022-03-15 17:45:57 +0000185 package_prefixes = dot_packages_to_slash_packages(args.package_prefix or [])
Paul Duffin1e18e982021-08-03 15:42:27 +0100186
187 if not errors:
Paul Duffin1024f1d2022-03-15 17:45:57 +0000188 errors = validate_package_prefixes(split_packages, package_prefixes)
Paul Duffin1e18e982021-08-03 15:42:27 +0100189
190 if errors:
191 for error in errors:
192 print(error)
193 sys.exit(1)
194
Paul Duffin67b9d612021-07-21 17:38:47 +0100195 # Read in all the patterns into a list.
Paul Duffin1024f1d2022-03-15 17:45:57 +0000196 patterns = produce_patterns_from_file(args.flags, split_packages,
197 package_prefixes)
Paul Duffin67b9d612021-07-21 17:38:47 +0100198
199 # Write out all the patterns.
Paul Duffin1024f1d2022-03-15 17:45:57 +0000200 with open(args.output, 'w', encoding='utf8') as outputFile:
Paul Duffin67b9d612021-07-21 17:38:47 +0100201 for pattern in patterns:
202 outputFile.write(pattern)
Paul Duffin1e18e982021-08-03 15:42:27 +0100203 outputFile.write('\n')
Paul Duffin67b9d612021-07-21 17:38:47 +0100204
Spandan Das2c2219b2021-08-25 17:47:43 +0000205
Paul Duffin1e18e982021-08-03 15:42:27 +0100206if __name__ == '__main__':
Paul Duffin67b9d612021-07-21 17:38:47 +0100207 main(sys.argv[1:])