armvixl | 4a102ba | 2014-07-14 09:02:40 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python2.7 |
| 2 | |
Alexandre Rames | b78f139 | 2016-07-01 14:22:22 +0100 | [diff] [blame] | 3 | # Copyright 2015, VIXL authors |
armvixl | 4a102ba | 2014-07-14 09:02:40 +0100 | [diff] [blame] | 4 | # All rights reserved. |
| 5 | # |
| 6 | # Redistribution and use in source and binary forms, with or without |
| 7 | # modification, are permitted provided that the following conditions are met: |
| 8 | # |
| 9 | # * Redistributions of source code must retain the above copyright notice, |
| 10 | # this list of conditions and the following disclaimer. |
| 11 | # * Redistributions in binary form must reproduce the above copyright notice, |
| 12 | # this list of conditions and the following disclaimer in the documentation |
| 13 | # and/or other materials provided with the distribution. |
| 14 | # * Neither the name of ARM Limited nor the names of its contributors may be |
| 15 | # used to endorse or promote products derived from this software without |
| 16 | # specific prior written permission. |
| 17 | # |
| 18 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND |
| 19 | # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 20 | # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 21 | # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE |
| 22 | # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 23 | # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 24 | # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 25 | # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 26 | # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 27 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | |
| 29 | import argparse |
Alexandre Rames | 61e54cf | 2016-07-08 14:32:36 +0100 | [diff] [blame^] | 30 | import fnmatch |
Alexandre Rames | b274662 | 2016-07-11 16:12:39 +0100 | [diff] [blame] | 31 | import hashlib |
armvixl | 4a102ba | 2014-07-14 09:02:40 +0100 | [diff] [blame] | 32 | import multiprocessing |
Alexandre Rames | b274662 | 2016-07-11 16:12:39 +0100 | [diff] [blame] | 33 | import os |
| 34 | import pickle |
armvixl | 4a102ba | 2014-07-14 09:02:40 +0100 | [diff] [blame] | 35 | import re |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 36 | import signal |
armvixl | 4a102ba | 2014-07-14 09:02:40 +0100 | [diff] [blame] | 37 | import subprocess |
| 38 | import sys |
| 39 | |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 40 | import config |
armvixl | 4a102ba | 2014-07-14 09:02:40 +0100 | [diff] [blame] | 41 | import git |
| 42 | import printer |
| 43 | import util |
| 44 | |
| 45 | |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 46 | # Catch SIGINT to gracefully exit when ctrl+C is pressed. |
| 47 | def sigint_handler(signal, frame): |
| 48 | sys.exit(1) |
| 49 | signal.signal(signal.SIGINT, sigint_handler) |
armvixl | 4a102ba | 2014-07-14 09:02:40 +0100 | [diff] [blame] | 50 | |
| 51 | def BuildOptions(): |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 52 | parser = argparse.ArgumentParser( |
armvixl | 4a102ba | 2014-07-14 09:02:40 +0100 | [diff] [blame] | 53 | description = |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 54 | '''This tool lints C++ files and produces a summary of the errors found. |
| 55 | If no files are provided on the command-line, all C++ source files in the |
Alexandre Rames | b274662 | 2016-07-11 16:12:39 +0100 | [diff] [blame] | 56 | repository are processed. |
| 57 | Results are cached to speed up the process. |
| 58 | ''', |
armvixl | 4a102ba | 2014-07-14 09:02:40 +0100 | [diff] [blame] | 59 | # Print default values. |
| 60 | formatter_class=argparse.ArgumentDefaultsHelpFormatter) |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 61 | parser.add_argument('files', nargs = '*') |
| 62 | parser.add_argument('--jobs', '-j', metavar='N', type=int, nargs='?', |
| 63 | default=multiprocessing.cpu_count(), |
| 64 | const=multiprocessing.cpu_count(), |
armvixl | 4a102ba | 2014-07-14 09:02:40 +0100 | [diff] [blame] | 65 | help='''Runs the tests using N jobs. If the option is set |
| 66 | but no value is provided, the script will use as many jobs |
| 67 | as it thinks useful.''') |
Alexandre Rames | b274662 | 2016-07-11 16:12:39 +0100 | [diff] [blame] | 68 | parser.add_argument('--no-cache', |
| 69 | action='store_true', default=False, |
| 70 | help='Do not use cached lint results.') |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 71 | return parser.parse_args() |
armvixl | 4a102ba | 2014-07-14 09:02:40 +0100 | [diff] [blame] | 72 | |
| 73 | |
| 74 | |
| 75 | __lint_results_lock__ = multiprocessing.Lock() |
| 76 | |
Alexandre Rames | b274662 | 2016-07-11 16:12:39 +0100 | [diff] [blame] | 77 | # Returns a tuple (filename, number of lint errors). |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 78 | def Lint(filename, progress_prefix = ''): |
| 79 | command = ['cpplint.py', filename] |
armvixl | 4a102ba | 2014-07-14 09:02:40 +0100 | [diff] [blame] | 80 | process = subprocess.Popen(command, |
| 81 | stdout=subprocess.PIPE, |
| 82 | stderr=subprocess.PIPE) |
| 83 | |
| 84 | # Use a lock to avoid mixing the output for different files. |
| 85 | with __lint_results_lock__: |
| 86 | # Process the output as the process is running, until it exits. |
| 87 | LINT_ERROR_LINE_REGEXP = re.compile('\[[1-5]\]$') |
| 88 | LINT_DONE_PROC_LINE_REGEXP = re.compile('Done processing') |
| 89 | LINT_STATUS_LINE_REGEXP = re.compile('Total errors found') |
| 90 | while True: |
| 91 | retcode = process.poll() |
| 92 | while True: |
| 93 | line = process.stderr.readline() |
| 94 | if line == '': break |
| 95 | output_line = progress_prefix + line.rstrip('\r\n') |
| 96 | |
| 97 | if LINT_ERROR_LINE_REGEXP.search(line): |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 98 | printer.PrintOverwritableLine(output_line, |
| 99 | type = printer.LINE_TYPE_LINTER) |
armvixl | 4a102ba | 2014-07-14 09:02:40 +0100 | [diff] [blame] | 100 | printer.EnsureNewLine() |
| 101 | elif LINT_DONE_PROC_LINE_REGEXP.search(line): |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 102 | printer.PrintOverwritableLine(output_line, |
| 103 | type = printer.LINE_TYPE_LINTER) |
armvixl | 4a102ba | 2014-07-14 09:02:40 +0100 | [diff] [blame] | 104 | elif LINT_STATUS_LINE_REGEXP.search(line): |
| 105 | status_line = line |
| 106 | |
| 107 | if retcode != None: break; |
| 108 | |
| 109 | if retcode == 0: |
Alexandre Rames | b274662 | 2016-07-11 16:12:39 +0100 | [diff] [blame] | 110 | return (filename, 0) |
armvixl | 4a102ba | 2014-07-14 09:02:40 +0100 | [diff] [blame] | 111 | |
| 112 | # Return the number of errors in this file. |
| 113 | res = re.search('\d+$', status_line) |
| 114 | n_errors_str = res.string[res.start():res.end()] |
| 115 | n_errors = int(n_errors_str) |
| 116 | status_line = \ |
| 117 | progress_prefix + 'Total errors found in %s : %d' % (filename, n_errors) |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 118 | printer.PrintOverwritableLine(status_line, type = printer.LINE_TYPE_LINTER) |
armvixl | 4a102ba | 2014-07-14 09:02:40 +0100 | [diff] [blame] | 119 | printer.EnsureNewLine() |
Alexandre Rames | b274662 | 2016-07-11 16:12:39 +0100 | [diff] [blame] | 120 | |
| 121 | return (filename, n_errors) |
armvixl | 4a102ba | 2014-07-14 09:02:40 +0100 | [diff] [blame] | 122 | |
| 123 | |
| 124 | # The multiprocessing map_async function does not allow passing multiple |
| 125 | # arguments directly, so use a wrapper. |
| 126 | def LintWrapper(args): |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 127 | # Run under a try-catch to avoid flooding the output when the script is |
| 128 | # interrupted from the keyboard with ctrl+C. |
| 129 | try: |
| 130 | return Lint(*args) |
| 131 | except: |
| 132 | sys.exit(1) |
armvixl | 4a102ba | 2014-07-14 09:02:40 +0100 | [diff] [blame] | 133 | |
| 134 | |
Alexandre Rames | b274662 | 2016-07-11 16:12:39 +0100 | [diff] [blame] | 135 | def ShouldLint(filename, cached_results): |
| 136 | filename = os.path.realpath(filename) |
| 137 | if filename not in cached_results: |
| 138 | return True |
| 139 | with open(filename, 'rb') as f: |
| 140 | file_hash = hashlib.md5(f.read()).hexdigest() |
| 141 | return file_hash != cached_results[filename] |
| 142 | |
| 143 | |
armvixl | 4a102ba | 2014-07-14 09:02:40 +0100 | [diff] [blame] | 144 | # Returns the total number of errors found in the files linted. |
Alexandre Rames | b274662 | 2016-07-11 16:12:39 +0100 | [diff] [blame] | 145 | # `cached_results` must be a dictionary, with the format: |
| 146 | # { 'filename': file_hash, 'other_filename': other_hash, ... } |
| 147 | # If not `None`, `cached_results` is used to avoid re-linting files, and new |
| 148 | # results are stored in it. |
| 149 | def LintFiles(files, |
| 150 | jobs = 1, |
| 151 | progress_prefix = '', |
| 152 | cached_results = None): |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 153 | if not IsCppLintAvailable(): |
| 154 | print( |
| 155 | printer.COLOUR_RED + \ |
| 156 | ("cpplint.py not found. Please ensure the depot" |
| 157 | " tools are installed and in your PATH. See" |
| 158 | " http://dev.chromium.org/developers/how-tos/install-depot-tools for" |
| 159 | " details.") + \ |
| 160 | printer.NO_COLOUR) |
| 161 | return -1 |
| 162 | |
Alexandre Rames | b274662 | 2016-07-11 16:12:39 +0100 | [diff] [blame] | 163 | # Filter out directories. |
| 164 | files = filter(os.path.isfile, files) |
| 165 | |
| 166 | # Filter out files for which we have a cached correct result. |
| 167 | if cached_results is not None and len(cached_results) != 0: |
| 168 | n_input_files = len(files) |
| 169 | files = filter(lambda f: ShouldLint(f, cached_results), files) |
| 170 | n_skipped_files = n_input_files - len(files) |
| 171 | if n_skipped_files != 0: |
| 172 | printer.Print( |
| 173 | progress_prefix + |
| 174 | 'Skipping %d correct files that were already processed.' % |
| 175 | n_skipped_files) |
| 176 | |
armvixl | 4a102ba | 2014-07-14 09:02:40 +0100 | [diff] [blame] | 177 | pool = multiprocessing.Pool(jobs) |
| 178 | # The '.get(9999999)' is workaround to allow killing the test script with |
| 179 | # ctrl+C from the shell. This bug is documented at |
| 180 | # http://bugs.python.org/issue8296. |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 181 | tasks = [(f, progress_prefix) for f in files] |
| 182 | # Run under a try-catch to avoid flooding the output when the script is |
| 183 | # interrupted from the keyboard with ctrl+C. |
| 184 | try: |
| 185 | results = pool.map_async(LintWrapper, tasks).get(9999999) |
| 186 | pool.close() |
| 187 | pool.join() |
| 188 | except KeyboardInterrupt: |
| 189 | pool.terminate() |
| 190 | sys.exit(1) |
Alexandre Rames | b274662 | 2016-07-11 16:12:39 +0100 | [diff] [blame] | 191 | |
| 192 | n_errors = sum(map(lambda (filename, errors): errors, results)) |
| 193 | |
| 194 | if cached_results is not None: |
| 195 | for filename, errors in results: |
| 196 | if errors == 0: |
| 197 | with open(filename, 'rb') as f: |
| 198 | filename = os.path.realpath(filename) |
| 199 | file_hash = hashlib.md5(f.read()).hexdigest() |
| 200 | cached_results[filename] = file_hash |
| 201 | |
armvixl | 4a102ba | 2014-07-14 09:02:40 +0100 | [diff] [blame] | 202 | |
| 203 | printer.PrintOverwritableLine( |
| 204 | progress_prefix + 'Total errors found: %d' % n_errors) |
| 205 | printer.EnsureNewLine() |
| 206 | return n_errors |
| 207 | |
| 208 | |
| 209 | def IsCppLintAvailable(): |
| 210 | retcode, unused_output = util.getstatusoutput('which cpplint.py') |
| 211 | return retcode == 0 |
| 212 | |
| 213 | |
| 214 | CPP_EXT_REGEXP = re.compile('\.(cc|h)$') |
Alexandre Rames | 61e54cf | 2016-07-08 14:32:36 +0100 | [diff] [blame^] | 215 | def IsLinterInput(filename): |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 216 | # lint all C++ files. |
armvixl | 4a102ba | 2014-07-14 09:02:40 +0100 | [diff] [blame] | 217 | return CPP_EXT_REGEXP.search(filename) != None |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 218 | |
Alexandre Rames | b274662 | 2016-07-11 16:12:39 +0100 | [diff] [blame] | 219 | |
| 220 | def GetDefaultFilesToLint(): |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 221 | if git.is_git_repository_root(config.dir_root): |
Alexandre Rames | 61e54cf | 2016-07-08 14:32:36 +0100 | [diff] [blame^] | 222 | files = git.get_tracked_files().split() |
| 223 | files = filter(IsLinterInput, files) |
| 224 | files = FilterOutTestTraceHeaders(files) |
| 225 | return 0, files |
armvixl | db64434 | 2015-07-21 11:37:10 +0100 | [diff] [blame] | 226 | else: |
| 227 | printer.Print(printer.COLOUR_ORANGE + 'WARNING: This script is not run ' \ |
| 228 | 'from its Git repository. The linter will not run.' + \ |
| 229 | printer.NO_COLOUR) |
| 230 | return 1, [] |
armvixl | 4a102ba | 2014-07-14 09:02:40 +0100 | [diff] [blame] | 231 | |
Alexandre Rames | b274662 | 2016-07-11 16:12:39 +0100 | [diff] [blame] | 232 | |
| 233 | cached_results_pkl_filename = \ |
| 234 | os.path.join(config.dir_tools, '.cached_lint_results.pkl') |
| 235 | |
| 236 | |
| 237 | def ReadCachedResults(): |
| 238 | cached_results = {} |
| 239 | if os.path.isfile(cached_results_pkl_filename): |
| 240 | with open(cached_results_pkl_filename, 'rb') as pkl_file: |
| 241 | cached_results = pickle.load(pkl_file) |
| 242 | return cached_results |
| 243 | |
| 244 | |
| 245 | def CacheResults(results): |
| 246 | with open(cached_results_pkl_filename, 'wb') as pkl_file: |
| 247 | pickle.dump(results, pkl_file) |
| 248 | |
| 249 | |
Alexandre Rames | 61e54cf | 2016-07-08 14:32:36 +0100 | [diff] [blame^] | 250 | def FilterOutTestTraceHeaders(files): |
| 251 | def IsTraceHeader(f): |
| 252 | relative_aarch32_traces_path = os.path.relpath(config.dir_aarch32_traces,'.') |
| 253 | relative_aarch64_traces_path = os.path.relpath(config.dir_aarch64_traces,'.') |
| 254 | return \ |
| 255 | fnmatch.fnmatch(f, os.path.join(relative_aarch32_traces_path, '*.h')) or \ |
| 256 | fnmatch.fnmatch(f, os.path.join(relative_aarch64_traces_path, '*.h')) |
| 257 | return filter(lambda f: not IsTraceHeader(f), files) |
| 258 | |
| 259 | |
Alexandre Rames | b274662 | 2016-07-11 16:12:39 +0100 | [diff] [blame] | 260 | def RunLinter(files, jobs=1, progress_prefix='', cached=True): |
| 261 | results = {} if not cached else ReadCachedResults() |
| 262 | |
| 263 | rc = LintFiles(files, |
| 264 | jobs=jobs, |
| 265 | progress_prefix=progress_prefix, |
| 266 | cached_results=results) |
| 267 | |
| 268 | CacheResults(results) |
| 269 | return rc |
| 270 | |
| 271 | |
armvixl | 4a102ba | 2014-07-14 09:02:40 +0100 | [diff] [blame] | 272 | if __name__ == '__main__': |
| 273 | # Parse the arguments. |
| 274 | args = BuildOptions() |
| 275 | |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 276 | files = args.files |
| 277 | if not files: |
Alexandre Rames | b274662 | 2016-07-11 16:12:39 +0100 | [diff] [blame] | 278 | retcode, files = GetDefaultFilesToLint() |
armvixl | 0f35e36 | 2016-05-10 13:57:58 +0100 | [diff] [blame] | 279 | if retcode: |
| 280 | sys.exit(retcode) |
Alexandre Rames | b274662 | 2016-07-11 16:12:39 +0100 | [diff] [blame] | 281 | |
| 282 | cached = not args.no_cache |
| 283 | retcode = RunLinter(files, jobs=args.jobs, cached=cached) |
| 284 | |
armvixl | 4a102ba | 2014-07-14 09:02:40 +0100 | [diff] [blame] | 285 | sys.exit(retcode) |