Paul Duffin | 67b9d61 | 2021-07-21 17:38:47 +0100 | [diff] [blame] | 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. |
Paul Duffin | 1e18e98 | 2021-08-03 15:42:27 +0100 | [diff] [blame] | 16 | """Generate a set of signature patterns for a bootclasspath_fragment. |
| 17 | |
| 18 | The patterns are generated from the modular flags produced by the |
| 19 | bootclasspath_fragment and are used to select a subset of the monolithic flags |
Paul Duffin | 67b9d61 | 2021-07-21 17:38:47 +0100 | [diff] [blame] | 20 | against which the modular flags can be compared. |
| 21 | """ |
| 22 | |
| 23 | import argparse |
| 24 | import csv |
Spandan Das | 2c2219b | 2021-08-25 17:47:43 +0000 | [diff] [blame] | 25 | import sys |
Paul Duffin | 67b9d61 | 2021-07-21 17:38:47 +0100 | [diff] [blame] | 26 | |
Paul Duffin | 1e18e98 | 2021-08-03 15:42:27 +0100 | [diff] [blame] | 27 | |
Paul Duffin | 1024f1d | 2022-03-15 17:45:57 +0000 | [diff] [blame] | 28 | def dict_reader(csv_file): |
Spandan Das | 2c2219b | 2021-08-25 17:47:43 +0000 | [diff] [blame] | 29 | return csv.DictReader( |
Paul Duffin | 1024f1d | 2022-03-15 17:45:57 +0000 | [diff] [blame] | 30 | csv_file, delimiter=',', quotechar='|', fieldnames=['signature']) |
Spandan Das | 2c2219b | 2021-08-25 17:47:43 +0000 | [diff] [blame] | 31 | |
Paul Duffin | 67b9d61 | 2021-07-21 17:38:47 +0100 | [diff] [blame] | 32 | |
Paul Duffin | 1024f1d | 2022-03-15 17:45:57 +0000 | [diff] [blame] | 33 | def dot_package_to_slash_package(pkg): |
Paul Duffin | 1e18e98 | 2021-08-03 15:42:27 +0100 | [diff] [blame] | 34 | return pkg.replace('.', '/') |
| 35 | |
| 36 | |
Paul Duffin | 1024f1d | 2022-03-15 17:45:57 +0000 | [diff] [blame] | 37 | def dot_packages_to_slash_packages(pkgs): |
| 38 | return [dot_package_to_slash_package(p) for p in pkgs] |
| 39 | |
| 40 | |
| 41 | def slash_package_to_dot_package(pkg): |
Paul Duffin | 1e18e98 | 2021-08-03 15:42:27 +0100 | [diff] [blame] | 42 | return pkg.replace('/', '.') |
| 43 | |
| 44 | |
Paul Duffin | 1024f1d | 2022-03-15 17:45:57 +0000 | [diff] [blame] | 45 | def slash_packages_to_dot_packages(pkgs): |
| 46 | return [slash_package_to_dot_package(p) for p in pkgs] |
Paul Duffin | 1e18e98 | 2021-08-03 15:42:27 +0100 | [diff] [blame] | 47 | |
| 48 | |
Paul Duffin | 1024f1d | 2022-03-15 17:45:57 +0000 | [diff] [blame] | 49 | def is_split_package(split_packages, pkg): |
| 50 | return split_packages and (pkg in split_packages or '*' in split_packages) |
| 51 | |
| 52 | |
| 53 | def matched_by_package_prefix_pattern(package_prefixes, prefix): |
| 54 | for packagePrefix in package_prefixes: |
Paul Duffin | 1e18e98 | 2021-08-03 15:42:27 +0100 | [diff] [blame] | 55 | if prefix == packagePrefix: |
| 56 | return packagePrefix |
Paul Duffin | 1024f1d | 2022-03-15 17:45:57 +0000 | [diff] [blame] | 57 | if (prefix.startswith(packagePrefix) and |
| 58 | prefix[len(packagePrefix)] == '/'): |
Paul Duffin | 1e18e98 | 2021-08-03 15:42:27 +0100 | [diff] [blame] | 59 | return packagePrefix |
| 60 | return False |
| 61 | |
| 62 | |
Paul Duffin | 1024f1d | 2022-03-15 17:45:57 +0000 | [diff] [blame] | 63 | def validate_package_prefixes(split_packages, package_prefixes): |
Paul Duffin | 1e18e98 | 2021-08-03 15:42:27 +0100 | [diff] [blame] | 64 | # If there are no package prefixes then there is no possible conflict |
| 65 | # between them and the split packages. |
Paul Duffin | 1024f1d | 2022-03-15 17:45:57 +0000 | [diff] [blame] | 66 | if len(package_prefixes) == 0: |
| 67 | return [] |
Paul Duffin | 1e18e98 | 2021-08-03 15:42:27 +0100 | [diff] [blame] | 68 | |
| 69 | # Check to make sure that the split packages and package prefixes do not |
| 70 | # overlap. |
| 71 | errors = [] |
Paul Duffin | 1024f1d | 2022-03-15 17:45:57 +0000 | [diff] [blame] | 72 | for split_package in split_packages: |
| 73 | if split_package == '*': |
Paul Duffin | 1e18e98 | 2021-08-03 15:42:27 +0100 | [diff] [blame] | 74 | # A package prefix matches a split package. |
Paul Duffin | 1024f1d | 2022-03-15 17:45:57 +0000 | [diff] [blame] | 75 | package_prefixes_for_output = ', '.join( |
| 76 | slash_packages_to_dot_packages(package_prefixes)) |
Paul Duffin | 1e18e98 | 2021-08-03 15:42:27 +0100 | [diff] [blame] | 77 | errors.append( |
Paul Duffin | 1024f1d | 2022-03-15 17:45:57 +0000 | [diff] [blame] | 78 | "split package '*' conflicts with all package prefixes " |
| 79 | f'{package_prefixes_for_output}\n' |
| 80 | ' add split_packages:[] to fix') |
Paul Duffin | 1e18e98 | 2021-08-03 15:42:27 +0100 | [diff] [blame] | 81 | else: |
Paul Duffin | 1024f1d | 2022-03-15 17:45:57 +0000 | [diff] [blame] | 82 | package_prefix = matched_by_package_prefix_pattern( |
| 83 | package_prefixes, split_package) |
| 84 | if package_prefix: |
Paul Duffin | 1e18e98 | 2021-08-03 15:42:27 +0100 | [diff] [blame] | 85 | # A package prefix matches a split package. |
Paul Duffin | 1024f1d | 2022-03-15 17:45:57 +0000 | [diff] [blame] | 86 | 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 Duffin | 1e18e98 | 2021-08-03 15:42:27 +0100 | [diff] [blame] | 90 | errors.append( |
Paul Duffin | 1024f1d | 2022-03-15 17:45:57 +0000 | [diff] [blame] | 91 | f'split package {split_package_for_output} is matched by ' |
| 92 | f'package prefix {package_prefix_for_output}') |
Paul Duffin | 1e18e98 | 2021-08-03 15:42:27 +0100 | [diff] [blame] | 93 | return errors |
| 94 | |
| 95 | |
Paul Duffin | 1024f1d | 2022-03-15 17:45:57 +0000 | [diff] [blame] | 96 | def validate_split_packages(split_packages): |
Paul Duffin | 1e18e98 | 2021-08-03 15:42:27 +0100 | [diff] [blame] | 97 | errors = [] |
Paul Duffin | 1024f1d | 2022-03-15 17:45:57 +0000 | [diff] [blame] | 98 | if '*' in split_packages and len(split_packages) > 1: |
Paul Duffin | 1e18e98 | 2021-08-03 15:42:27 +0100 | [diff] [blame] | 99 | 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 Duffin | 1024f1d | 2022-03-15 17:45:57 +0000 | [diff] [blame] | 105 | def 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 Duffin | 67b9d61 | 2021-07-21 17:38:47 +0100 | [diff] [blame] | 110 | |
Spandan Das | 2c2219b | 2021-08-25 17:47:43 +0000 | [diff] [blame] | 111 | |
Paul Duffin | 1e18e98 | 2021-08-03 15:42:27 +0100 | [diff] [blame] | 112 | def produce_patterns_from_stream(stream, |
Paul Duffin | 1024f1d | 2022-03-15 17:45:57 +0000 | [diff] [blame] | 113 | split_packages=None, |
| 114 | package_prefixes=None): |
| 115 | split_packages = set(split_packages or []) |
| 116 | package_prefixes = list(package_prefixes or []) |
Paul Duffin | 1e18e98 | 2021-08-03 15:42:27 +0100 | [diff] [blame] | 117 | # Read in all the signatures into a list and remove any unnecessary class |
| 118 | # and member names. |
Paul Duffin | 6ffdff8 | 2021-08-09 13:47:19 +0100 | [diff] [blame] | 119 | patterns = set() |
| 120 | for row in dict_reader(stream): |
Paul Duffin | 67b9d61 | 2021-07-21 17:38:47 +0100 | [diff] [blame] | 121 | signature = row['signature'] |
Paul Duffin | 1e18e98 | 2021-08-03 15:42:27 +0100 | [diff] [blame] | 122 | text = signature.removeprefix('L') |
Paul Duffin | 6ffdff8 | 2021-08-09 13:47:19 +0100 | [diff] [blame] | 123 | # Remove the class specific member signature |
Paul Duffin | 1e18e98 | 2021-08-03 15:42:27 +0100 | [diff] [blame] | 124 | pieces = text.split(';->') |
Paul Duffin | 1024f1d | 2022-03-15 17:45:57 +0000 | [diff] [blame] | 125 | qualified_class_name = pieces[0] |
| 126 | pieces = qualified_class_name.rsplit('/', maxsplit=1) |
Paul Duffin | 1e18e98 | 2021-08-03 15:42:27 +0100 | [diff] [blame] | 127 | 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 Duffin | 1024f1d | 2022-03-15 17:45:57 +0000 | [diff] [blame] | 137 | if is_split_package(split_packages, pkg): |
Paul Duffin | 1e18e98 | 2021-08-03 15:42:27 +0100 | [diff] [blame] | 138 | # Remove inner class names. |
Paul Duffin | 1024f1d | 2022-03-15 17:45:57 +0000 | [diff] [blame] | 139 | pieces = qualified_class_name.split('$', maxsplit=1) |
Paul Duffin | 1e18e98 | 2021-08-03 15:42:27 +0100 | [diff] [blame] | 140 | pattern = pieces[0] |
| 141 | else: |
| 142 | # Add a * to ensure that the pattern matches the classes in that |
| 143 | # package. |
| 144 | pattern = pkg + '/*' |
Paul Duffin | 6ffdff8 | 2021-08-09 13:47:19 +0100 | [diff] [blame] | 145 | patterns.add(pattern) |
| 146 | |
Paul Duffin | 1e18e98 | 2021-08-03 15:42:27 +0100 | [diff] [blame] | 147 | # Remove any patterns that would be matched by a package prefix pattern. |
Paul Duffin | 1024f1d | 2022-03-15 17:45:57 +0000 | [diff] [blame] | 148 | patterns = [ |
| 149 | p for p in patterns |
| 150 | if not matched_by_package_prefix_pattern(package_prefixes, p) |
| 151 | ] |
Paul Duffin | 1e18e98 | 2021-08-03 15:42:27 +0100 | [diff] [blame] | 152 | # 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 Duffin | 1024f1d | 2022-03-15 17:45:57 +0000 | [diff] [blame] | 155 | patterns = patterns + [f'{p}/**' for p in package_prefixes] |
Paul Duffin | 1e18e98 | 2021-08-03 15:42:27 +0100 | [diff] [blame] | 156 | # Sort the patterns. |
Paul Duffin | 6ffdff8 | 2021-08-09 13:47:19 +0100 | [diff] [blame] | 157 | patterns.sort() |
Paul Duffin | 67b9d61 | 2021-07-21 17:38:47 +0100 | [diff] [blame] | 158 | return patterns |
| 159 | |
Spandan Das | 2c2219b | 2021-08-25 17:47:43 +0000 | [diff] [blame] | 160 | |
Paul Duffin | 67b9d61 | 2021-07-21 17:38:47 +0100 | [diff] [blame] | 161 | def main(args): |
Spandan Das | 2c2219b | 2021-08-25 17:47:43 +0000 | [diff] [blame] | 162 | args_parser = argparse.ArgumentParser( |
| 163 | description='Generate a set of signature patterns ' |
Paul Duffin | 1e18e98 | 2021-08-03 15:42:27 +0100 | [diff] [blame] | 164 | 'that select a subset of monolithic hidden API files.') |
Spandan Das | 2c2219b | 2021-08-25 17:47:43 +0000 | [diff] [blame] | 165 | args_parser.add_argument( |
| 166 | '--flags', |
| 167 | help='The stub flags file which contains an entry for every dex member', |
| 168 | ) |
Paul Duffin | 1e18e98 | 2021-08-03 15:42:27 +0100 | [diff] [blame] | 169 | args_parser.add_argument( |
| 170 | '--split-package', |
| 171 | action='append', |
Paul Duffin | 1024f1d | 2022-03-15 17:45:57 +0000 | [diff] [blame] | 172 | help='A package that is split across multiple bootclasspath_fragment ' |
| 173 | 'modules') |
Paul Duffin | 1e18e98 | 2021-08-03 15:42:27 +0100 | [diff] [blame] | 174 | args_parser.add_argument( |
| 175 | '--package-prefix', |
| 176 | action='append', |
| 177 | help='A package prefix unique to this set of flags') |
Paul Duffin | 67b9d61 | 2021-07-21 17:38:47 +0100 | [diff] [blame] | 178 | args_parser.add_argument('--output', help='Generated signature prefixes') |
| 179 | args = args_parser.parse_args(args) |
| 180 | |
Paul Duffin | 1024f1d | 2022-03-15 17:45:57 +0000 | [diff] [blame] | 181 | split_packages = set( |
| 182 | dot_packages_to_slash_packages(args.split_package or [])) |
| 183 | errors = validate_split_packages(split_packages) |
Paul Duffin | 1e18e98 | 2021-08-03 15:42:27 +0100 | [diff] [blame] | 184 | |
Paul Duffin | 1024f1d | 2022-03-15 17:45:57 +0000 | [diff] [blame] | 185 | package_prefixes = dot_packages_to_slash_packages(args.package_prefix or []) |
Paul Duffin | 1e18e98 | 2021-08-03 15:42:27 +0100 | [diff] [blame] | 186 | |
| 187 | if not errors: |
Paul Duffin | 1024f1d | 2022-03-15 17:45:57 +0000 | [diff] [blame] | 188 | errors = validate_package_prefixes(split_packages, package_prefixes) |
Paul Duffin | 1e18e98 | 2021-08-03 15:42:27 +0100 | [diff] [blame] | 189 | |
| 190 | if errors: |
| 191 | for error in errors: |
| 192 | print(error) |
| 193 | sys.exit(1) |
| 194 | |
Paul Duffin | 67b9d61 | 2021-07-21 17:38:47 +0100 | [diff] [blame] | 195 | # Read in all the patterns into a list. |
Paul Duffin | 1024f1d | 2022-03-15 17:45:57 +0000 | [diff] [blame] | 196 | patterns = produce_patterns_from_file(args.flags, split_packages, |
| 197 | package_prefixes) |
Paul Duffin | 67b9d61 | 2021-07-21 17:38:47 +0100 | [diff] [blame] | 198 | |
| 199 | # Write out all the patterns. |
Paul Duffin | 1024f1d | 2022-03-15 17:45:57 +0000 | [diff] [blame] | 200 | with open(args.output, 'w', encoding='utf8') as outputFile: |
Paul Duffin | 67b9d61 | 2021-07-21 17:38:47 +0100 | [diff] [blame] | 201 | for pattern in patterns: |
| 202 | outputFile.write(pattern) |
Paul Duffin | 1e18e98 | 2021-08-03 15:42:27 +0100 | [diff] [blame] | 203 | outputFile.write('\n') |
Paul Duffin | 67b9d61 | 2021-07-21 17:38:47 +0100 | [diff] [blame] | 204 | |
Spandan Das | 2c2219b | 2021-08-25 17:47:43 +0000 | [diff] [blame] | 205 | |
Paul Duffin | 1e18e98 | 2021-08-03 15:42:27 +0100 | [diff] [blame] | 206 | if __name__ == '__main__': |
Paul Duffin | 67b9d61 | 2021-07-21 17:38:47 +0100 | [diff] [blame] | 207 | main(sys.argv[1:]) |