• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
18The patterns are generated from the modular flags produced by the
19bootclasspath_fragment and are used to select a subset of the monolithic flags
20against which the modular flags can be compared.
21"""
22
23import argparse
24import csv
25import sys
26
27
28def dict_reader(csv_file):
29    return csv.DictReader(
30        csv_file, delimiter=',', quotechar='|', fieldnames=['signature'])
31
32
33def dot_package_to_slash_package(pkg):
34    return pkg.replace('.', '/')
35
36
37def 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):
42    return pkg.replace('/', '.')
43
44
45def slash_packages_to_dot_packages(pkgs):
46    return [slash_package_to_dot_package(p) for p in pkgs]
47
48
49def 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:
55        if prefix == packagePrefix:
56            return packagePrefix
57        if (prefix.startswith(packagePrefix) and
58                prefix[len(packagePrefix)] == '/'):
59            return packagePrefix
60    return False
61
62
63def validate_package_is_not_matched_by_package_prefix(package_type, pkg,
64                                                      package_prefixes):
65    package_prefix = matched_by_package_prefix_pattern(package_prefixes, pkg)
66    if package_prefix:
67        # A package prefix matches the package.
68        package_for_output = slash_package_to_dot_package(pkg)
69        package_prefix_for_output = slash_package_to_dot_package(package_prefix)
70        return [
71            f'{package_type} {package_for_output} is matched by '
72            f'package prefix {package_prefix_for_output}'
73        ]
74    return []
75
76
77def validate_package_prefixes(split_packages, single_packages,
78                              package_prefixes):
79    # If there are no package prefixes then there is no possible conflict
80    # between them and the split packages.
81    if len(package_prefixes) == 0:
82        return []
83
84    # Check to make sure that the split packages and package prefixes do not
85    # overlap.
86    errors = []
87    for split_package in split_packages:
88        if split_package == '*':
89            # A package prefix matches a split package.
90            package_prefixes_for_output = ', '.join(
91                slash_packages_to_dot_packages(package_prefixes))
92            errors.append(
93                "split package '*' conflicts with all package prefixes "
94                f'{package_prefixes_for_output}\n'
95                '    add split_packages:[] to fix')
96        else:
97            errs = validate_package_is_not_matched_by_package_prefix(
98                'split package', split_package, package_prefixes)
99            errors.extend(errs)
100
101    # Check to make sure that the single packages and package prefixes do not
102    # overlap.
103    for single_package in single_packages:
104        errs = validate_package_is_not_matched_by_package_prefix(
105            'single package', single_package, package_prefixes)
106        errors.extend(errs)
107    return errors
108
109
110def validate_split_packages(split_packages):
111    errors = []
112    if '*' in split_packages and len(split_packages) > 1:
113        errors.append('split packages are invalid as they contain both the'
114                      ' wildcard (*) and specific packages, use the wildcard or'
115                      ' specific packages, not a mixture')
116    return errors
117
118
119def validate_single_packages(split_packages, single_packages):
120    overlaps = []
121    for single_package in single_packages:
122        if single_package in split_packages:
123            overlaps.append(single_package)
124    if overlaps:
125        indented = ''.join([f'\n    {o}' for o in overlaps])
126        return [
127            f'single_packages and split_packages overlap, please ensure the '
128            f'following packages are only present in one:{indented}'
129        ]
130    return []
131
132
133def produce_patterns_from_file(file,
134                               split_packages=None,
135                               single_packages=None,
136                               package_prefixes=None):
137    with open(file, 'r', encoding='utf8') as f:
138        return produce_patterns_from_stream(f, split_packages, single_packages,
139                                            package_prefixes)
140
141
142def produce_patterns_from_stream(stream,
143                                 split_packages=None,
144                                 single_packages=None,
145                                 package_prefixes=None):
146    split_packages = set(split_packages or [])
147    single_packages = set(single_packages or [])
148    package_prefixes = list(package_prefixes or [])
149    # Read in all the signatures into a list and remove any unnecessary class
150    # and member names.
151    patterns = set()
152    unmatched_packages = set()
153    for row in dict_reader(stream):
154        signature = row['signature']
155        text = signature.removeprefix('L')
156        # Remove the class specific member signature
157        pieces = text.split(';->')
158        qualified_class_name = pieces[0]
159        pieces = qualified_class_name.rsplit('/', maxsplit=1)
160        pkg = pieces[0]
161        # If the package is split across multiple modules then it cannot be used
162        # to select the subset of the monolithic flags that this module
163        # produces. In that case we need to keep the name of the class but can
164        # discard any nested class names as an outer class cannot be split
165        # across modules.
166        #
167        # If the package is not split then every class in the package must be
168        # provided by this module so there is no need to list the classes
169        # explicitly so just use the package name instead.
170        if is_split_package(split_packages, pkg):
171            # Remove inner class names.
172            pieces = qualified_class_name.split('$', maxsplit=1)
173            pattern = pieces[0]
174            patterns.add(pattern)
175        elif pkg in single_packages:
176            # Add a * to ensure that the pattern matches the classes in that
177            # package.
178            pattern = pkg + '/*'
179            patterns.add(pattern)
180        else:
181            unmatched_packages.add(pkg)
182
183    # Remove any unmatched packages that would be matched by a package prefix
184    # pattern.
185    unmatched_packages = [
186        p for p in unmatched_packages
187        if not matched_by_package_prefix_pattern(package_prefixes, p)
188    ]
189    errors = []
190    if unmatched_packages:
191        unmatched_packages.sort()
192        indented = ''.join([
193            f'\n    {slash_package_to_dot_package(p)}'
194            for p in unmatched_packages
195        ])
196        errors.append('The following packages were unexpected, please add them '
197                      'to one of the hidden_api properties, split_packages, '
198                      f'single_packages or package_prefixes:{indented}')
199
200    # Remove any patterns that would be matched by a package prefix pattern.
201    patterns = [
202        p for p in patterns
203        if not matched_by_package_prefix_pattern(package_prefixes, p)
204    ]
205    # Add the package prefix patterns to the list. Add a ** to ensure that each
206    # package prefix pattern will match the classes in that package and all
207    # sub-packages.
208    patterns = patterns + [f'{p}/**' for p in package_prefixes]
209    # Sort the patterns.
210    patterns.sort()
211    return patterns, errors
212
213
214def print_and_exit(errors):
215    for error in errors:
216        print(error)
217    sys.exit(1)
218
219
220def main(args):
221    args_parser = argparse.ArgumentParser(
222        description='Generate a set of signature patterns '
223        'that select a subset of monolithic hidden API files.')
224    args_parser.add_argument(
225        '--flags',
226        help='The stub flags file which contains an entry for every dex member',
227    )
228    args_parser.add_argument(
229        '--split-package',
230        action='append',
231        help='A package that is split across multiple bootclasspath_fragment '
232        'modules')
233    args_parser.add_argument(
234        '--package-prefix',
235        action='append',
236        help='A package prefix unique to this set of flags')
237    args_parser.add_argument(
238        '--single-package',
239        action='append',
240        help='A single package unique to this set of flags')
241    args_parser.add_argument('--output', help='Generated signature prefixes')
242    args = args_parser.parse_args(args)
243
244    split_packages = set(
245        dot_packages_to_slash_packages(args.split_package or []))
246    errors = validate_split_packages(split_packages)
247    if errors:
248        print_and_exit(errors)
249
250    single_packages = list(
251        dot_packages_to_slash_packages(args.single_package or []))
252
253    errors = validate_single_packages(split_packages, single_packages)
254    if errors:
255        print_and_exit(errors)
256
257    package_prefixes = dot_packages_to_slash_packages(args.package_prefix or [])
258
259    errors = validate_package_prefixes(split_packages, single_packages,
260                                       package_prefixes)
261    if errors:
262        print_and_exit(errors)
263
264    patterns = []
265    # Read in all the patterns into a list.
266    patterns, errors = produce_patterns_from_file(args.flags, split_packages,
267                                                  single_packages,
268                                                  package_prefixes)
269
270    if errors:
271        print_and_exit(errors)
272
273    # Write out all the patterns.
274    with open(args.output, 'w', encoding='utf8') as outputFile:
275        for pattern in patterns:
276            outputFile.write(pattern)
277            outputFile.write('\n')
278
279
280if __name__ == '__main__':
281    main(sys.argv[1:])
282