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