blob: 06a1c4a45e86d6555308215bba058739980465de [file] [log] [blame]
andrew@webrtc.org2442de12012-01-23 17:45:41 +00001# 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:34 +00008
kjellander986ee082015-06-16 04:32:13 -07009import json
kjellander@webrtc.orgaefe61a2014-12-08 13:00:30 +000010import os
kjellander986ee082015-06-16 04:32:13 -070011import platform
kjellander@webrtc.org85759802013-10-22 16:47:40 +000012import re
kjellander986ee082015-06-16 04:32:13 -070013import subprocess
kjellander@webrtc.org3bd41562014-09-01 11:06:37 +000014import sys
kjellander@webrtc.org85759802013-10-22 16:47:40 +000015
16
kjellander@webrtc.org0fcaf992015-11-26 15:24:52 +010017# Directories that will be scanned by cpplint by the presubmit script.
18CPPLINT_DIRS = [
Fredrik Solenbergea073732015-12-01 11:26:34 +010019 'webrtc/audio',
20 'webrtc/call',
kjellander@webrtc.org0fcaf992015-11-26 15:24:52 +010021 'webrtc/video_engine',
22]
23
24
kjellander@webrtc.org51198f12012-02-21 17:53:46 +000025def _CheckNoIOStreamInHeaders(input_api, output_api):
26 """Checks to make sure no .h files include <iostream>."""
27 files = []
28 pattern = input_api.re.compile(r'^#include\s*<iostream>',
29 input_api.re.MULTILINE)
30 for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile):
31 if not f.LocalPath().endswith('.h'):
32 continue
33 contents = input_api.ReadFile(f)
34 if pattern.search(contents):
35 files.append(f)
36
37 if len(files):
Henrik Kjellander57e5fd22015-05-25 12:55:39 +020038 return [output_api.PresubmitError(
kjellander@webrtc.org51198f12012-02-21 17:53:46 +000039 'Do not #include <iostream> in header files, since it inserts static ' +
40 'initialization into every file including the header. Instead, ' +
41 '#include <ostream>. See http://crbug.com/94794',
Henrik Kjellander57e5fd22015-05-25 12:55:39 +020042 files)]
kjellander@webrtc.org51198f12012-02-21 17:53:46 +000043 return []
44
kjellander@webrtc.orge4158642014-08-06 09:11:18 +000045
kjellander@webrtc.org51198f12012-02-21 17:53:46 +000046def _CheckNoFRIEND_TEST(input_api, output_api):
47 """Make sure that gtest's FRIEND_TEST() macro is not used, the
48 FRIEND_TEST_ALL_PREFIXES() macro from testsupport/gtest_prod_util.h should be
49 used instead since that allows for FLAKY_, FAILS_ and DISABLED_ prefixes."""
50 problems = []
51
52 file_filter = lambda f: f.LocalPath().endswith(('.cc', '.h'))
53 for f in input_api.AffectedFiles(file_filter=file_filter):
54 for line_num, line in f.ChangedContents():
55 if 'FRIEND_TEST(' in line:
56 problems.append(' %s:%d' % (f.LocalPath(), line_num))
57
58 if not problems:
59 return []
60 return [output_api.PresubmitPromptWarning('WebRTC\'s code should not use '
61 'gtest\'s FRIEND_TEST() macro. Include testsupport/gtest_prod_util.h and '
62 'use FRIEND_TEST_ALL_PREFIXES() instead.\n' + '\n'.join(problems))]
63
kjellander@webrtc.orge4158642014-08-06 09:11:18 +000064
kjellander@webrtc.org0fcaf992015-11-26 15:24:52 +010065def _IsLintWhitelisted(whitelist_dirs, file_path):
66 """ Checks if a file is whitelisted for lint check."""
67 for path in whitelist_dirs:
68 if os.path.dirname(file_path).startswith(path):
69 return True
70 return False
71
72
mflodman@webrtc.org2a452092012-07-01 05:55:23 +000073def _CheckApprovedFilesLintClean(input_api, output_api,
74 source_file_filter=None):
75 """Checks that all new or whitelisted .cc and .h files pass cpplint.py.
kjellander@webrtc.org51198f12012-02-21 17:53:46 +000076 This check is based on _CheckChangeLintsClean in
77 depot_tools/presubmit_canned_checks.py but has less filters and only checks
78 added files."""
79 result = []
80
81 # Initialize cpplint.
82 import cpplint
83 # Access to a protected member _XX of a client class
84 # pylint: disable=W0212
85 cpplint._cpplint_state.ResetErrorCounts()
86
kjellander@webrtc.org0fcaf992015-11-26 15:24:52 +010087 # Create a platform independent whitelist for the CPPLINT_DIRS.
88 whitelist_dirs = [input_api.os_path.join(*path.split('/'))
89 for path in CPPLINT_DIRS]
90
kjellander@webrtc.org51198f12012-02-21 17:53:46 +000091 # Use the strictest verbosity level for cpplint.py (level 1) which is the
92 # default when running cpplint.py from command line.
93 # To make it possible to work with not-yet-converted code, we're only applying
mflodman@webrtc.org2a452092012-07-01 05:55:23 +000094 # it to new (or moved/renamed) files and files listed in LINT_FOLDERS.
kjellander@webrtc.org51198f12012-02-21 17:53:46 +000095 verbosity_level = 1
96 files = []
97 for f in input_api.AffectedSourceFiles(source_file_filter):
Henrik Kjellander57e5fd22015-05-25 12:55:39 +020098 # Note that moved/renamed files also count as added.
kjellander@webrtc.org0fcaf992015-11-26 15:24:52 +010099 if f.Action() == 'A' or _IsLintWhitelisted(whitelist_dirs, f.LocalPath()):
kjellander@webrtc.org51198f12012-02-21 17:53:46 +0000100 files.append(f.AbsoluteLocalPath())
mflodman@webrtc.org2a452092012-07-01 05:55:23 +0000101
kjellander@webrtc.org51198f12012-02-21 17:53:46 +0000102 for file_name in files:
103 cpplint.ProcessFile(file_name, verbosity_level)
104
105 if cpplint._cpplint_state.error_count > 0:
106 if input_api.is_committing:
107 # TODO(kjellander): Change back to PresubmitError below when we're
108 # confident with the lint settings.
109 res_type = output_api.PresubmitPromptWarning
110 else:
111 res_type = output_api.PresubmitPromptWarning
112 result = [res_type('Changelist failed cpplint.py check.')]
113
114 return result
115
henrike@webrtc.org83fe69d2014-09-30 21:54:26 +0000116def _CheckNoRtcBaseDeps(input_api, gyp_files, output_api):
117 pattern = input_api.re.compile(r"base.gyp:rtc_base\s*'")
118 violating_files = []
119 for f in gyp_files:
henrike@webrtc.org36b0c1a2014-10-01 14:40:58 +0000120 gyp_exceptions = (
121 'base_tests.gyp',
122 'desktop_capture.gypi',
123 'libjingle.gyp',
henrike@webrtc.org28af6412014-11-04 15:11:46 +0000124 'libjingle_tests.gyp',
kjellander@webrtc.orge7237282015-02-26 11:12:17 +0000125 'p2p.gyp',
henrike@webrtc.org36b0c1a2014-10-01 14:40:58 +0000126 'sound.gyp',
127 'webrtc_test_common.gyp',
128 'webrtc_tests.gypi',
129 )
130 if f.LocalPath().endswith(gyp_exceptions):
131 continue
henrike@webrtc.org83fe69d2014-09-30 21:54:26 +0000132 contents = input_api.ReadFile(f)
133 if pattern.search(contents):
134 violating_files.append(f)
135 if violating_files:
136 return [output_api.PresubmitError(
137 'Depending on rtc_base is not allowed. Change your dependency to '
138 'rtc_base_approved and possibly sanitize and move the desired source '
139 'file(s) to rtc_base_approved.\nChanged GYP files:',
140 items=violating_files)]
141 return []
kjellander@webrtc.orge4158642014-08-06 09:11:18 +0000142
kjellander@webrtc.orgf68ffca2015-01-27 13:13:24 +0000143def _CheckNoSourcesAboveGyp(input_api, gyp_files, output_api):
144 # Disallow referencing source files with paths above the GYP file location.
145 source_pattern = input_api.re.compile(r'sources.*?\[(.*?)\]',
146 re.MULTILINE | re.DOTALL)
kjellander@webrtc.orga33f05e2015-01-29 14:29:45 +0000147 file_pattern = input_api.re.compile(r"'((\.\./.*?)|(<\(webrtc_root\).*?))'")
kjellander@webrtc.orgf68ffca2015-01-27 13:13:24 +0000148 violating_gyp_files = set()
149 violating_source_entries = []
150 for gyp_file in gyp_files:
151 contents = input_api.ReadFile(gyp_file)
152 for source_block_match in source_pattern.finditer(contents):
kjellander@webrtc.orgc98f6f32015-03-04 07:08:11 +0000153 # Find all source list entries starting with ../ in the source block
154 # (exclude overrides entries).
kjellander@webrtc.orgf68ffca2015-01-27 13:13:24 +0000155 for file_list_match in file_pattern.finditer(source_block_match.group(0)):
kjellander@webrtc.orgc98f6f32015-03-04 07:08:11 +0000156 source_file = file_list_match.group(0)
157 if 'overrides/' not in source_file:
158 violating_source_entries.append(source_file)
159 violating_gyp_files.add(gyp_file)
kjellander@webrtc.orgf68ffca2015-01-27 13:13:24 +0000160 if violating_gyp_files:
161 return [output_api.PresubmitError(
162 'Referencing source files above the directory of the GYP file is not '
163 'allowed. Please introduce new GYP targets and/or GYP files in the '
164 'proper location instead.\n'
165 'Invalid source entries:\n'
166 '%s\n'
167 'Violating GYP files:' % '\n'.join(violating_source_entries),
168 items=violating_gyp_files)]
169 return []
170
kjellander@webrtc.orge4158642014-08-06 09:11:18 +0000171def _CheckGypChanges(input_api, output_api):
172 source_file_filter = lambda x: input_api.FilterSourceFile(
173 x, white_list=(r'.+\.(gyp|gypi)$',))
174
175 gyp_files = []
176 for f in input_api.AffectedSourceFiles(source_file_filter):
kjellander@webrtc.org3398a4a2014-11-24 10:05:37 +0000177 if f.LocalPath().startswith('webrtc'):
178 gyp_files.append(f)
kjellander@webrtc.orge4158642014-08-06 09:11:18 +0000179
180 result = []
181 if gyp_files:
182 result.append(output_api.PresubmitNotifyResult(
183 'As you\'re changing GYP files: please make sure corresponding '
184 'BUILD.gn files are also updated.\nChanged GYP files:',
185 items=gyp_files))
henrike@webrtc.org83fe69d2014-09-30 21:54:26 +0000186 result.extend(_CheckNoRtcBaseDeps(input_api, gyp_files, output_api))
kjellander@webrtc.orgf68ffca2015-01-27 13:13:24 +0000187 result.extend(_CheckNoSourcesAboveGyp(input_api, gyp_files, output_api))
kjellander@webrtc.orge4158642014-08-06 09:11:18 +0000188 return result
189
kjellander@webrtc.org3bd41562014-09-01 11:06:37 +0000190def _CheckUnwantedDependencies(input_api, output_api):
191 """Runs checkdeps on #include statements added in this
192 change. Breaking - rules is an error, breaking ! rules is a
193 warning.
194 """
195 # Copied from Chromium's src/PRESUBMIT.py.
196
197 # We need to wait until we have an input_api object and use this
198 # roundabout construct to import checkdeps because this file is
199 # eval-ed and thus doesn't have __file__.
200 original_sys_path = sys.path
201 try:
kjellander@webrtc.orgaefe61a2014-12-08 13:00:30 +0000202 checkdeps_path = input_api.os_path.join(input_api.PresubmitLocalPath(),
203 'buildtools', 'checkdeps')
204 if not os.path.exists(checkdeps_path):
205 return [output_api.PresubmitError(
206 'Cannot find checkdeps at %s\nHave you run "gclient sync" to '
207 'download Chromium and setup the symlinks?' % checkdeps_path)]
208 sys.path.append(checkdeps_path)
kjellander@webrtc.org3bd41562014-09-01 11:06:37 +0000209 import checkdeps
210 from cpp_checker import CppChecker
211 from rules import Rule
212 finally:
213 # Restore sys.path to what it was before.
214 sys.path = original_sys_path
215
216 added_includes = []
217 for f in input_api.AffectedFiles():
218 if not CppChecker.IsCppFile(f.LocalPath()):
219 continue
220
Henrik Kjellander57e5fd22015-05-25 12:55:39 +0200221 changed_lines = [line for _, line in f.ChangedContents()]
kjellander@webrtc.org3bd41562014-09-01 11:06:37 +0000222 added_includes.append([f.LocalPath(), changed_lines])
223
224 deps_checker = checkdeps.DepsChecker(input_api.PresubmitLocalPath())
225
226 error_descriptions = []
227 warning_descriptions = []
228 for path, rule_type, rule_description in deps_checker.CheckAddedCppIncludes(
229 added_includes):
230 description_with_path = '%s\n %s' % (path, rule_description)
231 if rule_type == Rule.DISALLOW:
232 error_descriptions.append(description_with_path)
233 else:
234 warning_descriptions.append(description_with_path)
235
236 results = []
237 if error_descriptions:
238 results.append(output_api.PresubmitError(
239 'You added one or more #includes that violate checkdeps rules.',
240 error_descriptions))
241 if warning_descriptions:
242 results.append(output_api.PresubmitPromptOrNotify(
243 'You added one or more #includes of files that are temporarily\n'
244 'allowed but being removed. Can you avoid introducing the\n'
245 '#include? See relevant DEPS file(s) for details and contacts.',
246 warning_descriptions))
247 return results
248
kjellander@webrtc.orge4158642014-08-06 09:11:18 +0000249
Henrik Kjellander8d3ad822015-05-26 19:52:05 +0200250def _RunPythonTests(input_api, output_api):
251 def join(*args):
252 return input_api.os_path.join(input_api.PresubmitLocalPath(), *args)
253
254 test_directories = [
255 join('tools', 'autoroller', 'unittests'),
256 ]
257
258 tests = []
259 for directory in test_directories:
260 tests.extend(
261 input_api.canned_checks.GetUnitTestsInDirectory(
262 input_api,
263 output_api,
264 directory,
265 whitelist=[r'.+_test\.py$']))
266 return input_api.RunTests(tests, parallel=True)
267
268
andrew@webrtc.org53df1362012-01-26 21:24:23 +0000269def _CommonChecks(input_api, output_api):
270 """Checks common to both upload and commit."""
niklase@google.comda159d62011-05-30 11:51:34 +0000271 results = []
tkchin42f580e2015-11-26 23:18:23 -0800272 # Filter out files that are in objc or ios dirs from being cpplint-ed since
273 # they do not follow C++ lint rules.
274 black_list = input_api.DEFAULT_BLACK_LIST + (
275 r".*\bobjc[\\\/].*",
276 )
277 source_file_filter = lambda x: input_api.FilterSourceFile(x, None, black_list)
278 results.extend(_CheckApprovedFilesLintClean(
279 input_api, output_api, source_file_filter))
phoglund@webrtc.org5d3713932013-03-07 09:59:43 +0000280 results.extend(input_api.canned_checks.RunPylint(input_api, output_api,
281 black_list=(r'^.*gviz_api\.py$',
282 r'^.*gaeunit\.py$',
fischman@webrtc.org33584f92013-07-25 16:43:30 +0000283 # Embedded shell-script fakes out pylint.
Henrik Kjellander14771ac2015-06-02 13:10:04 +0200284 r'^build[\\\/].*\.py$',
285 r'^buildtools[\\\/].*\.py$',
286 r'^chromium[\\\/].*\.py$',
287 r'^google_apis[\\\/].*\.py$',
288 r'^net.*[\\\/].*\.py$',
289 r'^out.*[\\\/].*\.py$',
290 r'^testing[\\\/].*\.py$',
291 r'^third_party[\\\/].*\.py$',
292 r'^tools[\\\/]find_depot_tools.py$',
293 r'^tools[\\\/]clang[\\\/].*\.py$',
294 r'^tools[\\\/]generate_library_loader[\\\/].*\.py$',
295 r'^tools[\\\/]gn[\\\/].*\.py$',
296 r'^tools[\\\/]gyp[\\\/].*\.py$',
Henrik Kjellanderd6d27e72015-09-25 22:19:11 +0200297 r'^tools[\\\/]isolate_driver.py$',
Henrik Kjellander14771ac2015-06-02 13:10:04 +0200298 r'^tools[\\\/]protoc_wrapper[\\\/].*\.py$',
299 r'^tools[\\\/]python[\\\/].*\.py$',
300 r'^tools[\\\/]python_charts[\\\/]data[\\\/].*\.py$',
301 r'^tools[\\\/]refactoring[\\\/].*\.py$',
302 r'^tools[\\\/]swarming_client[\\\/].*\.py$',
303 r'^tools[\\\/]vim[\\\/].*\.py$',
phoglund@webrtc.org5d3713932013-03-07 09:59:43 +0000304 # TODO(phoglund): should arguably be checked.
Henrik Kjellander14771ac2015-06-02 13:10:04 +0200305 r'^tools[\\\/]valgrind-webrtc[\\\/].*\.py$',
306 r'^tools[\\\/]valgrind[\\\/].*\.py$',
307 r'^tools[\\\/]win[\\\/].*\.py$',
308 r'^xcodebuild.*[\\\/].*\.py$',),
phoglund@webrtc.org5d3713932013-03-07 09:59:43 +0000309 disabled_warnings=['F0401', # Failed to import x
310 'E0611', # No package y in x
311 'W0232', # Class has no __init__ method
Henrik Kjellander57e5fd22015-05-25 12:55:39 +0200312 ],
313 pylintrc='pylintrc'))
314 # WebRTC can't use the presubmit_canned_checks.PanProjectChecks function since
315 # we need to have different license checks in talk/ and webrtc/ directories.
316 # Instead, hand-picked checks are included below.
Henrik Kjellander63224672015-09-08 08:03:56 +0200317
318 # Skip long-lines check for DEPS, GN and GYP files.
319 long_lines_sources = lambda x: input_api.FilterSourceFile(x,
320 black_list=(r'.+\.gyp$', r'.+\.gypi$', r'.+\.gn$', r'.+\.gni$', 'DEPS'))
andrew@webrtc.org2442de12012-01-23 17:45:41 +0000321 results.extend(input_api.canned_checks.CheckLongLines(
Henrik Kjellander63224672015-09-08 08:03:56 +0200322 input_api, output_api, maxlen=80, source_file_filter=long_lines_sources))
andrew@webrtc.org2442de12012-01-23 17:45:41 +0000323 results.extend(input_api.canned_checks.CheckChangeHasNoTabs(
324 input_api, output_api))
andrew@webrtc.org53df1362012-01-26 21:24:23 +0000325 results.extend(input_api.canned_checks.CheckChangeHasNoStrayWhitespace(
326 input_api, output_api))
327 results.extend(input_api.canned_checks.CheckChangeTodoHasOwner(
328 input_api, output_api))
kjellander@webrtc.org51198f12012-02-21 17:53:46 +0000329 results.extend(_CheckNoIOStreamInHeaders(input_api, output_api))
330 results.extend(_CheckNoFRIEND_TEST(input_api, output_api))
kjellander@webrtc.orge4158642014-08-06 09:11:18 +0000331 results.extend(_CheckGypChanges(input_api, output_api))
kjellander@webrtc.org3bd41562014-09-01 11:06:37 +0000332 results.extend(_CheckUnwantedDependencies(input_api, output_api))
Henrik Kjellander8d3ad822015-05-26 19:52:05 +0200333 results.extend(_RunPythonTests(input_api, output_api))
andrew@webrtc.org53df1362012-01-26 21:24:23 +0000334 return results
andrew@webrtc.org2442de12012-01-23 17:45:41 +0000335
kjellander@webrtc.orge4158642014-08-06 09:11:18 +0000336
andrew@webrtc.org53df1362012-01-26 21:24:23 +0000337def CheckChangeOnUpload(input_api, output_api):
338 results = []
339 results.extend(_CommonChecks(input_api, output_api))
Henrik Kjellander57e5fd22015-05-25 12:55:39 +0200340 results.extend(
341 input_api.canned_checks.CheckGNFormatted(input_api, output_api))
niklase@google.comda159d62011-05-30 11:51:34 +0000342 return results
343
kjellander@webrtc.orge4158642014-08-06 09:11:18 +0000344
andrew@webrtc.org2442de12012-01-23 17:45:41 +0000345def CheckChangeOnCommit(input_api, output_api):
niklase@google.com1198db92011-06-09 07:07:24 +0000346 results = []
andrew@webrtc.org53df1362012-01-26 21:24:23 +0000347 results.extend(_CommonChecks(input_api, output_api))
niklase@google.com1198db92011-06-09 07:07:24 +0000348 results.extend(input_api.canned_checks.CheckOwners(input_api, output_api))
andrew@webrtc.org53df1362012-01-26 21:24:23 +0000349 results.extend(input_api.canned_checks.CheckChangeWasUploaded(
350 input_api, output_api))
351 results.extend(input_api.canned_checks.CheckChangeHasDescription(
352 input_api, output_api))
kjellander@webrtc.org51198f12012-02-21 17:53:46 +0000353 results.extend(input_api.canned_checks.CheckChangeHasBugField(
354 input_api, output_api))
355 results.extend(input_api.canned_checks.CheckChangeHasTestField(
356 input_api, output_api))
kjellander@webrtc.org12cb88c2014-02-13 11:53:43 +0000357 results.extend(input_api.canned_checks.CheckTreeIsOpen(
358 input_api, output_api,
359 json_url='http://webrtc-status.appspot.com/current?format=json'))
niklase@google.com1198db92011-06-09 07:07:24 +0000360 return results
kjellander@webrtc.org85759802013-10-22 16:47:40 +0000361
kjellander@webrtc.orge4158642014-08-06 09:11:18 +0000362
kjellander@webrtc.org85759802013-10-22 16:47:40 +0000363# pylint: disable=W0613
kjellander@webrtc.orgc7b8b2f2014-04-03 20:19:36 +0000364def GetPreferredTryMasters(project, change):
kjellander986ee082015-06-16 04:32:13 -0700365 cq_config_path = os.path.join(
tandrii04465d22015-06-20 04:00:49 -0700366 change.RepositoryRoot(), 'infra', 'config', 'cq.cfg')
kjellander986ee082015-06-16 04:32:13 -0700367 # commit_queue.py below is a script in depot_tools directory, which has a
368 # 'builders' command to retrieve a list of CQ builders from the CQ config.
369 is_win = platform.system() == 'Windows'
370 masters = json.loads(subprocess.check_output(
371 ['commit_queue', 'builders', cq_config_path], shell=is_win))
kjellander@webrtc.org85759802013-10-22 16:47:40 +0000372
kjellander986ee082015-06-16 04:32:13 -0700373 try_config = {}
374 for master in masters:
375 try_config.setdefault(master, {})
376 for builder in masters[master]:
377 if 'presubmit' in builder:
378 # Do not trigger presubmit builders, since they're likely to fail
379 # (e.g. OWNERS checks before finished code review), and we're running
380 # local presubmit anyway.
381 pass
382 else:
383 try_config[master][builder] = ['defaulttests']
kjellander@webrtc.org85759802013-10-22 16:47:40 +0000384
kjellander986ee082015-06-16 04:32:13 -0700385 return try_config