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', |
Amit Hilbuch | ce7032b | 2019-01-22 16:19:35 -0800 | [diff] [blame] | 22 | 'media/base/stream_params.h', |
| 23 | 'media/base/video_common.h', |
| 24 | 'media/sctp/sctp_transport.cc', |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 25 | 'modules/audio_coding', |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 26 | 'modules/audio_device', |
| 27 | 'modules/audio_processing', |
| 28 | 'modules/desktop_capture', |
| 29 | 'modules/include/module_common_types.h', |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 30 | 'modules/utility', |
| 31 | 'modules/video_capture', |
Amit Hilbuch | ce7032b | 2019-01-22 16:19:35 -0800 | [diff] [blame] | 32 | 'p2p/base/pseudo_tcp.cc', |
| 33 | 'p2p/base/pseudo_tcp.h', |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 34 | 'rtc_base', |
| 35 | 'sdk/android/src/jni', |
| 36 | 'sdk/objc', |
| 37 | 'system_wrappers', |
| 38 | 'test', |
Henrik Kjellander | 90fd7d8 | 2017-05-09 08:30:10 +0200 | [diff] [blame] | 39 | 'tools_webrtc', |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 40 | 'voice_engine', |
kjellander@webrtc.org | 0fcaf99 | 2015-11-26 15:24:52 +0100 | [diff] [blame] | 41 | ] |
| 42 | |
jbauch | c4e3ead | 2016-02-19 00:25:55 -0800 | [diff] [blame] | 43 | # These filters will always be removed, even if the caller specifies a filter |
| 44 | # set, as they are problematic or broken in some way. |
| 45 | # |
| 46 | # Justifications for each filter: |
| 47 | # - build/c++11 : Rvalue ref checks are unreliable (false positives), |
| 48 | # include file and feature blacklists are |
| 49 | # google3-specific. |
kjellander | e5a87a5 | 2016-04-27 02:32:12 -0700 | [diff] [blame] | 50 | # - whitespace/operators: Same as above (doesn't seem sufficient to eliminate |
| 51 | # all move-related errors). |
jbauch | c4e3ead | 2016-02-19 00:25:55 -0800 | [diff] [blame] | 52 | BLACKLIST_LINT_FILTERS = [ |
| 53 | '-build/c++11', |
kjellander | e5a87a5 | 2016-04-27 02:32:12 -0700 | [diff] [blame] | 54 | '-whitespace/operators', |
jbauch | c4e3ead | 2016-02-19 00:25:55 -0800 | [diff] [blame] | 55 | ] |
| 56 | |
kjellander | fd59523 | 2015-12-04 02:44:09 -0800 | [diff] [blame] | 57 | # List of directories of "supported" native APIs. That means changes to headers |
| 58 | # will be done in a compatible way following this scheme: |
| 59 | # 1. Non-breaking changes are made. |
| 60 | # 2. The old APIs as marked as deprecated (with comments). |
| 61 | # 3. Deprecation is announced to discuss-webrtc@googlegroups.com and |
| 62 | # webrtc-users@google.com (internal list). |
| 63 | # 4. (later) The deprecated APIs are removed. |
kjellander | 53047c9 | 2015-12-02 23:56:14 -0800 | [diff] [blame] | 64 | NATIVE_API_DIRS = ( |
Karl Wiberg | ef52d8b8 | 2017-10-25 13:20:03 +0200 | [diff] [blame] | 65 | 'api', # All subdirectories of api/ are included as well. |
Mirko Bonadei | a4eeeff | 2018-01-11 13:16:52 +0100 | [diff] [blame] | 66 | 'media/base', |
| 67 | 'media/engine', |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 68 | 'modules/audio_device/include', |
| 69 | 'pc', |
kjellander | dd70547 | 2016-06-09 11:17:27 -0700 | [diff] [blame] | 70 | ) |
Mirko Bonadei | 4dc4e25 | 2017-09-19 13:49:16 +0200 | [diff] [blame] | 71 | |
kjellander | dd70547 | 2016-06-09 11:17:27 -0700 | [diff] [blame] | 72 | # These directories should not be used but are maintained only to avoid breaking |
| 73 | # some legacy downstream code. |
| 74 | LEGACY_API_DIRS = ( |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 75 | 'common_audio/include', |
| 76 | 'modules/audio_coding/include', |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 77 | 'modules/audio_processing/include', |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 78 | 'modules/congestion_controller/include', |
| 79 | 'modules/include', |
| 80 | 'modules/remote_bitrate_estimator/include', |
| 81 | 'modules/rtp_rtcp/include', |
| 82 | 'modules/rtp_rtcp/source', |
| 83 | 'modules/utility/include', |
| 84 | 'modules/video_coding/codecs/h264/include', |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 85 | 'modules/video_coding/codecs/vp8/include', |
| 86 | 'modules/video_coding/codecs/vp9/include', |
| 87 | 'modules/video_coding/include', |
| 88 | 'rtc_base', |
| 89 | 'system_wrappers/include', |
kjellander | 53047c9 | 2015-12-02 23:56:14 -0800 | [diff] [blame] | 90 | ) |
Mirko Bonadei | 4dc4e25 | 2017-09-19 13:49:16 +0200 | [diff] [blame] | 91 | |
Karl Wiberg | d4f01c1 | 2017-11-10 10:55:45 +0100 | [diff] [blame] | 92 | # NOTE: The set of directories in API_DIRS should be the same as those |
| 93 | # listed in the table in native-api.md. |
kjellander | dd70547 | 2016-06-09 11:17:27 -0700 | [diff] [blame] | 94 | API_DIRS = NATIVE_API_DIRS[:] + LEGACY_API_DIRS[:] |
kjellander | 53047c9 | 2015-12-02 23:56:14 -0800 | [diff] [blame] | 95 | |
Mirko Bonadei | 4dc4e25 | 2017-09-19 13:49:16 +0200 | [diff] [blame] | 96 | # TARGET_RE matches a GN target, and extracts the target name and the contents. |
| 97 | TARGET_RE = re.compile(r'(?P<indent>\s*)\w+\("(?P<target_name>\w+)"\) {' |
| 98 | r'(?P<target_contents>.*?)' |
| 99 | r'(?P=indent)}', |
| 100 | re.MULTILINE | re.DOTALL) |
| 101 | |
| 102 | # SOURCES_RE matches a block of sources inside a GN target. |
| 103 | SOURCES_RE = re.compile(r'sources \+?= \[(?P<sources>.*?)\]', |
| 104 | re.MULTILINE | re.DOTALL) |
| 105 | |
| 106 | # FILE_PATH_RE matchies a file path. |
| 107 | FILE_PATH_RE = re.compile(r'"(?P<file_path>(\w|\/)+)(?P<extension>\.\w+)"') |
| 108 | |
kjellander | 53047c9 | 2015-12-02 23:56:14 -0800 | [diff] [blame] | 109 | |
Mirko Bonadei | d866544 | 2018-09-04 12:17:27 +0200 | [diff] [blame] | 110 | def FindSrcDirPath(starting_dir): |
| 111 | """Returns the abs path to the src/ dir of the project.""" |
| 112 | src_dir = starting_dir |
| 113 | while os.path.basename(src_dir) != 'src': |
| 114 | src_dir = os.path.normpath(os.path.join(src_dir, os.pardir)) |
| 115 | return src_dir |
| 116 | |
| 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 | |
Artem Titov | e92675b | 2018-05-22 10:21:27 +0200 | [diff] [blame] | 146 | |
kjellander | c88b5d5 | 2017-04-05 06:42:43 -0700 | [diff] [blame] | 147 | API_CHANGE_MSG = """ |
kwiberg | eb13302 | 2016-04-07 07:41:48 -0700 | [diff] [blame] | 148 | 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] | 149 | 1. Make compatible changes that don't break existing clients. Usually |
| 150 | this is done by keeping the existing method signatures unchanged. |
| 151 | 2. Mark the old stuff as deprecated (see RTC_DEPRECATED macro). |
kwiberg | eb13302 | 2016-04-07 07:41:48 -0700 | [diff] [blame] | 152 | 3. Create a timeline and plan for when the deprecated stuff will be |
| 153 | removed. (The amount of time we give users to change their code |
| 154 | should be informed by how much work it is for them. If they just |
| 155 | need to replace one name with another or something equally |
| 156 | simple, 1-2 weeks might be good; if they need to do serious work, |
| 157 | up to 3 months may be called for.) |
| 158 | 4. Update/inform existing downstream code owners to stop using the |
| 159 | deprecated stuff. (Send announcements to |
| 160 | discuss-webrtc@googlegroups.com and webrtc-users@google.com.) |
| 161 | 5. Remove the deprecated stuff, once the agreed-upon amount of time |
| 162 | has passed. |
| 163 | Related files: |
| 164 | """ |
kjellander | 53047c9 | 2015-12-02 23:56:14 -0800 | [diff] [blame] | 165 | |
Artem Titov | e92675b | 2018-05-22 10:21:27 +0200 | [diff] [blame] | 166 | |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 167 | def CheckNativeApiHeaderChanges(input_api, output_api): |
kjellander | 53047c9 | 2015-12-02 23:56:14 -0800 | [diff] [blame] | 168 | """Checks to remind proper changing of native APIs.""" |
| 169 | files = [] |
Karl Wiberg | 6bfac03 | 2017-10-27 15:14:20 +0200 | [diff] [blame] | 170 | source_file_filter = lambda x: input_api.FilterSourceFile( |
| 171 | x, white_list=[r'.+\.(gn|gni|h)$']) |
| 172 | for f in input_api.AffectedSourceFiles(source_file_filter): |
| 173 | for path in API_DIRS: |
| 174 | dn = os.path.dirname(f.LocalPath()) |
| 175 | if path == 'api': |
| 176 | # Special case: Subdirectories included. |
| 177 | if dn == 'api' or dn.startswith('api/'): |
Niels Möller | 1d20185 | 2019-06-26 12:58:27 +0200 | [diff] [blame] | 178 | files.append(f.LocalPath()) |
Karl Wiberg | 6bfac03 | 2017-10-27 15:14:20 +0200 | [diff] [blame] | 179 | else: |
| 180 | # Normal case: Subdirectories not included. |
| 181 | if dn == path: |
Niels Möller | 1d20185 | 2019-06-26 12:58:27 +0200 | [diff] [blame] | 182 | files.append(f.LocalPath()) |
kjellander | 53047c9 | 2015-12-02 23:56:14 -0800 | [diff] [blame] | 183 | |
| 184 | if files: |
kjellander | c88b5d5 | 2017-04-05 06:42:43 -0700 | [diff] [blame] | 185 | return [output_api.PresubmitNotifyResult(API_CHANGE_MSG, files)] |
kjellander | 53047c9 | 2015-12-02 23:56:14 -0800 | [diff] [blame] | 186 | return [] |
| 187 | |
kjellander@webrtc.org | 0fcaf99 | 2015-11-26 15:24:52 +0100 | [diff] [blame] | 188 | |
Artem Titov | a04d140 | 2018-05-11 11:23:00 +0200 | [diff] [blame] | 189 | def CheckNoIOStreamInHeaders(input_api, output_api, |
| 190 | source_file_filter): |
kjellander@webrtc.org | 51198f1 | 2012-02-21 17:53:46 +0000 | [diff] [blame] | 191 | """Checks to make sure no .h files include <iostream>.""" |
| 192 | files = [] |
| 193 | pattern = input_api.re.compile(r'^#include\s*<iostream>', |
| 194 | input_api.re.MULTILINE) |
Artem Titov | a04d140 | 2018-05-11 11:23:00 +0200 | [diff] [blame] | 195 | file_filter = lambda x: (input_api.FilterSourceFile(x) |
| 196 | and source_file_filter(x)) |
| 197 | for f in input_api.AffectedSourceFiles(file_filter): |
kjellander@webrtc.org | 51198f1 | 2012-02-21 17:53:46 +0000 | [diff] [blame] | 198 | if not f.LocalPath().endswith('.h'): |
| 199 | continue |
| 200 | contents = input_api.ReadFile(f) |
| 201 | if pattern.search(contents): |
| 202 | files.append(f) |
| 203 | |
| 204 | if len(files): |
Henrik Kjellander | 57e5fd2 | 2015-05-25 12:55:39 +0200 | [diff] [blame] | 205 | return [output_api.PresubmitError( |
kjellander@webrtc.org | 51198f1 | 2012-02-21 17:53:46 +0000 | [diff] [blame] | 206 | 'Do not #include <iostream> in header files, since it inserts static ' + |
| 207 | 'initialization into every file including the header. Instead, ' + |
| 208 | '#include <ostream>. See http://crbug.com/94794', |
Henrik Kjellander | 57e5fd2 | 2015-05-25 12:55:39 +0200 | [diff] [blame] | 209 | files)] |
kjellander@webrtc.org | 51198f1 | 2012-02-21 17:53:46 +0000 | [diff] [blame] | 210 | return [] |
| 211 | |
kjellander@webrtc.org | e415864 | 2014-08-06 09:11:18 +0000 | [diff] [blame] | 212 | |
Artem Titov | a04d140 | 2018-05-11 11:23:00 +0200 | [diff] [blame] | 213 | def CheckNoPragmaOnce(input_api, output_api, |
| 214 | source_file_filter): |
kjellander | 6aeef74 | 2017-02-20 01:13:18 -0800 | [diff] [blame] | 215 | """Make sure that banned functions are not used.""" |
| 216 | files = [] |
| 217 | pattern = input_api.re.compile(r'^#pragma\s+once', |
| 218 | input_api.re.MULTILINE) |
Artem Titov | a04d140 | 2018-05-11 11:23:00 +0200 | [diff] [blame] | 219 | file_filter = lambda x: (input_api.FilterSourceFile(x) |
| 220 | and source_file_filter(x)) |
| 221 | for f in input_api.AffectedSourceFiles(file_filter): |
kjellander | 6aeef74 | 2017-02-20 01:13:18 -0800 | [diff] [blame] | 222 | if not f.LocalPath().endswith('.h'): |
| 223 | continue |
| 224 | contents = input_api.ReadFile(f) |
| 225 | if pattern.search(contents): |
| 226 | files.append(f) |
| 227 | |
| 228 | if files: |
| 229 | return [output_api.PresubmitError( |
| 230 | 'Do not use #pragma once in header files.\n' |
| 231 | 'See http://www.chromium.org/developers/coding-style#TOC-File-headers', |
| 232 | files)] |
| 233 | return [] |
| 234 | |
| 235 | |
Artem Titov | a04d140 | 2018-05-11 11:23:00 +0200 | [diff] [blame] | 236 | def CheckNoFRIEND_TEST(input_api, output_api, # pylint: disable=invalid-name |
| 237 | source_file_filter): |
kjellander@webrtc.org | 51198f1 | 2012-02-21 17:53:46 +0000 | [diff] [blame] | 238 | """Make sure that gtest's FRIEND_TEST() macro is not used, the |
| 239 | FRIEND_TEST_ALL_PREFIXES() macro from testsupport/gtest_prod_util.h should be |
| 240 | used instead since that allows for FLAKY_, FAILS_ and DISABLED_ prefixes.""" |
| 241 | problems = [] |
| 242 | |
Artem Titov | a04d140 | 2018-05-11 11:23:00 +0200 | [diff] [blame] | 243 | file_filter = lambda f: (f.LocalPath().endswith(('.cc', '.h')) |
| 244 | and source_file_filter(f)) |
kjellander@webrtc.org | 51198f1 | 2012-02-21 17:53:46 +0000 | [diff] [blame] | 245 | for f in input_api.AffectedFiles(file_filter=file_filter): |
| 246 | for line_num, line in f.ChangedContents(): |
| 247 | if 'FRIEND_TEST(' in line: |
| 248 | problems.append(' %s:%d' % (f.LocalPath(), line_num)) |
| 249 | |
| 250 | if not problems: |
| 251 | return [] |
| 252 | return [output_api.PresubmitPromptWarning('WebRTC\'s code should not use ' |
| 253 | 'gtest\'s FRIEND_TEST() macro. Include testsupport/gtest_prod_util.h and ' |
| 254 | 'use FRIEND_TEST_ALL_PREFIXES() instead.\n' + '\n'.join(problems))] |
| 255 | |
kjellander@webrtc.org | e415864 | 2014-08-06 09:11:18 +0000 | [diff] [blame] | 256 | |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 257 | def IsLintBlacklisted(blacklist_paths, file_path): |
oprypin | 2aa463f | 2017-03-23 03:17:02 -0700 | [diff] [blame] | 258 | """ Checks if a file is blacklisted for lint check.""" |
| 259 | for path in blacklist_paths: |
| 260 | 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] | 261 | return True |
| 262 | return False |
| 263 | |
| 264 | |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 265 | def CheckApprovedFilesLintClean(input_api, output_api, |
Artem Titov | a04d140 | 2018-05-11 11:23:00 +0200 | [diff] [blame] | 266 | source_file_filter=None): |
oprypin | 2aa463f | 2017-03-23 03:17:02 -0700 | [diff] [blame] | 267 | """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] | 268 | This check is based on CheckChangeLintsClean in |
kjellander@webrtc.org | 51198f1 | 2012-02-21 17:53:46 +0000 | [diff] [blame] | 269 | depot_tools/presubmit_canned_checks.py but has less filters and only checks |
| 270 | added files.""" |
| 271 | result = [] |
| 272 | |
| 273 | # Initialize cpplint. |
| 274 | import cpplint |
| 275 | # Access to a protected member _XX of a client class |
| 276 | # pylint: disable=W0212 |
| 277 | cpplint._cpplint_state.ResetErrorCounts() |
| 278 | |
jbauch | c4e3ead | 2016-02-19 00:25:55 -0800 | [diff] [blame] | 279 | lint_filters = cpplint._Filters() |
| 280 | lint_filters.extend(BLACKLIST_LINT_FILTERS) |
| 281 | cpplint._SetFilters(','.join(lint_filters)) |
| 282 | |
oprypin | 2aa463f | 2017-03-23 03:17:02 -0700 | [diff] [blame] | 283 | # Create a platform independent blacklist for cpplint. |
| 284 | blacklist_paths = [input_api.os_path.join(*path.split('/')) |
| 285 | for path in CPPLINT_BLACKLIST] |
kjellander@webrtc.org | 0fcaf99 | 2015-11-26 15:24:52 +0100 | [diff] [blame] | 286 | |
kjellander@webrtc.org | 51198f1 | 2012-02-21 17:53:46 +0000 | [diff] [blame] | 287 | # Use the strictest verbosity level for cpplint.py (level 1) which is the |
oprypin | 2aa463f | 2017-03-23 03:17:02 -0700 | [diff] [blame] | 288 | # default when running cpplint.py from command line. To make it possible to |
| 289 | # work with not-yet-converted code, we're only applying it to new (or |
| 290 | # moved/renamed) files and files not listed in CPPLINT_BLACKLIST. |
kjellander@webrtc.org | 51198f1 | 2012-02-21 17:53:46 +0000 | [diff] [blame] | 291 | verbosity_level = 1 |
| 292 | files = [] |
| 293 | for f in input_api.AffectedSourceFiles(source_file_filter): |
Henrik Kjellander | 57e5fd2 | 2015-05-25 12:55:39 +0200 | [diff] [blame] | 294 | # Note that moved/renamed files also count as added. |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 295 | if f.Action() == 'A' or not IsLintBlacklisted(blacklist_paths, |
Artem Titov | e92675b | 2018-05-22 10:21:27 +0200 | [diff] [blame] | 296 | f.LocalPath()): |
kjellander@webrtc.org | 51198f1 | 2012-02-21 17:53:46 +0000 | [diff] [blame] | 297 | files.append(f.AbsoluteLocalPath()) |
mflodman@webrtc.org | 2a45209 | 2012-07-01 05:55:23 +0000 | [diff] [blame] | 298 | |
kjellander@webrtc.org | 51198f1 | 2012-02-21 17:53:46 +0000 | [diff] [blame] | 299 | for file_name in files: |
| 300 | cpplint.ProcessFile(file_name, verbosity_level) |
| 301 | |
| 302 | if cpplint._cpplint_state.error_count > 0: |
| 303 | if input_api.is_committing: |
oprypin | 8e58d65 | 2017-03-21 07:52:41 -0700 | [diff] [blame] | 304 | res_type = output_api.PresubmitError |
kjellander@webrtc.org | 51198f1 | 2012-02-21 17:53:46 +0000 | [diff] [blame] | 305 | else: |
| 306 | res_type = output_api.PresubmitPromptWarning |
| 307 | result = [res_type('Changelist failed cpplint.py check.')] |
| 308 | |
| 309 | return result |
| 310 | |
Artem Titov | e92675b | 2018-05-22 10:21:27 +0200 | [diff] [blame] | 311 | |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 312 | def CheckNoSourcesAbove(input_api, gn_files, output_api): |
ehmaldonado | 5b1ba08 | 2016-09-02 05:51:08 -0700 | [diff] [blame] | 313 | # Disallow referencing source files with paths above the GN file location. |
| 314 | source_pattern = input_api.re.compile(r' +sources \+?= \[(.*?)\]', |
| 315 | re.MULTILINE | re.DOTALL) |
| 316 | file_pattern = input_api.re.compile(r'"((\.\./.*?)|(//.*?))"') |
| 317 | violating_gn_files = set() |
| 318 | violating_source_entries = [] |
| 319 | for gn_file in gn_files: |
| 320 | contents = input_api.ReadFile(gn_file) |
| 321 | for source_block_match in source_pattern.finditer(contents): |
| 322 | # Find all source list entries starting with ../ in the source block |
| 323 | # (exclude overrides entries). |
| 324 | for file_list_match in file_pattern.finditer(source_block_match.group(1)): |
| 325 | source_file = file_list_match.group(1) |
| 326 | if 'overrides/' not in source_file: |
| 327 | violating_source_entries.append(source_file) |
| 328 | violating_gn_files.add(gn_file) |
| 329 | if violating_gn_files: |
| 330 | return [output_api.PresubmitError( |
| 331 | 'Referencing source files above the directory of the GN file is not ' |
Henrik Kjellander | b4af3d6 | 2016-11-16 20:11:29 +0100 | [diff] [blame] | 332 | 'allowed. Please introduce new GN targets in the proper location ' |
| 333 | 'instead.\n' |
ehmaldonado | 5b1ba08 | 2016-09-02 05:51:08 -0700 | [diff] [blame] | 334 | 'Invalid source entries:\n' |
| 335 | '%s\n' |
| 336 | 'Violating GN files:' % '\n'.join(violating_source_entries), |
| 337 | items=violating_gn_files)] |
| 338 | return [] |
| 339 | |
Artem Titov | e92675b | 2018-05-22 10:21:27 +0200 | [diff] [blame] | 340 | |
Mirko Bonadei | 4dc4e25 | 2017-09-19 13:49:16 +0200 | [diff] [blame] | 341 | def CheckNoMixingSources(input_api, gn_files, output_api): |
| 342 | """Disallow mixing C, C++ and Obj-C/Obj-C++ in the same target. |
| 343 | |
| 344 | See bugs.webrtc.org/7743 for more context. |
| 345 | """ |
Artem Titov | e92675b | 2018-05-22 10:21:27 +0200 | [diff] [blame] | 346 | |
Mirko Bonadei | 4dc4e25 | 2017-09-19 13:49:16 +0200 | [diff] [blame] | 347 | def _MoreThanOneSourceUsed(*sources_lists): |
| 348 | sources_used = 0 |
| 349 | for source_list in sources_lists: |
| 350 | if len(source_list): |
| 351 | sources_used += 1 |
| 352 | return sources_used > 1 |
| 353 | |
| 354 | errors = defaultdict(lambda: []) |
kjellander | 7439f97 | 2016-12-05 22:47:46 -0800 | [diff] [blame] | 355 | for gn_file in gn_files: |
Mirko Bonadei | 4dc4e25 | 2017-09-19 13:49:16 +0200 | [diff] [blame] | 356 | gn_file_content = input_api.ReadFile(gn_file) |
| 357 | for target_match in TARGET_RE.finditer(gn_file_content): |
| 358 | # list_of_sources is a list of tuples of the form |
| 359 | # (c_files, cc_files, objc_files) that keeps track of all the sources |
| 360 | # defined in a target. A GN target can have more that on definition of |
| 361 | # sources (since it supports if/else statements). |
| 362 | # E.g.: |
| 363 | # rtc_static_library("foo") { |
| 364 | # if (is_win) { |
| 365 | # sources = [ "foo.cc" ] |
| 366 | # } else { |
| 367 | # sources = [ "foo.mm" ] |
| 368 | # } |
| 369 | # } |
| 370 | # This is allowed and the presubmit check should support this case. |
| 371 | list_of_sources = [] |
kjellander | 7439f97 | 2016-12-05 22:47:46 -0800 | [diff] [blame] | 372 | c_files = [] |
| 373 | cc_files = [] |
Mirko Bonadei | 4dc4e25 | 2017-09-19 13:49:16 +0200 | [diff] [blame] | 374 | objc_files = [] |
| 375 | target_name = target_match.group('target_name') |
| 376 | target_contents = target_match.group('target_contents') |
| 377 | for sources_match in SOURCES_RE.finditer(target_contents): |
| 378 | if '+=' not in sources_match.group(0): |
| 379 | if c_files or cc_files or objc_files: |
| 380 | list_of_sources.append((c_files, cc_files, objc_files)) |
| 381 | c_files = [] |
| 382 | cc_files = [] |
| 383 | objc_files = [] |
| 384 | for file_match in FILE_PATH_RE.finditer(sources_match.group(1)): |
| 385 | file_path = file_match.group('file_path') |
| 386 | extension = file_match.group('extension') |
| 387 | if extension == '.c': |
| 388 | c_files.append(file_path + extension) |
| 389 | if extension == '.cc': |
| 390 | cc_files.append(file_path + extension) |
| 391 | if extension in ['.m', '.mm']: |
| 392 | objc_files.append(file_path + extension) |
| 393 | list_of_sources.append((c_files, cc_files, objc_files)) |
| 394 | for c_files_list, cc_files_list, objc_files_list in list_of_sources: |
| 395 | if _MoreThanOneSourceUsed(c_files_list, cc_files_list, objc_files_list): |
| 396 | all_sources = sorted(c_files_list + cc_files_list + objc_files_list) |
| 397 | errors[gn_file.LocalPath()].append((target_name, all_sources)) |
| 398 | if errors: |
kjellander | 7439f97 | 2016-12-05 22:47:46 -0800 | [diff] [blame] | 399 | return [output_api.PresubmitError( |
Mirko Bonadei | 4dc4e25 | 2017-09-19 13:49:16 +0200 | [diff] [blame] | 400 | 'GN targets cannot mix .c, .cc and .m (or .mm) source files.\n' |
| 401 | 'Please create a separate target for each collection of sources.\n' |
kjellander | 7439f97 | 2016-12-05 22:47:46 -0800 | [diff] [blame] | 402 | 'Mixed sources: \n' |
| 403 | '%s\n' |
Mirko Bonadei | 4dc4e25 | 2017-09-19 13:49:16 +0200 | [diff] [blame] | 404 | 'Violating GN files:\n%s\n' % (json.dumps(errors, indent=2), |
| 405 | '\n'.join(errors.keys())))] |
kjellander | 7439f97 | 2016-12-05 22:47:46 -0800 | [diff] [blame] | 406 | return [] |
| 407 | |
Artem Titov | e92675b | 2018-05-22 10:21:27 +0200 | [diff] [blame] | 408 | |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 409 | def CheckNoPackageBoundaryViolations(input_api, gn_files, output_api): |
ehmaldonado | 4fb9746 | 2017-01-30 05:27:22 -0800 | [diff] [blame] | 410 | cwd = input_api.PresubmitLocalPath() |
Oleh Prypin | 2f33a56 | 2017-10-04 20:17:54 +0200 | [diff] [blame] | 411 | with _AddToPath(input_api.os_path.join( |
| 412 | cwd, 'tools_webrtc', 'presubmit_checks_lib')): |
| 413 | from check_package_boundaries import CheckPackageBoundaries |
| 414 | build_files = [os.path.join(cwd, gn_file.LocalPath()) for gn_file in gn_files] |
| 415 | errors = CheckPackageBoundaries(cwd, build_files)[:5] |
| 416 | if errors: |
ehmaldonado | 4fb9746 | 2017-01-30 05:27:22 -0800 | [diff] [blame] | 417 | return [output_api.PresubmitError( |
Oleh Prypin | 2f33a56 | 2017-10-04 20:17:54 +0200 | [diff] [blame] | 418 | 'There are package boundary violations in the following GN files:', |
| 419 | long_text='\n\n'.join(str(err) for err in errors))] |
ehmaldonado | 4fb9746 | 2017-01-30 05:27:22 -0800 | [diff] [blame] | 420 | return [] |
| 421 | |
Mirko Bonadei | a51bbd8 | 2018-03-08 16:15:45 +0100 | [diff] [blame] | 422 | |
Mirko Bonadei | f0e0d75 | 2018-07-04 08:48:18 +0200 | [diff] [blame] | 423 | def _ReportFileAndLine(filename, line_num): |
Mirko Bonadei | a51bbd8 | 2018-03-08 16:15:45 +0100 | [diff] [blame] | 424 | """Default error formatter for _FindNewViolationsOfRule.""" |
| 425 | return '%s (line %s)' % (filename, line_num) |
| 426 | |
| 427 | |
Mirko Bonadei | f0e0d75 | 2018-07-04 08:48:18 +0200 | [diff] [blame] | 428 | def CheckNoWarningSuppressionFlagsAreAdded(gn_files, input_api, output_api, |
| 429 | error_formatter=_ReportFileAndLine): |
| 430 | """Make sure that warning suppression flags are not added wihtout a reason.""" |
| 431 | msg = ('Usage of //build/config/clang:extra_warnings is discouraged ' |
| 432 | 'in WebRTC.\n' |
| 433 | 'If you are not adding this code (e.g. you are just moving ' |
| 434 | 'existing code) or you want to add an exception,\n' |
| 435 | 'you can add a comment on the line that causes the problem:\n\n' |
| 436 | '"-Wno-odr" # no-presubmit-check TODO(bugs.webrtc.org/BUG_ID)\n' |
| 437 | '\n' |
| 438 | 'Affected files:\n') |
| 439 | errors = [] # 2-element tuples with (file, line number) |
| 440 | clang_warn_re = input_api.re.compile(r'//build/config/clang:extra_warnings') |
| 441 | no_presubmit_re = input_api.re.compile( |
| 442 | r'# no-presubmit-check TODO\(bugs\.webrtc\.org/\d+\)') |
| 443 | for f in gn_files: |
| 444 | for line_num, line in f.ChangedContents(): |
| 445 | if clang_warn_re.search(line) and not no_presubmit_re.search(line): |
| 446 | errors.append(error_formatter(f.LocalPath(), line_num)) |
| 447 | if errors: |
| 448 | return [output_api.PresubmitError(msg, errors)] |
| 449 | return [] |
| 450 | |
Mirko Bonadei | 9ce800d | 2019-02-05 16:48:13 +0100 | [diff] [blame] | 451 | |
| 452 | def CheckNoTestCaseUsageIsAdded(input_api, output_api, source_file_filter, |
| 453 | error_formatter=_ReportFileAndLine): |
| 454 | error_msg = ('Usage of legacy GoogleTest API detected!\nPlease use the ' |
| 455 | 'new API: https://github.com/google/googletest/blob/master/' |
| 456 | 'googletest/docs/primer.md#beware-of-the-nomenclature.\n' |
| 457 | 'Affected files:\n') |
| 458 | errors = [] # 2-element tuples with (file, line number) |
| 459 | test_case_re = input_api.re.compile(r'TEST_CASE') |
| 460 | file_filter = lambda f: (source_file_filter(f) |
| 461 | and f.LocalPath().endswith('.cc')) |
| 462 | for f in input_api.AffectedSourceFiles(file_filter): |
| 463 | for line_num, line in f.ChangedContents(): |
| 464 | if test_case_re.search(line): |
| 465 | errors.append(error_formatter(f.LocalPath(), line_num)) |
| 466 | if errors: |
| 467 | return [output_api.PresubmitError(error_msg, errors)] |
| 468 | return [] |
| 469 | |
| 470 | |
Mirko Bonadei | a51bbd8 | 2018-03-08 16:15:45 +0100 | [diff] [blame] | 471 | def CheckNoStreamUsageIsAdded(input_api, output_api, |
Artem Titov | 739351d | 2018-05-11 12:21:36 +0200 | [diff] [blame] | 472 | source_file_filter, |
Mirko Bonadei | f0e0d75 | 2018-07-04 08:48:18 +0200 | [diff] [blame] | 473 | error_formatter=_ReportFileAndLine): |
Mirko Bonadei | a51bbd8 | 2018-03-08 16:15:45 +0100 | [diff] [blame] | 474 | """Make sure that no more dependencies on stringstream are added.""" |
| 475 | error_msg = ('Usage of <sstream>, <istream> and <ostream> in WebRTC is ' |
| 476 | 'deprecated.\n' |
| 477 | 'This includes the following types:\n' |
| 478 | 'std::istringstream, std::ostringstream, std::wistringstream, ' |
| 479 | 'std::wostringstream,\n' |
| 480 | 'std::wstringstream, std::ostream, std::wostream, std::istream,' |
| 481 | 'std::wistream,\n' |
| 482 | 'std::iostream, std::wiostream.\n' |
| 483 | 'If you are not adding this code (e.g. you are just moving ' |
| 484 | 'existing code),\n' |
| 485 | 'you can add a comment on the line that causes the problem:\n\n' |
| 486 | '#include <sstream> // no-presubmit-check TODO(webrtc:8982)\n' |
| 487 | 'std::ostream& F() { // no-presubmit-check TODO(webrtc:8982)\n' |
| 488 | '\n' |
Karl Wiberg | ebd01e8 | 2018-03-14 15:08:39 +0100 | [diff] [blame] | 489 | 'If you are adding new code, consider using ' |
| 490 | 'rtc::SimpleStringBuilder\n' |
| 491 | '(in rtc_base/strings/string_builder.h).\n' |
Mirko Bonadei | a51bbd8 | 2018-03-08 16:15:45 +0100 | [diff] [blame] | 492 | 'Affected files:\n') |
| 493 | errors = [] # 2-element tuples with (file, line number) |
| 494 | include_re = input_api.re.compile(r'#include <(i|o|s)stream>') |
| 495 | usage_re = input_api.re.compile(r'std::(w|i|o|io|wi|wo|wio)(string)*stream') |
| 496 | no_presubmit_re = input_api.re.compile( |
Jonas Olsson | 7439534 | 2018-04-03 12:22:07 +0200 | [diff] [blame] | 497 | r'// no-presubmit-check TODO\(webrtc:8982\)') |
Artem Titov | a04d140 | 2018-05-11 11:23:00 +0200 | [diff] [blame] | 498 | file_filter = lambda x: (input_api.FilterSourceFile(x) |
| 499 | and source_file_filter(x)) |
Mirko Bonadei | 571791a | 2019-05-07 14:08:05 +0200 | [diff] [blame] | 500 | |
| 501 | def _IsException(file_path): |
| 502 | is_test = any(file_path.endswith(x) for x in ['_test.cc', '_tests.cc', |
| 503 | '_unittest.cc', |
| 504 | '_unittests.cc']) |
Patrik Höglund | 2ea2796 | 2020-01-13 15:10:40 +0100 | [diff] [blame] | 505 | return (file_path.startswith('examples') or |
| 506 | file_path.startswith('test') or |
| 507 | is_test) |
| 508 | |
Mirko Bonadei | 571791a | 2019-05-07 14:08:05 +0200 | [diff] [blame] | 509 | |
Artem Titov | a04d140 | 2018-05-11 11:23:00 +0200 | [diff] [blame] | 510 | for f in input_api.AffectedSourceFiles(file_filter): |
Mirko Bonadei | 571791a | 2019-05-07 14:08:05 +0200 | [diff] [blame] | 511 | # Usage of stringstream is allowed under examples/ and in tests. |
| 512 | if f.LocalPath() == 'PRESUBMIT.py' or _IsException(f.LocalPath()): |
Mirko Bonadei | d2c8332 | 2018-03-19 10:31:47 +0000 | [diff] [blame] | 513 | continue |
| 514 | for line_num, line in f.ChangedContents(): |
| 515 | if ((include_re.search(line) or usage_re.search(line)) |
| 516 | and not no_presubmit_re.search(line)): |
| 517 | errors.append(error_formatter(f.LocalPath(), line_num)) |
Mirko Bonadei | a51bbd8 | 2018-03-08 16:15:45 +0100 | [diff] [blame] | 518 | if errors: |
| 519 | return [output_api.PresubmitError(error_msg, errors)] |
| 520 | return [] |
| 521 | |
Artem Titov | e92675b | 2018-05-22 10:21:27 +0200 | [diff] [blame] | 522 | |
Mirko Bonadei | a05d47e | 2018-05-09 11:03:38 +0200 | [diff] [blame] | 523 | def CheckPublicDepsIsNotUsed(gn_files, input_api, output_api): |
| 524 | """Checks that public_deps is not used without a good reason.""" |
Mirko Bonadei | 5c1ad59 | 2017-12-12 11:52:27 +0100 | [diff] [blame] | 525 | result = [] |
Mirko Bonadei | a05d47e | 2018-05-09 11:03:38 +0200 | [diff] [blame] | 526 | no_presubmit_check_re = input_api.re.compile( |
Joe Chen | 0b3a6e3 | 2019-12-26 23:01:42 -0800 | [diff] [blame] | 527 | r'# no-presubmit-check TODO\(webrtc:\d+\)') |
Mirko Bonadei | a05d47e | 2018-05-09 11:03:38 +0200 | [diff] [blame] | 528 | error_msg = ('public_deps is not recommended in WebRTC BUILD.gn files ' |
| 529 | 'because it doesn\'t map well to downstream build systems.\n' |
| 530 | 'Used in: %s (line %d).\n' |
| 531 | 'If you are not adding this code (e.g. you are just moving ' |
Patrik Höglund | 81c7a60 | 2020-01-30 11:32:33 +0100 | [diff] [blame] | 532 | 'existing code) or you have a good reason, you can add this ' |
| 533 | 'comment (verbatim) on the line that causes the problem:\n\n' |
Mirko Bonadei | a05d47e | 2018-05-09 11:03:38 +0200 | [diff] [blame] | 534 | 'public_deps = [ # no-presubmit-check TODO(webrtc:8603)\n') |
Mirko Bonadei | 5c1ad59 | 2017-12-12 11:52:27 +0100 | [diff] [blame] | 535 | for affected_file in gn_files: |
| 536 | for (line_number, affected_line) in affected_file.ChangedContents(): |
Patrik Höglund | 81c7a60 | 2020-01-30 11:32:33 +0100 | [diff] [blame] | 537 | if 'public_deps' in affected_line: |
| 538 | surpressed = no_presubmit_check_re.search(affected_line) |
| 539 | if not surpressed: |
| 540 | result.append( |
| 541 | output_api.PresubmitError(error_msg % (affected_file.LocalPath(), |
| 542 | line_number))) |
Mirko Bonadei | 5c1ad59 | 2017-12-12 11:52:27 +0100 | [diff] [blame] | 543 | return result |
| 544 | |
Artem Titov | e92675b | 2018-05-22 10:21:27 +0200 | [diff] [blame] | 545 | |
Mirko Bonadei | 05691dd | 2019-10-22 07:34:24 -0700 | [diff] [blame] | 546 | def CheckCheckIncludesIsNotUsed(gn_files, input_api, output_api): |
Patrik Höglund | 6f49106 | 2018-01-11 12:04:23 +0100 | [diff] [blame] | 547 | result = [] |
| 548 | error_msg = ('check_includes overrides are not allowed since it can cause ' |
| 549 | 'incorrect dependencies to form. It effectively means that your ' |
| 550 | 'module can include any .h file without depending on its ' |
| 551 | 'corresponding target. There are some exceptional cases when ' |
Mirko Bonadei | 05691dd | 2019-10-22 07:34:24 -0700 | [diff] [blame] | 552 | 'this is allowed: if so, get approval from a .gn owner in the ' |
Patrik Höglund | 6f49106 | 2018-01-11 12:04:23 +0100 | [diff] [blame] | 553 | 'root OWNERS file.\n' |
| 554 | 'Used in: %s (line %d).') |
Mirko Bonadei | 05691dd | 2019-10-22 07:34:24 -0700 | [diff] [blame] | 555 | no_presubmit_re = input_api.re.compile( |
| 556 | r'# no-presubmit-check TODO\(bugs\.webrtc\.org/\d+\)') |
Patrik Höglund | 6f49106 | 2018-01-11 12:04:23 +0100 | [diff] [blame] | 557 | for affected_file in gn_files: |
| 558 | for (line_number, affected_line) in affected_file.ChangedContents(): |
Mirko Bonadei | 05691dd | 2019-10-22 07:34:24 -0700 | [diff] [blame] | 559 | if ('check_includes' in affected_line |
| 560 | and not no_presubmit_re.search(affected_line)): |
Patrik Höglund | 6f49106 | 2018-01-11 12:04:23 +0100 | [diff] [blame] | 561 | result.append( |
| 562 | output_api.PresubmitError(error_msg % (affected_file.LocalPath(), |
| 563 | line_number))) |
| 564 | return result |
| 565 | |
Artem Titov | e92675b | 2018-05-22 10:21:27 +0200 | [diff] [blame] | 566 | |
Mirko Bonadei | f0e0d75 | 2018-07-04 08:48:18 +0200 | [diff] [blame] | 567 | def CheckGnChanges(input_api, output_api): |
Artem Titov | a04d140 | 2018-05-11 11:23:00 +0200 | [diff] [blame] | 568 | file_filter = lambda x: (input_api.FilterSourceFile( |
Oleh Prypin | afe0165 | 2017-10-04 15:56:08 +0200 | [diff] [blame] | 569 | x, white_list=(r'.+\.(gn|gni)$',), |
Mirko Bonadei | f0e0d75 | 2018-07-04 08:48:18 +0200 | [diff] [blame] | 570 | black_list=(r'.*/presubmit_checks_lib/testdata/.*',))) |
ehmaldonado | 5b1ba08 | 2016-09-02 05:51:08 -0700 | [diff] [blame] | 571 | |
| 572 | gn_files = [] |
Artem Titov | a04d140 | 2018-05-11 11:23:00 +0200 | [diff] [blame] | 573 | for f in input_api.AffectedSourceFiles(file_filter): |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 574 | gn_files.append(f) |
ehmaldonado | 5b1ba08 | 2016-09-02 05:51:08 -0700 | [diff] [blame] | 575 | |
| 576 | result = [] |
| 577 | if gn_files: |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 578 | result.extend(CheckNoSourcesAbove(input_api, gn_files, output_api)) |
Mirko Bonadei | 4dc4e25 | 2017-09-19 13:49:16 +0200 | [diff] [blame] | 579 | result.extend(CheckNoMixingSources(input_api, gn_files, output_api)) |
| 580 | result.extend(CheckNoPackageBoundaryViolations(input_api, gn_files, |
| 581 | output_api)) |
Mirko Bonadei | a05d47e | 2018-05-09 11:03:38 +0200 | [diff] [blame] | 582 | result.extend(CheckPublicDepsIsNotUsed(gn_files, input_api, output_api)) |
Mirko Bonadei | 05691dd | 2019-10-22 07:34:24 -0700 | [diff] [blame] | 583 | result.extend(CheckCheckIncludesIsNotUsed(gn_files, input_api, output_api)) |
Mirko Bonadei | f0e0d75 | 2018-07-04 08:48:18 +0200 | [diff] [blame] | 584 | result.extend(CheckNoWarningSuppressionFlagsAreAdded(gn_files, input_api, |
| 585 | output_api)) |
ehmaldonado | 5b1ba08 | 2016-09-02 05:51:08 -0700 | [diff] [blame] | 586 | return result |
| 587 | |
Artem Titov | e92675b | 2018-05-22 10:21:27 +0200 | [diff] [blame] | 588 | |
Oleh Prypin | 920b653 | 2017-10-05 11:28:51 +0200 | [diff] [blame] | 589 | def CheckGnGen(input_api, output_api): |
| 590 | """Runs `gn gen --check` with default args to detect mismatches between |
| 591 | #includes and dependencies in the BUILD.gn files, as well as general build |
| 592 | errors. |
| 593 | """ |
| 594 | with _AddToPath(input_api.os_path.join( |
| 595 | input_api.PresubmitLocalPath(), 'tools_webrtc', 'presubmit_checks_lib')): |
Yves Gerey | 546ee61 | 2019-02-26 17:04:16 +0100 | [diff] [blame] | 596 | from build_helpers import RunGnCheck |
Mirko Bonadei | d866544 | 2018-09-04 12:17:27 +0200 | [diff] [blame] | 597 | errors = RunGnCheck(FindSrcDirPath(input_api.PresubmitLocalPath()))[:5] |
Oleh Prypin | 920b653 | 2017-10-05 11:28:51 +0200 | [diff] [blame] | 598 | if errors: |
| 599 | return [output_api.PresubmitPromptWarning( |
| 600 | 'Some #includes do not match the build dependency graph. Please run:\n' |
| 601 | ' gn gen --check <out_dir>', |
| 602 | long_text='\n\n'.join(errors))] |
| 603 | return [] |
| 604 | |
Artem Titov | e92675b | 2018-05-22 10:21:27 +0200 | [diff] [blame] | 605 | |
Artem Titov | a04d140 | 2018-05-11 11:23:00 +0200 | [diff] [blame] | 606 | def CheckUnwantedDependencies(input_api, output_api, source_file_filter): |
kjellander@webrtc.org | 3bd4156 | 2014-09-01 11:06:37 +0000 | [diff] [blame] | 607 | """Runs checkdeps on #include statements added in this |
| 608 | change. Breaking - rules is an error, breaking ! rules is a |
| 609 | warning. |
| 610 | """ |
| 611 | # Copied from Chromium's src/PRESUBMIT.py. |
| 612 | |
| 613 | # We need to wait until we have an input_api object and use this |
| 614 | # roundabout construct to import checkdeps because this file is |
| 615 | # eval-ed and thus doesn't have __file__. |
Mirko Bonadei | d866544 | 2018-09-04 12:17:27 +0200 | [diff] [blame] | 616 | src_path = FindSrcDirPath(input_api.PresubmitLocalPath()) |
| 617 | checkdeps_path = input_api.os_path.join(src_path, 'buildtools', 'checkdeps') |
Oleh Prypin | 2f33a56 | 2017-10-04 20:17:54 +0200 | [diff] [blame] | 618 | if not os.path.exists(checkdeps_path): |
| 619 | return [output_api.PresubmitError( |
| 620 | 'Cannot find checkdeps at %s\nHave you run "gclient sync" to ' |
| 621 | 'download all the DEPS entries?' % checkdeps_path)] |
| 622 | with _AddToPath(checkdeps_path): |
kjellander@webrtc.org | 3bd4156 | 2014-09-01 11:06:37 +0000 | [diff] [blame] | 623 | import checkdeps |
| 624 | from cpp_checker import CppChecker |
| 625 | from rules import Rule |
kjellander@webrtc.org | 3bd4156 | 2014-09-01 11:06:37 +0000 | [diff] [blame] | 626 | |
| 627 | added_includes = [] |
Artem Titov | a04d140 | 2018-05-11 11:23:00 +0200 | [diff] [blame] | 628 | for f in input_api.AffectedFiles(file_filter=source_file_filter): |
kjellander@webrtc.org | 3bd4156 | 2014-09-01 11:06:37 +0000 | [diff] [blame] | 629 | if not CppChecker.IsCppFile(f.LocalPath()): |
| 630 | continue |
| 631 | |
Henrik Kjellander | 57e5fd2 | 2015-05-25 12:55:39 +0200 | [diff] [blame] | 632 | changed_lines = [line for _, line in f.ChangedContents()] |
kjellander@webrtc.org | 3bd4156 | 2014-09-01 11:06:37 +0000 | [diff] [blame] | 633 | added_includes.append([f.LocalPath(), changed_lines]) |
| 634 | |
| 635 | deps_checker = checkdeps.DepsChecker(input_api.PresubmitLocalPath()) |
| 636 | |
| 637 | error_descriptions = [] |
| 638 | warning_descriptions = [] |
| 639 | for path, rule_type, rule_description in deps_checker.CheckAddedCppIncludes( |
| 640 | added_includes): |
| 641 | description_with_path = '%s\n %s' % (path, rule_description) |
| 642 | if rule_type == Rule.DISALLOW: |
| 643 | error_descriptions.append(description_with_path) |
| 644 | else: |
| 645 | warning_descriptions.append(description_with_path) |
| 646 | |
| 647 | results = [] |
| 648 | if error_descriptions: |
| 649 | results.append(output_api.PresubmitError( |
kjellander | a7066a3 | 2017-03-23 03:47:05 -0700 | [diff] [blame] | 650 | 'You added one or more #includes that violate checkdeps rules.\n' |
| 651 | 'Check that the DEPS files in these locations contain valid rules.\n' |
| 652 | 'See https://cs.chromium.org/chromium/src/buildtools/checkdeps/ for ' |
| 653 | 'more details about checkdeps.', |
kjellander@webrtc.org | 3bd4156 | 2014-09-01 11:06:37 +0000 | [diff] [blame] | 654 | error_descriptions)) |
| 655 | if warning_descriptions: |
| 656 | results.append(output_api.PresubmitPromptOrNotify( |
| 657 | 'You added one or more #includes of files that are temporarily\n' |
| 658 | 'allowed but being removed. Can you avoid introducing the\n' |
kjellander | a7066a3 | 2017-03-23 03:47:05 -0700 | [diff] [blame] | 659 | '#include? See relevant DEPS file(s) for details and contacts.\n' |
| 660 | 'See https://cs.chromium.org/chromium/src/buildtools/checkdeps/ for ' |
| 661 | 'more details about checkdeps.', |
kjellander@webrtc.org | 3bd4156 | 2014-09-01 11:06:37 +0000 | [diff] [blame] | 662 | warning_descriptions)) |
| 663 | return results |
| 664 | |
Artem Titov | e92675b | 2018-05-22 10:21:27 +0200 | [diff] [blame] | 665 | |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 666 | def CheckCommitMessageBugEntry(input_api, output_api): |
| 667 | """Check that bug entries are well-formed in commit message.""" |
| 668 | bogus_bug_msg = ( |
Mirko Bonadei | 6188018 | 2017-10-12 15:12:35 +0200 | [diff] [blame] | 669 | 'Bogus Bug entry: %s. Please specify the issue tracker prefix and the ' |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 670 | 'issue number, separated by a colon, e.g. webrtc:123 or chromium:12345.') |
| 671 | results = [] |
Mirko Bonadei | 6188018 | 2017-10-12 15:12:35 +0200 | [diff] [blame] | 672 | for bug in input_api.change.BugsFromDescription(): |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 673 | bug = bug.strip() |
| 674 | if bug.lower() == 'none': |
| 675 | continue |
charujain | 81a58c7 | 2017-09-25 13:25:45 +0200 | [diff] [blame] | 676 | if 'b/' not in bug and ':' not in bug: |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 677 | try: |
| 678 | if int(bug) > 100000: |
| 679 | # Rough indicator for current chromium bugs. |
| 680 | prefix_guess = 'chromium' |
| 681 | else: |
| 682 | prefix_guess = 'webrtc' |
Mirko Bonadei | 6188018 | 2017-10-12 15:12:35 +0200 | [diff] [blame] | 683 | results.append('Bug entry requires issue tracker prefix, e.g. %s:%s' % |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 684 | (prefix_guess, bug)) |
| 685 | except ValueError: |
| 686 | results.append(bogus_bug_msg % bug) |
charujain | 81a58c7 | 2017-09-25 13:25:45 +0200 | [diff] [blame] | 687 | 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] | 688 | results.append(bogus_bug_msg % bug) |
| 689 | return [output_api.PresubmitError(r) for r in results] |
| 690 | |
Artem Titov | e92675b | 2018-05-22 10:21:27 +0200 | [diff] [blame] | 691 | |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 692 | def CheckChangeHasBugField(input_api, output_api): |
Mirko Bonadei | 6188018 | 2017-10-12 15:12:35 +0200 | [diff] [blame] | 693 | """Requires that the changelist is associated with a bug. |
kjellander | d1e26a9 | 2016-09-19 08:11:16 -0700 | [diff] [blame] | 694 | |
| 695 | 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] | 696 | 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] | 697 | a bug reference. |
Mirko Bonadei | 6188018 | 2017-10-12 15:12:35 +0200 | [diff] [blame] | 698 | |
| 699 | This supports both 'BUG=' and 'Bug:' since we are in the process of migrating |
| 700 | to Gerrit and it encourages the usage of 'Bug:'. |
kjellander | d1e26a9 | 2016-09-19 08:11:16 -0700 | [diff] [blame] | 701 | """ |
Mirko Bonadei | 6188018 | 2017-10-12 15:12:35 +0200 | [diff] [blame] | 702 | if input_api.change.BugsFromDescription(): |
kjellander | d1e26a9 | 2016-09-19 08:11:16 -0700 | [diff] [blame] | 703 | return [] |
| 704 | else: |
| 705 | return [output_api.PresubmitError( |
Mirko Bonadei | 6188018 | 2017-10-12 15:12:35 +0200 | [diff] [blame] | 706 | 'The "Bug: [bug number]" footer is mandatory. Please create a bug and ' |
kjellander | d1e26a9 | 2016-09-19 08:11:16 -0700 | [diff] [blame] | 707 | 'reference it using either of:\n' |
Mirko Bonadei | 6188018 | 2017-10-12 15:12:35 +0200 | [diff] [blame] | 708 | ' * https://bugs.webrtc.org - reference it using Bug: webrtc:XXXX\n' |
| 709 | ' * https://crbug.com - reference it using Bug: chromium:XXXXXX')] |
kjellander@webrtc.org | e415864 | 2014-08-06 09:11:18 +0000 | [diff] [blame] | 710 | |
Artem Titov | e92675b | 2018-05-22 10:21:27 +0200 | [diff] [blame] | 711 | |
Artem Titov | a04d140 | 2018-05-11 11:23:00 +0200 | [diff] [blame] | 712 | def CheckJSONParseErrors(input_api, output_api, source_file_filter): |
kjellander | 569cf94 | 2016-02-11 05:02:59 -0800 | [diff] [blame] | 713 | """Check that JSON files do not contain syntax errors.""" |
| 714 | |
| 715 | def FilterFile(affected_file): |
Artem Titov | a04d140 | 2018-05-11 11:23:00 +0200 | [diff] [blame] | 716 | return (input_api.os_path.splitext(affected_file.LocalPath())[1] == '.json' |
| 717 | and source_file_filter(affected_file)) |
kjellander | 569cf94 | 2016-02-11 05:02:59 -0800 | [diff] [blame] | 718 | |
| 719 | def GetJSONParseError(input_api, filename): |
| 720 | try: |
| 721 | contents = input_api.ReadFile(filename) |
| 722 | input_api.json.loads(contents) |
| 723 | except ValueError as e: |
| 724 | return e |
| 725 | return None |
| 726 | |
| 727 | results = [] |
| 728 | for affected_file in input_api.AffectedFiles( |
| 729 | file_filter=FilterFile, include_deletes=False): |
| 730 | parse_error = GetJSONParseError(input_api, |
| 731 | affected_file.AbsoluteLocalPath()) |
| 732 | if parse_error: |
| 733 | results.append(output_api.PresubmitError('%s could not be parsed: %s' % |
Artem Titov | e92675b | 2018-05-22 10:21:27 +0200 | [diff] [blame] | 734 | (affected_file.LocalPath(), |
| 735 | parse_error))) |
kjellander | 569cf94 | 2016-02-11 05:02:59 -0800 | [diff] [blame] | 736 | return results |
| 737 | |
| 738 | |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 739 | def RunPythonTests(input_api, output_api): |
kjellander | c88b5d5 | 2017-04-05 06:42:43 -0700 | [diff] [blame] | 740 | def Join(*args): |
Henrik Kjellander | 8d3ad82 | 2015-05-26 19:52:05 +0200 | [diff] [blame] | 741 | return input_api.os_path.join(input_api.PresubmitLocalPath(), *args) |
| 742 | |
| 743 | test_directories = [ |
Edward Lemur | 6d01f6d | 2017-09-14 17:02:01 +0200 | [diff] [blame] | 744 | input_api.PresubmitLocalPath(), |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 745 | Join('rtc_tools', 'py_event_log_analyzer'), |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 746 | Join('audio', 'test', 'unittests'), |
ehmaldonado | 4fb9746 | 2017-01-30 05:27:22 -0800 | [diff] [blame] | 747 | ] + [ |
Henrik Kjellander | 90fd7d8 | 2017-05-09 08:30:10 +0200 | [diff] [blame] | 748 | root for root, _, files in os.walk(Join('tools_webrtc')) |
ehmaldonado | 4fb9746 | 2017-01-30 05:27:22 -0800 | [diff] [blame] | 749 | if any(f.endswith('_test.py') for f in files) |
Henrik Kjellander | 8d3ad82 | 2015-05-26 19:52:05 +0200 | [diff] [blame] | 750 | ] |
| 751 | |
| 752 | tests = [] |
| 753 | for directory in test_directories: |
| 754 | tests.extend( |
| 755 | input_api.canned_checks.GetUnitTestsInDirectory( |
| 756 | input_api, |
| 757 | output_api, |
| 758 | directory, |
| 759 | whitelist=[r'.+_test\.py$'])) |
| 760 | return input_api.RunTests(tests, parallel=True) |
| 761 | |
| 762 | |
Artem Titov | a04d140 | 2018-05-11 11:23:00 +0200 | [diff] [blame] | 763 | def CheckUsageOfGoogleProtobufNamespace(input_api, output_api, |
| 764 | source_file_filter): |
mbonadei | 38415b2 | 2017-04-07 05:38:01 -0700 | [diff] [blame] | 765 | """Checks that the namespace google::protobuf has not been used.""" |
| 766 | files = [] |
| 767 | pattern = input_api.re.compile(r'google::protobuf') |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 768 | proto_utils_path = os.path.join('rtc_base', 'protobuf_utils.h') |
Artem Titov | a04d140 | 2018-05-11 11:23:00 +0200 | [diff] [blame] | 769 | file_filter = lambda x: (input_api.FilterSourceFile(x) |
| 770 | and source_file_filter(x)) |
| 771 | for f in input_api.AffectedSourceFiles(file_filter): |
mbonadei | 38415b2 | 2017-04-07 05:38:01 -0700 | [diff] [blame] | 772 | if f.LocalPath() in [proto_utils_path, 'PRESUBMIT.py']: |
| 773 | continue |
| 774 | contents = input_api.ReadFile(f) |
| 775 | if pattern.search(contents): |
| 776 | files.append(f) |
| 777 | |
| 778 | if files: |
| 779 | return [output_api.PresubmitError( |
| 780 | 'Please avoid to use namespace `google::protobuf` directly.\n' |
| 781 | 'Add a using directive in `%s` and include that header instead.' |
| 782 | % proto_utils_path, files)] |
| 783 | return [] |
| 784 | |
| 785 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 786 | def _LicenseHeader(input_api): |
| 787 | """Returns the license header regexp.""" |
| 788 | # Accept any year number from 2003 to the current year |
| 789 | current_year = int(input_api.time.strftime('%Y')) |
| 790 | allowed_years = (str(s) for s in reversed(xrange(2003, current_year + 1))) |
| 791 | years_re = '(' + '|'.join(allowed_years) + ')' |
| 792 | license_header = ( |
| 793 | r'.*? Copyright( \(c\))? %(year)s The WebRTC [Pp]roject [Aa]uthors\. ' |
| 794 | r'All [Rr]ights [Rr]eserved\.\n' |
| 795 | r'.*?\n' |
| 796 | r'.*? Use of this source code is governed by a BSD-style license\n' |
| 797 | r'.*? that can be found in the LICENSE file in the root of the source\n' |
| 798 | r'.*? tree\. An additional intellectual property rights grant can be ' |
| 799 | r'found\n' |
| 800 | r'.*? in the file PATENTS\. All contributing project authors may\n' |
| 801 | r'.*? be found in the AUTHORS file in the root of the source tree\.\n' |
| 802 | ) % { |
| 803 | 'year': years_re, |
| 804 | } |
| 805 | return license_header |
| 806 | |
| 807 | |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 808 | def CommonChecks(input_api, output_api): |
andrew@webrtc.org | 53df136 | 2012-01-26 21:24:23 +0000 | [diff] [blame] | 809 | """Checks common to both upload and commit.""" |
niklase@google.com | da159d6 | 2011-05-30 11:51:34 +0000 | [diff] [blame] | 810 | results = [] |
tkchin | 42f580e | 2015-11-26 23:18:23 -0800 | [diff] [blame] | 811 | # Filter out files that are in objc or ios dirs from being cpplint-ed since |
| 812 | # they do not follow C++ lint rules. |
| 813 | black_list = input_api.DEFAULT_BLACK_LIST + ( |
| 814 | r".*\bobjc[\\\/].*", |
Kári Tristan Helgason | 3fa3517 | 2016-09-09 08:55:05 +0000 | [diff] [blame] | 815 | r".*objc\.[hcm]+$", |
tkchin | 42f580e | 2015-11-26 23:18:23 -0800 | [diff] [blame] | 816 | ) |
| 817 | source_file_filter = lambda x: input_api.FilterSourceFile(x, None, black_list) |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 818 | results.extend(CheckApprovedFilesLintClean( |
tkchin | 42f580e | 2015-11-26 23:18:23 -0800 | [diff] [blame] | 819 | input_api, output_api, source_file_filter)) |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 820 | results.extend(input_api.canned_checks.CheckLicense( |
| 821 | input_api, output_api, _LicenseHeader(input_api))) |
phoglund@webrtc.org | 5d371393 | 2013-03-07 09:59:43 +0000 | [diff] [blame] | 822 | results.extend(input_api.canned_checks.RunPylint(input_api, output_api, |
kjellander@webrtc.org | 177567c | 2016-12-22 10:40:28 +0100 | [diff] [blame] | 823 | black_list=(r'^base[\\\/].*\.py$', |
Henrik Kjellander | 14771ac | 2015-06-02 13:10:04 +0200 | [diff] [blame] | 824 | r'^build[\\\/].*\.py$', |
| 825 | r'^buildtools[\\\/].*\.py$', |
kjellander | 38c65c8 | 2017-04-12 22:43:38 -0700 | [diff] [blame] | 826 | r'^infra[\\\/].*\.py$', |
Henrik Kjellander | 0779e8f | 2016-12-22 12:01:17 +0100 | [diff] [blame] | 827 | r'^ios[\\\/].*\.py$', |
Henrik Kjellander | 14771ac | 2015-06-02 13:10:04 +0200 | [diff] [blame] | 828 | r'^out.*[\\\/].*\.py$', |
| 829 | r'^testing[\\\/].*\.py$', |
| 830 | r'^third_party[\\\/].*\.py$', |
kjellander@webrtc.org | 177567c | 2016-12-22 10:40:28 +0100 | [diff] [blame] | 831 | r'^tools[\\\/].*\.py$', |
kjellander | afd5494 | 2016-12-17 12:21:39 -0800 | [diff] [blame] | 832 | # TODO(phoglund): should arguably be checked. |
Henrik Kjellander | 90fd7d8 | 2017-05-09 08:30:10 +0200 | [diff] [blame] | 833 | r'^tools_webrtc[\\\/]mb[\\\/].*\.py$', |
Henrik Kjellander | 14771ac | 2015-06-02 13:10:04 +0200 | [diff] [blame] | 834 | r'^xcodebuild.*[\\\/].*\.py$',), |
Henrik Kjellander | 57e5fd2 | 2015-05-25 12:55:39 +0200 | [diff] [blame] | 835 | pylintrc='pylintrc')) |
kjellander | 569cf94 | 2016-02-11 05:02:59 -0800 | [diff] [blame] | 836 | |
nisse | 3d21e23 | 2016-09-02 03:07:06 -0700 | [diff] [blame] | 837 | # TODO(nisse): talk/ is no more, so make below checks simpler? |
Henrik Kjellander | 57e5fd2 | 2015-05-25 12:55:39 +0200 | [diff] [blame] | 838 | # WebRTC can't use the presubmit_canned_checks.PanProjectChecks function since |
| 839 | # we need to have different license checks in talk/ and webrtc/ directories. |
| 840 | # Instead, hand-picked checks are included below. |
Henrik Kjellander | 6322467 | 2015-09-08 08:03:56 +0200 | [diff] [blame] | 841 | |
tkchin | 3cd9a30 | 2016-06-08 12:40:28 -0700 | [diff] [blame] | 842 | # .m and .mm files are ObjC files. For simplicity we will consider .h files in |
| 843 | # ObjC subdirectories ObjC headers. |
| 844 | objc_filter_list = (r'.+\.m$', r'.+\.mm$', r'.+objc\/.+\.h$') |
Henrik Kjellander | b4af3d6 | 2016-11-16 20:11:29 +0100 | [diff] [blame] | 845 | # Skip long-lines check for DEPS and GN files. |
| 846 | build_file_filter_list = (r'.+\.gn$', r'.+\.gni$', 'DEPS') |
Artem Titov | a04d140 | 2018-05-11 11:23:00 +0200 | [diff] [blame] | 847 | # Also we will skip most checks for third_party directory. |
Artem Titov | 42f0d78 | 2018-06-27 13:23:17 +0200 | [diff] [blame] | 848 | third_party_filter_list = (r'^third_party[\\\/].+',) |
tkchin | 3cd9a30 | 2016-06-08 12:40:28 -0700 | [diff] [blame] | 849 | eighty_char_sources = lambda x: input_api.FilterSourceFile(x, |
Artem Titov | a04d140 | 2018-05-11 11:23:00 +0200 | [diff] [blame] | 850 | black_list=build_file_filter_list + objc_filter_list + |
| 851 | third_party_filter_list) |
tkchin | 3cd9a30 | 2016-06-08 12:40:28 -0700 | [diff] [blame] | 852 | hundred_char_sources = lambda x: input_api.FilterSourceFile(x, |
| 853 | white_list=objc_filter_list) |
Artem Titov | e92675b | 2018-05-22 10:21:27 +0200 | [diff] [blame] | 854 | non_third_party_sources = lambda x: input_api.FilterSourceFile(x, |
| 855 | black_list=third_party_filter_list) |
| 856 | |
andrew@webrtc.org | 2442de1 | 2012-01-23 17:45:41 +0000 | [diff] [blame] | 857 | results.extend(input_api.canned_checks.CheckLongLines( |
tkchin | 3cd9a30 | 2016-06-08 12:40:28 -0700 | [diff] [blame] | 858 | input_api, output_api, maxlen=80, source_file_filter=eighty_char_sources)) |
| 859 | results.extend(input_api.canned_checks.CheckLongLines( |
| 860 | input_api, output_api, maxlen=100, |
| 861 | source_file_filter=hundred_char_sources)) |
andrew@webrtc.org | 2442de1 | 2012-01-23 17:45:41 +0000 | [diff] [blame] | 862 | results.extend(input_api.canned_checks.CheckChangeHasNoTabs( |
Artem Titov | a04d140 | 2018-05-11 11:23:00 +0200 | [diff] [blame] | 863 | input_api, output_api, source_file_filter=non_third_party_sources)) |
andrew@webrtc.org | 53df136 | 2012-01-26 21:24:23 +0000 | [diff] [blame] | 864 | results.extend(input_api.canned_checks.CheckChangeHasNoStrayWhitespace( |
Artem Titov | a04d140 | 2018-05-11 11:23:00 +0200 | [diff] [blame] | 865 | input_api, output_api, source_file_filter=non_third_party_sources)) |
kjellander | e5dc62a | 2016-12-14 00:16:21 -0800 | [diff] [blame] | 866 | results.extend(input_api.canned_checks.CheckAuthorizedAuthor( |
Oleh Prypin | e073514 | 2018-10-04 11:15:54 +0200 | [diff] [blame] | 867 | input_api, output_api, bot_whitelist=[ |
| 868 | 'chromium-webrtc-autoroll@webrtc-ci.iam.gserviceaccount.com' |
| 869 | ])) |
andrew@webrtc.org | 53df136 | 2012-01-26 21:24:23 +0000 | [diff] [blame] | 870 | results.extend(input_api.canned_checks.CheckChangeTodoHasOwner( |
Artem Titov | a04d140 | 2018-05-11 11:23:00 +0200 | [diff] [blame] | 871 | input_api, output_api, source_file_filter=non_third_party_sources)) |
Yves Gerey | 87a9353 | 2018-06-20 15:51:49 +0200 | [diff] [blame] | 872 | results.extend(input_api.canned_checks.CheckPatchFormatted( |
| 873 | input_api, output_api)) |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 874 | results.extend(CheckNativeApiHeaderChanges(input_api, output_api)) |
Artem Titov | a04d140 | 2018-05-11 11:23:00 +0200 | [diff] [blame] | 875 | results.extend(CheckNoIOStreamInHeaders( |
| 876 | input_api, output_api, source_file_filter=non_third_party_sources)) |
| 877 | results.extend(CheckNoPragmaOnce( |
| 878 | input_api, output_api, source_file_filter=non_third_party_sources)) |
| 879 | results.extend(CheckNoFRIEND_TEST( |
| 880 | input_api, output_api, source_file_filter=non_third_party_sources)) |
Mirko Bonadei | f0e0d75 | 2018-07-04 08:48:18 +0200 | [diff] [blame] | 881 | results.extend(CheckGnChanges(input_api, output_api)) |
Artem Titov | a04d140 | 2018-05-11 11:23:00 +0200 | [diff] [blame] | 882 | results.extend(CheckUnwantedDependencies( |
| 883 | input_api, output_api, source_file_filter=non_third_party_sources)) |
| 884 | results.extend(CheckJSONParseErrors( |
| 885 | input_api, output_api, source_file_filter=non_third_party_sources)) |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 886 | results.extend(RunPythonTests(input_api, output_api)) |
Artem Titov | a04d140 | 2018-05-11 11:23:00 +0200 | [diff] [blame] | 887 | results.extend(CheckUsageOfGoogleProtobufNamespace( |
| 888 | input_api, output_api, source_file_filter=non_third_party_sources)) |
| 889 | results.extend(CheckOrphanHeaders( |
| 890 | input_api, output_api, source_file_filter=non_third_party_sources)) |
| 891 | results.extend(CheckNewlineAtTheEndOfProtoFiles( |
| 892 | input_api, output_api, source_file_filter=non_third_party_sources)) |
| 893 | results.extend(CheckNoStreamUsageIsAdded( |
Artem Titov | 739351d | 2018-05-11 12:21:36 +0200 | [diff] [blame] | 894 | input_api, output_api, non_third_party_sources)) |
Mirko Bonadei | 9ce800d | 2019-02-05 16:48:13 +0100 | [diff] [blame] | 895 | results.extend(CheckNoTestCaseUsageIsAdded( |
| 896 | input_api, output_api, non_third_party_sources)) |
Mirko Bonadei | 7e4ee6e | 2018-09-28 11:45:23 +0200 | [diff] [blame] | 897 | results.extend(CheckAddedDepsHaveTargetApprovals(input_api, output_api)) |
Mirko Bonadei | a418e67 | 2018-10-24 13:57:25 +0200 | [diff] [blame] | 898 | results.extend(CheckApiDepsFileIsUpToDate(input_api, output_api)) |
tzik | a06bf85 | 2018-11-15 20:37:35 +0900 | [diff] [blame] | 899 | results.extend(CheckAbslMemoryInclude( |
| 900 | input_api, output_api, non_third_party_sources)) |
Mirko Bonadei | 9fa8ef1 | 2019-09-17 19:14:13 +0200 | [diff] [blame] | 901 | results.extend(CheckBannedAbslMakeUnique( |
| 902 | input_api, output_api, non_third_party_sources)) |
Mirko Bonadei | a418e67 | 2018-10-24 13:57:25 +0200 | [diff] [blame] | 903 | return results |
| 904 | |
| 905 | |
| 906 | def CheckApiDepsFileIsUpToDate(input_api, output_api): |
Mirko Bonadei | 9049037 | 2018-10-26 13:17:47 +0200 | [diff] [blame] | 907 | """Check that 'include_rules' in api/DEPS is up to date. |
| 908 | |
| 909 | The file api/DEPS must be kept up to date in order to avoid to avoid to |
| 910 | include internal header from WebRTC's api/ headers. |
| 911 | |
| 912 | This check is focused on ensuring that 'include_rules' contains a deny |
| 913 | rule for each root level directory. More focused allow rules can be |
| 914 | added to 'specific_include_rules'. |
| 915 | """ |
Mirko Bonadei | a418e67 | 2018-10-24 13:57:25 +0200 | [diff] [blame] | 916 | results = [] |
| 917 | api_deps = os.path.join(input_api.PresubmitLocalPath(), 'api', 'DEPS') |
| 918 | with open(api_deps) as f: |
| 919 | deps_content = _ParseDeps(f.read()) |
| 920 | |
| 921 | include_rules = deps_content.get('include_rules', []) |
Mirko Bonadei | 01e97ae | 2019-09-05 14:36:42 +0200 | [diff] [blame] | 922 | dirs_to_skip = set(['api', 'docs']) |
Mirko Bonadei | a418e67 | 2018-10-24 13:57:25 +0200 | [diff] [blame] | 923 | |
Mirko Bonadei | 9049037 | 2018-10-26 13:17:47 +0200 | [diff] [blame] | 924 | # Only check top level directories affected by the current CL. |
| 925 | dirs_to_check = set() |
| 926 | for f in input_api.AffectedFiles(): |
| 927 | path_tokens = [t for t in f.LocalPath().split(os.sep) if t] |
| 928 | if len(path_tokens) > 1: |
Mirko Bonadei | 01e97ae | 2019-09-05 14:36:42 +0200 | [diff] [blame] | 929 | if (path_tokens[0] not in dirs_to_skip and |
Mirko Bonadei | 9049037 | 2018-10-26 13:17:47 +0200 | [diff] [blame] | 930 | os.path.isdir(os.path.join(input_api.PresubmitLocalPath(), |
| 931 | path_tokens[0]))): |
| 932 | dirs_to_check.add(path_tokens[0]) |
Mirko Bonadei | a418e67 | 2018-10-24 13:57:25 +0200 | [diff] [blame] | 933 | |
Mirko Bonadei | 9049037 | 2018-10-26 13:17:47 +0200 | [diff] [blame] | 934 | missing_include_rules = set() |
| 935 | for p in dirs_to_check: |
Mirko Bonadei | a418e67 | 2018-10-24 13:57:25 +0200 | [diff] [blame] | 936 | rule = '-%s' % p |
| 937 | if rule not in include_rules: |
Mirko Bonadei | 9049037 | 2018-10-26 13:17:47 +0200 | [diff] [blame] | 938 | missing_include_rules.add(rule) |
| 939 | |
Mirko Bonadei | a418e67 | 2018-10-24 13:57:25 +0200 | [diff] [blame] | 940 | if missing_include_rules: |
Mirko Bonadei | 9049037 | 2018-10-26 13:17:47 +0200 | [diff] [blame] | 941 | error_msg = [ |
| 942 | 'include_rules = [\n', |
| 943 | ' ...\n', |
| 944 | ] |
Mirko Bonadei | a418e67 | 2018-10-24 13:57:25 +0200 | [diff] [blame] | 945 | |
Mirko Bonadei | 9049037 | 2018-10-26 13:17:47 +0200 | [diff] [blame] | 946 | for r in sorted(missing_include_rules): |
| 947 | error_msg.append(' "%s",\n' % str(r)) |
Mirko Bonadei | a418e67 | 2018-10-24 13:57:25 +0200 | [diff] [blame] | 948 | |
Mirko Bonadei | 9049037 | 2018-10-26 13:17:47 +0200 | [diff] [blame] | 949 | error_msg.append(' ...\n') |
| 950 | error_msg.append(']\n') |
| 951 | |
Mirko Bonadei | a418e67 | 2018-10-24 13:57:25 +0200 | [diff] [blame] | 952 | results.append(output_api.PresubmitError( |
Mirko Bonadei | 9049037 | 2018-10-26 13:17:47 +0200 | [diff] [blame] | 953 | 'New root level directory detected! WebRTC api/ headers should ' |
| 954 | 'not #include headers from \n' |
| 955 | 'the new directory, so please update "include_rules" in file\n' |
| 956 | '"%s". Example:\n%s\n' % (api_deps, ''.join(error_msg)))) |
| 957 | |
andrew@webrtc.org | 53df136 | 2012-01-26 21:24:23 +0000 | [diff] [blame] | 958 | return results |
andrew@webrtc.org | 2442de1 | 2012-01-23 17:45:41 +0000 | [diff] [blame] | 959 | |
Mirko Bonadei | 9fa8ef1 | 2019-09-17 19:14:13 +0200 | [diff] [blame] | 960 | def CheckBannedAbslMakeUnique(input_api, output_api, source_file_filter): |
| 961 | file_filter = lambda f: (f.LocalPath().endswith(('.cc', '.h')) |
| 962 | and source_file_filter(f)) |
| 963 | |
| 964 | files = [] |
| 965 | for f in input_api.AffectedFiles( |
| 966 | include_deletes=False, file_filter=file_filter): |
| 967 | for _, line in f.ChangedContents(): |
| 968 | if 'absl::make_unique' in line: |
| 969 | files.append(f) |
| 970 | break |
| 971 | |
| 972 | if len(files): |
| 973 | return [output_api.PresubmitError( |
| 974 | 'Please use std::make_unique instead of absl::make_unique.\n' |
| 975 | 'Affected files:', |
| 976 | files)] |
| 977 | return [] |
| 978 | |
tzik | a06bf85 | 2018-11-15 20:37:35 +0900 | [diff] [blame] | 979 | def CheckAbslMemoryInclude(input_api, output_api, source_file_filter): |
| 980 | pattern = input_api.re.compile( |
| 981 | r'^#include\s*"absl/memory/memory.h"', input_api.re.MULTILINE) |
| 982 | file_filter = lambda f: (f.LocalPath().endswith(('.cc', '.h')) |
| 983 | and source_file_filter(f)) |
| 984 | |
| 985 | files = [] |
| 986 | for f in input_api.AffectedFiles( |
| 987 | include_deletes=False, file_filter=file_filter): |
| 988 | contents = input_api.ReadFile(f) |
| 989 | if pattern.search(contents): |
| 990 | continue |
| 991 | for _, line in f.ChangedContents(): |
Mirko Bonadei | 9fa8ef1 | 2019-09-17 19:14:13 +0200 | [diff] [blame] | 992 | if 'absl::WrapUnique' in line: |
tzik | a06bf85 | 2018-11-15 20:37:35 +0900 | [diff] [blame] | 993 | files.append(f) |
| 994 | break |
| 995 | |
| 996 | if len(files): |
| 997 | return [output_api.PresubmitError( |
Mirko Bonadei | 9fa8ef1 | 2019-09-17 19:14:13 +0200 | [diff] [blame] | 998 | 'Please include "absl/memory/memory.h" header for absl::WrapUnique.\n' |
| 999 | 'This header may or may not be included transitively depending on the ' |
| 1000 | 'C++ standard version.', |
tzik | a06bf85 | 2018-11-15 20:37:35 +0900 | [diff] [blame] | 1001 | files)] |
| 1002 | return [] |
kjellander@webrtc.org | e415864 | 2014-08-06 09:11:18 +0000 | [diff] [blame] | 1003 | |
andrew@webrtc.org | 53df136 | 2012-01-26 21:24:23 +0000 | [diff] [blame] | 1004 | def CheckChangeOnUpload(input_api, output_api): |
| 1005 | results = [] |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 1006 | results.extend(CommonChecks(input_api, output_api)) |
Oleh Prypin | 920b653 | 2017-10-05 11:28:51 +0200 | [diff] [blame] | 1007 | results.extend(CheckGnGen(input_api, output_api)) |
Henrik Kjellander | 57e5fd2 | 2015-05-25 12:55:39 +0200 | [diff] [blame] | 1008 | results.extend( |
| 1009 | input_api.canned_checks.CheckGNFormatted(input_api, output_api)) |
niklase@google.com | da159d6 | 2011-05-30 11:51:34 +0000 | [diff] [blame] | 1010 | return results |
| 1011 | |
kjellander@webrtc.org | e415864 | 2014-08-06 09:11:18 +0000 | [diff] [blame] | 1012 | |
andrew@webrtc.org | 2442de1 | 2012-01-23 17:45:41 +0000 | [diff] [blame] | 1013 | def CheckChangeOnCommit(input_api, output_api): |
niklase@google.com | 1198db9 | 2011-06-09 07:07:24 +0000 | [diff] [blame] | 1014 | results = [] |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 1015 | results.extend(CommonChecks(input_api, output_api)) |
| 1016 | results.extend(VerifyNativeApiHeadersListIsValid(input_api, output_api)) |
Artem Titov | 42f0d78 | 2018-06-27 13:23:17 +0200 | [diff] [blame] | 1017 | results.extend(input_api.canned_checks.CheckOwners(input_api, output_api)) |
andrew@webrtc.org | 53df136 | 2012-01-26 21:24:23 +0000 | [diff] [blame] | 1018 | results.extend(input_api.canned_checks.CheckChangeWasUploaded( |
| 1019 | input_api, output_api)) |
| 1020 | results.extend(input_api.canned_checks.CheckChangeHasDescription( |
| 1021 | input_api, output_api)) |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 1022 | results.extend(CheckChangeHasBugField(input_api, output_api)) |
| 1023 | results.extend(CheckCommitMessageBugEntry(input_api, output_api)) |
kjellander@webrtc.org | 12cb88c | 2014-02-13 11:53:43 +0000 | [diff] [blame] | 1024 | results.extend(input_api.canned_checks.CheckTreeIsOpen( |
| 1025 | input_api, output_api, |
| 1026 | json_url='http://webrtc-status.appspot.com/current?format=json')) |
niklase@google.com | 1198db9 | 2011-06-09 07:07:24 +0000 | [diff] [blame] | 1027 | return results |
mbonadei | 74973ed | 2017-05-09 07:58:05 -0700 | [diff] [blame] | 1028 | |
| 1029 | |
Artem Titov | a04d140 | 2018-05-11 11:23:00 +0200 | [diff] [blame] | 1030 | def CheckOrphanHeaders(input_api, output_api, source_file_filter): |
mbonadei | 74973ed | 2017-05-09 07:58:05 -0700 | [diff] [blame] | 1031 | # We need to wait until we have an input_api object and use this |
| 1032 | # roundabout construct to import prebubmit_checks_lib because this file is |
| 1033 | # eval-ed and thus doesn't have __file__. |
Patrik Höglund | 2f3f722 | 2017-12-19 11:08:56 +0100 | [diff] [blame] | 1034 | error_msg = """{} should be listed in {}.""" |
mbonadei | 74973ed | 2017-05-09 07:58:05 -0700 | [diff] [blame] | 1035 | results = [] |
Patrik Höglund | 7e60de2 | 2018-01-09 14:22:00 +0100 | [diff] [blame] | 1036 | orphan_blacklist = [ |
| 1037 | os.path.join('tools_webrtc', 'ios', 'SDK'), |
| 1038 | ] |
Oleh Prypin | 2f33a56 | 2017-10-04 20:17:54 +0200 | [diff] [blame] | 1039 | with _AddToPath(input_api.os_path.join( |
| 1040 | input_api.PresubmitLocalPath(), 'tools_webrtc', 'presubmit_checks_lib')): |
mbonadei | 74973ed | 2017-05-09 07:58:05 -0700 | [diff] [blame] | 1041 | from check_orphan_headers import GetBuildGnPathFromFilePath |
| 1042 | from check_orphan_headers import IsHeaderInBuildGn |
mbonadei | 74973ed | 2017-05-09 07:58:05 -0700 | [diff] [blame] | 1043 | |
Artem Titov | a04d140 | 2018-05-11 11:23:00 +0200 | [diff] [blame] | 1044 | file_filter = lambda x: input_api.FilterSourceFile( |
| 1045 | x, black_list=orphan_blacklist) and source_file_filter(x) |
| 1046 | for f in input_api.AffectedSourceFiles(file_filter): |
Patrik Höglund | 7e60de2 | 2018-01-09 14:22:00 +0100 | [diff] [blame] | 1047 | if f.LocalPath().endswith('.h'): |
mbonadei | 74973ed | 2017-05-09 07:58:05 -0700 | [diff] [blame] | 1048 | file_path = os.path.abspath(f.LocalPath()) |
| 1049 | root_dir = os.getcwd() |
| 1050 | gn_file_path = GetBuildGnPathFromFilePath(file_path, os.path.exists, |
| 1051 | root_dir) |
| 1052 | in_build_gn = IsHeaderInBuildGn(file_path, gn_file_path) |
| 1053 | if not in_build_gn: |
| 1054 | results.append(output_api.PresubmitError(error_msg.format( |
Patrik Höglund | 2f3f722 | 2017-12-19 11:08:56 +0100 | [diff] [blame] | 1055 | f.LocalPath(), os.path.relpath(gn_file_path)))) |
mbonadei | 74973ed | 2017-05-09 07:58:05 -0700 | [diff] [blame] | 1056 | return results |
Mirko Bonadei | 960fd5b | 2017-06-29 14:59:36 +0200 | [diff] [blame] | 1057 | |
| 1058 | |
Artem Titov | e92675b | 2018-05-22 10:21:27 +0200 | [diff] [blame] | 1059 | def CheckNewlineAtTheEndOfProtoFiles(input_api, output_api, source_file_filter): |
Mirko Bonadei | 960fd5b | 2017-06-29 14:59:36 +0200 | [diff] [blame] | 1060 | """Checks that all .proto files are terminated with a newline.""" |
| 1061 | error_msg = 'File {} must end with exactly one newline.' |
| 1062 | results = [] |
Artem Titov | a04d140 | 2018-05-11 11:23:00 +0200 | [diff] [blame] | 1063 | file_filter = lambda x: input_api.FilterSourceFile( |
| 1064 | x, white_list=(r'.+\.proto$',)) and source_file_filter(x) |
| 1065 | for f in input_api.AffectedSourceFiles(file_filter): |
Mirko Bonadei | 960fd5b | 2017-06-29 14:59:36 +0200 | [diff] [blame] | 1066 | file_path = f.LocalPath() |
| 1067 | with open(file_path) as f: |
| 1068 | lines = f.readlines() |
Mirko Bonadei | a730c1c | 2017-09-18 11:33:13 +0200 | [diff] [blame] | 1069 | if len(lines) > 0 and not lines[-1].endswith('\n'): |
Mirko Bonadei | 960fd5b | 2017-06-29 14:59:36 +0200 | [diff] [blame] | 1070 | results.append(output_api.PresubmitError(error_msg.format(file_path))) |
| 1071 | return results |
Mirko Bonadei | 7e4ee6e | 2018-09-28 11:45:23 +0200 | [diff] [blame] | 1072 | |
| 1073 | |
| 1074 | def _ExtractAddRulesFromParsedDeps(parsed_deps): |
| 1075 | """Extract the rules that add dependencies from a parsed DEPS file. |
| 1076 | |
| 1077 | Args: |
| 1078 | parsed_deps: the locals dictionary from evaluating the DEPS file.""" |
| 1079 | add_rules = set() |
| 1080 | add_rules.update([ |
| 1081 | rule[1:] for rule in parsed_deps.get('include_rules', []) |
| 1082 | if rule.startswith('+') or rule.startswith('!') |
| 1083 | ]) |
| 1084 | for _, rules in parsed_deps.get('specific_include_rules', |
| 1085 | {}).iteritems(): |
| 1086 | add_rules.update([ |
| 1087 | rule[1:] for rule in rules |
| 1088 | if rule.startswith('+') or rule.startswith('!') |
| 1089 | ]) |
| 1090 | return add_rules |
| 1091 | |
| 1092 | |
| 1093 | def _ParseDeps(contents): |
| 1094 | """Simple helper for parsing DEPS files.""" |
| 1095 | # Stubs for handling special syntax in the root DEPS file. |
| 1096 | class VarImpl(object): |
| 1097 | |
| 1098 | def __init__(self, local_scope): |
| 1099 | self._local_scope = local_scope |
| 1100 | |
| 1101 | def Lookup(self, var_name): |
| 1102 | """Implements the Var syntax.""" |
| 1103 | try: |
| 1104 | return self._local_scope['vars'][var_name] |
| 1105 | except KeyError: |
| 1106 | raise Exception('Var is not defined: %s' % var_name) |
| 1107 | |
| 1108 | local_scope = {} |
| 1109 | global_scope = { |
| 1110 | 'Var': VarImpl(local_scope).Lookup, |
| 1111 | } |
| 1112 | exec contents in global_scope, local_scope |
| 1113 | return local_scope |
| 1114 | |
| 1115 | |
| 1116 | def _CalculateAddedDeps(os_path, old_contents, new_contents): |
| 1117 | """Helper method for _CheckAddedDepsHaveTargetApprovals. Returns |
| 1118 | a set of DEPS entries that we should look up. |
| 1119 | |
| 1120 | For a directory (rather than a specific filename) we fake a path to |
| 1121 | a specific filename by adding /DEPS. This is chosen as a file that |
| 1122 | will seldom or never be subject to per-file include_rules. |
| 1123 | """ |
| 1124 | # We ignore deps entries on auto-generated directories. |
| 1125 | auto_generated_dirs = ['grit', 'jni'] |
| 1126 | |
| 1127 | old_deps = _ExtractAddRulesFromParsedDeps(_ParseDeps(old_contents)) |
| 1128 | new_deps = _ExtractAddRulesFromParsedDeps(_ParseDeps(new_contents)) |
| 1129 | |
| 1130 | added_deps = new_deps.difference(old_deps) |
| 1131 | |
| 1132 | results = set() |
| 1133 | for added_dep in added_deps: |
| 1134 | if added_dep.split('/')[0] in auto_generated_dirs: |
| 1135 | continue |
| 1136 | # Assume that a rule that ends in .h is a rule for a specific file. |
| 1137 | if added_dep.endswith('.h'): |
| 1138 | results.add(added_dep) |
| 1139 | else: |
| 1140 | results.add(os_path.join(added_dep, 'DEPS')) |
| 1141 | return results |
| 1142 | |
| 1143 | |
| 1144 | def CheckAddedDepsHaveTargetApprovals(input_api, output_api): |
| 1145 | """When a dependency prefixed with + is added to a DEPS file, we |
| 1146 | want to make sure that the change is reviewed by an OWNER of the |
| 1147 | target file or directory, to avoid layering violations from being |
| 1148 | introduced. This check verifies that this happens. |
| 1149 | """ |
| 1150 | virtual_depended_on_files = set() |
| 1151 | |
| 1152 | file_filter = lambda f: not input_api.re.match( |
| 1153 | r"^third_party[\\\/](WebKit|blink)[\\\/].*", f.LocalPath()) |
| 1154 | for f in input_api.AffectedFiles(include_deletes=False, |
| 1155 | file_filter=file_filter): |
| 1156 | filename = input_api.os_path.basename(f.LocalPath()) |
| 1157 | if filename == 'DEPS': |
| 1158 | virtual_depended_on_files.update(_CalculateAddedDeps( |
| 1159 | input_api.os_path, |
| 1160 | '\n'.join(f.OldContents()), |
| 1161 | '\n'.join(f.NewContents()))) |
| 1162 | |
| 1163 | if not virtual_depended_on_files: |
| 1164 | return [] |
| 1165 | |
| 1166 | if input_api.is_committing: |
| 1167 | if input_api.tbr: |
| 1168 | return [output_api.PresubmitNotifyResult( |
| 1169 | '--tbr was specified, skipping OWNERS check for DEPS additions')] |
| 1170 | if input_api.dry_run: |
| 1171 | return [output_api.PresubmitNotifyResult( |
| 1172 | 'This is a dry run, skipping OWNERS check for DEPS additions')] |
| 1173 | if not input_api.change.issue: |
| 1174 | return [output_api.PresubmitError( |
| 1175 | "DEPS approval by OWNERS check failed: this change has " |
| 1176 | "no change number, so we can't check it for approvals.")] |
| 1177 | output = output_api.PresubmitError |
| 1178 | else: |
| 1179 | output = output_api.PresubmitNotifyResult |
| 1180 | |
| 1181 | owners_db = input_api.owners_db |
| 1182 | owner_email, reviewers = ( |
| 1183 | input_api.canned_checks.GetCodereviewOwnerAndReviewers( |
| 1184 | input_api, |
| 1185 | owners_db.email_regexp, |
| 1186 | approval_needed=input_api.is_committing)) |
| 1187 | |
| 1188 | owner_email = owner_email or input_api.change.author_email |
| 1189 | |
| 1190 | reviewers_plus_owner = set(reviewers) |
| 1191 | if owner_email: |
| 1192 | reviewers_plus_owner.add(owner_email) |
| 1193 | missing_files = owners_db.files_not_covered_by(virtual_depended_on_files, |
| 1194 | reviewers_plus_owner) |
| 1195 | |
| 1196 | # We strip the /DEPS part that was added by |
| 1197 | # _FilesToCheckForIncomingDeps to fake a path to a file in a |
| 1198 | # directory. |
| 1199 | def StripDeps(path): |
| 1200 | start_deps = path.rfind('/DEPS') |
| 1201 | if start_deps != -1: |
| 1202 | return path[:start_deps] |
| 1203 | else: |
| 1204 | return path |
| 1205 | unapproved_dependencies = ["'+%s'," % StripDeps(path) |
| 1206 | for path in missing_files] |
| 1207 | |
| 1208 | if unapproved_dependencies: |
| 1209 | output_list = [ |
| 1210 | output('You need LGTM from owners of depends-on paths in DEPS that were ' |
| 1211 | 'modified in this CL:\n %s' % |
| 1212 | '\n '.join(sorted(unapproved_dependencies)))] |
| 1213 | suggested_owners = owners_db.reviewers_for(missing_files, owner_email) |
| 1214 | output_list.append(output( |
| 1215 | 'Suggested missing target path OWNERS:\n %s' % |
| 1216 | '\n '.join(suggested_owners or []))) |
| 1217 | return output_list |
| 1218 | |
| 1219 | return [] |