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