andrew@webrtc.org | 2442de1 | 2012-01-23 17:45:41 +0000 | [diff] [blame] | 1 | # Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
| 2 | # |
| 3 | # Use of this source code is governed by a BSD-style license |
| 4 | # that can be found in the LICENSE file in the root of the source |
| 5 | # tree. An additional intellectual property rights grant can be found |
| 6 | # in the file PATENTS. All contributing project authors may |
| 7 | # be found in the AUTHORS file in the root of the source tree. |
niklase@google.com | da159d6 | 2011-05-30 11:51:34 +0000 | [diff] [blame] | 8 | |
kjellander | 7439f97 | 2016-12-05 22:47:46 -0800 | [diff] [blame] | 9 | import json |
kjellander@webrtc.org | aefe61a | 2014-12-08 13:00:30 +0000 | [diff] [blame] | 10 | import os |
kjellander@webrtc.org | 8575980 | 2013-10-22 16:47:40 +0000 | [diff] [blame] | 11 | import re |
kjellander@webrtc.org | 3bd4156 | 2014-09-01 11:06:37 +0000 | [diff] [blame] | 12 | import sys |
Mirko Bonadei | 4dc4e25 | 2017-09-19 13:49:16 +0200 | [diff] [blame] | 13 | from collections import defaultdict |
Oleh Prypin | 2f33a56 | 2017-10-04 20:17:54 +0200 | [diff] [blame] | 14 | from contextlib import contextmanager |
kjellander@webrtc.org | 8575980 | 2013-10-22 16:47:40 +0000 | [diff] [blame] | 15 | |
| 16 | |
oprypin | 2aa463f | 2017-03-23 03:17:02 -0700 | [diff] [blame] | 17 | # Files and directories that are *skipped* by cpplint in the presubmit script. |
| 18 | CPPLINT_BLACKLIST = [ |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 19 | 'api/video_codecs/video_decoder.h', |
| 20 | 'common_types.cc', |
| 21 | 'common_types.h', |
| 22 | 'examples/objc', |
Steve Anton | e78bcb9 | 2017-10-31 09:53:08 -0700 | [diff] [blame] | 23 | 'media/base/streamparams.h', |
| 24 | 'media/base/videocommon.h', |
| 25 | 'media/engine/fakewebrtcdeviceinfo.h', |
| 26 | 'media/sctp/sctptransport.cc', |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 27 | 'modules/audio_coding', |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 28 | 'modules/audio_device', |
| 29 | 'modules/audio_processing', |
| 30 | 'modules/desktop_capture', |
| 31 | 'modules/include/module_common_types.h', |
| 32 | 'modules/media_file', |
| 33 | 'modules/utility', |
| 34 | 'modules/video_capture', |
| 35 | 'p2p', |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 36 | 'rtc_base', |
| 37 | 'sdk/android/src/jni', |
| 38 | 'sdk/objc', |
| 39 | 'system_wrappers', |
| 40 | 'test', |
Henrik Kjellander | 90fd7d8 | 2017-05-09 08:30:10 +0200 | [diff] [blame] | 41 | 'tools_webrtc', |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 42 | 'voice_engine', |
kjellander@webrtc.org | 0fcaf99 | 2015-11-26 15:24:52 +0100 | [diff] [blame] | 43 | ] |
| 44 | |
jbauch | c4e3ead | 2016-02-19 00:25:55 -0800 | [diff] [blame] | 45 | # These filters will always be removed, even if the caller specifies a filter |
| 46 | # set, as they are problematic or broken in some way. |
| 47 | # |
| 48 | # Justifications for each filter: |
| 49 | # - build/c++11 : Rvalue ref checks are unreliable (false positives), |
| 50 | # include file and feature blacklists are |
| 51 | # google3-specific. |
kjellander | e5a87a5 | 2016-04-27 02:32:12 -0700 | [diff] [blame] | 52 | # - whitespace/operators: Same as above (doesn't seem sufficient to eliminate |
| 53 | # all move-related errors). |
jbauch | c4e3ead | 2016-02-19 00:25:55 -0800 | [diff] [blame] | 54 | BLACKLIST_LINT_FILTERS = [ |
| 55 | '-build/c++11', |
kjellander | e5a87a5 | 2016-04-27 02:32:12 -0700 | [diff] [blame] | 56 | '-whitespace/operators', |
jbauch | c4e3ead | 2016-02-19 00:25:55 -0800 | [diff] [blame] | 57 | ] |
| 58 | |
kjellander | fd59523 | 2015-12-04 02:44:09 -0800 | [diff] [blame] | 59 | # List of directories of "supported" native APIs. That means changes to headers |
| 60 | # will be done in a compatible way following this scheme: |
| 61 | # 1. Non-breaking changes are made. |
| 62 | # 2. The old APIs as marked as deprecated (with comments). |
| 63 | # 3. Deprecation is announced to discuss-webrtc@googlegroups.com and |
| 64 | # webrtc-users@google.com (internal list). |
| 65 | # 4. (later) The deprecated APIs are removed. |
kjellander | 53047c9 | 2015-12-02 23:56:14 -0800 | [diff] [blame] | 66 | NATIVE_API_DIRS = ( |
Karl Wiberg | ef52d8b8 | 2017-10-25 13:20:03 +0200 | [diff] [blame] | 67 | 'api', # All subdirectories of api/ are included as well. |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 68 | 'media', |
| 69 | 'modules/audio_device/include', |
| 70 | 'pc', |
kjellander | dd70547 | 2016-06-09 11:17:27 -0700 | [diff] [blame] | 71 | ) |
Mirko Bonadei | 4dc4e25 | 2017-09-19 13:49:16 +0200 | [diff] [blame] | 72 | |
kjellander | dd70547 | 2016-06-09 11:17:27 -0700 | [diff] [blame] | 73 | # These directories should not be used but are maintained only to avoid breaking |
| 74 | # some legacy downstream code. |
| 75 | LEGACY_API_DIRS = ( |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 76 | 'common_audio/include', |
| 77 | 'modules/audio_coding/include', |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 78 | 'modules/audio_processing/include', |
| 79 | 'modules/bitrate_controller/include', |
| 80 | 'modules/congestion_controller/include', |
| 81 | 'modules/include', |
| 82 | 'modules/remote_bitrate_estimator/include', |
| 83 | 'modules/rtp_rtcp/include', |
| 84 | 'modules/rtp_rtcp/source', |
| 85 | 'modules/utility/include', |
| 86 | 'modules/video_coding/codecs/h264/include', |
| 87 | 'modules/video_coding/codecs/i420/include', |
| 88 | 'modules/video_coding/codecs/vp8/include', |
| 89 | 'modules/video_coding/codecs/vp9/include', |
| 90 | 'modules/video_coding/include', |
| 91 | 'rtc_base', |
| 92 | 'system_wrappers/include', |
| 93 | 'voice_engine/include', |
kjellander | 53047c9 | 2015-12-02 23:56:14 -0800 | [diff] [blame] | 94 | ) |
Mirko Bonadei | 4dc4e25 | 2017-09-19 13:49:16 +0200 | [diff] [blame] | 95 | |
Karl Wiberg | d4f01c1 | 2017-11-10 10:55:45 +0100 | [diff] [blame] | 96 | # NOTE: The set of directories in API_DIRS should be the same as those |
| 97 | # listed in the table in native-api.md. |
kjellander | dd70547 | 2016-06-09 11:17:27 -0700 | [diff] [blame] | 98 | API_DIRS = NATIVE_API_DIRS[:] + LEGACY_API_DIRS[:] |
kjellander | 53047c9 | 2015-12-02 23:56:14 -0800 | [diff] [blame] | 99 | |
Mirko Bonadei | 4dc4e25 | 2017-09-19 13:49:16 +0200 | [diff] [blame] | 100 | # TARGET_RE matches a GN target, and extracts the target name and the contents. |
| 101 | TARGET_RE = re.compile(r'(?P<indent>\s*)\w+\("(?P<target_name>\w+)"\) {' |
| 102 | r'(?P<target_contents>.*?)' |
| 103 | r'(?P=indent)}', |
| 104 | re.MULTILINE | re.DOTALL) |
| 105 | |
| 106 | # SOURCES_RE matches a block of sources inside a GN target. |
| 107 | SOURCES_RE = re.compile(r'sources \+?= \[(?P<sources>.*?)\]', |
| 108 | re.MULTILINE | re.DOTALL) |
| 109 | |
| 110 | # FILE_PATH_RE matchies a file path. |
| 111 | FILE_PATH_RE = re.compile(r'"(?P<file_path>(\w|\/)+)(?P<extension>\.\w+)"') |
| 112 | |
kjellander | 53047c9 | 2015-12-02 23:56:14 -0800 | [diff] [blame] | 113 | |
Oleh Prypin | 2f33a56 | 2017-10-04 20:17:54 +0200 | [diff] [blame] | 114 | @contextmanager |
| 115 | def _AddToPath(*paths): |
| 116 | original_sys_path = sys.path |
| 117 | sys.path.extend(paths) |
| 118 | try: |
| 119 | yield |
| 120 | finally: |
| 121 | # Restore sys.path to what it was before. |
| 122 | sys.path = original_sys_path |
ehmaldonado | 4fb9746 | 2017-01-30 05:27:22 -0800 | [diff] [blame] | 123 | |
| 124 | |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 125 | def VerifyNativeApiHeadersListIsValid(input_api, output_api): |
kjellander | 53047c9 | 2015-12-02 23:56:14 -0800 | [diff] [blame] | 126 | """Ensures the list of native API header directories is up to date.""" |
| 127 | non_existing_paths = [] |
| 128 | native_api_full_paths = [ |
| 129 | input_api.os_path.join(input_api.PresubmitLocalPath(), |
kjellander | dd70547 | 2016-06-09 11:17:27 -0700 | [diff] [blame] | 130 | *path.split('/')) for path in API_DIRS] |
kjellander | 53047c9 | 2015-12-02 23:56:14 -0800 | [diff] [blame] | 131 | for path in native_api_full_paths: |
| 132 | if not os.path.isdir(path): |
| 133 | non_existing_paths.append(path) |
| 134 | if non_existing_paths: |
| 135 | return [output_api.PresubmitError( |
| 136 | 'Directories to native API headers have changed which has made the ' |
| 137 | 'list in PRESUBMIT.py outdated.\nPlease update it to the current ' |
| 138 | 'location of our native APIs.', |
| 139 | non_existing_paths)] |
| 140 | return [] |
| 141 | |
kjellander | c88b5d5 | 2017-04-05 06:42:43 -0700 | [diff] [blame] | 142 | API_CHANGE_MSG = """ |
kwiberg | eb13302 | 2016-04-07 07:41:48 -0700 | [diff] [blame] | 143 | You seem to be changing native API header files. Please make sure that you: |
oprypin | 375b9ac | 2017-02-13 04:13:23 -0800 | [diff] [blame] | 144 | 1. Make compatible changes that don't break existing clients. Usually |
| 145 | this is done by keeping the existing method signatures unchanged. |
| 146 | 2. Mark the old stuff as deprecated (see RTC_DEPRECATED macro). |
kwiberg | eb13302 | 2016-04-07 07:41:48 -0700 | [diff] [blame] | 147 | 3. Create a timeline and plan for when the deprecated stuff will be |
| 148 | removed. (The amount of time we give users to change their code |
| 149 | should be informed by how much work it is for them. If they just |
| 150 | need to replace one name with another or something equally |
| 151 | simple, 1-2 weeks might be good; if they need to do serious work, |
| 152 | up to 3 months may be called for.) |
| 153 | 4. Update/inform existing downstream code owners to stop using the |
| 154 | deprecated stuff. (Send announcements to |
| 155 | discuss-webrtc@googlegroups.com and webrtc-users@google.com.) |
| 156 | 5. Remove the deprecated stuff, once the agreed-upon amount of time |
| 157 | has passed. |
| 158 | Related files: |
| 159 | """ |
kjellander | 53047c9 | 2015-12-02 23:56:14 -0800 | [diff] [blame] | 160 | |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 161 | def CheckNativeApiHeaderChanges(input_api, output_api): |
kjellander | 53047c9 | 2015-12-02 23:56:14 -0800 | [diff] [blame] | 162 | """Checks to remind proper changing of native APIs.""" |
| 163 | files = [] |
Karl Wiberg | 6bfac03 | 2017-10-27 15:14:20 +0200 | [diff] [blame] | 164 | source_file_filter = lambda x: input_api.FilterSourceFile( |
| 165 | x, white_list=[r'.+\.(gn|gni|h)$']) |
| 166 | for f in input_api.AffectedSourceFiles(source_file_filter): |
| 167 | for path in API_DIRS: |
| 168 | dn = os.path.dirname(f.LocalPath()) |
| 169 | if path == 'api': |
| 170 | # Special case: Subdirectories included. |
| 171 | if dn == 'api' or dn.startswith('api/'): |
| 172 | files.append(f) |
| 173 | else: |
| 174 | # Normal case: Subdirectories not included. |
| 175 | if dn == path: |
| 176 | files.append(f) |
kjellander | 53047c9 | 2015-12-02 23:56:14 -0800 | [diff] [blame] | 177 | |
| 178 | if files: |
kjellander | c88b5d5 | 2017-04-05 06:42:43 -0700 | [diff] [blame] | 179 | return [output_api.PresubmitNotifyResult(API_CHANGE_MSG, files)] |
kjellander | 53047c9 | 2015-12-02 23:56:14 -0800 | [diff] [blame] | 180 | return [] |
| 181 | |
kjellander@webrtc.org | 0fcaf99 | 2015-11-26 15:24:52 +0100 | [diff] [blame] | 182 | |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 183 | def CheckNoIOStreamInHeaders(input_api, output_api): |
kjellander@webrtc.org | 51198f1 | 2012-02-21 17:53:46 +0000 | [diff] [blame] | 184 | """Checks to make sure no .h files include <iostream>.""" |
| 185 | files = [] |
| 186 | pattern = input_api.re.compile(r'^#include\s*<iostream>', |
| 187 | input_api.re.MULTILINE) |
| 188 | for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile): |
| 189 | if not f.LocalPath().endswith('.h'): |
| 190 | continue |
| 191 | contents = input_api.ReadFile(f) |
| 192 | if pattern.search(contents): |
| 193 | files.append(f) |
| 194 | |
| 195 | if len(files): |
Henrik Kjellander | 57e5fd2 | 2015-05-25 12:55:39 +0200 | [diff] [blame] | 196 | return [output_api.PresubmitError( |
kjellander@webrtc.org | 51198f1 | 2012-02-21 17:53:46 +0000 | [diff] [blame] | 197 | 'Do not #include <iostream> in header files, since it inserts static ' + |
| 198 | 'initialization into every file including the header. Instead, ' + |
| 199 | '#include <ostream>. See http://crbug.com/94794', |
Henrik Kjellander | 57e5fd2 | 2015-05-25 12:55:39 +0200 | [diff] [blame] | 200 | files)] |
kjellander@webrtc.org | 51198f1 | 2012-02-21 17:53:46 +0000 | [diff] [blame] | 201 | return [] |
| 202 | |
kjellander@webrtc.org | e415864 | 2014-08-06 09:11:18 +0000 | [diff] [blame] | 203 | |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 204 | def CheckNoPragmaOnce(input_api, output_api): |
kjellander | 6aeef74 | 2017-02-20 01:13:18 -0800 | [diff] [blame] | 205 | """Make sure that banned functions are not used.""" |
| 206 | files = [] |
| 207 | pattern = input_api.re.compile(r'^#pragma\s+once', |
| 208 | input_api.re.MULTILINE) |
| 209 | for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile): |
| 210 | if not f.LocalPath().endswith('.h'): |
| 211 | continue |
| 212 | contents = input_api.ReadFile(f) |
| 213 | if pattern.search(contents): |
| 214 | files.append(f) |
| 215 | |
| 216 | if files: |
| 217 | return [output_api.PresubmitError( |
| 218 | 'Do not use #pragma once in header files.\n' |
| 219 | 'See http://www.chromium.org/developers/coding-style#TOC-File-headers', |
| 220 | files)] |
| 221 | return [] |
| 222 | |
| 223 | |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 224 | def CheckNoFRIEND_TEST(input_api, output_api): # pylint: disable=invalid-name |
kjellander@webrtc.org | 51198f1 | 2012-02-21 17:53:46 +0000 | [diff] [blame] | 225 | """Make sure that gtest's FRIEND_TEST() macro is not used, the |
| 226 | FRIEND_TEST_ALL_PREFIXES() macro from testsupport/gtest_prod_util.h should be |
| 227 | used instead since that allows for FLAKY_, FAILS_ and DISABLED_ prefixes.""" |
| 228 | problems = [] |
| 229 | |
| 230 | file_filter = lambda f: f.LocalPath().endswith(('.cc', '.h')) |
| 231 | for f in input_api.AffectedFiles(file_filter=file_filter): |
| 232 | for line_num, line in f.ChangedContents(): |
| 233 | if 'FRIEND_TEST(' in line: |
| 234 | problems.append(' %s:%d' % (f.LocalPath(), line_num)) |
| 235 | |
| 236 | if not problems: |
| 237 | return [] |
| 238 | return [output_api.PresubmitPromptWarning('WebRTC\'s code should not use ' |
| 239 | 'gtest\'s FRIEND_TEST() macro. Include testsupport/gtest_prod_util.h and ' |
| 240 | 'use FRIEND_TEST_ALL_PREFIXES() instead.\n' + '\n'.join(problems))] |
| 241 | |
kjellander@webrtc.org | e415864 | 2014-08-06 09:11:18 +0000 | [diff] [blame] | 242 | |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 243 | def IsLintBlacklisted(blacklist_paths, file_path): |
oprypin | 2aa463f | 2017-03-23 03:17:02 -0700 | [diff] [blame] | 244 | """ Checks if a file is blacklisted for lint check.""" |
| 245 | for path in blacklist_paths: |
| 246 | if file_path == path or os.path.dirname(file_path).startswith(path): |
kjellander@webrtc.org | 0fcaf99 | 2015-11-26 15:24:52 +0100 | [diff] [blame] | 247 | return True |
| 248 | return False |
| 249 | |
| 250 | |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 251 | def CheckApprovedFilesLintClean(input_api, output_api, |
mflodman@webrtc.org | 2a45209 | 2012-07-01 05:55:23 +0000 | [diff] [blame] | 252 | source_file_filter=None): |
oprypin | 2aa463f | 2017-03-23 03:17:02 -0700 | [diff] [blame] | 253 | """Checks that all new or non-blacklisted .cc and .h files pass cpplint.py. |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 254 | This check is based on CheckChangeLintsClean in |
kjellander@webrtc.org | 51198f1 | 2012-02-21 17:53:46 +0000 | [diff] [blame] | 255 | depot_tools/presubmit_canned_checks.py but has less filters and only checks |
| 256 | added files.""" |
| 257 | result = [] |
| 258 | |
| 259 | # Initialize cpplint. |
| 260 | import cpplint |
| 261 | # Access to a protected member _XX of a client class |
| 262 | # pylint: disable=W0212 |
| 263 | cpplint._cpplint_state.ResetErrorCounts() |
| 264 | |
jbauch | c4e3ead | 2016-02-19 00:25:55 -0800 | [diff] [blame] | 265 | lint_filters = cpplint._Filters() |
| 266 | lint_filters.extend(BLACKLIST_LINT_FILTERS) |
| 267 | cpplint._SetFilters(','.join(lint_filters)) |
| 268 | |
oprypin | 2aa463f | 2017-03-23 03:17:02 -0700 | [diff] [blame] | 269 | # Create a platform independent blacklist for cpplint. |
| 270 | blacklist_paths = [input_api.os_path.join(*path.split('/')) |
| 271 | for path in CPPLINT_BLACKLIST] |
kjellander@webrtc.org | 0fcaf99 | 2015-11-26 15:24:52 +0100 | [diff] [blame] | 272 | |
kjellander@webrtc.org | 51198f1 | 2012-02-21 17:53:46 +0000 | [diff] [blame] | 273 | # Use the strictest verbosity level for cpplint.py (level 1) which is the |
oprypin | 2aa463f | 2017-03-23 03:17:02 -0700 | [diff] [blame] | 274 | # default when running cpplint.py from command line. To make it possible to |
| 275 | # work with not-yet-converted code, we're only applying it to new (or |
| 276 | # moved/renamed) files and files not listed in CPPLINT_BLACKLIST. |
kjellander@webrtc.org | 51198f1 | 2012-02-21 17:53:46 +0000 | [diff] [blame] | 277 | verbosity_level = 1 |
| 278 | files = [] |
| 279 | for f in input_api.AffectedSourceFiles(source_file_filter): |
Henrik Kjellander | 57e5fd2 | 2015-05-25 12:55:39 +0200 | [diff] [blame] | 280 | # Note that moved/renamed files also count as added. |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 281 | if f.Action() == 'A' or not IsLintBlacklisted(blacklist_paths, |
oprypin | 2aa463f | 2017-03-23 03:17:02 -0700 | [diff] [blame] | 282 | f.LocalPath()): |
kjellander@webrtc.org | 51198f1 | 2012-02-21 17:53:46 +0000 | [diff] [blame] | 283 | files.append(f.AbsoluteLocalPath()) |
mflodman@webrtc.org | 2a45209 | 2012-07-01 05:55:23 +0000 | [diff] [blame] | 284 | |
kjellander@webrtc.org | 51198f1 | 2012-02-21 17:53:46 +0000 | [diff] [blame] | 285 | for file_name in files: |
| 286 | cpplint.ProcessFile(file_name, verbosity_level) |
| 287 | |
| 288 | if cpplint._cpplint_state.error_count > 0: |
| 289 | if input_api.is_committing: |
oprypin | 8e58d65 | 2017-03-21 07:52:41 -0700 | [diff] [blame] | 290 | res_type = output_api.PresubmitError |
kjellander@webrtc.org | 51198f1 | 2012-02-21 17:53:46 +0000 | [diff] [blame] | 291 | else: |
| 292 | res_type = output_api.PresubmitPromptWarning |
| 293 | result = [res_type('Changelist failed cpplint.py check.')] |
| 294 | |
| 295 | return result |
| 296 | |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 297 | def CheckNoSourcesAbove(input_api, gn_files, output_api): |
ehmaldonado | 5b1ba08 | 2016-09-02 05:51:08 -0700 | [diff] [blame] | 298 | # Disallow referencing source files with paths above the GN file location. |
| 299 | source_pattern = input_api.re.compile(r' +sources \+?= \[(.*?)\]', |
| 300 | re.MULTILINE | re.DOTALL) |
| 301 | file_pattern = input_api.re.compile(r'"((\.\./.*?)|(//.*?))"') |
| 302 | violating_gn_files = set() |
| 303 | violating_source_entries = [] |
| 304 | for gn_file in gn_files: |
| 305 | contents = input_api.ReadFile(gn_file) |
| 306 | for source_block_match in source_pattern.finditer(contents): |
| 307 | # Find all source list entries starting with ../ in the source block |
| 308 | # (exclude overrides entries). |
| 309 | for file_list_match in file_pattern.finditer(source_block_match.group(1)): |
| 310 | source_file = file_list_match.group(1) |
| 311 | if 'overrides/' not in source_file: |
| 312 | violating_source_entries.append(source_file) |
| 313 | violating_gn_files.add(gn_file) |
| 314 | if violating_gn_files: |
| 315 | return [output_api.PresubmitError( |
| 316 | 'Referencing source files above the directory of the GN file is not ' |
Henrik Kjellander | b4af3d6 | 2016-11-16 20:11:29 +0100 | [diff] [blame] | 317 | 'allowed. Please introduce new GN targets in the proper location ' |
| 318 | 'instead.\n' |
ehmaldonado | 5b1ba08 | 2016-09-02 05:51:08 -0700 | [diff] [blame] | 319 | 'Invalid source entries:\n' |
| 320 | '%s\n' |
| 321 | 'Violating GN files:' % '\n'.join(violating_source_entries), |
| 322 | items=violating_gn_files)] |
| 323 | return [] |
| 324 | |
Mirko Bonadei | 4dc4e25 | 2017-09-19 13:49:16 +0200 | [diff] [blame] | 325 | def CheckNoMixingSources(input_api, gn_files, output_api): |
| 326 | """Disallow mixing C, C++ and Obj-C/Obj-C++ in the same target. |
| 327 | |
| 328 | See bugs.webrtc.org/7743 for more context. |
| 329 | """ |
| 330 | def _MoreThanOneSourceUsed(*sources_lists): |
| 331 | sources_used = 0 |
| 332 | for source_list in sources_lists: |
| 333 | if len(source_list): |
| 334 | sources_used += 1 |
| 335 | return sources_used > 1 |
| 336 | |
| 337 | errors = defaultdict(lambda: []) |
kjellander | 7439f97 | 2016-12-05 22:47:46 -0800 | [diff] [blame] | 338 | for gn_file in gn_files: |
Mirko Bonadei | 4dc4e25 | 2017-09-19 13:49:16 +0200 | [diff] [blame] | 339 | gn_file_content = input_api.ReadFile(gn_file) |
| 340 | for target_match in TARGET_RE.finditer(gn_file_content): |
| 341 | # list_of_sources is a list of tuples of the form |
| 342 | # (c_files, cc_files, objc_files) that keeps track of all the sources |
| 343 | # defined in a target. A GN target can have more that on definition of |
| 344 | # sources (since it supports if/else statements). |
| 345 | # E.g.: |
| 346 | # rtc_static_library("foo") { |
| 347 | # if (is_win) { |
| 348 | # sources = [ "foo.cc" ] |
| 349 | # } else { |
| 350 | # sources = [ "foo.mm" ] |
| 351 | # } |
| 352 | # } |
| 353 | # This is allowed and the presubmit check should support this case. |
| 354 | list_of_sources = [] |
kjellander | 7439f97 | 2016-12-05 22:47:46 -0800 | [diff] [blame] | 355 | c_files = [] |
| 356 | cc_files = [] |
Mirko Bonadei | 4dc4e25 | 2017-09-19 13:49:16 +0200 | [diff] [blame] | 357 | objc_files = [] |
| 358 | target_name = target_match.group('target_name') |
| 359 | target_contents = target_match.group('target_contents') |
| 360 | for sources_match in SOURCES_RE.finditer(target_contents): |
| 361 | if '+=' not in sources_match.group(0): |
| 362 | if c_files or cc_files or objc_files: |
| 363 | list_of_sources.append((c_files, cc_files, objc_files)) |
| 364 | c_files = [] |
| 365 | cc_files = [] |
| 366 | objc_files = [] |
| 367 | for file_match in FILE_PATH_RE.finditer(sources_match.group(1)): |
| 368 | file_path = file_match.group('file_path') |
| 369 | extension = file_match.group('extension') |
| 370 | if extension == '.c': |
| 371 | c_files.append(file_path + extension) |
| 372 | if extension == '.cc': |
| 373 | cc_files.append(file_path + extension) |
| 374 | if extension in ['.m', '.mm']: |
| 375 | objc_files.append(file_path + extension) |
| 376 | list_of_sources.append((c_files, cc_files, objc_files)) |
| 377 | for c_files_list, cc_files_list, objc_files_list in list_of_sources: |
| 378 | if _MoreThanOneSourceUsed(c_files_list, cc_files_list, objc_files_list): |
| 379 | all_sources = sorted(c_files_list + cc_files_list + objc_files_list) |
| 380 | errors[gn_file.LocalPath()].append((target_name, all_sources)) |
| 381 | if errors: |
kjellander | 7439f97 | 2016-12-05 22:47:46 -0800 | [diff] [blame] | 382 | return [output_api.PresubmitError( |
Mirko Bonadei | 4dc4e25 | 2017-09-19 13:49:16 +0200 | [diff] [blame] | 383 | 'GN targets cannot mix .c, .cc and .m (or .mm) source files.\n' |
| 384 | 'Please create a separate target for each collection of sources.\n' |
kjellander | 7439f97 | 2016-12-05 22:47:46 -0800 | [diff] [blame] | 385 | 'Mixed sources: \n' |
| 386 | '%s\n' |
Mirko Bonadei | 4dc4e25 | 2017-09-19 13:49:16 +0200 | [diff] [blame] | 387 | 'Violating GN files:\n%s\n' % (json.dumps(errors, indent=2), |
| 388 | '\n'.join(errors.keys())))] |
kjellander | 7439f97 | 2016-12-05 22:47:46 -0800 | [diff] [blame] | 389 | return [] |
| 390 | |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 391 | def CheckNoPackageBoundaryViolations(input_api, gn_files, output_api): |
ehmaldonado | 4fb9746 | 2017-01-30 05:27:22 -0800 | [diff] [blame] | 392 | cwd = input_api.PresubmitLocalPath() |
Oleh Prypin | 2f33a56 | 2017-10-04 20:17:54 +0200 | [diff] [blame] | 393 | with _AddToPath(input_api.os_path.join( |
| 394 | cwd, 'tools_webrtc', 'presubmit_checks_lib')): |
| 395 | from check_package_boundaries import CheckPackageBoundaries |
| 396 | build_files = [os.path.join(cwd, gn_file.LocalPath()) for gn_file in gn_files] |
| 397 | errors = CheckPackageBoundaries(cwd, build_files)[:5] |
| 398 | if errors: |
ehmaldonado | 4fb9746 | 2017-01-30 05:27:22 -0800 | [diff] [blame] | 399 | return [output_api.PresubmitError( |
Oleh Prypin | 2f33a56 | 2017-10-04 20:17:54 +0200 | [diff] [blame] | 400 | 'There are package boundary violations in the following GN files:', |
| 401 | long_text='\n\n'.join(str(err) for err in errors))] |
ehmaldonado | 4fb9746 | 2017-01-30 05:27:22 -0800 | [diff] [blame] | 402 | return [] |
| 403 | |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 404 | def CheckGnChanges(input_api, output_api): |
ehmaldonado | 5b1ba08 | 2016-09-02 05:51:08 -0700 | [diff] [blame] | 405 | source_file_filter = lambda x: input_api.FilterSourceFile( |
Oleh Prypin | afe0165 | 2017-10-04 15:56:08 +0200 | [diff] [blame] | 406 | x, white_list=(r'.+\.(gn|gni)$',), |
| 407 | black_list=r'.*/presubmit_checks_lib/testdata/.*') |
ehmaldonado | 5b1ba08 | 2016-09-02 05:51:08 -0700 | [diff] [blame] | 408 | |
| 409 | gn_files = [] |
| 410 | for f in input_api.AffectedSourceFiles(source_file_filter): |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 411 | gn_files.append(f) |
ehmaldonado | 5b1ba08 | 2016-09-02 05:51:08 -0700 | [diff] [blame] | 412 | |
| 413 | result = [] |
| 414 | if gn_files: |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 415 | result.extend(CheckNoSourcesAbove(input_api, gn_files, output_api)) |
Mirko Bonadei | 4dc4e25 | 2017-09-19 13:49:16 +0200 | [diff] [blame] | 416 | result.extend(CheckNoMixingSources(input_api, gn_files, output_api)) |
| 417 | result.extend(CheckNoPackageBoundaryViolations(input_api, gn_files, |
| 418 | output_api)) |
ehmaldonado | 5b1ba08 | 2016-09-02 05:51:08 -0700 | [diff] [blame] | 419 | return result |
| 420 | |
Oleh Prypin | 920b653 | 2017-10-05 11:28:51 +0200 | [diff] [blame] | 421 | def CheckGnGen(input_api, output_api): |
| 422 | """Runs `gn gen --check` with default args to detect mismatches between |
| 423 | #includes and dependencies in the BUILD.gn files, as well as general build |
| 424 | errors. |
| 425 | """ |
| 426 | with _AddToPath(input_api.os_path.join( |
| 427 | input_api.PresubmitLocalPath(), 'tools_webrtc', 'presubmit_checks_lib')): |
| 428 | from gn_check import RunGnCheck |
| 429 | errors = RunGnCheck(input_api.PresubmitLocalPath())[:5] |
| 430 | if errors: |
| 431 | return [output_api.PresubmitPromptWarning( |
| 432 | 'Some #includes do not match the build dependency graph. Please run:\n' |
| 433 | ' gn gen --check <out_dir>', |
| 434 | long_text='\n\n'.join(errors))] |
| 435 | return [] |
| 436 | |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 437 | def CheckUnwantedDependencies(input_api, output_api): |
kjellander@webrtc.org | 3bd4156 | 2014-09-01 11:06:37 +0000 | [diff] [blame] | 438 | """Runs checkdeps on #include statements added in this |
| 439 | change. Breaking - rules is an error, breaking ! rules is a |
| 440 | warning. |
| 441 | """ |
| 442 | # Copied from Chromium's src/PRESUBMIT.py. |
| 443 | |
| 444 | # We need to wait until we have an input_api object and use this |
| 445 | # roundabout construct to import checkdeps because this file is |
| 446 | # eval-ed and thus doesn't have __file__. |
Oleh Prypin | 2f33a56 | 2017-10-04 20:17:54 +0200 | [diff] [blame] | 447 | checkdeps_path = input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 448 | 'buildtools', 'checkdeps') |
| 449 | if not os.path.exists(checkdeps_path): |
| 450 | return [output_api.PresubmitError( |
| 451 | 'Cannot find checkdeps at %s\nHave you run "gclient sync" to ' |
| 452 | 'download all the DEPS entries?' % checkdeps_path)] |
| 453 | with _AddToPath(checkdeps_path): |
kjellander@webrtc.org | 3bd4156 | 2014-09-01 11:06:37 +0000 | [diff] [blame] | 454 | import checkdeps |
| 455 | from cpp_checker import CppChecker |
| 456 | from rules import Rule |
kjellander@webrtc.org | 3bd4156 | 2014-09-01 11:06:37 +0000 | [diff] [blame] | 457 | |
| 458 | added_includes = [] |
| 459 | for f in input_api.AffectedFiles(): |
| 460 | if not CppChecker.IsCppFile(f.LocalPath()): |
| 461 | continue |
| 462 | |
Henrik Kjellander | 57e5fd2 | 2015-05-25 12:55:39 +0200 | [diff] [blame] | 463 | changed_lines = [line for _, line in f.ChangedContents()] |
kjellander@webrtc.org | 3bd4156 | 2014-09-01 11:06:37 +0000 | [diff] [blame] | 464 | added_includes.append([f.LocalPath(), changed_lines]) |
| 465 | |
| 466 | deps_checker = checkdeps.DepsChecker(input_api.PresubmitLocalPath()) |
| 467 | |
| 468 | error_descriptions = [] |
| 469 | warning_descriptions = [] |
| 470 | for path, rule_type, rule_description in deps_checker.CheckAddedCppIncludes( |
| 471 | added_includes): |
| 472 | description_with_path = '%s\n %s' % (path, rule_description) |
| 473 | if rule_type == Rule.DISALLOW: |
| 474 | error_descriptions.append(description_with_path) |
| 475 | else: |
| 476 | warning_descriptions.append(description_with_path) |
| 477 | |
| 478 | results = [] |
| 479 | if error_descriptions: |
| 480 | results.append(output_api.PresubmitError( |
kjellander | a7066a3 | 2017-03-23 03:47:05 -0700 | [diff] [blame] | 481 | 'You added one or more #includes that violate checkdeps rules.\n' |
| 482 | 'Check that the DEPS files in these locations contain valid rules.\n' |
| 483 | 'See https://cs.chromium.org/chromium/src/buildtools/checkdeps/ for ' |
| 484 | 'more details about checkdeps.', |
kjellander@webrtc.org | 3bd4156 | 2014-09-01 11:06:37 +0000 | [diff] [blame] | 485 | error_descriptions)) |
| 486 | if warning_descriptions: |
| 487 | results.append(output_api.PresubmitPromptOrNotify( |
| 488 | 'You added one or more #includes of files that are temporarily\n' |
| 489 | 'allowed but being removed. Can you avoid introducing the\n' |
kjellander | a7066a3 | 2017-03-23 03:47:05 -0700 | [diff] [blame] | 490 | '#include? See relevant DEPS file(s) for details and contacts.\n' |
| 491 | 'See https://cs.chromium.org/chromium/src/buildtools/checkdeps/ for ' |
| 492 | 'more details about checkdeps.', |
kjellander@webrtc.org | 3bd4156 | 2014-09-01 11:06:37 +0000 | [diff] [blame] | 493 | warning_descriptions)) |
| 494 | return results |
| 495 | |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 496 | def CheckCommitMessageBugEntry(input_api, output_api): |
| 497 | """Check that bug entries are well-formed in commit message.""" |
| 498 | bogus_bug_msg = ( |
Mirko Bonadei | 6188018 | 2017-10-12 15:12:35 +0200 | [diff] [blame] | 499 | 'Bogus Bug entry: %s. Please specify the issue tracker prefix and the ' |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 500 | 'issue number, separated by a colon, e.g. webrtc:123 or chromium:12345.') |
| 501 | results = [] |
Mirko Bonadei | 6188018 | 2017-10-12 15:12:35 +0200 | [diff] [blame] | 502 | for bug in input_api.change.BugsFromDescription(): |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 503 | bug = bug.strip() |
| 504 | if bug.lower() == 'none': |
| 505 | continue |
charujain | 81a58c7 | 2017-09-25 13:25:45 +0200 | [diff] [blame] | 506 | if 'b/' not in bug and ':' not in bug: |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 507 | try: |
| 508 | if int(bug) > 100000: |
| 509 | # Rough indicator for current chromium bugs. |
| 510 | prefix_guess = 'chromium' |
| 511 | else: |
| 512 | prefix_guess = 'webrtc' |
Mirko Bonadei | 6188018 | 2017-10-12 15:12:35 +0200 | [diff] [blame] | 513 | results.append('Bug entry requires issue tracker prefix, e.g. %s:%s' % |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 514 | (prefix_guess, bug)) |
| 515 | except ValueError: |
| 516 | results.append(bogus_bug_msg % bug) |
charujain | 81a58c7 | 2017-09-25 13:25:45 +0200 | [diff] [blame] | 517 | elif not (re.match(r'\w+:\d+', bug) or re.match(r'b/\d+', bug)): |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 518 | results.append(bogus_bug_msg % bug) |
| 519 | return [output_api.PresubmitError(r) for r in results] |
| 520 | |
| 521 | def CheckChangeHasBugField(input_api, output_api): |
Mirko Bonadei | 6188018 | 2017-10-12 15:12:35 +0200 | [diff] [blame] | 522 | """Requires that the changelist is associated with a bug. |
kjellander | d1e26a9 | 2016-09-19 08:11:16 -0700 | [diff] [blame] | 523 | |
| 524 | This check is stricter than the one in depot_tools/presubmit_canned_checks.py |
Mirko Bonadei | 6188018 | 2017-10-12 15:12:35 +0200 | [diff] [blame] | 525 | since it fails the presubmit if the bug field is missing or doesn't contain |
kjellander | d1e26a9 | 2016-09-19 08:11:16 -0700 | [diff] [blame] | 526 | a bug reference. |
Mirko Bonadei | 6188018 | 2017-10-12 15:12:35 +0200 | [diff] [blame] | 527 | |
| 528 | This supports both 'BUG=' and 'Bug:' since we are in the process of migrating |
| 529 | to Gerrit and it encourages the usage of 'Bug:'. |
kjellander | d1e26a9 | 2016-09-19 08:11:16 -0700 | [diff] [blame] | 530 | """ |
Mirko Bonadei | 6188018 | 2017-10-12 15:12:35 +0200 | [diff] [blame] | 531 | if input_api.change.BugsFromDescription(): |
kjellander | d1e26a9 | 2016-09-19 08:11:16 -0700 | [diff] [blame] | 532 | return [] |
| 533 | else: |
| 534 | return [output_api.PresubmitError( |
Mirko Bonadei | 6188018 | 2017-10-12 15:12:35 +0200 | [diff] [blame] | 535 | 'The "Bug: [bug number]" footer is mandatory. Please create a bug and ' |
kjellander | d1e26a9 | 2016-09-19 08:11:16 -0700 | [diff] [blame] | 536 | 'reference it using either of:\n' |
Mirko Bonadei | 6188018 | 2017-10-12 15:12:35 +0200 | [diff] [blame] | 537 | ' * https://bugs.webrtc.org - reference it using Bug: webrtc:XXXX\n' |
| 538 | ' * https://crbug.com - reference it using Bug: chromium:XXXXXX')] |
kjellander@webrtc.org | e415864 | 2014-08-06 09:11:18 +0000 | [diff] [blame] | 539 | |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 540 | def CheckJSONParseErrors(input_api, output_api): |
kjellander | 569cf94 | 2016-02-11 05:02:59 -0800 | [diff] [blame] | 541 | """Check that JSON files do not contain syntax errors.""" |
| 542 | |
| 543 | def FilterFile(affected_file): |
| 544 | return input_api.os_path.splitext(affected_file.LocalPath())[1] == '.json' |
| 545 | |
| 546 | def GetJSONParseError(input_api, filename): |
| 547 | try: |
| 548 | contents = input_api.ReadFile(filename) |
| 549 | input_api.json.loads(contents) |
| 550 | except ValueError as e: |
| 551 | return e |
| 552 | return None |
| 553 | |
| 554 | results = [] |
| 555 | for affected_file in input_api.AffectedFiles( |
| 556 | file_filter=FilterFile, include_deletes=False): |
| 557 | parse_error = GetJSONParseError(input_api, |
| 558 | affected_file.AbsoluteLocalPath()) |
| 559 | if parse_error: |
| 560 | results.append(output_api.PresubmitError('%s could not be parsed: %s' % |
| 561 | (affected_file.LocalPath(), parse_error))) |
| 562 | return results |
| 563 | |
| 564 | |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 565 | def RunPythonTests(input_api, output_api): |
kjellander | c88b5d5 | 2017-04-05 06:42:43 -0700 | [diff] [blame] | 566 | def Join(*args): |
Henrik Kjellander | 8d3ad82 | 2015-05-26 19:52:05 +0200 | [diff] [blame] | 567 | return input_api.os_path.join(input_api.PresubmitLocalPath(), *args) |
| 568 | |
| 569 | test_directories = [ |
Edward Lemur | 6d01f6d | 2017-09-14 17:02:01 +0200 | [diff] [blame] | 570 | input_api.PresubmitLocalPath(), |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 571 | Join('rtc_tools', 'py_event_log_analyzer'), |
| 572 | Join('rtc_tools'), |
| 573 | Join('audio', 'test', 'unittests'), |
ehmaldonado | 4fb9746 | 2017-01-30 05:27:22 -0800 | [diff] [blame] | 574 | ] + [ |
Henrik Kjellander | 90fd7d8 | 2017-05-09 08:30:10 +0200 | [diff] [blame] | 575 | root for root, _, files in os.walk(Join('tools_webrtc')) |
ehmaldonado | 4fb9746 | 2017-01-30 05:27:22 -0800 | [diff] [blame] | 576 | if any(f.endswith('_test.py') for f in files) |
Henrik Kjellander | 8d3ad82 | 2015-05-26 19:52:05 +0200 | [diff] [blame] | 577 | ] |
| 578 | |
| 579 | tests = [] |
| 580 | for directory in test_directories: |
| 581 | tests.extend( |
| 582 | input_api.canned_checks.GetUnitTestsInDirectory( |
| 583 | input_api, |
| 584 | output_api, |
| 585 | directory, |
| 586 | whitelist=[r'.+_test\.py$'])) |
| 587 | return input_api.RunTests(tests, parallel=True) |
| 588 | |
| 589 | |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 590 | def CheckUsageOfGoogleProtobufNamespace(input_api, output_api): |
mbonadei | 38415b2 | 2017-04-07 05:38:01 -0700 | [diff] [blame] | 591 | """Checks that the namespace google::protobuf has not been used.""" |
| 592 | files = [] |
| 593 | pattern = input_api.re.compile(r'google::protobuf') |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 594 | proto_utils_path = os.path.join('rtc_base', 'protobuf_utils.h') |
mbonadei | 38415b2 | 2017-04-07 05:38:01 -0700 | [diff] [blame] | 595 | for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile): |
| 596 | if f.LocalPath() in [proto_utils_path, 'PRESUBMIT.py']: |
| 597 | continue |
| 598 | contents = input_api.ReadFile(f) |
| 599 | if pattern.search(contents): |
| 600 | files.append(f) |
| 601 | |
| 602 | if files: |
| 603 | return [output_api.PresubmitError( |
| 604 | 'Please avoid to use namespace `google::protobuf` directly.\n' |
| 605 | 'Add a using directive in `%s` and include that header instead.' |
| 606 | % proto_utils_path, files)] |
| 607 | return [] |
| 608 | |
| 609 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 610 | def _LicenseHeader(input_api): |
| 611 | """Returns the license header regexp.""" |
| 612 | # Accept any year number from 2003 to the current year |
| 613 | current_year = int(input_api.time.strftime('%Y')) |
| 614 | allowed_years = (str(s) for s in reversed(xrange(2003, current_year + 1))) |
| 615 | years_re = '(' + '|'.join(allowed_years) + ')' |
| 616 | license_header = ( |
| 617 | r'.*? Copyright( \(c\))? %(year)s The WebRTC [Pp]roject [Aa]uthors\. ' |
| 618 | r'All [Rr]ights [Rr]eserved\.\n' |
| 619 | r'.*?\n' |
| 620 | r'.*? Use of this source code is governed by a BSD-style license\n' |
| 621 | r'.*? that can be found in the LICENSE file in the root of the source\n' |
| 622 | r'.*? tree\. An additional intellectual property rights grant can be ' |
| 623 | r'found\n' |
| 624 | r'.*? in the file PATENTS\. All contributing project authors may\n' |
| 625 | r'.*? be found in the AUTHORS file in the root of the source tree\.\n' |
| 626 | ) % { |
| 627 | 'year': years_re, |
| 628 | } |
| 629 | return license_header |
| 630 | |
| 631 | |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 632 | def CommonChecks(input_api, output_api): |
andrew@webrtc.org | 53df136 | 2012-01-26 21:24:23 +0000 | [diff] [blame] | 633 | """Checks common to both upload and commit.""" |
niklase@google.com | da159d6 | 2011-05-30 11:51:34 +0000 | [diff] [blame] | 634 | results = [] |
tkchin | 42f580e | 2015-11-26 23:18:23 -0800 | [diff] [blame] | 635 | # Filter out files that are in objc or ios dirs from being cpplint-ed since |
| 636 | # they do not follow C++ lint rules. |
| 637 | black_list = input_api.DEFAULT_BLACK_LIST + ( |
| 638 | r".*\bobjc[\\\/].*", |
Kári Tristan Helgason | 3fa3517 | 2016-09-09 08:55:05 +0000 | [diff] [blame] | 639 | r".*objc\.[hcm]+$", |
tkchin | 42f580e | 2015-11-26 23:18:23 -0800 | [diff] [blame] | 640 | ) |
| 641 | source_file_filter = lambda x: input_api.FilterSourceFile(x, None, black_list) |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 642 | results.extend(CheckApprovedFilesLintClean( |
tkchin | 42f580e | 2015-11-26 23:18:23 -0800 | [diff] [blame] | 643 | input_api, output_api, source_file_filter)) |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 644 | results.extend(input_api.canned_checks.CheckLicense( |
| 645 | input_api, output_api, _LicenseHeader(input_api))) |
phoglund@webrtc.org | 5d371393 | 2013-03-07 09:59:43 +0000 | [diff] [blame] | 646 | results.extend(input_api.canned_checks.RunPylint(input_api, output_api, |
kjellander@webrtc.org | 177567c | 2016-12-22 10:40:28 +0100 | [diff] [blame] | 647 | black_list=(r'^base[\\\/].*\.py$', |
Henrik Kjellander | 14771ac | 2015-06-02 13:10:04 +0200 | [diff] [blame] | 648 | r'^build[\\\/].*\.py$', |
| 649 | r'^buildtools[\\\/].*\.py$', |
kjellander | 38c65c8 | 2017-04-12 22:43:38 -0700 | [diff] [blame] | 650 | r'^infra[\\\/].*\.py$', |
Henrik Kjellander | 0779e8f | 2016-12-22 12:01:17 +0100 | [diff] [blame] | 651 | r'^ios[\\\/].*\.py$', |
Henrik Kjellander | 14771ac | 2015-06-02 13:10:04 +0200 | [diff] [blame] | 652 | r'^out.*[\\\/].*\.py$', |
| 653 | r'^testing[\\\/].*\.py$', |
| 654 | r'^third_party[\\\/].*\.py$', |
kjellander@webrtc.org | 177567c | 2016-12-22 10:40:28 +0100 | [diff] [blame] | 655 | r'^tools[\\\/].*\.py$', |
kjellander | afd5494 | 2016-12-17 12:21:39 -0800 | [diff] [blame] | 656 | # TODO(phoglund): should arguably be checked. |
Henrik Kjellander | 90fd7d8 | 2017-05-09 08:30:10 +0200 | [diff] [blame] | 657 | r'^tools_webrtc[\\\/]mb[\\\/].*\.py$', |
| 658 | r'^tools_webrtc[\\\/]valgrind[\\\/].*\.py$', |
Henrik Kjellander | 14771ac | 2015-06-02 13:10:04 +0200 | [diff] [blame] | 659 | r'^xcodebuild.*[\\\/].*\.py$',), |
Henrik Kjellander | 57e5fd2 | 2015-05-25 12:55:39 +0200 | [diff] [blame] | 660 | pylintrc='pylintrc')) |
kjellander | 569cf94 | 2016-02-11 05:02:59 -0800 | [diff] [blame] | 661 | |
nisse | 3d21e23 | 2016-09-02 03:07:06 -0700 | [diff] [blame] | 662 | # TODO(nisse): talk/ is no more, so make below checks simpler? |
Henrik Kjellander | 57e5fd2 | 2015-05-25 12:55:39 +0200 | [diff] [blame] | 663 | # WebRTC can't use the presubmit_canned_checks.PanProjectChecks function since |
| 664 | # we need to have different license checks in talk/ and webrtc/ directories. |
| 665 | # Instead, hand-picked checks are included below. |
Henrik Kjellander | 6322467 | 2015-09-08 08:03:56 +0200 | [diff] [blame] | 666 | |
tkchin | 3cd9a30 | 2016-06-08 12:40:28 -0700 | [diff] [blame] | 667 | # .m and .mm files are ObjC files. For simplicity we will consider .h files in |
| 668 | # ObjC subdirectories ObjC headers. |
| 669 | objc_filter_list = (r'.+\.m$', r'.+\.mm$', r'.+objc\/.+\.h$') |
Henrik Kjellander | b4af3d6 | 2016-11-16 20:11:29 +0100 | [diff] [blame] | 670 | # Skip long-lines check for DEPS and GN files. |
| 671 | build_file_filter_list = (r'.+\.gn$', r'.+\.gni$', 'DEPS') |
tkchin | 3cd9a30 | 2016-06-08 12:40:28 -0700 | [diff] [blame] | 672 | eighty_char_sources = lambda x: input_api.FilterSourceFile(x, |
| 673 | black_list=build_file_filter_list + objc_filter_list) |
| 674 | hundred_char_sources = lambda x: input_api.FilterSourceFile(x, |
| 675 | white_list=objc_filter_list) |
andrew@webrtc.org | 2442de1 | 2012-01-23 17:45:41 +0000 | [diff] [blame] | 676 | results.extend(input_api.canned_checks.CheckLongLines( |
tkchin | 3cd9a30 | 2016-06-08 12:40:28 -0700 | [diff] [blame] | 677 | input_api, output_api, maxlen=80, source_file_filter=eighty_char_sources)) |
| 678 | results.extend(input_api.canned_checks.CheckLongLines( |
| 679 | input_api, output_api, maxlen=100, |
| 680 | source_file_filter=hundred_char_sources)) |
| 681 | |
andrew@webrtc.org | 2442de1 | 2012-01-23 17:45:41 +0000 | [diff] [blame] | 682 | results.extend(input_api.canned_checks.CheckChangeHasNoTabs( |
| 683 | input_api, output_api)) |
andrew@webrtc.org | 53df136 | 2012-01-26 21:24:23 +0000 | [diff] [blame] | 684 | results.extend(input_api.canned_checks.CheckChangeHasNoStrayWhitespace( |
| 685 | input_api, output_api)) |
kjellander | e5dc62a | 2016-12-14 00:16:21 -0800 | [diff] [blame] | 686 | results.extend(input_api.canned_checks.CheckAuthorizedAuthor( |
| 687 | input_api, output_api)) |
andrew@webrtc.org | 53df136 | 2012-01-26 21:24:23 +0000 | [diff] [blame] | 688 | results.extend(input_api.canned_checks.CheckChangeTodoHasOwner( |
| 689 | input_api, output_api)) |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 690 | results.extend(CheckNativeApiHeaderChanges(input_api, output_api)) |
| 691 | results.extend(CheckNoIOStreamInHeaders(input_api, output_api)) |
| 692 | results.extend(CheckNoPragmaOnce(input_api, output_api)) |
| 693 | results.extend(CheckNoFRIEND_TEST(input_api, output_api)) |
| 694 | results.extend(CheckGnChanges(input_api, output_api)) |
| 695 | results.extend(CheckUnwantedDependencies(input_api, output_api)) |
| 696 | results.extend(CheckJSONParseErrors(input_api, output_api)) |
| 697 | results.extend(RunPythonTests(input_api, output_api)) |
| 698 | results.extend(CheckUsageOfGoogleProtobufNamespace(input_api, output_api)) |
Mirko Bonadei | 866d337 | 2017-09-15 12:35:26 +0200 | [diff] [blame] | 699 | results.extend(CheckOrphanHeaders(input_api, output_api)) |
Mirko Bonadei | a730c1c | 2017-09-18 11:33:13 +0200 | [diff] [blame] | 700 | results.extend(CheckNewlineAtTheEndOfProtoFiles(input_api, output_api)) |
andrew@webrtc.org | 53df136 | 2012-01-26 21:24:23 +0000 | [diff] [blame] | 701 | return results |
andrew@webrtc.org | 2442de1 | 2012-01-23 17:45:41 +0000 | [diff] [blame] | 702 | |
kjellander@webrtc.org | e415864 | 2014-08-06 09:11:18 +0000 | [diff] [blame] | 703 | |
andrew@webrtc.org | 53df136 | 2012-01-26 21:24:23 +0000 | [diff] [blame] | 704 | def CheckChangeOnUpload(input_api, output_api): |
| 705 | results = [] |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 706 | results.extend(CommonChecks(input_api, output_api)) |
Oleh Prypin | 920b653 | 2017-10-05 11:28:51 +0200 | [diff] [blame] | 707 | results.extend(CheckGnGen(input_api, output_api)) |
Henrik Kjellander | 57e5fd2 | 2015-05-25 12:55:39 +0200 | [diff] [blame] | 708 | results.extend( |
| 709 | input_api.canned_checks.CheckGNFormatted(input_api, output_api)) |
niklase@google.com | da159d6 | 2011-05-30 11:51:34 +0000 | [diff] [blame] | 710 | return results |
| 711 | |
kjellander@webrtc.org | e415864 | 2014-08-06 09:11:18 +0000 | [diff] [blame] | 712 | |
andrew@webrtc.org | 2442de1 | 2012-01-23 17:45:41 +0000 | [diff] [blame] | 713 | def CheckChangeOnCommit(input_api, output_api): |
niklase@google.com | 1198db9 | 2011-06-09 07:07:24 +0000 | [diff] [blame] | 714 | results = [] |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 715 | results.extend(CommonChecks(input_api, output_api)) |
| 716 | results.extend(VerifyNativeApiHeadersListIsValid(input_api, output_api)) |
niklase@google.com | 1198db9 | 2011-06-09 07:07:24 +0000 | [diff] [blame] | 717 | results.extend(input_api.canned_checks.CheckOwners(input_api, output_api)) |
andrew@webrtc.org | 53df136 | 2012-01-26 21:24:23 +0000 | [diff] [blame] | 718 | results.extend(input_api.canned_checks.CheckChangeWasUploaded( |
| 719 | input_api, output_api)) |
| 720 | results.extend(input_api.canned_checks.CheckChangeHasDescription( |
| 721 | input_api, output_api)) |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 722 | results.extend(CheckChangeHasBugField(input_api, output_api)) |
| 723 | results.extend(CheckCommitMessageBugEntry(input_api, output_api)) |
kjellander@webrtc.org | 12cb88c | 2014-02-13 11:53:43 +0000 | [diff] [blame] | 724 | results.extend(input_api.canned_checks.CheckTreeIsOpen( |
| 725 | input_api, output_api, |
| 726 | json_url='http://webrtc-status.appspot.com/current?format=json')) |
niklase@google.com | 1198db9 | 2011-06-09 07:07:24 +0000 | [diff] [blame] | 727 | return results |
mbonadei | 74973ed | 2017-05-09 07:58:05 -0700 | [diff] [blame] | 728 | |
| 729 | |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 730 | def CheckOrphanHeaders(input_api, output_api): |
mbonadei | 74973ed | 2017-05-09 07:58:05 -0700 | [diff] [blame] | 731 | # We need to wait until we have an input_api object and use this |
| 732 | # roundabout construct to import prebubmit_checks_lib because this file is |
| 733 | # eval-ed and thus doesn't have __file__. |
| 734 | error_msg = """Header file {} is not listed in any GN target. |
| 735 | Please create a target or add it to an existing one in {}""" |
| 736 | results = [] |
Oleh Prypin | 2f33a56 | 2017-10-04 20:17:54 +0200 | [diff] [blame] | 737 | with _AddToPath(input_api.os_path.join( |
| 738 | input_api.PresubmitLocalPath(), 'tools_webrtc', 'presubmit_checks_lib')): |
mbonadei | 74973ed | 2017-05-09 07:58:05 -0700 | [diff] [blame] | 739 | from check_orphan_headers import GetBuildGnPathFromFilePath |
| 740 | from check_orphan_headers import IsHeaderInBuildGn |
mbonadei | 74973ed | 2017-05-09 07:58:05 -0700 | [diff] [blame] | 741 | |
| 742 | for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile): |
mbonadei | a644ad3 | 2017-05-10 05:21:55 -0700 | [diff] [blame] | 743 | if f.LocalPath().endswith('.h') and f.Action() == 'A': |
mbonadei | 74973ed | 2017-05-09 07:58:05 -0700 | [diff] [blame] | 744 | file_path = os.path.abspath(f.LocalPath()) |
| 745 | root_dir = os.getcwd() |
| 746 | gn_file_path = GetBuildGnPathFromFilePath(file_path, os.path.exists, |
| 747 | root_dir) |
| 748 | in_build_gn = IsHeaderInBuildGn(file_path, gn_file_path) |
| 749 | if not in_build_gn: |
| 750 | results.append(output_api.PresubmitError(error_msg.format( |
| 751 | file_path, gn_file_path))) |
| 752 | return results |
Mirko Bonadei | 960fd5b | 2017-06-29 14:59:36 +0200 | [diff] [blame] | 753 | |
| 754 | |
Mirko Bonadei | a730c1c | 2017-09-18 11:33:13 +0200 | [diff] [blame] | 755 | def CheckNewlineAtTheEndOfProtoFiles(input_api, output_api): |
Mirko Bonadei | 960fd5b | 2017-06-29 14:59:36 +0200 | [diff] [blame] | 756 | """Checks that all .proto files are terminated with a newline.""" |
| 757 | error_msg = 'File {} must end with exactly one newline.' |
| 758 | results = [] |
| 759 | source_file_filter = lambda x: input_api.FilterSourceFile( |
| 760 | x, white_list=(r'.+\.proto$',)) |
| 761 | for f in input_api.AffectedSourceFiles(source_file_filter): |
| 762 | file_path = f.LocalPath() |
| 763 | with open(file_path) as f: |
| 764 | lines = f.readlines() |
Mirko Bonadei | a730c1c | 2017-09-18 11:33:13 +0200 | [diff] [blame] | 765 | if len(lines) > 0 and not lines[-1].endswith('\n'): |
Mirko Bonadei | 960fd5b | 2017-06-29 14:59:36 +0200 | [diff] [blame] | 766 | results.append(output_api.PresubmitError(error_msg.format(file_path))) |
| 767 | return results |