blob: 3a9895db8df02806d2af560970041d9b2c60d323 [file] [log] [blame]
verwaest@chromium.org33e09c82012-10-10 17:07:22 +00001# Copyright 2012 the V8 project authors. All rights reserved.
2# Redistribution and use in source and binary forms, with or without
3# modification, are permitted provided that the following conditions are
4# met:
5#
6# * Redistributions of source code must retain the above copyright
7# notice, this list of conditions and the following disclaimer.
8# * Redistributions in binary form must reproduce the above
9# copyright notice, this list of conditions and the following
10# disclaimer in the documentation and/or other materials provided
11# with the distribution.
12# * Neither the name of Google Inc. nor the names of its
13# contributors may be used to endorse or promote products derived
14# from this software without specific prior written permission.
15#
16# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28"""Top-level presubmit script for V8.
29
30See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
31for more details about the presubmit API built into gcl.
32"""
33
machenbach@chromium.org196eb602014-06-04 00:06:13 +000034import sys
35
36
machenbach@chromium.org06b26962014-09-24 00:05:07 +000037_EXCLUDED_PATHS = (
38 r"^test[\\\/].*",
39 r"^testing[\\\/].*",
40 r"^third_party[\\\/].*",
41 r"^tools[\\\/].*",
42)
43
44
45# Regular expression that matches code only used for test binaries
46# (best effort).
47_TEST_CODE_EXCLUDED_PATHS = (
48 r'.+-unittest\.cc',
49 # Has a method VisitForTest().
50 r'src[\\\/]compiler[\\\/]ast-graph-builder\.cc',
51 # Test extension.
52 r'src[\\\/]extensions[\\\/]gc-extension\.cc',
53)
54
55
56_TEST_ONLY_WARNING = (
57 'You might be calling functions intended only for testing from\n'
58 'production code. It is OK to ignore this warning if you know what\n'
59 'you are doing, as the heuristics used to detect the situation are\n'
60 'not perfect. The commit queue will not block on this warning.')
61
62
verwaest@chromium.org33e09c82012-10-10 17:07:22 +000063def _V8PresubmitChecks(input_api, output_api):
64 """Runs the V8 presubmit checks."""
65 import sys
66 sys.path.append(input_api.os_path.join(
67 input_api.PresubmitLocalPath(), 'tools'))
68 from presubmit import CppLintProcessor
69 from presubmit import SourceProcessor
machenbach@chromium.org2c81ceb2014-09-11 00:05:22 +000070 from presubmit import CheckRuntimeVsNativesNameClashes
machenbach@chromium.org9d72b8d2014-08-07 08:39:21 +000071 from presubmit import CheckExternalReferenceRegistration
verwaest@chromium.org33e09c82012-10-10 17:07:22 +000072
73 results = []
74 if not CppLintProcessor().Run(input_api.PresubmitLocalPath()):
75 results.append(output_api.PresubmitError("C++ lint check failed"))
76 if not SourceProcessor().Run(input_api.PresubmitLocalPath()):
77 results.append(output_api.PresubmitError(
mstarzinger@chromium.orge0e1b0d2013-07-08 08:38:06 +000078 "Copyright header, trailing whitespaces and two empty lines " \
79 "between declarations check failed"))
machenbach@chromium.org2c81ceb2014-09-11 00:05:22 +000080 if not CheckRuntimeVsNativesNameClashes(input_api.PresubmitLocalPath()):
machenbach@chromium.org08e75692014-06-25 00:05:37 +000081 results.append(output_api.PresubmitError(
machenbach@chromium.org2c81ceb2014-09-11 00:05:22 +000082 "Runtime/natives name clash check failed"))
machenbach@chromium.org9d72b8d2014-08-07 08:39:21 +000083 if not CheckExternalReferenceRegistration(input_api.PresubmitLocalPath()):
84 results.append(output_api.PresubmitError(
85 "External references registration check failed"))
verwaest@chromium.org33e09c82012-10-10 17:07:22 +000086 return results
87
88
machenbach@chromium.org196eb602014-06-04 00:06:13 +000089def _CheckUnwantedDependencies(input_api, output_api):
90 """Runs checkdeps on #include statements added in this
91 change. Breaking - rules is an error, breaking ! rules is a
92 warning.
93 """
94 # We need to wait until we have an input_api object and use this
95 # roundabout construct to import checkdeps because this file is
96 # eval-ed and thus doesn't have __file__.
97 original_sys_path = sys.path
98 try:
99 sys.path = sys.path + [input_api.os_path.join(
100 input_api.PresubmitLocalPath(), 'buildtools', 'checkdeps')]
101 import checkdeps
102 from cpp_checker import CppChecker
103 from rules import Rule
104 finally:
105 # Restore sys.path to what it was before.
106 sys.path = original_sys_path
107
108 added_includes = []
109 for f in input_api.AffectedFiles():
110 if not CppChecker.IsCppFile(f.LocalPath()):
111 continue
112
113 changed_lines = [line for line_num, line in f.ChangedContents()]
114 added_includes.append([f.LocalPath(), changed_lines])
115
116 deps_checker = checkdeps.DepsChecker(input_api.PresubmitLocalPath())
117
118 error_descriptions = []
119 warning_descriptions = []
120 for path, rule_type, rule_description in deps_checker.CheckAddedCppIncludes(
121 added_includes):
122 description_with_path = '%s\n %s' % (path, rule_description)
123 if rule_type == Rule.DISALLOW:
124 error_descriptions.append(description_with_path)
125 else:
126 warning_descriptions.append(description_with_path)
127
128 results = []
129 if error_descriptions:
130 results.append(output_api.PresubmitError(
131 'You added one or more #includes that violate checkdeps rules.',
132 error_descriptions))
133 if warning_descriptions:
134 results.append(output_api.PresubmitPromptOrNotify(
135 'You added one or more #includes of files that are temporarily\n'
136 'allowed but being removed. Can you avoid introducing the\n'
137 '#include? See relevant DEPS file(s) for details and contacts.',
138 warning_descriptions))
139 return results
140
141
machenbach@chromium.org06b26962014-09-24 00:05:07 +0000142def _CheckNoProductionCodeUsingTestOnlyFunctions(input_api, output_api):
143 """Attempts to prevent use of functions intended only for testing in
144 non-testing code. For now this is just a best-effort implementation
145 that ignores header files and may have some false positives. A
146 better implementation would probably need a proper C++ parser.
147 """
148 # We only scan .cc files, as the declaration of for-testing functions in
149 # header files are hard to distinguish from calls to such functions without a
150 # proper C++ parser.
151 file_inclusion_pattern = r'.+\.cc'
152
153 base_function_pattern = r'[ :]test::[^\s]+|ForTest(ing)?|for_test(ing)?'
154 inclusion_pattern = input_api.re.compile(r'(%s)\s*\(' % base_function_pattern)
155 comment_pattern = input_api.re.compile(r'//.*(%s)' % base_function_pattern)
156 exclusion_pattern = input_api.re.compile(
157 r'::[A-Za-z0-9_]+(%s)|(%s)[^;]+\{' % (
158 base_function_pattern, base_function_pattern))
159
160 def FilterFile(affected_file):
161 black_list = (_EXCLUDED_PATHS +
162 _TEST_CODE_EXCLUDED_PATHS +
163 input_api.DEFAULT_BLACK_LIST)
164 return input_api.FilterSourceFile(
165 affected_file,
166 white_list=(file_inclusion_pattern, ),
167 black_list=black_list)
168
169 problems = []
170 for f in input_api.AffectedSourceFiles(FilterFile):
171 local_path = f.LocalPath()
172 for line_number, line in f.ChangedContents():
173 if (inclusion_pattern.search(line) and
174 not comment_pattern.search(line) and
175 not exclusion_pattern.search(line)):
176 problems.append(
177 '%s:%d\n %s' % (local_path, line_number, line.strip()))
178
179 if problems:
180 return [output_api.PresubmitPromptOrNotify(_TEST_ONLY_WARNING, problems)]
181 else:
182 return []
183
184
verwaest@chromium.org33e09c82012-10-10 17:07:22 +0000185def _CommonChecks(input_api, output_api):
186 """Checks common to both upload and commit."""
187 results = []
188 results.extend(input_api.canned_checks.CheckOwners(
189 input_api, output_api, source_file_filter=None))
machenbach@chromium.orgd0bddc62014-07-07 00:05:07 +0000190 results.extend(input_api.canned_checks.CheckPatchFormatted(
191 input_api, output_api))
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000192 results.extend(_V8PresubmitChecks(input_api, output_api))
machenbach@chromium.org196eb602014-06-04 00:06:13 +0000193 results.extend(_CheckUnwantedDependencies(input_api, output_api))
machenbach@chromium.org06b26962014-09-24 00:05:07 +0000194 results.extend(
195 _CheckNoProductionCodeUsingTestOnlyFunctions(input_api, output_api))
verwaest@chromium.org33e09c82012-10-10 17:07:22 +0000196 return results
197
198
machenbach@chromium.org3d079fe2013-09-25 08:19:55 +0000199def _SkipTreeCheck(input_api, output_api):
200 """Check the env var whether we want to skip tree check.
201 Only skip if src/version.cc has been updated."""
202 src_version = 'src/version.cc'
machenbach@chromium.org4ddd2f12014-01-14 08:13:44 +0000203 FilterFile = lambda file: file.LocalPath() == src_version
204 if not input_api.AffectedSourceFiles(
205 lambda file: file.LocalPath() == src_version):
machenbach@chromium.org3d079fe2013-09-25 08:19:55 +0000206 return False
207 return input_api.environ.get('PRESUBMIT_TREE_CHECK') == 'skip'
208
209
machenbach@chromium.orgb5be0a92013-11-15 10:32:41 +0000210def _CheckChangeLogFlag(input_api, output_api):
211 """Checks usage of LOG= flag in the commit message."""
212 results = []
213 if input_api.change.BUG and not 'LOG' in input_api.change.tags:
214 results.append(output_api.PresubmitError(
215 'An issue reference (BUG=) requires a change log flag (LOG=). '
216 'Use LOG=Y for including this commit message in the change log. '
217 'Use LOG=N or leave blank otherwise.'))
218 return results
219
220
verwaest@chromium.org33e09c82012-10-10 17:07:22 +0000221def CheckChangeOnUpload(input_api, output_api):
222 results = []
223 results.extend(_CommonChecks(input_api, output_api))
machenbach@chromium.orgb5be0a92013-11-15 10:32:41 +0000224 results.extend(_CheckChangeLogFlag(input_api, output_api))
verwaest@chromium.org33e09c82012-10-10 17:07:22 +0000225 return results
226
227
228def CheckChangeOnCommit(input_api, output_api):
229 results = []
230 results.extend(_CommonChecks(input_api, output_api))
machenbach@chromium.orgb5be0a92013-11-15 10:32:41 +0000231 results.extend(_CheckChangeLogFlag(input_api, output_api))
verwaest@chromium.org33e09c82012-10-10 17:07:22 +0000232 results.extend(input_api.canned_checks.CheckChangeHasDescription(
233 input_api, output_api))
machenbach@chromium.org3d079fe2013-09-25 08:19:55 +0000234 if not _SkipTreeCheck(input_api, output_api):
235 results.extend(input_api.canned_checks.CheckTreeIsOpen(
236 input_api, output_api,
237 json_url='http://v8-status.appspot.com/current?format=json'))
verwaest@chromium.org33e09c82012-10-10 17:07:22 +0000238 return results
titzer@chromium.orgf5a24542014-03-04 09:06:17 +0000239
240
241def GetPreferredTryMasters(project, change):
242 return {
243 'tryserver.v8': {
machenbach@chromium.org63a7c9f2014-04-01 00:04:36 +0000244 'v8_linux_rel': set(['defaulttests']),
machenbach@chromium.orged1a6312014-04-02 00:05:15 +0000245 'v8_linux_dbg': set(['defaulttests']),
246 'v8_linux_nosnap_rel': set(['defaulttests']),
247 'v8_linux_nosnap_dbg': set(['defaulttests']),
248 'v8_linux64_rel': set(['defaulttests']),
249 'v8_linux_arm_dbg': set(['defaulttests']),
250 'v8_linux_arm64_rel': set(['defaulttests']),
machenbach@chromium.orgaa107b22014-05-15 00:04:44 +0000251 'v8_linux_layout_dbg': set(['defaulttests']),
titzer@chromium.orgf5a24542014-03-04 09:06:17 +0000252 'v8_mac_rel': set(['defaulttests']),
253 'v8_win_rel': set(['defaulttests']),
machenbach@chromium.org9e2b4662014-09-16 07:50:38 +0000254 'v8_win64_compile_rel': set(['defaulttests']),
titzer@chromium.orgf5a24542014-03-04 09:06:17 +0000255 },
256 }