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