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