blob: 430628c180b5f7e6b5e36cf6d1049a5e6ccba944 [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
kjellander@webrtc.org85759802013-10-22 16:47:40 +00009import re
10
11
kjellander@webrtc.org51198f12012-02-21 17:53:46 +000012def _CheckNoIOStreamInHeaders(input_api, output_api):
13 """Checks to make sure no .h files include <iostream>."""
14 files = []
15 pattern = input_api.re.compile(r'^#include\s*<iostream>',
16 input_api.re.MULTILINE)
17 for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile):
18 if not f.LocalPath().endswith('.h'):
19 continue
20 contents = input_api.ReadFile(f)
21 if pattern.search(contents):
22 files.append(f)
23
24 if len(files):
25 return [ output_api.PresubmitError(
26 'Do not #include <iostream> in header files, since it inserts static ' +
27 'initialization into every file including the header. Instead, ' +
28 '#include <ostream>. See http://crbug.com/94794',
29 files) ]
30 return []
31
kjellander@webrtc.orge4158642014-08-06 09:11:18 +000032
kjellander@webrtc.org51198f12012-02-21 17:53:46 +000033def _CheckNoFRIEND_TEST(input_api, output_api):
34 """Make sure that gtest's FRIEND_TEST() macro is not used, the
35 FRIEND_TEST_ALL_PREFIXES() macro from testsupport/gtest_prod_util.h should be
36 used instead since that allows for FLAKY_, FAILS_ and DISABLED_ prefixes."""
37 problems = []
38
39 file_filter = lambda f: f.LocalPath().endswith(('.cc', '.h'))
40 for f in input_api.AffectedFiles(file_filter=file_filter):
41 for line_num, line in f.ChangedContents():
42 if 'FRIEND_TEST(' in line:
43 problems.append(' %s:%d' % (f.LocalPath(), line_num))
44
45 if not problems:
46 return []
47 return [output_api.PresubmitPromptWarning('WebRTC\'s code should not use '
48 'gtest\'s FRIEND_TEST() macro. Include testsupport/gtest_prod_util.h and '
49 'use FRIEND_TEST_ALL_PREFIXES() instead.\n' + '\n'.join(problems))]
50
kjellander@webrtc.orge4158642014-08-06 09:11:18 +000051
mflodman@webrtc.org2a452092012-07-01 05:55:23 +000052def _CheckApprovedFilesLintClean(input_api, output_api,
53 source_file_filter=None):
54 """Checks that all new or whitelisted .cc and .h files pass cpplint.py.
kjellander@webrtc.org51198f12012-02-21 17:53:46 +000055 This check is based on _CheckChangeLintsClean in
56 depot_tools/presubmit_canned_checks.py but has less filters and only checks
57 added files."""
58 result = []
59
60 # Initialize cpplint.
61 import cpplint
62 # Access to a protected member _XX of a client class
63 # pylint: disable=W0212
64 cpplint._cpplint_state.ResetErrorCounts()
65
66 # Justifications for each filter:
67 #
68 # - build/header_guard : WebRTC coding style says they should be prefixed
69 # with WEBRTC_, which is not possible to configure in
70 # cpplint.py.
71 cpplint._SetFilters('-build/header_guard')
72
73 # Use the strictest verbosity level for cpplint.py (level 1) which is the
74 # default when running cpplint.py from command line.
75 # To make it possible to work with not-yet-converted code, we're only applying
mflodman@webrtc.org2a452092012-07-01 05:55:23 +000076 # it to new (or moved/renamed) files and files listed in LINT_FOLDERS.
kjellander@webrtc.org51198f12012-02-21 17:53:46 +000077 verbosity_level = 1
78 files = []
79 for f in input_api.AffectedSourceFiles(source_file_filter):
80 # Note that moved/renamed files also count as added for svn.
phoglund@webrtc.org5d3713932013-03-07 09:59:43 +000081 if (f.Action() == 'A'):
kjellander@webrtc.org51198f12012-02-21 17:53:46 +000082 files.append(f.AbsoluteLocalPath())
mflodman@webrtc.org2a452092012-07-01 05:55:23 +000083
kjellander@webrtc.org51198f12012-02-21 17:53:46 +000084 for file_name in files:
85 cpplint.ProcessFile(file_name, verbosity_level)
86
87 if cpplint._cpplint_state.error_count > 0:
88 if input_api.is_committing:
89 # TODO(kjellander): Change back to PresubmitError below when we're
90 # confident with the lint settings.
91 res_type = output_api.PresubmitPromptWarning
92 else:
93 res_type = output_api.PresubmitPromptWarning
94 result = [res_type('Changelist failed cpplint.py check.')]
95
96 return result
97
kjellander@webrtc.orge4158642014-08-06 09:11:18 +000098
99def _CheckGypChanges(input_api, output_api):
100 source_file_filter = lambda x: input_api.FilterSourceFile(
101 x, white_list=(r'.+\.(gyp|gypi)$',))
102
103 gyp_files = []
104 for f in input_api.AffectedSourceFiles(source_file_filter):
105 gyp_files.append(f.LocalPath())
106
107 result = []
108 if gyp_files:
109 result.append(output_api.PresubmitNotifyResult(
110 'As you\'re changing GYP files: please make sure corresponding '
111 'BUILD.gn files are also updated.\nChanged GYP files:',
112 items=gyp_files))
113 return result
114
115
andrew@webrtc.org53df1362012-01-26 21:24:23 +0000116def _CommonChecks(input_api, output_api):
117 """Checks common to both upload and commit."""
kjellander@webrtc.org51198f12012-02-21 17:53:46 +0000118 # TODO(kjellander): Use presubmit_canned_checks.PanProjectChecks too.
niklase@google.comda159d62011-05-30 11:51:34 +0000119 results = []
phoglund@webrtc.org5d3713932013-03-07 09:59:43 +0000120 results.extend(input_api.canned_checks.RunPylint(input_api, output_api,
121 black_list=(r'^.*gviz_api\.py$',
122 r'^.*gaeunit\.py$',
fischman@webrtc.org33584f92013-07-25 16:43:30 +0000123 # Embedded shell-script fakes out pylint.
kjellander@webrtc.orge4158642014-08-06 09:11:18 +0000124 r'^build/.*\.py$',
125 r'^buildtools/.*\.py$',
kjellander@webrtc.org89256622014-08-20 12:10:11 +0000126 r'^chromium/.*\.py$',
kjellander@webrtc.orge4158642014-08-06 09:11:18 +0000127 r'^out.*/.*\.py$',
fischman@webrtc.org33584f92013-07-25 16:43:30 +0000128 r'^talk/site_scons/site_tools/talk_linux.py$',
phoglund@webrtc.org5d3713932013-03-07 09:59:43 +0000129 r'^testing/.*\.py$',
kjellander@webrtc.orge4158642014-08-06 09:11:18 +0000130 r'^third_party/.*\.py$',
kjellander@webrtc.orgc7b8b2f2014-04-03 20:19:36 +0000131 r'^tools/clang/.*\.py$',
kjellander@webrtc.orge4158642014-08-06 09:11:18 +0000132 r'^tools/gn/.*\.py$',
phoglund@webrtc.org5d3713932013-03-07 09:59:43 +0000133 r'^tools/gyp/.*\.py$',
134 r'^tools/perf_expectations/.*\.py$',
phoglund@webrtc.org6d07ad92013-05-14 09:42:39 +0000135 r'^tools/protoc_wrapper/.*\.py$',
phoglund@webrtc.org5d3713932013-03-07 09:59:43 +0000136 r'^tools/python/.*\.py$',
137 r'^tools/python_charts/data/.*\.py$',
kjellander@webrtc.org33654222013-08-22 07:57:00 +0000138 r'^tools/refactoring/.*\.py$',
kjellander@webrtc.orgf9bdbe32013-12-11 13:37:12 +0000139 r'^tools/swarming_client/.*\.py$',
phoglund@webrtc.org5d3713932013-03-07 09:59:43 +0000140 # TODO(phoglund): should arguably be checked.
141 r'^tools/valgrind-webrtc/.*\.py$',
142 r'^tools/valgrind/.*\.py$',
143 # TODO(phoglund): should arguably be checked.
144 r'^webrtc/build/.*\.py$',
kjellander@webrtc.orge4158642014-08-06 09:11:18 +0000145 r'^xcodebuild.*/.*\.py$',),
146
phoglund@webrtc.org5d3713932013-03-07 09:59:43 +0000147 disabled_warnings=['F0401', # Failed to import x
148 'E0611', # No package y in x
149 'W0232', # Class has no __init__ method
150 ]))
andrew@webrtc.org2442de12012-01-23 17:45:41 +0000151 results.extend(input_api.canned_checks.CheckLongLines(
pbos@webrtc.orgf2e7bc62013-04-08 15:46:07 +0000152 input_api, output_api, maxlen=80))
andrew@webrtc.org2442de12012-01-23 17:45:41 +0000153 results.extend(input_api.canned_checks.CheckChangeHasNoTabs(
154 input_api, output_api))
andrew@webrtc.org53df1362012-01-26 21:24:23 +0000155 results.extend(input_api.canned_checks.CheckChangeHasNoStrayWhitespace(
156 input_api, output_api))
157 results.extend(input_api.canned_checks.CheckChangeTodoHasOwner(
158 input_api, output_api))
mflodman@webrtc.org2a452092012-07-01 05:55:23 +0000159 results.extend(_CheckApprovedFilesLintClean(input_api, output_api))
kjellander@webrtc.org51198f12012-02-21 17:53:46 +0000160 results.extend(_CheckNoIOStreamInHeaders(input_api, output_api))
161 results.extend(_CheckNoFRIEND_TEST(input_api, output_api))
kjellander@webrtc.orge4158642014-08-06 09:11:18 +0000162 results.extend(_CheckGypChanges(input_api, output_api))
andrew@webrtc.org53df1362012-01-26 21:24:23 +0000163 return results
andrew@webrtc.org2442de12012-01-23 17:45:41 +0000164
kjellander@webrtc.orge4158642014-08-06 09:11:18 +0000165
andrew@webrtc.org53df1362012-01-26 21:24:23 +0000166def CheckChangeOnUpload(input_api, output_api):
167 results = []
168 results.extend(_CommonChecks(input_api, output_api))
niklase@google.comda159d62011-05-30 11:51:34 +0000169 return results
170
kjellander@webrtc.orge4158642014-08-06 09:11:18 +0000171
andrew@webrtc.org2442de12012-01-23 17:45:41 +0000172def CheckChangeOnCommit(input_api, output_api):
niklase@google.com1198db92011-06-09 07:07:24 +0000173 results = []
andrew@webrtc.org53df1362012-01-26 21:24:23 +0000174 results.extend(_CommonChecks(input_api, output_api))
niklase@google.com1198db92011-06-09 07:07:24 +0000175 results.extend(input_api.canned_checks.CheckOwners(input_api, output_api))
andrew@webrtc.org53df1362012-01-26 21:24:23 +0000176 results.extend(input_api.canned_checks.CheckChangeWasUploaded(
177 input_api, output_api))
178 results.extend(input_api.canned_checks.CheckChangeHasDescription(
179 input_api, output_api))
kjellander@webrtc.org51198f12012-02-21 17:53:46 +0000180 results.extend(input_api.canned_checks.CheckChangeHasBugField(
181 input_api, output_api))
182 results.extend(input_api.canned_checks.CheckChangeHasTestField(
183 input_api, output_api))
kjellander@webrtc.org12cb88c2014-02-13 11:53:43 +0000184 results.extend(input_api.canned_checks.CheckTreeIsOpen(
185 input_api, output_api,
186 json_url='http://webrtc-status.appspot.com/current?format=json'))
niklase@google.com1198db92011-06-09 07:07:24 +0000187 return results
kjellander@webrtc.org85759802013-10-22 16:47:40 +0000188
kjellander@webrtc.orge4158642014-08-06 09:11:18 +0000189
kjellander@webrtc.orgc7b8b2f2014-04-03 20:19:36 +0000190def GetDefaultTryConfigs(bots=None):
191 """Returns a list of ('bot', set(['tests']), optionally filtered by [bots].
192
193 For WebRTC purposes, we always return an empty list of tests, since we want
194 to run all tests by default on all our trybots.
195 """
196 return { 'tryserver.webrtc': dict((bot, []) for bot in bots)}
197
kjellander@webrtc.orge4158642014-08-06 09:11:18 +0000198
kjellander@webrtc.org85759802013-10-22 16:47:40 +0000199# pylint: disable=W0613
kjellander@webrtc.orgc7b8b2f2014-04-03 20:19:36 +0000200def GetPreferredTryMasters(project, change):
kjellander@webrtc.org85759802013-10-22 16:47:40 +0000201 files = change.LocalPaths()
202
kjellander@webrtc.orge4158642014-08-06 09:11:18 +0000203 android_gn_bots = [
204 'android_gn',
205 'android_gn_rel',
206 ]
kjellander@webrtc.orgcf2b3ac2013-12-20 21:20:42 +0000207 android_bots = [
208 'android',
kjellander@webrtc.org9359eda2014-06-08 17:55:51 +0000209 'android_arm64',
kjellander@webrtc.org2a260d92014-01-27 16:08:43 +0000210 'android_apk',
211 'android_apk_rel',
kjellander@webrtc.orgcf2b3ac2013-12-20 21:20:42 +0000212 'android_rel',
213 'android_clang',
kjellander@webrtc.orge4158642014-08-06 09:11:18 +0000214 ] + android_gn_bots
kjellander@webrtc.org85759802013-10-22 16:47:40 +0000215 ios_bots = [
216 'ios',
217 'ios_rel',
218 ]
kjellander@webrtc.orge4158642014-08-06 09:11:18 +0000219 linux_gn_bots = [
220 'linux_gn',
221 'linux_gn_rel',
222 ]
kjellander@webrtc.org85759802013-10-22 16:47:40 +0000223 linux_bots = [
224 'linux',
225 'linux_asan',
kjellander@webrtc.org570bc3d2014-01-22 19:00:01 +0000226 'linux_baremetal',
kjellander@webrtc.org85759802013-10-22 16:47:40 +0000227 'linux_memcheck',
228 'linux_rel',
kjellander@webrtc.orgc7b8b2f2014-04-03 20:19:36 +0000229 'linux_tsan2',
kjellander@webrtc.orge4158642014-08-06 09:11:18 +0000230 ] + linux_gn_bots
kjellander@webrtc.org85759802013-10-22 16:47:40 +0000231 mac_bots = [
232 'mac',
233 'mac_asan',
kjellander@webrtc.org570bc3d2014-01-22 19:00:01 +0000234 'mac_baremetal',
kjellander@webrtc.org85759802013-10-22 16:47:40 +0000235 'mac_rel',
236 'mac_x64_rel',
237 ]
238 win_bots = [
239 'win',
kjellander@webrtc.orgc7b8b2f2014-04-03 20:19:36 +0000240 'win_asan',
kjellander@webrtc.org570bc3d2014-01-22 19:00:01 +0000241 'win_baremetal',
kjellander@webrtc.orga956ec22014-04-14 08:38:27 +0000242 'win_drmemory_light',
kjellander@webrtc.org85759802013-10-22 16:47:40 +0000243 'win_rel',
244 'win_x64_rel',
245 ]
kjellander@webrtc.org85759802013-10-22 16:47:40 +0000246 if not files or all(re.search(r'[\\/]OWNERS$', f) for f in files):
kjellander@webrtc.orgc7b8b2f2014-04-03 20:19:36 +0000247 return {}
kjellander@webrtc.orge4158642014-08-06 09:11:18 +0000248 if all(re.search(r'[\\/]BUILD.gn$', f) for f in files):
249 return GetDefaultTryConfigs(android_gn_bots + linux_gn_bots)
kjellander@webrtc.org85759802013-10-22 16:47:40 +0000250 if all(re.search('\.(m|mm)$|(^|[/_])mac[/_.]', f) for f in files):
kjellander@webrtc.orgc7b8b2f2014-04-03 20:19:36 +0000251 return GetDefaultTryConfigs(mac_bots)
kjellander@webrtc.org85759802013-10-22 16:47:40 +0000252 if all(re.search('(^|[/_])win[/_.]', f) for f in files):
kjellander@webrtc.orgc7b8b2f2014-04-03 20:19:36 +0000253 return GetDefaultTryConfigs(win_bots)
254 if all(re.search('(^|[/_])android[/_.]', f) for f in files):
255 return GetDefaultTryConfigs(android_bots)
256 if all(re.search('[/_]ios[/_.]', f) for f in files):
257 return GetDefaultTryConfigs(ios_bots)
kjellander@webrtc.org85759802013-10-22 16:47:40 +0000258
kjellander@webrtc.orgc7b8b2f2014-04-03 20:19:36 +0000259 return GetDefaultTryConfigs(android_bots + ios_bots + linux_bots + mac_bots +
260 win_bots)