blob: b83f262e3d6a52361e5c10293dd305e7c63c8327 [file] [log] [blame]
Christoffer Jansson4e8a7732022-02-08 08:01:121#!/usr/bin/env vpython3
2
andrew@webrtc.org2442de12012-01-23 17:45:413# 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.comda159d62011-05-30 11:51:3410
kjellander7439f972016-12-06 06:47:4611import json
kjellander@webrtc.orgaefe61a2014-12-08 13:00:3012import os
kjellander@webrtc.org85759802013-10-22 16:47:4013import re
kjellander@webrtc.org3bd41562014-09-01 11:06:3714import sys
Mirko Bonadei4dc4e252017-09-19 11:49:1615from collections import defaultdict
Oleh Prypin2f33a562017-10-04 18:17:5416from contextlib import contextmanager
kjellander@webrtc.org85759802013-10-22 16:47:4017
Christoffer Jansson4e8a7732022-02-08 08:01:1218# Runs PRESUBMIT.py in py3 mode by git cl presubmit.
19USE_PYTHON3 = True
20
oprypin2aa463f2017-03-23 10:17:0221# Files and directories that are *skipped* by cpplint in the presubmit script.
Mirko Bonadeifc17a782020-06-30 12:31:3722CPPLINT_EXCEPTIONS = [
Mirko Bonadei8cc66952020-10-30 09:13:4523 '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 Castelli22379fc2021-04-08 13:06:0929 'media/sctp/usrsctp_transport.cc',
Mirko Bonadei8cc66952020-10-30 09:13:4530 '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 Jansson4e8a7732022-02-08 08:01:1239 'PRESUBMIT.py',
40 'presubmit_test_mocks.py',
41 'presubmit_test.py',
Mirko Bonadei8cc66952020-10-30 09:13:4542 'rtc_base',
43 'sdk/android/src/jni',
44 'sdk/objc',
45 'system_wrappers',
46 'test',
47 'tools_webrtc',
48 'voice_engine',
kjellander@webrtc.org0fcaf992015-11-26 14:24:5249]
50
jbauchc4e3ead2016-02-19 08:25:5551# 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 Bonadeifc17a782020-06-30 12:31:3756# include file and feature blocklists are
jbauchc4e3ead2016-02-19 08:25:5557# google3-specific.
Mirko Bonadeie92e2862020-05-29 13:23:0958# - runtime/references : Mutable references are not banned by the Google
59# C++ style guide anymore (starting from May 2020).
kjellandere5a87a52016-04-27 09:32:1260# - whitespace/operators: Same as above (doesn't seem sufficient to eliminate
61# all move-related errors).
Mirko Bonadeifc17a782020-06-30 12:31:3762DISABLED_LINT_FILTERS = [
Mirko Bonadei8cc66952020-10-30 09:13:4563 '-build/c++11',
64 '-runtime/references',
65 '-whitespace/operators',
jbauchc4e3ead2016-02-19 08:25:5566]
67
kjellanderfd595232015-12-04 10:44:0968# 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.
kjellander53047c92015-12-03 07:56:1475NATIVE_API_DIRS = (
Mirko Bonadei8cc66952020-10-30 09:13:4576 'api', # All subdirectories of api/ are included as well.
77 'media/base',
78 'media/engine',
79 'modules/audio_device/include',
80 'pc',
kjellanderdd705472016-06-09 18:17:2781)
Mirko Bonadei4dc4e252017-09-19 11:49:1682
kjellanderdd705472016-06-09 18:17:2783# These directories should not be used but are maintained only to avoid breaking
84# some legacy downstream code.
85LEGACY_API_DIRS = (
Mirko Bonadei8cc66952020-10-30 09:13:4586 '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',
kjellander53047c92015-12-03 07:56:14101)
Mirko Bonadei4dc4e252017-09-19 11:49:16102
Karl Wibergd4f01c12017-11-10 09:55:45103# NOTE: The set of directories in API_DIRS should be the same as those
104# listed in the table in native-api.md.
kjellanderdd705472016-06-09 18:17:27105API_DIRS = NATIVE_API_DIRS[:] + LEGACY_API_DIRS[:]
kjellander53047c92015-12-03 07:56:14106
Mirko Bonadei4dc4e252017-09-19 11:49:16107# TARGET_RE matches a GN target, and extracts the target name and the contents.
Mirko Bonadei2dcf3482020-06-05 12:30:41108TARGET_RE = re.compile(
Mirko Bonadei8cc66952020-10-30 09:13:45109 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 Bonadei4dc4e252017-09-19 11:49:16112
113# SOURCES_RE matches a block of sources inside a GN target.
114SOURCES_RE = re.compile(r'sources \+?= \[(?P<sources>.*?)\]',
115 re.MULTILINE | re.DOTALL)
116
Mirko Bonadei2dcf3482020-06-05 12:30:41117# DEPS_RE matches a block of sources inside a GN target.
118DEPS_RE = re.compile(r'\bdeps \+?= \[(?P<deps>.*?)\]',
119 re.MULTILINE | re.DOTALL)
120
Philipp Hancke0c2a9ca2021-08-11 10:00:27121# FILE_PATH_RE matches a file path.
Mirko Bonadei4dc4e252017-09-19 11:49:16122FILE_PATH_RE = re.compile(r'"(?P<file_path>(\w|\/)+)(?P<extension>\.\w+)"')
123
kjellander53047c92015-12-03 07:56:14124
Mirko Bonadeid8665442018-09-04 10:17:27125def FindSrcDirPath(starting_dir):
Christoffer Jansson4e8a7732022-02-08 08:01:12126 """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 Bonadeid8665442018-09-04 10:17:27131
132
Oleh Prypin2f33a562017-10-04 18:17:54133@contextmanager
134def _AddToPath(*paths):
Christoffer Jansson4e8a7732022-02-08 08:01:12135 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
ehmaldonado4fb97462017-01-30 13:27:22142
143
charujain9893e252017-09-14 11:33:22144def VerifyNativeApiHeadersListIsValid(input_api, output_api):
Christoffer Jansson4e8a7732022-02-08 08:01:12145 """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 Bonadei8cc66952020-10-30 09:13:45160 ]
Christoffer Jansson4e8a7732022-02-08 08:01:12161 return []
kjellander53047c92015-12-03 07:56:14162
Artem Titove92675b2018-05-22 08:21:27163
kjellanderc88b5d52017-04-05 13:42:43164API_CHANGE_MSG = """
kwibergeb133022016-04-07 14:41:48165You seem to be changing native API header files. Please make sure that you:
oprypin375b9ac2017-02-13 12:13:23166 1. Make compatible changes that don't break existing clients. Usually
167 this is done by keeping the existing method signatures unchanged.
Danil Chapovalov7013b3b2021-02-22 13:31:26168 2. Mark the old stuff as deprecated (use the ABSL_DEPRECATED macro).
kwibergeb133022016-04-07 14:41:48169 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.
180Related files:
181"""
kjellander53047c92015-12-03 07:56:14182
Artem Titove92675b2018-05-22 08:21:27183
charujain9893e252017-09-14 11:33:22184def CheckNativeApiHeaderChanges(input_api, output_api):
Christoffer Jansson4e8a7732022-02-08 08:01:12185 """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())
kjellander53047c92015-12-03 07:56:14200
Christoffer Jansson4e8a7732022-02-08 08:01:12201 if files:
202 return [output_api.PresubmitNotifyResult(API_CHANGE_MSG, files)]
203 return []
kjellander53047c92015-12-03 07:56:14204
kjellander@webrtc.org0fcaf992015-11-26 14:24:52205
Mirko Bonadei8cc66952020-10-30 09:13:45206def CheckNoIOStreamInHeaders(input_api, output_api, source_file_filter):
Christoffer Jansson4e8a7732022-02-08 08:01:12207 """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.org51198f12012-02-21 17:53:46219
Christoffer Jansson4e8a7732022-02-08 08:01:12220 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.org51198f12012-02-21 17:53:46228
kjellander@webrtc.orge4158642014-08-06 09:11:18229
Mirko Bonadei8cc66952020-10-30 09:13:45230def CheckNoPragmaOnce(input_api, output_api, source_file_filter):
Christoffer Jansson4e8a7732022-02-08 08:01:12231 """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)
kjellander6aeef742017-02-20 09:13:18242
Christoffer Jansson4e8a7732022-02-08 08:01:12243 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
kjellander6aeef742017-02-20 09:13:18252
Byoungchan Lee94f2ef22021-07-01 13:21:44253def CheckNoFRIEND_TEST(# pylint: disable=invalid-name
Mirko Bonadei8cc66952020-10-30 09:13:45254 input_api,
Byoungchan Lee94f2ef22021-07-01 13:21:44255 output_api,
Mirko Bonadei8cc66952020-10-30 09:13:45256 source_file_filter):
Christoffer Jansson4e8a7732022-02-08 08:01:12257 """Make sure that gtest's FRIEND_TEST() macro is not used, the
kjellander@webrtc.org51198f12012-02-21 17:53:46258 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 Jansson4e8a7732022-02-08 08:01:12260 problems = []
kjellander@webrtc.org51198f12012-02-21 17:53:46261
Christoffer Jansson4e8a7732022-02-08 08:01:12262 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.org51198f12012-02-21 17:53:46268
Christoffer Jansson4e8a7732022-02-08 08:01:12269 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.org51198f12012-02-21 17:53:46277
kjellander@webrtc.orge4158642014-08-06 09:11:18278
Mirko Bonadeifc17a782020-06-30 12:31:37279def IsLintDisabled(disabled_paths, file_path):
Christoffer Jansson4e8a7732022-02-08 08:01:12280 """ 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.org0fcaf992015-11-26 14:24:52285
286
charujain9893e252017-09-14 11:33:22287def CheckApprovedFilesLintClean(input_api, output_api,
Artem Titova04d1402018-05-11 09:23:00288 source_file_filter=None):
Christoffer Jansson4e8a7732022-02-08 08:01:12289 """Checks that all new or non-exempt .cc and .h files pass cpplint.py.
charujain9893e252017-09-14 11:33:22290 This check is based on CheckChangeLintsClean in
kjellander@webrtc.org51198f12012-02-21 17:53:46291 depot_tools/presubmit_canned_checks.py but has less filters and only checks
292 added files."""
Christoffer Jansson4e8a7732022-02-08 08:01:12293 result = []
kjellander@webrtc.org51198f12012-02-21 17:53:46294
Christoffer Jansson4e8a7732022-02-08 08:01:12295 # 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.org51198f12012-02-21 17:53:46300
Christoffer Jansson4e8a7732022-02-08 08:01:12301 lint_filters = cpplint._Filters()
302 lint_filters.extend(DISABLED_LINT_FILTERS)
303 cpplint._SetFilters(','.join(lint_filters))
jbauchc4e3ead2016-02-19 08:25:55304
Christoffer Jansson4e8a7732022-02-08 08:01:12305 # 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.org0fcaf992015-11-26 14:24:52309
Christoffer Jansson4e8a7732022-02-08 08:01:12310 # 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.org2a452092012-07-01 05:55:23320
Christoffer Jansson4e8a7732022-02-08 08:01:12321 for file_name in files:
322 cpplint.ProcessFile(file_name, verbosity_level)
kjellander@webrtc.org51198f12012-02-21 17:53:46323
Christoffer Jansson4e8a7732022-02-08 08:01:12324 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.org51198f12012-02-21 17:53:46330
Christoffer Jansson4e8a7732022-02-08 08:01:12331 return result
kjellander@webrtc.org51198f12012-02-21 17:53:46332
Artem Titove92675b2018-05-22 08:21:27333
charujain9893e252017-09-14 11:33:22334def CheckNoSourcesAbove(input_api, gn_files, output_api):
Christoffer Jansson4e8a7732022-02-08 08:01:12335 # 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 []
ehmaldonado5b1ba082016-09-02 12:51:08363
Artem Titove92675b2018-05-22 08:21:27364
Mirko Bonadei2dcf3482020-06-05 12:30:41365def CheckAbseilDependencies(input_api, gn_files, output_api):
Christoffer Jansson4e8a7732022-02-08 08:01:12366 """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 Bonadei2dcf3482020-06-05 12:30:41377
Christoffer Jansson4e8a7732022-02-08 08:01:12378 # 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 Bonadei2dcf3482020-06-05 12:30:41395
396
Mirko Bonadei4dc4e252017-09-19 11:49:16397def CheckNoMixingSources(input_api, gn_files, output_api):
Christoffer Jansson4e8a7732022-02-08 08:01:12398 """Disallow mixing C, C++ and Obj-C/Obj-C++ in the same target.
Mirko Bonadei4dc4e252017-09-19 11:49:16399
400 See bugs.webrtc.org/7743 for more context.
401 """
Artem Titove92675b2018-05-22 08:21:27402
Christoffer Jansson4e8a7732022-02-08 08:01:12403 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 Bonadei4dc4e252017-09-19 11:49:16409
Christoffer Jansson4e8a7732022-02-08 08:01:12410 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 Bonadei4dc4e252017-09-19 11:49:16436 list_of_sources.append((c_files, cc_files, objc_files))
Christoffer Jansson4e8a7732022-02-08 08:01:12437 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 []
kjellander7439f972016-12-06 06:47:46466
Artem Titove92675b2018-05-22 08:21:27467
charujain9893e252017-09-14 11:33:22468def CheckNoPackageBoundaryViolations(input_api, gn_files, output_api):
Christoffer Jansson4e8a7732022-02-08 08:01:12469 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 Bonadei8cc66952020-10-30 09:13:45481 ]
Christoffer Jansson4e8a7732022-02-08 08:01:12482 return []
ehmaldonado4fb97462017-01-30 13:27:22483
Mirko Bonadeia51bbd82018-03-08 15:15:45484
Mirko Bonadeif0e0d752018-07-04 06:48:18485def _ReportFileAndLine(filename, line_num):
Christoffer Jansson4e8a7732022-02-08 08:01:12486 """Default error formatter for _FindNewViolationsOfRule."""
487 return '%s (line %s)' % (filename, line_num)
Mirko Bonadeia51bbd82018-03-08 15:15:45488
489
Mirko Bonadei8cc66952020-10-30 09:13:45490def CheckNoWarningSuppressionFlagsAreAdded(gn_files,
491 input_api,
492 output_api,
Mirko Bonadeif0e0d752018-07-04 06:48:18493 error_formatter=_ReportFileAndLine):
Christoffer Jansson4e8a7732022-02-08 08:01:12494 """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 Bonadeif0e0d752018-07-04 06:48:18515
Mirko Bonadei9ce800d2019-02-05 15:48:13516
Mirko Bonadei8cc66952020-10-30 09:13:45517def CheckNoTestCaseUsageIsAdded(input_api,
518 output_api,
519 source_file_filter,
Mirko Bonadei9ce800d2019-02-05 15:48:13520 error_formatter=_ReportFileAndLine):
Christoffer Jansson4e8a7732022-02-08 08:01:12521 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 Bonadei9ce800d2019-02-05 15:48:13536
537
Mirko Bonadei8cc66952020-10-30 09:13:45538def CheckNoStreamUsageIsAdded(input_api,
539 output_api,
Artem Titov739351d2018-05-11 10:21:36540 source_file_filter,
Mirko Bonadeif0e0d752018-07-04 06:48:18541 error_formatter=_ReportFileAndLine):
Christoffer Jansson4e8a7732022-02-08 08:01:12542 """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 Bonadei571791a2019-05-07 12:08:05568
Christoffer Jansson4e8a7732022-02-08 08:01:12569 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öglund2ea27962020-01-13 14:10:40575
Christoffer Jansson4e8a7732022-02-08 08:01:12576 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 Bonadeia51bbd82018-03-08 15:15:45587
Artem Titove92675b2018-05-22 08:21:27588
Mirko Bonadeia05d47e2018-05-09 09:03:38589def CheckPublicDepsIsNotUsed(gn_files, input_api, output_api):
Christoffer Jansson4e8a7732022-02-08 08:01:12590 """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 Bonadei5c1ad592017-12-12 10:52:27610
Artem Titove92675b2018-05-22 08:21:27611
Mirko Bonadei05691dd2019-10-22 14:34:24612def CheckCheckIncludesIsNotUsed(gn_files, input_api, output_api):
Christoffer Jansson4e8a7732022-02-08 08:01:12613 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öglund6f491062018-01-11 11:04:23632
Artem Titove92675b2018-05-22 08:21:27633
Mirko Bonadeif0e0d752018-07-04 06:48:18634def CheckGnChanges(input_api, output_api):
Christoffer Jansson4e8a7732022-02-08 08:01:12635 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/.*', )))
ehmaldonado5b1ba082016-09-02 12:51:08639
Christoffer Jansson4e8a7732022-02-08 08:01:12640 gn_files = []
641 for f in input_api.AffectedSourceFiles(file_filter):
642 gn_files.append(f)
ehmaldonado5b1ba082016-09-02 12:51:08643
Christoffer Jansson4e8a7732022-02-08 08:01:12644 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
ehmaldonado5b1ba082016-09-02 12:51:08656
Artem Titove92675b2018-05-22 08:21:27657
Oleh Prypin920b6532017-10-05 09:28:51658def CheckGnGen(input_api, output_api):
Christoffer Jansson4e8a7732022-02-08 08:01:12659 """Runs `gn gen --check` with default args to detect mismatches between
Oleh Prypin920b6532017-10-05 09:28:51660 #includes and dependencies in the BUILD.gn files, as well as general build
661 errors.
662 """
Christoffer Jansson4e8a7732022-02-08 08:01:12663 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 Prypin920b6532017-10-05 09:28:51677
Artem Titove92675b2018-05-22 08:21:27678
Artem Titova04d1402018-05-11 09:23:00679def CheckUnwantedDependencies(input_api, output_api, source_file_filter):
Christoffer Jansson4e8a7732022-02-08 08:01:12680 """Runs checkdeps on #include statements added in this
kjellander@webrtc.org3bd41562014-09-01 11:06:37681 change. Breaking - rules is an error, breaking ! rules is a
682 warning.
683 """
Christoffer Jansson4e8a7732022-02-08 08:01:12684 # Copied from Chromium's src/PRESUBMIT.py.
kjellander@webrtc.org3bd41562014-09-01 11:06:37685
Christoffer Jansson4e8a7732022-02-08 08:01:12686 # 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.org3bd41562014-09-01 11:06:37701
Christoffer Jansson4e8a7732022-02-08 08:01:12702 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.org3bd41562014-09-01 11:06:37706
Christoffer Jansson4e8a7732022-02-08 08:01:12707 changed_lines = [line for _, line in f.ChangedContents()]
708 added_includes.append([f.LocalPath(), changed_lines])
kjellander@webrtc.org3bd41562014-09-01 11:06:37709
Christoffer Jansson4e8a7732022-02-08 08:01:12710 deps_checker = checkdeps.DepsChecker(input_api.PresubmitLocalPath())
kjellander@webrtc.org3bd41562014-09-01 11:06:37711
Christoffer Jansson4e8a7732022-02-08 08:01:12712 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.org3bd41562014-09-01 11:06:37721
Christoffer Jansson4e8a7732022-02-08 08:01:12722 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.org3bd41562014-09-01 11:06:37741
Artem Titove92675b2018-05-22 08:21:27742
charujain9893e252017-09-14 11:33:22743def CheckCommitMessageBugEntry(input_api, output_api):
Christoffer Jansson4e8a7732022-02-08 08:01:12744 """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]
charujain9893e252017-09-14 11:33:22767
Artem Titove92675b2018-05-22 08:21:27768
charujain9893e252017-09-14 11:33:22769def CheckChangeHasBugField(input_api, output_api):
Christoffer Jansson4e8a7732022-02-08 08:01:12770 """Requires that the changelist is associated with a bug.
kjellanderd1e26a92016-09-19 15:11:16771
772 This check is stricter than the one in depot_tools/presubmit_canned_checks.py
Mirko Bonadei61880182017-10-12 13:12:35773 since it fails the presubmit if the bug field is missing or doesn't contain
kjellanderd1e26a92016-09-19 15:11:16774 a bug reference.
Mirko Bonadei61880182017-10-12 13:12:35775
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:'.
kjellanderd1e26a92016-09-19 15:11:16778 """
Christoffer Jansson4e8a7732022-02-08 08:01:12779 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.orge4158642014-08-06 09:11:18789
Artem Titove92675b2018-05-22 08:21:27790
Artem Titova04d1402018-05-11 09:23:00791def CheckJSONParseErrors(input_api, output_api, source_file_filter):
Christoffer Jansson4e8a7732022-02-08 08:01:12792 """Check that JSON files do not contain syntax errors."""
kjellander569cf942016-02-11 13:02:59793
Christoffer Jansson4e8a7732022-02-08 08:01:12794 def FilterFile(affected_file):
795 return (input_api.os_path.splitext(affected_file.LocalPath())[1] == '.json'
796 and source_file_filter(affected_file))
kjellander569cf942016-02-11 13:02:59797
Christoffer Jansson4e8a7732022-02-08 08:01:12798 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
kjellander569cf942016-02-11 13:02:59805
Christoffer Jansson4e8a7732022-02-08 08:01:12806 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
kjellander569cf942016-02-11 13:02:59816
817
charujain9893e252017-09-14 11:33:22818def RunPythonTests(input_api, output_api):
Christoffer Jansson4e8a7732022-02-08 08:01:12819 def Join(*args):
820 return input_api.os_path.join(input_api.PresubmitLocalPath(), *args)
Henrik Kjellander8d3ad822015-05-26 17:52:05821
Christoffer Jansson4e8a7732022-02-08 08:01:12822 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 Kjellander8d3ad822015-05-26 17:52:05830
Christoffer Jansson4e8a7732022-02-08 08:01:12831 tests = []
Christoffer Jansson1b083a92022-02-15 13:52:31832 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 Jansson4e8a7732022-02-08 08:01:12838 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 Jansson1b083a92022-02-15 13:52:31845 files_to_skip=skipped_tests,
Christoffer Jansson4e8a7732022-02-08 08:01:12846 run_on_python2=False))
847 return input_api.RunTests(tests, parallel=True)
Henrik Kjellander8d3ad822015-05-26 17:52:05848
849
Artem Titova04d1402018-05-11 09:23:00850def CheckUsageOfGoogleProtobufNamespace(input_api, output_api,
851 source_file_filter):
Christoffer Jansson4e8a7732022-02-08 08:01:12852 """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)
mbonadei38415b22017-04-07 12:38:01864
Christoffer Jansson4e8a7732022-02-08 08:01:12865 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 []
mbonadei38415b22017-04-07 12:38:01873
874
Mirko Bonadei92ea95e2017-09-15 04:47:31875def _LicenseHeader(input_api):
Christoffer Jansson4e8a7732022-02-08 08:01:12876 """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 Bonadei92ea95e2017-09-15 04:47:31895
896
charujain9893e252017-09-14 11:33:22897def CommonChecks(input_api, output_api):
Christoffer Jansson4e8a7732022-02-08 08:01:12898 """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 Lee94f2ef22021-07-01 13:21:44913
Christoffer Jansson4e8a7732022-02-08 08:01:12914 # 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 Lee94f2ef22021-07-01 13:21:44923
Christoffer Jansson4e8a7732022-02-08 08:01:12924 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'))
kjellander569cf942016-02-11 13:02:59945
Christoffer Jansson4e8a7732022-02-08 08:01:12946 # 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 Kjellander63224672015-09-08 06:03:56951
Christoffer Jansson4e8a7732022-02-08 08:01:12952 # .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 Titove92675b2018-05-22 08:21:27967
Christoffer Jansson4e8a7732022-02-08 08:01:12968 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 Bonadei8cc66952020-10-30 09:13:451019 output_api,
1020 source_file_filter=non_third_party_sources))
Christoffer Jansson4e8a7732022-02-08 08:01:121021 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 Bonadei8cc66952020-10-30 09:13:451036 non_third_party_sources))
Christoffer Jansson4e8a7732022-02-08 08:01:121037 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 Bonadeia418e672018-10-24 11:57:251048
1049
1050def CheckApiDepsFileIsUpToDate(input_api, output_api):
Christoffer Jansson4e8a7732022-02-08 08:01:121051 """Check that 'include_rules' in api/DEPS is up to date.
Mirko Bonadei90490372018-10-26 11:17:471052
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 Jansson4e8a7732022-02-08 08:01:121060 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 Bonadeia418e672018-10-24 11:57:251064
Christoffer Jansson4e8a7732022-02-08 08:01:121065 include_rules = deps_content.get('include_rules', [])
1066 dirs_to_skip = set(['api', 'docs'])
Mirko Bonadeia418e672018-10-24 11:57:251067
Christoffer Jansson4e8a7732022-02-08 08:01:121068 # 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 Bonadeia418e672018-10-24 11:57:251076
Christoffer Jansson4e8a7732022-02-08 08:01:121077 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 Bonadei90490372018-10-26 11:17:471082
Christoffer Jansson4e8a7732022-02-08 08:01:121083 if missing_include_rules:
1084 error_msg = [
1085 'include_rules = [\n',
1086 ' ...\n',
1087 ]
Mirko Bonadeia418e672018-10-24 11:57:251088
Christoffer Jansson4e8a7732022-02-08 08:01:121089 for r in sorted(missing_include_rules):
1090 error_msg.append(' "%s",\n' % str(r))
Mirko Bonadeia418e672018-10-24 11:57:251091
Christoffer Jansson4e8a7732022-02-08 08:01:121092 error_msg.append(' ...\n')
1093 error_msg.append(']\n')
Mirko Bonadei90490372018-10-26 11:17:471094
Christoffer Jansson4e8a7732022-02-08 08:01:121095 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 Bonadei90490372018-10-26 11:17:471101
Christoffer Jansson4e8a7732022-02-08 08:01:121102 return results
Mirko Bonadei8cc66952020-10-30 09:13:451103
andrew@webrtc.org2442de12012-01-23 17:45:411104
Mirko Bonadei9fa8ef12019-09-17 17:14:131105def CheckBannedAbslMakeUnique(input_api, output_api, source_file_filter):
Christoffer Jansson4e8a7732022-02-08 08:01:121106 file_filter = lambda f: (f.LocalPath().endswith(('.cc', '.h')) and
1107 source_file_filter(f))
Mirko Bonadei9fa8ef12019-09-17 17:14:131108
Christoffer Jansson4e8a7732022-02-08 08:01:121109 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 Bonadei9fa8ef12019-09-17 17:14:131116
Christoffer Jansson4e8a7732022-02-08 08:01:121117 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 Bonadei8cc66952020-10-30 09:13:451124
Mirko Bonadei9fa8ef12019-09-17 17:14:131125
Mirko Bonadeid74c0e62020-07-16 19:57:011126def CheckObjcApiSymbols(input_api, output_api, source_file_filter):
Christoffer Jansson4e8a7732022-02-08 08:01:121127 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 Bonadeid74c0e62020-07-16 19:57:011131
Christoffer Jansson4e8a7732022-02-08 08:01:121132 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 Bonadeid74c0e62020-07-16 19:57:011145
Christoffer Jansson4e8a7732022-02-08 08:01:121146 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 Bonadei8cc66952020-10-30 09:13:451156
Mirko Bonadeid74c0e62020-07-16 19:57:011157
Mirko Bonadeia6395132021-07-22 15:35:591158def CheckAssertUsage(input_api, output_api, source_file_filter):
Christoffer Jansson4e8a7732022-02-08 08:01:121159 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 Bonadeia6395132021-07-22 15:35:591162
Christoffer Jansson4e8a7732022-02-08 08:01:121163 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 Bonadeia6395132021-07-22 15:35:591170
Christoffer Jansson4e8a7732022-02-08 08:01:121171 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 Bonadeia6395132021-07-22 15:35:591178
1179
tzika06bf852018-11-15 11:37:351180def CheckAbslMemoryInclude(input_api, output_api, source_file_filter):
Christoffer Jansson4e8a7732022-02-08 08:01:121181 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))
tzika06bf852018-11-15 11:37:351185
Christoffer Jansson4e8a7732022-02-08 08:01:121186 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
tzika06bf852018-11-15 11:37:351196
Christoffer Jansson4e8a7732022-02-08 08:01:121197 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 Bonadei8cc66952020-10-30 09:13:451205
kjellander@webrtc.orge4158642014-08-06 09:11:181206
andrew@webrtc.org53df1362012-01-26 21:24:231207def CheckChangeOnUpload(input_api, output_api):
Christoffer Jansson4e8a7732022-02-08 08:01:121208 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.comda159d62011-05-30 11:51:341214
kjellander@webrtc.orge4158642014-08-06 09:11:181215
andrew@webrtc.org2442de12012-01-23 17:45:411216def CheckChangeOnCommit(input_api, output_api):
Christoffer Jansson4e8a7732022-02-08 08:01:121217 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
mbonadei74973ed2017-05-09 14:58:051233
1234
Artem Titova04d1402018-05-11 09:23:001235def CheckOrphanHeaders(input_api, output_api, source_file_filter):
Christoffer Jansson4e8a7732022-02-08 08:01:121236 # 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 Jansson884e8ae2022-02-11 20:29:381241 exempt_paths = [re.escape(os.path.join('tools_webrtc', 'ios', 'SDK'))]
1242
Christoffer Jansson4e8a7732022-02-08 08:01:121243 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
mbonadei74973ed2017-05-09 14:58:051248
Christoffer Jansson4e8a7732022-02-08 08:01:121249 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 Bonadei960fd5b2017-06-29 12:59:361263
1264
Mirko Bonadei8cc66952020-10-30 09:13:451265def CheckNewlineAtTheEndOfProtoFiles(input_api, output_api,
1266 source_file_filter):
Christoffer Jansson4e8a7732022-02-08 08:01:121267 """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 Bonadei7e4ee6e2018-09-28 09:45:231279
1280
1281def _ExtractAddRulesFromParsedDeps(parsed_deps):
Christoffer Jansson4e8a7732022-02-08 08:01:121282 """Extract the rules that add dependencies from a parsed DEPS file.
Mirko Bonadei7e4ee6e2018-09-28 09:45:231283
1284 Args:
1285 parsed_deps: the locals dictionary from evaluating the DEPS file."""
Christoffer Jansson4e8a7732022-02-08 08:01:121286 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 Bonadei7e4ee6e2018-09-28 09:45:231292 add_rules.update([
Christoffer Jansson4e8a7732022-02-08 08:01:121293 rule[1:] for rule in rules
Mirko Bonadei7e4ee6e2018-09-28 09:45:231294 if rule.startswith('+') or rule.startswith('!')
1295 ])
Christoffer Jansson4e8a7732022-02-08 08:01:121296 return add_rules
Mirko Bonadei7e4ee6e2018-09-28 09:45:231297
1298
1299def _ParseDeps(contents):
Christoffer Jansson4e8a7732022-02-08 08:01:121300 """Simple helper for parsing DEPS files."""
Mirko Bonadei7e4ee6e2018-09-28 09:45:231301
Christoffer Jansson4e8a7732022-02-08 08:01:121302 # 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 Bonadei7e4ee6e2018-09-28 09:45:231306
Christoffer Jansson4e8a7732022-02-08 08:01:121307 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 Bonadei7e4ee6e2018-09-28 09:45:231314
Christoffer Jansson4e8a7732022-02-08 08:01:121315 local_scope = {}
1316 global_scope = {
1317 'Var': VarImpl(local_scope).Lookup,
1318 }
1319 exec(contents, global_scope, local_scope)
1320 return local_scope
Mirko Bonadei7e4ee6e2018-09-28 09:45:231321
1322
1323def _CalculateAddedDeps(os_path, old_contents, new_contents):
Christoffer Jansson4e8a7732022-02-08 08:01:121324 """Helper method for _CheckAddedDepsHaveTargetApprovals. Returns
Mirko Bonadei7e4ee6e2018-09-28 09:45:231325 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 Jansson4e8a7732022-02-08 08:01:121331 # We ignore deps entries on auto-generated directories.
1332 auto_generated_dirs = ['grit', 'jni']
Mirko Bonadei7e4ee6e2018-09-28 09:45:231333
Christoffer Jansson4e8a7732022-02-08 08:01:121334 old_deps = _ExtractAddRulesFromParsedDeps(_ParseDeps(old_contents))
1335 new_deps = _ExtractAddRulesFromParsedDeps(_ParseDeps(new_contents))
Mirko Bonadei7e4ee6e2018-09-28 09:45:231336
Christoffer Jansson4e8a7732022-02-08 08:01:121337 added_deps = new_deps.difference(old_deps)
Mirko Bonadei7e4ee6e2018-09-28 09:45:231338
Christoffer Jansson4e8a7732022-02-08 08:01:121339 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 Bonadei7e4ee6e2018-09-28 09:45:231349
1350
1351def CheckAddedDepsHaveTargetApprovals(input_api, output_api):
Christoffer Jansson4e8a7732022-02-08 08:01:121352 """When a dependency prefixed with + is added to a DEPS file, we
Edward Lesmesbef08502021-02-05 22:08:321353 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 Jansson4e8a7732022-02-08 08:01:121357 virtual_depended_on_files = set()
Mirko Bonadei7e4ee6e2018-09-28 09:45:231358
Christoffer Jansson4e8a7732022-02-08 08:01:121359 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 Bonadei7e4ee6e2018-09-28 09:45:231368
Christoffer Jansson4e8a7732022-02-08 08:01:121369 if not virtual_depended_on_files:
Mirko Bonadei8cc66952020-10-30 09:13:451370 return []
Christoffer Jansson4e8a7732022-02-08 08:01:121371
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 []