Christoffer Jansson | 4e8a773 | 2022-02-08 08:01:12 | [diff] [blame] | 1 | #!/usr/bin/env vpython3 |
ehmaldonado | 4fb9746 | 2017-01-30 13:27:22 | [diff] [blame] | 2 | |
| 3 | # Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. |
| 4 | # |
| 5 | # Use of this source code is governed by a BSD-style license |
| 6 | # that can be found in the LICENSE file in the root of the source |
| 7 | # tree. An additional intellectual property rights grant can be found |
| 8 | # in the file PATENTS. All contributing project authors may |
| 9 | # be found in the AUTHORS file in the root of the source tree. |
| 10 | |
| 11 | import argparse |
Oleh Prypin | 2f33a56 | 2017-10-04 18:17:54 | [diff] [blame] | 12 | import collections |
ehmaldonado | 4fb9746 | 2017-01-30 13:27:22 | [diff] [blame] | 13 | import os |
| 14 | import re |
| 15 | import sys |
| 16 | |
ehmaldonado | 4fb9746 | 2017-01-30 13:27:22 | [diff] [blame] | 17 | # TARGET_RE matches a GN target, and extracts the target name and the contents. |
Mirko Bonadei | 8cc6695 | 2020-10-30 09:13:45 | [diff] [blame] | 18 | TARGET_RE = re.compile( |
| 19 | r'(?P<indent>\s*)\w+\("(?P<target_name>\w+)"\) {' |
| 20 | r'(?P<target_contents>.*?)' |
| 21 | r'(?P=indent)}', re.MULTILINE | re.DOTALL) |
ehmaldonado | 4fb9746 | 2017-01-30 13:27:22 | [diff] [blame] | 22 | |
| 23 | # SOURCES_RE matches a block of sources inside a GN target. |
| 24 | SOURCES_RE = re.compile(r'sources \+?= \[(?P<sources>.*?)\]', |
| 25 | re.MULTILINE | re.DOTALL) |
| 26 | |
Mirko Bonadei | b7dc45f | 2020-01-21 07:42:41 | [diff] [blame] | 27 | ERROR_MESSAGE = ("{build_file_path} in target '{target_name}':\n" |
Oleh Prypin | 2f33a56 | 2017-10-04 18:17:54 | [diff] [blame] | 28 | " Source file '{source_file}'\n" |
| 29 | " crosses boundary of package '{subpackage}'.") |
ehmaldonado | 4fb9746 | 2017-01-30 13:27:22 | [diff] [blame] | 30 | |
| 31 | |
Oleh Prypin | 2f33a56 | 2017-10-04 18:17:54 | [diff] [blame] | 32 | class PackageBoundaryViolation( |
Mirko Bonadei | 8cc6695 | 2020-10-30 09:13:45 | [diff] [blame] | 33 | collections.namedtuple( |
| 34 | 'PackageBoundaryViolation', |
| 35 | 'build_file_path target_name source_file subpackage')): |
Christoffer Jansson | 4e8a773 | 2022-02-08 08:01:12 | [diff] [blame] | 36 | def __str__(self): |
| 37 | return ERROR_MESSAGE.format(**self._asdict()) |
ehmaldonado | 4fb9746 | 2017-01-30 13:27:22 | [diff] [blame] | 38 | |
| 39 | |
| 40 | def _BuildSubpackagesPattern(packages, query): |
Christoffer Jansson | 4e8a773 | 2022-02-08 08:01:12 | [diff] [blame] | 41 | """Returns a regular expression that matches source files inside subpackages |
ehmaldonado | 4fb9746 | 2017-01-30 13:27:22 | [diff] [blame] | 42 | of the given query.""" |
Christoffer Jansson | 4e8a773 | 2022-02-08 08:01:12 | [diff] [blame] | 43 | query += os.path.sep |
| 44 | length = len(query) |
| 45 | pattern = r'\s*"(?P<source_file>(?P<subpackage>' |
| 46 | pattern += '|'.join( |
| 47 | re.escape(package[length:].replace(os.path.sep, '/')) |
| 48 | for package in packages if package.startswith(query)) |
| 49 | pattern += r')/[\w\./]*)"' |
| 50 | return re.compile(pattern) |
ehmaldonado | 4fb9746 | 2017-01-30 13:27:22 | [diff] [blame] | 51 | |
| 52 | |
| 53 | def _ReadFileAndPrependLines(file_path): |
Christoffer Jansson | 4e8a773 | 2022-02-08 08:01:12 | [diff] [blame] | 54 | """Reads the contents of a file.""" |
| 55 | with open(file_path) as f: |
| 56 | return "".join(f.readlines()) |
ehmaldonado | 4fb9746 | 2017-01-30 13:27:22 | [diff] [blame] | 57 | |
| 58 | |
Oleh Prypin | 2f33a56 | 2017-10-04 18:17:54 | [diff] [blame] | 59 | def _CheckBuildFile(build_file_path, packages): |
Christoffer Jansson | 4e8a773 | 2022-02-08 08:01:12 | [diff] [blame] | 60 | """Iterates over all the targets of the given BUILD.gn file, and verifies that |
ehmaldonado | 4fb9746 | 2017-01-30 13:27:22 | [diff] [blame] | 61 | the source files referenced by it don't belong to any of it's subpackages. |
Oleh Prypin | 2f33a56 | 2017-10-04 18:17:54 | [diff] [blame] | 62 | Returns an iterator over PackageBoundaryViolations for this package. |
ehmaldonado | 4fb9746 | 2017-01-30 13:27:22 | [diff] [blame] | 63 | """ |
Christoffer Jansson | 4e8a773 | 2022-02-08 08:01:12 | [diff] [blame] | 64 | package = os.path.dirname(build_file_path) |
| 65 | subpackages_re = _BuildSubpackagesPattern(packages, package) |
ehmaldonado | 4fb9746 | 2017-01-30 13:27:22 | [diff] [blame] | 66 | |
Christoffer Jansson | 4e8a773 | 2022-02-08 08:01:12 | [diff] [blame] | 67 | build_file_contents = _ReadFileAndPrependLines(build_file_path) |
| 68 | for target_match in TARGET_RE.finditer(build_file_contents): |
| 69 | target_name = target_match.group('target_name') |
| 70 | target_contents = target_match.group('target_contents') |
| 71 | for sources_match in SOURCES_RE.finditer(target_contents): |
| 72 | sources = sources_match.group('sources') |
| 73 | for subpackages_match in subpackages_re.finditer(sources): |
| 74 | subpackage = subpackages_match.group('subpackage') |
| 75 | source_file = subpackages_match.group('source_file') |
| 76 | if subpackage: |
| 77 | yield PackageBoundaryViolation(build_file_path, target_name, |
| 78 | source_file, subpackage) |
ehmaldonado | 4fb9746 | 2017-01-30 13:27:22 | [diff] [blame] | 79 | |
| 80 | |
Oleh Prypin | 2f33a56 | 2017-10-04 18:17:54 | [diff] [blame] | 81 | def CheckPackageBoundaries(root_dir, build_files=None): |
Christoffer Jansson | 4e8a773 | 2022-02-08 08:01:12 | [diff] [blame] | 82 | packages = [ |
| 83 | root for root, _, files in os.walk(root_dir) if 'BUILD.gn' in files |
| 84 | ] |
ehmaldonado | 4fb9746 | 2017-01-30 13:27:22 | [diff] [blame] | 85 | |
Christoffer Jansson | 4e8a773 | 2022-02-08 08:01:12 | [diff] [blame] | 86 | if build_files is not None: |
Oleh Prypin | afe0165 | 2017-10-04 13:56:08 | [diff] [blame] | 87 | for build_file_path in build_files: |
Christoffer Jansson | 4e8a773 | 2022-02-08 08:01:12 | [diff] [blame] | 88 | assert build_file_path.startswith(root_dir) |
| 89 | else: |
| 90 | build_files = [os.path.join(package, 'BUILD.gn') for package in packages] |
| 91 | |
| 92 | messages = [] |
| 93 | for build_file_path in build_files: |
| 94 | messages.extend(_CheckBuildFile(build_file_path, packages)) |
| 95 | return messages |
ehmaldonado | 4fb9746 | 2017-01-30 13:27:22 | [diff] [blame] | 96 | |
| 97 | |
Oleh Prypin | 2f33a56 | 2017-10-04 18:17:54 | [diff] [blame] | 98 | def main(argv): |
Christoffer Jansson | 4e8a773 | 2022-02-08 08:01:12 | [diff] [blame] | 99 | parser = argparse.ArgumentParser( |
| 100 | description='Script that checks package boundary violations in GN ' |
| 101 | 'build files.') |
ehmaldonado | 4fb9746 | 2017-01-30 13:27:22 | [diff] [blame] | 102 | |
Christoffer Jansson | 4e8a773 | 2022-02-08 08:01:12 | [diff] [blame] | 103 | parser.add_argument('root_dir', |
| 104 | metavar='ROOT_DIR', |
| 105 | help='The root directory that contains all BUILD.gn ' |
| 106 | 'files to be processed.') |
| 107 | parser.add_argument('build_files', |
| 108 | metavar='BUILD_FILE', |
| 109 | nargs='*', |
| 110 | help='A list of BUILD.gn files to be processed. If no ' |
| 111 | 'files are given, all BUILD.gn files under ROOT_DIR ' |
| 112 | 'will be processed.') |
| 113 | parser.add_argument('--max_messages', |
| 114 | type=int, |
| 115 | default=None, |
| 116 | help='If set, the maximum number of violations to be ' |
| 117 | 'displayed.') |
ehmaldonado | 4fb9746 | 2017-01-30 13:27:22 | [diff] [blame] | 118 | |
Christoffer Jansson | 4e8a773 | 2022-02-08 08:01:12 | [diff] [blame] | 119 | args = parser.parse_args(argv) |
ehmaldonado | 4fb9746 | 2017-01-30 13:27:22 | [diff] [blame] | 120 | |
Christoffer Jansson | 4e8a773 | 2022-02-08 08:01:12 | [diff] [blame] | 121 | messages = CheckPackageBoundaries(args.root_dir, args.build_files) |
| 122 | messages = messages[:args.max_messages] |
ehmaldonado | 4fb9746 | 2017-01-30 13:27:22 | [diff] [blame] | 123 | |
Christoffer Jansson | 4e8a773 | 2022-02-08 08:01:12 | [diff] [blame] | 124 | for i, message in enumerate(messages): |
| 125 | if i > 0: |
| 126 | print() |
| 127 | print(message) |
Oleh Prypin | 2f33a56 | 2017-10-04 18:17:54 | [diff] [blame] | 128 | |
Christoffer Jansson | 4e8a773 | 2022-02-08 08:01:12 | [diff] [blame] | 129 | return bool(messages) |
ehmaldonado | 4fb9746 | 2017-01-30 13:27:22 | [diff] [blame] | 130 | |
| 131 | |
| 132 | if __name__ == '__main__': |
Christoffer Jansson | 4e8a773 | 2022-02-08 08:01:12 | [diff] [blame] | 133 | sys.exit(main(sys.argv[1:])) |