blob: 506ffadc6cd7ba4a79b27f96624c7bae494c6964 [file] [log] [blame]
andrew@webrtc.org2442de12012-01-23 17:45:411# Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
2#
3# Use of this source code is governed by a BSD-style license
4# that can be found in the LICENSE file in the root of the source
5# tree. An additional intellectual property rights grant can be found
6# in the file PATENTS. All contributing project authors may
7# be found in the AUTHORS file in the root of the source tree.
niklase@google.comda159d62011-05-30 11:51:348
kjellander7439f972016-12-06 06:47:469import json
kjellander@webrtc.orgaefe61a2014-12-08 13:00:3010import os
kjellander@webrtc.org85759802013-10-22 16:47:4011import re
kjellander@webrtc.org3bd41562014-09-01 11:06:3712import sys
Mirko Bonadei4dc4e252017-09-19 11:49:1613from collections import defaultdict
Oleh Prypin2f33a562017-10-04 18:17:5414from contextlib import contextmanager
kjellander@webrtc.org85759802013-10-22 16:47:4015
16
oprypin2aa463f2017-03-23 10:17:0217# Files and directories that are *skipped* by cpplint in the presubmit script.
18CPPLINT_BLACKLIST = [
Mirko Bonadei92ea95e2017-09-15 04:47:3119 'api/video_codecs/video_decoder.h',
20 'common_types.cc',
21 'common_types.h',
22 'examples/objc',
Steve Antone78bcb92017-10-31 16:53:0823 'media/base/streamparams.h',
24 'media/base/videocommon.h',
25 'media/engine/fakewebrtcdeviceinfo.h',
26 'media/sctp/sctptransport.cc',
Mirko Bonadei92ea95e2017-09-15 04:47:3127 'modules/audio_coding',
Mirko Bonadei92ea95e2017-09-15 04:47:3128 'modules/audio_device',
29 'modules/audio_processing',
30 'modules/desktop_capture',
31 'modules/include/module_common_types.h',
Mirko Bonadei92ea95e2017-09-15 04:47:3132 'modules/utility',
33 'modules/video_capture',
Steve Anton6c38cc72017-11-29 18:25:5834 'p2p/base/session.cc',
35 'p2p/base/session.h',
36 'p2p/base/pseudotcp.cc',
37 'p2p/base/pseudotcp.h',
Mirko Bonadei92ea95e2017-09-15 04:47:3138 'rtc_base',
39 'sdk/android/src/jni',
40 'sdk/objc',
41 'system_wrappers',
42 'test',
Henrik Kjellander90fd7d82017-05-09 06:30:1043 'tools_webrtc',
Mirko Bonadei92ea95e2017-09-15 04:47:3144 'voice_engine',
kjellander@webrtc.org0fcaf992015-11-26 14:24:5245]
46
jbauchc4e3ead2016-02-19 08:25:5547# These filters will always be removed, even if the caller specifies a filter
48# set, as they are problematic or broken in some way.
49#
50# Justifications for each filter:
51# - build/c++11 : Rvalue ref checks are unreliable (false positives),
52# include file and feature blacklists are
53# google3-specific.
kjellandere5a87a52016-04-27 09:32:1254# - whitespace/operators: Same as above (doesn't seem sufficient to eliminate
55# all move-related errors).
jbauchc4e3ead2016-02-19 08:25:5556BLACKLIST_LINT_FILTERS = [
57 '-build/c++11',
kjellandere5a87a52016-04-27 09:32:1258 '-whitespace/operators',
jbauchc4e3ead2016-02-19 08:25:5559]
60
kjellanderfd595232015-12-04 10:44:0961# List of directories of "supported" native APIs. That means changes to headers
62# will be done in a compatible way following this scheme:
63# 1. Non-breaking changes are made.
64# 2. The old APIs as marked as deprecated (with comments).
65# 3. Deprecation is announced to discuss-webrtc@googlegroups.com and
66# webrtc-users@google.com (internal list).
67# 4. (later) The deprecated APIs are removed.
kjellander53047c92015-12-03 07:56:1468NATIVE_API_DIRS = (
Karl Wibergef52d8b82017-10-25 11:20:0369 'api', # All subdirectories of api/ are included as well.
Mirko Bonadeia4eeeff2018-01-11 12:16:5270 'media/base',
71 'media/engine',
Mirko Bonadei92ea95e2017-09-15 04:47:3172 'modules/audio_device/include',
73 'pc',
kjellanderdd705472016-06-09 18:17:2774)
Mirko Bonadei4dc4e252017-09-19 11:49:1675
kjellanderdd705472016-06-09 18:17:2776# These directories should not be used but are maintained only to avoid breaking
77# some legacy downstream code.
78LEGACY_API_DIRS = (
Mirko Bonadei92ea95e2017-09-15 04:47:3179 'common_audio/include',
80 'modules/audio_coding/include',
Mirko Bonadei92ea95e2017-09-15 04:47:3181 'modules/audio_processing/include',
82 'modules/bitrate_controller/include',
83 'modules/congestion_controller/include',
84 'modules/include',
85 'modules/remote_bitrate_estimator/include',
86 'modules/rtp_rtcp/include',
87 'modules/rtp_rtcp/source',
88 'modules/utility/include',
89 'modules/video_coding/codecs/h264/include',
90 'modules/video_coding/codecs/i420/include',
91 'modules/video_coding/codecs/vp8/include',
92 'modules/video_coding/codecs/vp9/include',
93 'modules/video_coding/include',
94 'rtc_base',
95 'system_wrappers/include',
kjellander53047c92015-12-03 07:56:1496)
Mirko Bonadei4dc4e252017-09-19 11:49:1697
Karl Wibergd4f01c12017-11-10 09:55:4598# NOTE: The set of directories in API_DIRS should be the same as those
99# listed in the table in native-api.md.
kjellanderdd705472016-06-09 18:17:27100API_DIRS = NATIVE_API_DIRS[:] + LEGACY_API_DIRS[:]
kjellander53047c92015-12-03 07:56:14101
Mirko Bonadei4dc4e252017-09-19 11:49:16102# TARGET_RE matches a GN target, and extracts the target name and the contents.
103TARGET_RE = re.compile(r'(?P<indent>\s*)\w+\("(?P<target_name>\w+)"\) {'
104 r'(?P<target_contents>.*?)'
105 r'(?P=indent)}',
106 re.MULTILINE | re.DOTALL)
107
108# SOURCES_RE matches a block of sources inside a GN target.
109SOURCES_RE = re.compile(r'sources \+?= \[(?P<sources>.*?)\]',
110 re.MULTILINE | re.DOTALL)
111
112# FILE_PATH_RE matchies a file path.
113FILE_PATH_RE = re.compile(r'"(?P<file_path>(\w|\/)+)(?P<extension>\.\w+)"')
114
kjellander53047c92015-12-03 07:56:14115
Oleh Prypin2f33a562017-10-04 18:17:54116@contextmanager
117def _AddToPath(*paths):
118 original_sys_path = sys.path
119 sys.path.extend(paths)
120 try:
121 yield
122 finally:
123 # Restore sys.path to what it was before.
124 sys.path = original_sys_path
ehmaldonado4fb97462017-01-30 13:27:22125
126
charujain9893e252017-09-14 11:33:22127def VerifyNativeApiHeadersListIsValid(input_api, output_api):
kjellander53047c92015-12-03 07:56:14128 """Ensures the list of native API header directories is up to date."""
129 non_existing_paths = []
130 native_api_full_paths = [
131 input_api.os_path.join(input_api.PresubmitLocalPath(),
kjellanderdd705472016-06-09 18:17:27132 *path.split('/')) for path in API_DIRS]
kjellander53047c92015-12-03 07:56:14133 for path in native_api_full_paths:
134 if not os.path.isdir(path):
135 non_existing_paths.append(path)
136 if non_existing_paths:
137 return [output_api.PresubmitError(
138 'Directories to native API headers have changed which has made the '
139 'list in PRESUBMIT.py outdated.\nPlease update it to the current '
140 'location of our native APIs.',
141 non_existing_paths)]
142 return []
143
kjellanderc88b5d52017-04-05 13:42:43144API_CHANGE_MSG = """
kwibergeb133022016-04-07 14:41:48145You seem to be changing native API header files. Please make sure that you:
oprypin375b9ac2017-02-13 12:13:23146 1. Make compatible changes that don't break existing clients. Usually
147 this is done by keeping the existing method signatures unchanged.
148 2. Mark the old stuff as deprecated (see RTC_DEPRECATED macro).
kwibergeb133022016-04-07 14:41:48149 3. Create a timeline and plan for when the deprecated stuff will be
150 removed. (The amount of time we give users to change their code
151 should be informed by how much work it is for them. If they just
152 need to replace one name with another or something equally
153 simple, 1-2 weeks might be good; if they need to do serious work,
154 up to 3 months may be called for.)
155 4. Update/inform existing downstream code owners to stop using the
156 deprecated stuff. (Send announcements to
157 discuss-webrtc@googlegroups.com and webrtc-users@google.com.)
158 5. Remove the deprecated stuff, once the agreed-upon amount of time
159 has passed.
160Related files:
161"""
kjellander53047c92015-12-03 07:56:14162
charujain9893e252017-09-14 11:33:22163def CheckNativeApiHeaderChanges(input_api, output_api):
kjellander53047c92015-12-03 07:56:14164 """Checks to remind proper changing of native APIs."""
165 files = []
Karl Wiberg6bfac032017-10-27 13:14:20166 source_file_filter = lambda x: input_api.FilterSourceFile(
167 x, white_list=[r'.+\.(gn|gni|h)$'])
168 for f in input_api.AffectedSourceFiles(source_file_filter):
169 for path in API_DIRS:
170 dn = os.path.dirname(f.LocalPath())
171 if path == 'api':
172 # Special case: Subdirectories included.
173 if dn == 'api' or dn.startswith('api/'):
174 files.append(f)
175 else:
176 # Normal case: Subdirectories not included.
177 if dn == path:
178 files.append(f)
kjellander53047c92015-12-03 07:56:14179
180 if files:
kjellanderc88b5d52017-04-05 13:42:43181 return [output_api.PresubmitNotifyResult(API_CHANGE_MSG, files)]
kjellander53047c92015-12-03 07:56:14182 return []
183
kjellander@webrtc.org0fcaf992015-11-26 14:24:52184
charujain9893e252017-09-14 11:33:22185def CheckNoIOStreamInHeaders(input_api, output_api):
kjellander@webrtc.org51198f12012-02-21 17:53:46186 """Checks to make sure no .h files include <iostream>."""
187 files = []
188 pattern = input_api.re.compile(r'^#include\s*<iostream>',
189 input_api.re.MULTILINE)
190 for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile):
191 if not f.LocalPath().endswith('.h'):
192 continue
193 contents = input_api.ReadFile(f)
194 if pattern.search(contents):
195 files.append(f)
196
197 if len(files):
Henrik Kjellander57e5fd22015-05-25 10:55:39198 return [output_api.PresubmitError(
kjellander@webrtc.org51198f12012-02-21 17:53:46199 'Do not #include <iostream> in header files, since it inserts static ' +
200 'initialization into every file including the header. Instead, ' +
201 '#include <ostream>. See http://crbug.com/94794',
Henrik Kjellander57e5fd22015-05-25 10:55:39202 files)]
kjellander@webrtc.org51198f12012-02-21 17:53:46203 return []
204
kjellander@webrtc.orge4158642014-08-06 09:11:18205
charujain9893e252017-09-14 11:33:22206def CheckNoPragmaOnce(input_api, output_api):
kjellander6aeef742017-02-20 09:13:18207 """Make sure that banned functions are not used."""
208 files = []
209 pattern = input_api.re.compile(r'^#pragma\s+once',
210 input_api.re.MULTILINE)
211 for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile):
212 if not f.LocalPath().endswith('.h'):
213 continue
214 contents = input_api.ReadFile(f)
215 if pattern.search(contents):
216 files.append(f)
217
218 if files:
219 return [output_api.PresubmitError(
220 'Do not use #pragma once in header files.\n'
221 'See http://www.chromium.org/developers/coding-style#TOC-File-headers',
222 files)]
223 return []
224
225
charujain9893e252017-09-14 11:33:22226def CheckNoFRIEND_TEST(input_api, output_api): # pylint: disable=invalid-name
kjellander@webrtc.org51198f12012-02-21 17:53:46227 """Make sure that gtest's FRIEND_TEST() macro is not used, the
228 FRIEND_TEST_ALL_PREFIXES() macro from testsupport/gtest_prod_util.h should be
229 used instead since that allows for FLAKY_, FAILS_ and DISABLED_ prefixes."""
230 problems = []
231
232 file_filter = lambda f: f.LocalPath().endswith(('.cc', '.h'))
233 for f in input_api.AffectedFiles(file_filter=file_filter):
234 for line_num, line in f.ChangedContents():
235 if 'FRIEND_TEST(' in line:
236 problems.append(' %s:%d' % (f.LocalPath(), line_num))
237
238 if not problems:
239 return []
240 return [output_api.PresubmitPromptWarning('WebRTC\'s code should not use '
241 'gtest\'s FRIEND_TEST() macro. Include testsupport/gtest_prod_util.h and '
242 'use FRIEND_TEST_ALL_PREFIXES() instead.\n' + '\n'.join(problems))]
243
kjellander@webrtc.orge4158642014-08-06 09:11:18244
charujain9893e252017-09-14 11:33:22245def IsLintBlacklisted(blacklist_paths, file_path):
oprypin2aa463f2017-03-23 10:17:02246 """ Checks if a file is blacklisted for lint check."""
247 for path in blacklist_paths:
248 if file_path == path or os.path.dirname(file_path).startswith(path):
kjellander@webrtc.org0fcaf992015-11-26 14:24:52249 return True
250 return False
251
252
charujain9893e252017-09-14 11:33:22253def CheckApprovedFilesLintClean(input_api, output_api,
mflodman@webrtc.org2a452092012-07-01 05:55:23254 source_file_filter=None):
oprypin2aa463f2017-03-23 10:17:02255 """Checks that all new or non-blacklisted .cc and .h files pass cpplint.py.
charujain9893e252017-09-14 11:33:22256 This check is based on CheckChangeLintsClean in
kjellander@webrtc.org51198f12012-02-21 17:53:46257 depot_tools/presubmit_canned_checks.py but has less filters and only checks
258 added files."""
259 result = []
260
261 # Initialize cpplint.
262 import cpplint
263 # Access to a protected member _XX of a client class
264 # pylint: disable=W0212
265 cpplint._cpplint_state.ResetErrorCounts()
266
jbauchc4e3ead2016-02-19 08:25:55267 lint_filters = cpplint._Filters()
268 lint_filters.extend(BLACKLIST_LINT_FILTERS)
269 cpplint._SetFilters(','.join(lint_filters))
270
oprypin2aa463f2017-03-23 10:17:02271 # Create a platform independent blacklist for cpplint.
272 blacklist_paths = [input_api.os_path.join(*path.split('/'))
273 for path in CPPLINT_BLACKLIST]
kjellander@webrtc.org0fcaf992015-11-26 14:24:52274
kjellander@webrtc.org51198f12012-02-21 17:53:46275 # Use the strictest verbosity level for cpplint.py (level 1) which is the
oprypin2aa463f2017-03-23 10:17:02276 # default when running cpplint.py from command line. To make it possible to
277 # work with not-yet-converted code, we're only applying it to new (or
278 # moved/renamed) files and files not listed in CPPLINT_BLACKLIST.
kjellander@webrtc.org51198f12012-02-21 17:53:46279 verbosity_level = 1
280 files = []
281 for f in input_api.AffectedSourceFiles(source_file_filter):
Henrik Kjellander57e5fd22015-05-25 10:55:39282 # Note that moved/renamed files also count as added.
charujain9893e252017-09-14 11:33:22283 if f.Action() == 'A' or not IsLintBlacklisted(blacklist_paths,
oprypin2aa463f2017-03-23 10:17:02284 f.LocalPath()):
kjellander@webrtc.org51198f12012-02-21 17:53:46285 files.append(f.AbsoluteLocalPath())
mflodman@webrtc.org2a452092012-07-01 05:55:23286
kjellander@webrtc.org51198f12012-02-21 17:53:46287 for file_name in files:
288 cpplint.ProcessFile(file_name, verbosity_level)
289
290 if cpplint._cpplint_state.error_count > 0:
291 if input_api.is_committing:
oprypin8e58d652017-03-21 14:52:41292 res_type = output_api.PresubmitError
kjellander@webrtc.org51198f12012-02-21 17:53:46293 else:
294 res_type = output_api.PresubmitPromptWarning
295 result = [res_type('Changelist failed cpplint.py check.')]
296
297 return result
298
charujain9893e252017-09-14 11:33:22299def CheckNoSourcesAbove(input_api, gn_files, output_api):
ehmaldonado5b1ba082016-09-02 12:51:08300 # Disallow referencing source files with paths above the GN file location.
301 source_pattern = input_api.re.compile(r' +sources \+?= \[(.*?)\]',
302 re.MULTILINE | re.DOTALL)
303 file_pattern = input_api.re.compile(r'"((\.\./.*?)|(//.*?))"')
304 violating_gn_files = set()
305 violating_source_entries = []
306 for gn_file in gn_files:
307 contents = input_api.ReadFile(gn_file)
308 for source_block_match in source_pattern.finditer(contents):
309 # Find all source list entries starting with ../ in the source block
310 # (exclude overrides entries).
311 for file_list_match in file_pattern.finditer(source_block_match.group(1)):
312 source_file = file_list_match.group(1)
313 if 'overrides/' not in source_file:
314 violating_source_entries.append(source_file)
315 violating_gn_files.add(gn_file)
316 if violating_gn_files:
317 return [output_api.PresubmitError(
318 'Referencing source files above the directory of the GN file is not '
Henrik Kjellanderb4af3d62016-11-16 19:11:29319 'allowed. Please introduce new GN targets in the proper location '
320 'instead.\n'
ehmaldonado5b1ba082016-09-02 12:51:08321 'Invalid source entries:\n'
322 '%s\n'
323 'Violating GN files:' % '\n'.join(violating_source_entries),
324 items=violating_gn_files)]
325 return []
326
Mirko Bonadei4dc4e252017-09-19 11:49:16327def CheckNoMixingSources(input_api, gn_files, output_api):
328 """Disallow mixing C, C++ and Obj-C/Obj-C++ in the same target.
329
330 See bugs.webrtc.org/7743 for more context.
331 """
332 def _MoreThanOneSourceUsed(*sources_lists):
333 sources_used = 0
334 for source_list in sources_lists:
335 if len(source_list):
336 sources_used += 1
337 return sources_used > 1
338
339 errors = defaultdict(lambda: [])
kjellander7439f972016-12-06 06:47:46340 for gn_file in gn_files:
Mirko Bonadei4dc4e252017-09-19 11:49:16341 gn_file_content = input_api.ReadFile(gn_file)
342 for target_match in TARGET_RE.finditer(gn_file_content):
343 # list_of_sources is a list of tuples of the form
344 # (c_files, cc_files, objc_files) that keeps track of all the sources
345 # defined in a target. A GN target can have more that on definition of
346 # sources (since it supports if/else statements).
347 # E.g.:
348 # rtc_static_library("foo") {
349 # if (is_win) {
350 # sources = [ "foo.cc" ]
351 # } else {
352 # sources = [ "foo.mm" ]
353 # }
354 # }
355 # This is allowed and the presubmit check should support this case.
356 list_of_sources = []
kjellander7439f972016-12-06 06:47:46357 c_files = []
358 cc_files = []
Mirko Bonadei4dc4e252017-09-19 11:49:16359 objc_files = []
360 target_name = target_match.group('target_name')
361 target_contents = target_match.group('target_contents')
362 for sources_match in SOURCES_RE.finditer(target_contents):
363 if '+=' not in sources_match.group(0):
364 if c_files or cc_files or objc_files:
365 list_of_sources.append((c_files, cc_files, objc_files))
366 c_files = []
367 cc_files = []
368 objc_files = []
369 for file_match in FILE_PATH_RE.finditer(sources_match.group(1)):
370 file_path = file_match.group('file_path')
371 extension = file_match.group('extension')
372 if extension == '.c':
373 c_files.append(file_path + extension)
374 if extension == '.cc':
375 cc_files.append(file_path + extension)
376 if extension in ['.m', '.mm']:
377 objc_files.append(file_path + extension)
378 list_of_sources.append((c_files, cc_files, objc_files))
379 for c_files_list, cc_files_list, objc_files_list in list_of_sources:
380 if _MoreThanOneSourceUsed(c_files_list, cc_files_list, objc_files_list):
381 all_sources = sorted(c_files_list + cc_files_list + objc_files_list)
382 errors[gn_file.LocalPath()].append((target_name, all_sources))
383 if errors:
kjellander7439f972016-12-06 06:47:46384 return [output_api.PresubmitError(
Mirko Bonadei4dc4e252017-09-19 11:49:16385 'GN targets cannot mix .c, .cc and .m (or .mm) source files.\n'
386 'Please create a separate target for each collection of sources.\n'
kjellander7439f972016-12-06 06:47:46387 'Mixed sources: \n'
388 '%s\n'
Mirko Bonadei4dc4e252017-09-19 11:49:16389 'Violating GN files:\n%s\n' % (json.dumps(errors, indent=2),
390 '\n'.join(errors.keys())))]
kjellander7439f972016-12-06 06:47:46391 return []
392
charujain9893e252017-09-14 11:33:22393def CheckNoPackageBoundaryViolations(input_api, gn_files, output_api):
ehmaldonado4fb97462017-01-30 13:27:22394 cwd = input_api.PresubmitLocalPath()
Oleh Prypin2f33a562017-10-04 18:17:54395 with _AddToPath(input_api.os_path.join(
396 cwd, 'tools_webrtc', 'presubmit_checks_lib')):
397 from check_package_boundaries import CheckPackageBoundaries
398 build_files = [os.path.join(cwd, gn_file.LocalPath()) for gn_file in gn_files]
399 errors = CheckPackageBoundaries(cwd, build_files)[:5]
400 if errors:
ehmaldonado4fb97462017-01-30 13:27:22401 return [output_api.PresubmitError(
Oleh Prypin2f33a562017-10-04 18:17:54402 'There are package boundary violations in the following GN files:',
403 long_text='\n\n'.join(str(err) for err in errors))]
ehmaldonado4fb97462017-01-30 13:27:22404 return []
405
Mirko Bonadei5c1ad592017-12-12 10:52:27406def CheckPublicDepsIsNotUsed(gn_files, output_api):
407 result = []
408 error_msg = ('public_deps is not allowed in WebRTC BUILD.gn files because '
409 'it doesn\'t map well to downstream build systems.\n'
410 'Used in: %s (line %d).')
411 for affected_file in gn_files:
412 for (line_number, affected_line) in affected_file.ChangedContents():
413 if 'public_deps' in affected_line:
414 result.append(
415 output_api.PresubmitError(error_msg % (affected_file.LocalPath(),
416 line_number)))
417 return result
418
Patrik Höglund6f491062018-01-11 11:04:23419def CheckCheckIncludesIsNotUsed(gn_files, output_api):
420 result = []
421 error_msg = ('check_includes overrides are not allowed since it can cause '
422 'incorrect dependencies to form. It effectively means that your '
423 'module can include any .h file without depending on its '
424 'corresponding target. There are some exceptional cases when '
425 'this is allowed: if so, get approval from a .gn owner in the'
426 'root OWNERS file.\n'
427 'Used in: %s (line %d).')
428 for affected_file in gn_files:
429 for (line_number, affected_line) in affected_file.ChangedContents():
430 if 'check_includes' in affected_line:
431 result.append(
432 output_api.PresubmitError(error_msg % (affected_file.LocalPath(),
433 line_number)))
434 return result
435
charujain9893e252017-09-14 11:33:22436def CheckGnChanges(input_api, output_api):
ehmaldonado5b1ba082016-09-02 12:51:08437 source_file_filter = lambda x: input_api.FilterSourceFile(
Oleh Prypinafe01652017-10-04 13:56:08438 x, white_list=(r'.+\.(gn|gni)$',),
Mirko Bonadei5c1ad592017-12-12 10:52:27439 black_list=(r'.*/presubmit_checks_lib/testdata/.*',))
ehmaldonado5b1ba082016-09-02 12:51:08440
441 gn_files = []
442 for f in input_api.AffectedSourceFiles(source_file_filter):
Mirko Bonadei92ea95e2017-09-15 04:47:31443 gn_files.append(f)
ehmaldonado5b1ba082016-09-02 12:51:08444
445 result = []
446 if gn_files:
charujain9893e252017-09-14 11:33:22447 result.extend(CheckNoSourcesAbove(input_api, gn_files, output_api))
Mirko Bonadei4dc4e252017-09-19 11:49:16448 result.extend(CheckNoMixingSources(input_api, gn_files, output_api))
449 result.extend(CheckNoPackageBoundaryViolations(input_api, gn_files,
450 output_api))
Mirko Bonadei5c1ad592017-12-12 10:52:27451 result.extend(CheckPublicDepsIsNotUsed(gn_files, output_api))
Patrik Höglund6f491062018-01-11 11:04:23452 result.extend(CheckCheckIncludesIsNotUsed(gn_files, output_api))
ehmaldonado5b1ba082016-09-02 12:51:08453 return result
454
Oleh Prypin920b6532017-10-05 09:28:51455def CheckGnGen(input_api, output_api):
456 """Runs `gn gen --check` with default args to detect mismatches between
457 #includes and dependencies in the BUILD.gn files, as well as general build
458 errors.
459 """
460 with _AddToPath(input_api.os_path.join(
461 input_api.PresubmitLocalPath(), 'tools_webrtc', 'presubmit_checks_lib')):
462 from gn_check import RunGnCheck
463 errors = RunGnCheck(input_api.PresubmitLocalPath())[:5]
464 if errors:
465 return [output_api.PresubmitPromptWarning(
466 'Some #includes do not match the build dependency graph. Please run:\n'
467 ' gn gen --check <out_dir>',
468 long_text='\n\n'.join(errors))]
469 return []
470
charujain9893e252017-09-14 11:33:22471def CheckUnwantedDependencies(input_api, output_api):
kjellander@webrtc.org3bd41562014-09-01 11:06:37472 """Runs checkdeps on #include statements added in this
473 change. Breaking - rules is an error, breaking ! rules is a
474 warning.
475 """
476 # Copied from Chromium's src/PRESUBMIT.py.
477
478 # We need to wait until we have an input_api object and use this
479 # roundabout construct to import checkdeps because this file is
480 # eval-ed and thus doesn't have __file__.
Oleh Prypin2f33a562017-10-04 18:17:54481 checkdeps_path = input_api.os_path.join(input_api.PresubmitLocalPath(),
482 'buildtools', 'checkdeps')
483 if not os.path.exists(checkdeps_path):
484 return [output_api.PresubmitError(
485 'Cannot find checkdeps at %s\nHave you run "gclient sync" to '
486 'download all the DEPS entries?' % checkdeps_path)]
487 with _AddToPath(checkdeps_path):
kjellander@webrtc.org3bd41562014-09-01 11:06:37488 import checkdeps
489 from cpp_checker import CppChecker
490 from rules import Rule
kjellander@webrtc.org3bd41562014-09-01 11:06:37491
492 added_includes = []
493 for f in input_api.AffectedFiles():
494 if not CppChecker.IsCppFile(f.LocalPath()):
495 continue
496
Henrik Kjellander57e5fd22015-05-25 10:55:39497 changed_lines = [line for _, line in f.ChangedContents()]
kjellander@webrtc.org3bd41562014-09-01 11:06:37498 added_includes.append([f.LocalPath(), changed_lines])
499
500 deps_checker = checkdeps.DepsChecker(input_api.PresubmitLocalPath())
501
502 error_descriptions = []
503 warning_descriptions = []
504 for path, rule_type, rule_description in deps_checker.CheckAddedCppIncludes(
505 added_includes):
506 description_with_path = '%s\n %s' % (path, rule_description)
507 if rule_type == Rule.DISALLOW:
508 error_descriptions.append(description_with_path)
509 else:
510 warning_descriptions.append(description_with_path)
511
512 results = []
513 if error_descriptions:
514 results.append(output_api.PresubmitError(
kjellandera7066a32017-03-23 10:47:05515 'You added one or more #includes that violate checkdeps rules.\n'
516 'Check that the DEPS files in these locations contain valid rules.\n'
517 'See https://cs.chromium.org/chromium/src/buildtools/checkdeps/ for '
518 'more details about checkdeps.',
kjellander@webrtc.org3bd41562014-09-01 11:06:37519 error_descriptions))
520 if warning_descriptions:
521 results.append(output_api.PresubmitPromptOrNotify(
522 'You added one or more #includes of files that are temporarily\n'
523 'allowed but being removed. Can you avoid introducing the\n'
kjellandera7066a32017-03-23 10:47:05524 '#include? See relevant DEPS file(s) for details and contacts.\n'
525 'See https://cs.chromium.org/chromium/src/buildtools/checkdeps/ for '
526 'more details about checkdeps.',
kjellander@webrtc.org3bd41562014-09-01 11:06:37527 warning_descriptions))
528 return results
529
charujain9893e252017-09-14 11:33:22530def CheckCommitMessageBugEntry(input_api, output_api):
531 """Check that bug entries are well-formed in commit message."""
532 bogus_bug_msg = (
Mirko Bonadei61880182017-10-12 13:12:35533 'Bogus Bug entry: %s. Please specify the issue tracker prefix and the '
charujain9893e252017-09-14 11:33:22534 'issue number, separated by a colon, e.g. webrtc:123 or chromium:12345.')
535 results = []
Mirko Bonadei61880182017-10-12 13:12:35536 for bug in input_api.change.BugsFromDescription():
charujain9893e252017-09-14 11:33:22537 bug = bug.strip()
538 if bug.lower() == 'none':
539 continue
charujain81a58c72017-09-25 11:25:45540 if 'b/' not in bug and ':' not in bug:
charujain9893e252017-09-14 11:33:22541 try:
542 if int(bug) > 100000:
543 # Rough indicator for current chromium bugs.
544 prefix_guess = 'chromium'
545 else:
546 prefix_guess = 'webrtc'
Mirko Bonadei61880182017-10-12 13:12:35547 results.append('Bug entry requires issue tracker prefix, e.g. %s:%s' %
charujain9893e252017-09-14 11:33:22548 (prefix_guess, bug))
549 except ValueError:
550 results.append(bogus_bug_msg % bug)
charujain81a58c72017-09-25 11:25:45551 elif not (re.match(r'\w+:\d+', bug) or re.match(r'b/\d+', bug)):
charujain9893e252017-09-14 11:33:22552 results.append(bogus_bug_msg % bug)
553 return [output_api.PresubmitError(r) for r in results]
554
555def CheckChangeHasBugField(input_api, output_api):
Mirko Bonadei61880182017-10-12 13:12:35556 """Requires that the changelist is associated with a bug.
kjellanderd1e26a92016-09-19 15:11:16557
558 This check is stricter than the one in depot_tools/presubmit_canned_checks.py
Mirko Bonadei61880182017-10-12 13:12:35559 since it fails the presubmit if the bug field is missing or doesn't contain
kjellanderd1e26a92016-09-19 15:11:16560 a bug reference.
Mirko Bonadei61880182017-10-12 13:12:35561
562 This supports both 'BUG=' and 'Bug:' since we are in the process of migrating
563 to Gerrit and it encourages the usage of 'Bug:'.
kjellanderd1e26a92016-09-19 15:11:16564 """
Mirko Bonadei61880182017-10-12 13:12:35565 if input_api.change.BugsFromDescription():
kjellanderd1e26a92016-09-19 15:11:16566 return []
567 else:
568 return [output_api.PresubmitError(
Mirko Bonadei61880182017-10-12 13:12:35569 'The "Bug: [bug number]" footer is mandatory. Please create a bug and '
kjellanderd1e26a92016-09-19 15:11:16570 'reference it using either of:\n'
Mirko Bonadei61880182017-10-12 13:12:35571 ' * https://bugs.webrtc.org - reference it using Bug: webrtc:XXXX\n'
572 ' * https://crbug.com - reference it using Bug: chromium:XXXXXX')]
kjellander@webrtc.orge4158642014-08-06 09:11:18573
charujain9893e252017-09-14 11:33:22574def CheckJSONParseErrors(input_api, output_api):
kjellander569cf942016-02-11 13:02:59575 """Check that JSON files do not contain syntax errors."""
576
577 def FilterFile(affected_file):
578 return input_api.os_path.splitext(affected_file.LocalPath())[1] == '.json'
579
580 def GetJSONParseError(input_api, filename):
581 try:
582 contents = input_api.ReadFile(filename)
583 input_api.json.loads(contents)
584 except ValueError as e:
585 return e
586 return None
587
588 results = []
589 for affected_file in input_api.AffectedFiles(
590 file_filter=FilterFile, include_deletes=False):
591 parse_error = GetJSONParseError(input_api,
592 affected_file.AbsoluteLocalPath())
593 if parse_error:
594 results.append(output_api.PresubmitError('%s could not be parsed: %s' %
595 (affected_file.LocalPath(), parse_error)))
596 return results
597
598
charujain9893e252017-09-14 11:33:22599def RunPythonTests(input_api, output_api):
kjellanderc88b5d52017-04-05 13:42:43600 def Join(*args):
Henrik Kjellander8d3ad822015-05-26 17:52:05601 return input_api.os_path.join(input_api.PresubmitLocalPath(), *args)
602
603 test_directories = [
Edward Lemur6d01f6d2017-09-14 15:02:01604 input_api.PresubmitLocalPath(),
Mirko Bonadei92ea95e2017-09-15 04:47:31605 Join('rtc_tools', 'py_event_log_analyzer'),
606 Join('rtc_tools'),
607 Join('audio', 'test', 'unittests'),
ehmaldonado4fb97462017-01-30 13:27:22608 ] + [
Henrik Kjellander90fd7d82017-05-09 06:30:10609 root for root, _, files in os.walk(Join('tools_webrtc'))
ehmaldonado4fb97462017-01-30 13:27:22610 if any(f.endswith('_test.py') for f in files)
Henrik Kjellander8d3ad822015-05-26 17:52:05611 ]
612
613 tests = []
614 for directory in test_directories:
615 tests.extend(
616 input_api.canned_checks.GetUnitTestsInDirectory(
617 input_api,
618 output_api,
619 directory,
620 whitelist=[r'.+_test\.py$']))
621 return input_api.RunTests(tests, parallel=True)
622
623
charujain9893e252017-09-14 11:33:22624def CheckUsageOfGoogleProtobufNamespace(input_api, output_api):
mbonadei38415b22017-04-07 12:38:01625 """Checks that the namespace google::protobuf has not been used."""
626 files = []
627 pattern = input_api.re.compile(r'google::protobuf')
Mirko Bonadei92ea95e2017-09-15 04:47:31628 proto_utils_path = os.path.join('rtc_base', 'protobuf_utils.h')
mbonadei38415b22017-04-07 12:38:01629 for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile):
630 if f.LocalPath() in [proto_utils_path, 'PRESUBMIT.py']:
631 continue
632 contents = input_api.ReadFile(f)
633 if pattern.search(contents):
634 files.append(f)
635
636 if files:
637 return [output_api.PresubmitError(
638 'Please avoid to use namespace `google::protobuf` directly.\n'
639 'Add a using directive in `%s` and include that header instead.'
640 % proto_utils_path, files)]
641 return []
642
643
Mirko Bonadei92ea95e2017-09-15 04:47:31644def _LicenseHeader(input_api):
645 """Returns the license header regexp."""
646 # Accept any year number from 2003 to the current year
647 current_year = int(input_api.time.strftime('%Y'))
648 allowed_years = (str(s) for s in reversed(xrange(2003, current_year + 1)))
649 years_re = '(' + '|'.join(allowed_years) + ')'
650 license_header = (
651 r'.*? Copyright( \(c\))? %(year)s The WebRTC [Pp]roject [Aa]uthors\. '
652 r'All [Rr]ights [Rr]eserved\.\n'
653 r'.*?\n'
654 r'.*? Use of this source code is governed by a BSD-style license\n'
655 r'.*? that can be found in the LICENSE file in the root of the source\n'
656 r'.*? tree\. An additional intellectual property rights grant can be '
657 r'found\n'
658 r'.*? in the file PATENTS\. All contributing project authors may\n'
659 r'.*? be found in the AUTHORS file in the root of the source tree\.\n'
660 ) % {
661 'year': years_re,
662 }
663 return license_header
664
665
charujain9893e252017-09-14 11:33:22666def CommonChecks(input_api, output_api):
andrew@webrtc.org53df1362012-01-26 21:24:23667 """Checks common to both upload and commit."""
niklase@google.comda159d62011-05-30 11:51:34668 results = []
tkchin42f580e2015-11-27 07:18:23669 # Filter out files that are in objc or ios dirs from being cpplint-ed since
670 # they do not follow C++ lint rules.
671 black_list = input_api.DEFAULT_BLACK_LIST + (
672 r".*\bobjc[\\\/].*",
Kári Tristan Helgason3fa35172016-09-09 08:55:05673 r".*objc\.[hcm]+$",
tkchin42f580e2015-11-27 07:18:23674 )
675 source_file_filter = lambda x: input_api.FilterSourceFile(x, None, black_list)
charujain9893e252017-09-14 11:33:22676 results.extend(CheckApprovedFilesLintClean(
tkchin42f580e2015-11-27 07:18:23677 input_api, output_api, source_file_filter))
Mirko Bonadei92ea95e2017-09-15 04:47:31678 results.extend(input_api.canned_checks.CheckLicense(
679 input_api, output_api, _LicenseHeader(input_api)))
phoglund@webrtc.org5d3713932013-03-07 09:59:43680 results.extend(input_api.canned_checks.RunPylint(input_api, output_api,
kjellander@webrtc.org177567c2016-12-22 09:40:28681 black_list=(r'^base[\\\/].*\.py$',
Henrik Kjellander14771ac2015-06-02 11:10:04682 r'^build[\\\/].*\.py$',
683 r'^buildtools[\\\/].*\.py$',
kjellander38c65c82017-04-13 05:43:38684 r'^infra[\\\/].*\.py$',
Henrik Kjellander0779e8f2016-12-22 11:01:17685 r'^ios[\\\/].*\.py$',
Henrik Kjellander14771ac2015-06-02 11:10:04686 r'^out.*[\\\/].*\.py$',
687 r'^testing[\\\/].*\.py$',
688 r'^third_party[\\\/].*\.py$',
kjellander@webrtc.org177567c2016-12-22 09:40:28689 r'^tools[\\\/].*\.py$',
kjellanderafd54942016-12-17 20:21:39690 # TODO(phoglund): should arguably be checked.
Henrik Kjellander90fd7d82017-05-09 06:30:10691 r'^tools_webrtc[\\\/]mb[\\\/].*\.py$',
692 r'^tools_webrtc[\\\/]valgrind[\\\/].*\.py$',
Henrik Kjellander14771ac2015-06-02 11:10:04693 r'^xcodebuild.*[\\\/].*\.py$',),
Henrik Kjellander57e5fd22015-05-25 10:55:39694 pylintrc='pylintrc'))
kjellander569cf942016-02-11 13:02:59695
nisse3d21e232016-09-02 10:07:06696 # TODO(nisse): talk/ is no more, so make below checks simpler?
Henrik Kjellander57e5fd22015-05-25 10:55:39697 # WebRTC can't use the presubmit_canned_checks.PanProjectChecks function since
698 # we need to have different license checks in talk/ and webrtc/ directories.
699 # Instead, hand-picked checks are included below.
Henrik Kjellander63224672015-09-08 06:03:56700
tkchin3cd9a302016-06-08 19:40:28701 # .m and .mm files are ObjC files. For simplicity we will consider .h files in
702 # ObjC subdirectories ObjC headers.
703 objc_filter_list = (r'.+\.m$', r'.+\.mm$', r'.+objc\/.+\.h$')
Henrik Kjellanderb4af3d62016-11-16 19:11:29704 # Skip long-lines check for DEPS and GN files.
705 build_file_filter_list = (r'.+\.gn$', r'.+\.gni$', 'DEPS')
tkchin3cd9a302016-06-08 19:40:28706 eighty_char_sources = lambda x: input_api.FilterSourceFile(x,
707 black_list=build_file_filter_list + objc_filter_list)
708 hundred_char_sources = lambda x: input_api.FilterSourceFile(x,
709 white_list=objc_filter_list)
andrew@webrtc.org2442de12012-01-23 17:45:41710 results.extend(input_api.canned_checks.CheckLongLines(
tkchin3cd9a302016-06-08 19:40:28711 input_api, output_api, maxlen=80, source_file_filter=eighty_char_sources))
712 results.extend(input_api.canned_checks.CheckLongLines(
713 input_api, output_api, maxlen=100,
714 source_file_filter=hundred_char_sources))
715
andrew@webrtc.org2442de12012-01-23 17:45:41716 results.extend(input_api.canned_checks.CheckChangeHasNoTabs(
717 input_api, output_api))
andrew@webrtc.org53df1362012-01-26 21:24:23718 results.extend(input_api.canned_checks.CheckChangeHasNoStrayWhitespace(
719 input_api, output_api))
kjellandere5dc62a2016-12-14 08:16:21720 results.extend(input_api.canned_checks.CheckAuthorizedAuthor(
721 input_api, output_api))
andrew@webrtc.org53df1362012-01-26 21:24:23722 results.extend(input_api.canned_checks.CheckChangeTodoHasOwner(
723 input_api, output_api))
charujain9893e252017-09-14 11:33:22724 results.extend(CheckNativeApiHeaderChanges(input_api, output_api))
725 results.extend(CheckNoIOStreamInHeaders(input_api, output_api))
726 results.extend(CheckNoPragmaOnce(input_api, output_api))
727 results.extend(CheckNoFRIEND_TEST(input_api, output_api))
728 results.extend(CheckGnChanges(input_api, output_api))
729 results.extend(CheckUnwantedDependencies(input_api, output_api))
730 results.extend(CheckJSONParseErrors(input_api, output_api))
731 results.extend(RunPythonTests(input_api, output_api))
732 results.extend(CheckUsageOfGoogleProtobufNamespace(input_api, output_api))
Mirko Bonadei866d3372017-09-15 10:35:26733 results.extend(CheckOrphanHeaders(input_api, output_api))
Mirko Bonadeia730c1c2017-09-18 09:33:13734 results.extend(CheckNewlineAtTheEndOfProtoFiles(input_api, output_api))
andrew@webrtc.org53df1362012-01-26 21:24:23735 return results
andrew@webrtc.org2442de12012-01-23 17:45:41736
kjellander@webrtc.orge4158642014-08-06 09:11:18737
andrew@webrtc.org53df1362012-01-26 21:24:23738def CheckChangeOnUpload(input_api, output_api):
739 results = []
charujain9893e252017-09-14 11:33:22740 results.extend(CommonChecks(input_api, output_api))
Oleh Prypin920b6532017-10-05 09:28:51741 results.extend(CheckGnGen(input_api, output_api))
Henrik Kjellander57e5fd22015-05-25 10:55:39742 results.extend(
743 input_api.canned_checks.CheckGNFormatted(input_api, output_api))
niklase@google.comda159d62011-05-30 11:51:34744 return results
745
kjellander@webrtc.orge4158642014-08-06 09:11:18746
andrew@webrtc.org2442de12012-01-23 17:45:41747def CheckChangeOnCommit(input_api, output_api):
niklase@google.com1198db92011-06-09 07:07:24748 results = []
charujain9893e252017-09-14 11:33:22749 results.extend(CommonChecks(input_api, output_api))
750 results.extend(VerifyNativeApiHeadersListIsValid(input_api, output_api))
niklase@google.com1198db92011-06-09 07:07:24751 results.extend(input_api.canned_checks.CheckOwners(input_api, output_api))
andrew@webrtc.org53df1362012-01-26 21:24:23752 results.extend(input_api.canned_checks.CheckChangeWasUploaded(
753 input_api, output_api))
754 results.extend(input_api.canned_checks.CheckChangeHasDescription(
755 input_api, output_api))
charujain9893e252017-09-14 11:33:22756 results.extend(CheckChangeHasBugField(input_api, output_api))
757 results.extend(CheckCommitMessageBugEntry(input_api, output_api))
kjellander@webrtc.org12cb88c2014-02-13 11:53:43758 results.extend(input_api.canned_checks.CheckTreeIsOpen(
759 input_api, output_api,
760 json_url='http://webrtc-status.appspot.com/current?format=json'))
niklase@google.com1198db92011-06-09 07:07:24761 return results
mbonadei74973ed2017-05-09 14:58:05762
763
charujain9893e252017-09-14 11:33:22764def CheckOrphanHeaders(input_api, output_api):
mbonadei74973ed2017-05-09 14:58:05765 # We need to wait until we have an input_api object and use this
766 # roundabout construct to import prebubmit_checks_lib because this file is
767 # eval-ed and thus doesn't have __file__.
Patrik Höglund2f3f7222017-12-19 10:08:56768 error_msg = """{} should be listed in {}."""
mbonadei74973ed2017-05-09 14:58:05769 results = []
Patrik Höglund7e60de22018-01-09 13:22:00770 orphan_blacklist = [
771 os.path.join('tools_webrtc', 'ios', 'SDK'),
772 ]
Oleh Prypin2f33a562017-10-04 18:17:54773 with _AddToPath(input_api.os_path.join(
774 input_api.PresubmitLocalPath(), 'tools_webrtc', 'presubmit_checks_lib')):
mbonadei74973ed2017-05-09 14:58:05775 from check_orphan_headers import GetBuildGnPathFromFilePath
776 from check_orphan_headers import IsHeaderInBuildGn
mbonadei74973ed2017-05-09 14:58:05777
Patrik Höglund7e60de22018-01-09 13:22:00778 source_file_filter = lambda x: input_api.FilterSourceFile(
779 x, black_list=orphan_blacklist)
780 for f in input_api.AffectedSourceFiles(source_file_filter):
781 if f.LocalPath().endswith('.h'):
mbonadei74973ed2017-05-09 14:58:05782 file_path = os.path.abspath(f.LocalPath())
783 root_dir = os.getcwd()
784 gn_file_path = GetBuildGnPathFromFilePath(file_path, os.path.exists,
785 root_dir)
786 in_build_gn = IsHeaderInBuildGn(file_path, gn_file_path)
787 if not in_build_gn:
788 results.append(output_api.PresubmitError(error_msg.format(
Patrik Höglund2f3f7222017-12-19 10:08:56789 f.LocalPath(), os.path.relpath(gn_file_path))))
mbonadei74973ed2017-05-09 14:58:05790 return results
Mirko Bonadei960fd5b2017-06-29 12:59:36791
792
Mirko Bonadeia730c1c2017-09-18 09:33:13793def CheckNewlineAtTheEndOfProtoFiles(input_api, output_api):
Mirko Bonadei960fd5b2017-06-29 12:59:36794 """Checks that all .proto files are terminated with a newline."""
795 error_msg = 'File {} must end with exactly one newline.'
796 results = []
797 source_file_filter = lambda x: input_api.FilterSourceFile(
798 x, white_list=(r'.+\.proto$',))
799 for f in input_api.AffectedSourceFiles(source_file_filter):
800 file_path = f.LocalPath()
801 with open(file_path) as f:
802 lines = f.readlines()
Mirko Bonadeia730c1c2017-09-18 09:33:13803 if len(lines) > 0 and not lines[-1].endswith('\n'):
Mirko Bonadei960fd5b2017-06-29 12:59:36804 results.append(output_api.PresubmitError(error_msg.format(file_path)))
805 return results