blob: 1f8dec331881bd978f5d0a95b98e9f19c3c7c609 [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
mflodman@webrtc.org2a452092012-07-01 05:55:23 +00009import os.path
10
11# All folders in LINT_FOLDERS will be scanned by cpplint by the presubmit
12# script. Note that subfolders are not included.
13LINT_FOLDERS = ['src/video_engine']
14
andrew@webrtc.org53df1362012-01-26 21:24:23 +000015def _LicenseHeader(input_api):
16 """Returns the license header regexp."""
kjellander@webrtc.org6307dbf2012-08-31 07:07:11 +000017 # Accept any year number from 2011 to the current year
18 current_year = int(input_api.time.strftime('%Y'))
19 allowed_years = (str(s) for s in reversed(xrange(2011, current_year + 1)))
20 years_re = '(' + '|'.join(allowed_years) + ')'
andrew@webrtc.org53df1362012-01-26 21:24:23 +000021 license_header = (
andrew@webrtc.org2442de12012-01-23 17:45:41 +000022 r'.*? Copyright \(c\) %(year)s The WebRTC project authors\. '
23 r'All Rights Reserved\.\n'
24 r'.*?\n'
25 r'.*? Use of this source code is governed by a BSD-style license\n'
26 r'.*? that can be found in the LICENSE file in the root of the source\n'
27 r'.*? tree\. An additional intellectual property rights grant can be '
28 r'found\n'
29 r'.*? in the file PATENTS\. All contributing project authors may\n'
30 r'.*? be found in the AUTHORS file in the root of the source tree\.\n'
31 ) % {
kjellander@webrtc.org6307dbf2012-08-31 07:07:11 +000032 'year': years_re,
andrew@webrtc.org2442de12012-01-23 17:45:41 +000033 }
andrew@webrtc.org53df1362012-01-26 21:24:23 +000034 return license_header
andrew@webrtc.org2442de12012-01-23 17:45:41 +000035
kjellander@webrtc.org51198f12012-02-21 17:53:46 +000036def _CheckNoIOStreamInHeaders(input_api, output_api):
37 """Checks to make sure no .h files include <iostream>."""
38 files = []
39 pattern = input_api.re.compile(r'^#include\s*<iostream>',
40 input_api.re.MULTILINE)
41 for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile):
42 if not f.LocalPath().endswith('.h'):
43 continue
44 contents = input_api.ReadFile(f)
45 if pattern.search(contents):
46 files.append(f)
47
48 if len(files):
49 return [ output_api.PresubmitError(
50 'Do not #include <iostream> in header files, since it inserts static ' +
51 'initialization into every file including the header. Instead, ' +
52 '#include <ostream>. See http://crbug.com/94794',
53 files) ]
54 return []
55
56def _CheckNoFRIEND_TEST(input_api, output_api):
57 """Make sure that gtest's FRIEND_TEST() macro is not used, the
58 FRIEND_TEST_ALL_PREFIXES() macro from testsupport/gtest_prod_util.h should be
59 used instead since that allows for FLAKY_, FAILS_ and DISABLED_ prefixes."""
60 problems = []
61
62 file_filter = lambda f: f.LocalPath().endswith(('.cc', '.h'))
63 for f in input_api.AffectedFiles(file_filter=file_filter):
64 for line_num, line in f.ChangedContents():
65 if 'FRIEND_TEST(' in line:
66 problems.append(' %s:%d' % (f.LocalPath(), line_num))
67
68 if not problems:
69 return []
70 return [output_api.PresubmitPromptWarning('WebRTC\'s code should not use '
71 'gtest\'s FRIEND_TEST() macro. Include testsupport/gtest_prod_util.h and '
72 'use FRIEND_TEST_ALL_PREFIXES() instead.\n' + '\n'.join(problems))]
73
mflodman@webrtc.org2a452092012-07-01 05:55:23 +000074def _IsLintWhitelisted(file_name):
75 """ Checks if a file is whitelisted for lint check."""
76 # TODO(mflodman) Include subfolders in the check.
77 return (os.path.dirname(file_name) in LINT_FOLDERS)
78
79def _CheckApprovedFilesLintClean(input_api, output_api,
80 source_file_filter=None):
81 """Checks that all new or whitelisted .cc and .h files pass cpplint.py.
kjellander@webrtc.org51198f12012-02-21 17:53:46 +000082 This check is based on _CheckChangeLintsClean in
83 depot_tools/presubmit_canned_checks.py but has less filters and only checks
84 added files."""
85 result = []
86
87 # Initialize cpplint.
88 import cpplint
89 # Access to a protected member _XX of a client class
90 # pylint: disable=W0212
91 cpplint._cpplint_state.ResetErrorCounts()
92
93 # Justifications for each filter:
94 #
95 # - build/header_guard : WebRTC coding style says they should be prefixed
96 # with WEBRTC_, which is not possible to configure in
97 # cpplint.py.
98 cpplint._SetFilters('-build/header_guard')
99
100 # Use the strictest verbosity level for cpplint.py (level 1) which is the
101 # default when running cpplint.py from command line.
102 # To make it possible to work with not-yet-converted code, we're only applying
mflodman@webrtc.org2a452092012-07-01 05:55:23 +0000103 # it to new (or moved/renamed) files and files listed in LINT_FOLDERS.
kjellander@webrtc.org51198f12012-02-21 17:53:46 +0000104 verbosity_level = 1
105 files = []
106 for f in input_api.AffectedSourceFiles(source_file_filter):
107 # Note that moved/renamed files also count as added for svn.
mflodman@webrtc.org2a452092012-07-01 05:55:23 +0000108 if (f.Action() == 'A' or _IsLintWhitelisted(f.LocalPath())):
kjellander@webrtc.org51198f12012-02-21 17:53:46 +0000109 files.append(f.AbsoluteLocalPath())
mflodman@webrtc.org2a452092012-07-01 05:55:23 +0000110
kjellander@webrtc.org51198f12012-02-21 17:53:46 +0000111 for file_name in files:
112 cpplint.ProcessFile(file_name, verbosity_level)
113
114 if cpplint._cpplint_state.error_count > 0:
115 if input_api.is_committing:
116 # TODO(kjellander): Change back to PresubmitError below when we're
117 # confident with the lint settings.
118 res_type = output_api.PresubmitPromptWarning
119 else:
120 res_type = output_api.PresubmitPromptWarning
121 result = [res_type('Changelist failed cpplint.py check.')]
122
123 return result
124
andrew@webrtc.org53df1362012-01-26 21:24:23 +0000125def _CommonChecks(input_api, output_api):
126 """Checks common to both upload and commit."""
kjellander@webrtc.org51198f12012-02-21 17:53:46 +0000127 # TODO(kjellander): Use presubmit_canned_checks.PanProjectChecks too.
niklase@google.comda159d62011-05-30 11:51:34 +0000128 results = []
andrew@webrtc.org2442de12012-01-23 17:45:41 +0000129 results.extend(input_api.canned_checks.CheckLongLines(
andrew@webrtc.org53df1362012-01-26 21:24:23 +0000130 input_api, output_api))
andrew@webrtc.org2442de12012-01-23 17:45:41 +0000131 results.extend(input_api.canned_checks.CheckChangeHasNoTabs(
132 input_api, output_api))
andrew@webrtc.org53df1362012-01-26 21:24:23 +0000133 results.extend(input_api.canned_checks.CheckChangeHasNoStrayWhitespace(
134 input_api, output_api))
135 results.extend(input_api.canned_checks.CheckChangeTodoHasOwner(
136 input_api, output_api))
mflodman@webrtc.org2a452092012-07-01 05:55:23 +0000137 results.extend(_CheckApprovedFilesLintClean(input_api, output_api))
andrew@webrtc.org2442de12012-01-23 17:45:41 +0000138 results.extend(input_api.canned_checks.CheckLicense(
andrew@webrtc.org53df1362012-01-26 21:24:23 +0000139 input_api, output_api, _LicenseHeader(input_api)))
kjellander@webrtc.org51198f12012-02-21 17:53:46 +0000140 results.extend(_CheckNoIOStreamInHeaders(input_api, output_api))
141 results.extend(_CheckNoFRIEND_TEST(input_api, output_api))
andrew@webrtc.org53df1362012-01-26 21:24:23 +0000142 return results
andrew@webrtc.org2442de12012-01-23 17:45:41 +0000143
andrew@webrtc.org53df1362012-01-26 21:24:23 +0000144def CheckChangeOnUpload(input_api, output_api):
145 results = []
146 results.extend(_CommonChecks(input_api, output_api))
niklase@google.comda159d62011-05-30 11:51:34 +0000147 return results
148
andrew@webrtc.org2442de12012-01-23 17:45:41 +0000149def CheckChangeOnCommit(input_api, output_api):
niklase@google.com1198db92011-06-09 07:07:24 +0000150 results = []
andrew@webrtc.org53df1362012-01-26 21:24:23 +0000151 results.extend(_CommonChecks(input_api, output_api))
niklase@google.com1198db92011-06-09 07:07:24 +0000152 results.extend(input_api.canned_checks.CheckOwners(input_api, output_api))
andrew@webrtc.org53df1362012-01-26 21:24:23 +0000153 results.extend(input_api.canned_checks.CheckChangeWasUploaded(
154 input_api, output_api))
155 results.extend(input_api.canned_checks.CheckChangeHasDescription(
156 input_api, output_api))
kjellander@webrtc.org51198f12012-02-21 17:53:46 +0000157 results.extend(input_api.canned_checks.CheckChangeHasBugField(
158 input_api, output_api))
159 results.extend(input_api.canned_checks.CheckChangeHasTestField(
160 input_api, output_api))
niklase@google.com1198db92011-06-09 07:07:24 +0000161 return results
kjellander@webrtc.orgf3ffcce2012-10-31 14:52:21 +0000162
163def GetPreferredTrySlaves(project, change):
164 files = change.LocalPaths()
165 if not files:
166 return []
167 # Default trybots to run jobs at (if --bot is not specified).
168 return ['win', 'win_rel', 'mac', 'mac_rel', 'linux', 'linux_rel',
169 'android', 'android_ndk']