1#!/usr/bin/env vpython3 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 11import argparse 12import collections 13import os 14import re 15import sys 16 17# TARGET_RE matches a GN target, and extracts the target name and the contents. 18TARGET_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) 22 23# SOURCES_RE matches a block of sources inside a GN target. 24SOURCES_RE = re.compile(r'sources \+?= \[(?P<sources>.*?)\]', 25 re.MULTILINE | re.DOTALL) 26 27ERROR_MESSAGE = ("{build_file_path} in target '{target_name}':\n" 28 " Source file '{source_file}'\n" 29 " crosses boundary of package '{subpackage}'.") 30 31 32class PackageBoundaryViolation( 33 collections.namedtuple( 34 'PackageBoundaryViolation', 35 'build_file_path target_name source_file subpackage')): 36 def __str__(self): 37 return ERROR_MESSAGE.format(**self._asdict()) 38 39 40def _BuildSubpackagesPattern(packages, query): 41 """Returns a regular expression that matches source files inside subpackages 42 of the given query.""" 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) 51 52 53def _ReadFileAndPrependLines(file_path): 54 """Reads the contents of a file.""" 55 with open(file_path) as f: 56 return "".join(f.readlines()) 57 58 59def _CheckBuildFile(build_file_path, packages): 60 """Iterates over all the targets of the given BUILD.gn file, and verifies that 61 the source files referenced by it don't belong to any of it's subpackages. 62 Returns an iterator over PackageBoundaryViolations for this package. 63 """ 64 package = os.path.dirname(build_file_path) 65 subpackages_re = _BuildSubpackagesPattern(packages, package) 66 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) 79 80 81def CheckPackageBoundaries(root_dir, build_files=None): 82 packages = [ 83 root for root, _, files in os.walk(root_dir) if 'BUILD.gn' in files 84 ] 85 86 if build_files is not None: 87 for build_file_path in build_files: 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 96 97 98def main(argv): 99 parser = argparse.ArgumentParser( 100 description='Script that checks package boundary violations in GN ' 101 'build files.') 102 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.') 118 119 args = parser.parse_args(argv) 120 121 messages = CheckPackageBoundaries(args.root_dir, args.build_files) 122 messages = messages[:args.max_messages] 123 124 for i, message in enumerate(messages): 125 if i > 0: 126 print() 127 print(message) 128 129 return bool(messages) 130 131 132if __name__ == '__main__': 133 sys.exit(main(sys.argv[1:])) 134