1#!/usr/bin/env python 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 18# TARGET_RE matches a GN target, and extracts the target name and the contents. 19TARGET_RE = re.compile(r'(?P<indent>\s*)\w+\("(?P<target_name>\w+)"\) {' 20 r'(?P<target_contents>.*?)' 21 r'(?P=indent)}', 22 re.MULTILINE | re.DOTALL) 23 24# SOURCES_RE matches a block of sources inside a GN target. 25SOURCES_RE = re.compile(r'sources \+?= \[(?P<sources>.*?)\]', 26 re.MULTILINE | re.DOTALL) 27 28ERROR_MESSAGE = ("{build_file_path} in target '{target_name}':\n" 29 " Source file '{source_file}'\n" 30 " crosses boundary of package '{subpackage}'.") 31 32 33class PackageBoundaryViolation( 34 collections.namedtuple('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(re.escape(package[length:].replace(os.path.sep, '/')) 47 for package in packages if package.startswith(query)) 48 pattern += r')/[\w\./]*)"' 49 return re.compile(pattern) 50 51 52def _ReadFileAndPrependLines(file_path): 53 """Reads the contents of a file.""" 54 with open(file_path) as f: 55 return "".join(f.readlines()) 56 57 58def _CheckBuildFile(build_file_path, packages): 59 """Iterates over all the targets of the given BUILD.gn file, and verifies that 60 the source files referenced by it don't belong to any of it's subpackages. 61 Returns an iterator over PackageBoundaryViolations for this package. 62 """ 63 package = os.path.dirname(build_file_path) 64 subpackages_re = _BuildSubpackagesPattern(packages, package) 65 66 build_file_contents = _ReadFileAndPrependLines(build_file_path) 67 for target_match in TARGET_RE.finditer(build_file_contents): 68 target_name = target_match.group('target_name') 69 target_contents = target_match.group('target_contents') 70 for sources_match in SOURCES_RE.finditer(target_contents): 71 sources = sources_match.group('sources') 72 for subpackages_match in subpackages_re.finditer(sources): 73 subpackage = subpackages_match.group('subpackage') 74 source_file = subpackages_match.group('source_file') 75 if subpackage: 76 yield PackageBoundaryViolation(build_file_path, 77 target_name, source_file, subpackage) 78 79 80def CheckPackageBoundaries(root_dir, build_files=None): 81 packages = [root for root, _, files in os.walk(root_dir) 82 if 'BUILD.gn' in files] 83 84 if build_files is not None: 85 for build_file_path in build_files: 86 assert build_file_path.startswith(root_dir) 87 else: 88 build_files = [os.path.join(package, 'BUILD.gn') for package in packages] 89 90 messages = [] 91 for build_file_path in build_files: 92 messages.extend(_CheckBuildFile(build_file_path, packages)) 93 return messages 94 95 96def main(argv): 97 parser = argparse.ArgumentParser( 98 description='Script that checks package boundary violations in GN ' 99 'build files.') 100 101 parser.add_argument('root_dir', metavar='ROOT_DIR', 102 help='The root directory that contains all BUILD.gn ' 103 'files to be processed.') 104 parser.add_argument('build_files', metavar='BUILD_FILE', nargs='*', 105 help='A list of BUILD.gn files to be processed. If no ' 106 'files are given, all BUILD.gn files under ROOT_DIR ' 107 'will be processed.') 108 parser.add_argument('--max_messages', type=int, default=None, 109 help='If set, the maximum number of violations to be ' 110 'displayed.') 111 112 args = parser.parse_args(argv) 113 114 messages = CheckPackageBoundaries(args.root_dir, args.build_files) 115 messages = messages[:args.max_messages] 116 117 for i, message in enumerate(messages): 118 if i > 0: 119 print 120 print message 121 122 return bool(messages) 123 124 125if __name__ == '__main__': 126 sys.exit(main(sys.argv[1:])) 127