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