1 # 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
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.
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.
28 """Top-level presubmit script for V8.
30 See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
31 for more details about the presubmit API built into gcl.
40 r"^third_party[\\\/].*",
45 # Regular expression that matches code only used for test binaries
47 _TEST_CODE_EXCLUDED_PATHS = (
49 # Has a method VisitForTest().
50 r'src[\\\/]compiler[\\\/]ast-graph-builder\.cc',
52 r'src[\\\/]extensions[\\\/]gc-extension\.cc',
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.')
63 def _V8PresubmitChecks(input_api, output_api):
64 """Runs the V8 presubmit checks."""
66 sys.path.append(input_api.os_path.join(
67 input_api.PresubmitLocalPath(), 'tools'))
68 from presubmit import CppLintProcessor
69 from presubmit import SourceProcessor
70 from presubmit import CheckRuntimeVsNativesNameClashes
71 from presubmit import CheckExternalReferenceRegistration
72 from presubmit import CheckAuthorizedAuthor
75 if not CppLintProcessor().Run(input_api.PresubmitLocalPath()):
76 results.append(output_api.PresubmitError("C++ lint check failed"))
77 if not SourceProcessor().Run(input_api.PresubmitLocalPath()):
78 results.append(output_api.PresubmitError(
79 "Copyright header, trailing whitespaces and two empty lines " \
80 "between declarations check failed"))
81 if not CheckRuntimeVsNativesNameClashes(input_api.PresubmitLocalPath()):
82 results.append(output_api.PresubmitError(
83 "Runtime/natives name clash check failed"))
84 if not CheckExternalReferenceRegistration(input_api.PresubmitLocalPath()):
85 results.append(output_api.PresubmitError(
86 "External references registration check failed"))
87 results.extend(CheckAuthorizedAuthor(input_api, output_api))
91 def _CheckUnwantedDependencies(input_api, output_api):
92 """Runs checkdeps on #include statements added in this
93 change. Breaking - rules is an error, breaking ! rules is a
96 # We need to wait until we have an input_api object and use this
97 # roundabout construct to import checkdeps because this file is
98 # eval-ed and thus doesn't have __file__.
99 original_sys_path = sys.path
101 sys.path = sys.path + [input_api.os_path.join(
102 input_api.PresubmitLocalPath(), 'buildtools', 'checkdeps')]
104 from cpp_checker import CppChecker
105 from rules import Rule
107 # Restore sys.path to what it was before.
108 sys.path = original_sys_path
111 for f in input_api.AffectedFiles():
112 if not CppChecker.IsCppFile(f.LocalPath()):
115 changed_lines = [line for line_num, line in f.ChangedContents()]
116 added_includes.append([f.LocalPath(), changed_lines])
118 deps_checker = checkdeps.DepsChecker(input_api.PresubmitLocalPath())
120 error_descriptions = []
121 warning_descriptions = []
122 for path, rule_type, rule_description in deps_checker.CheckAddedCppIncludes(
124 description_with_path = '%s\n %s' % (path, rule_description)
125 if rule_type == Rule.DISALLOW:
126 error_descriptions.append(description_with_path)
128 warning_descriptions.append(description_with_path)
131 if error_descriptions:
132 results.append(output_api.PresubmitError(
133 'You added one or more #includes that violate checkdeps rules.',
135 if warning_descriptions:
136 results.append(output_api.PresubmitPromptOrNotify(
137 'You added one or more #includes of files that are temporarily\n'
138 'allowed but being removed. Can you avoid introducing the\n'
139 '#include? See relevant DEPS file(s) for details and contacts.',
140 warning_descriptions))
144 def _CheckNoProductionCodeUsingTestOnlyFunctions(input_api, output_api):
145 """Attempts to prevent use of functions intended only for testing in
146 non-testing code. For now this is just a best-effort implementation
147 that ignores header files and may have some false positives. A
148 better implementation would probably need a proper C++ parser.
150 # We only scan .cc files, as the declaration of for-testing functions in
151 # header files are hard to distinguish from calls to such functions without a
153 file_inclusion_pattern = r'.+\.cc'
155 base_function_pattern = r'[ :]test::[^\s]+|ForTest(ing)?|for_test(ing)?'
156 inclusion_pattern = input_api.re.compile(r'(%s)\s*\(' % base_function_pattern)
157 comment_pattern = input_api.re.compile(r'//.*(%s)' % base_function_pattern)
158 exclusion_pattern = input_api.re.compile(
159 r'::[A-Za-z0-9_]+(%s)|(%s)[^;]+\{' % (
160 base_function_pattern, base_function_pattern))
162 def FilterFile(affected_file):
163 black_list = (_EXCLUDED_PATHS +
164 _TEST_CODE_EXCLUDED_PATHS +
165 input_api.DEFAULT_BLACK_LIST)
166 return input_api.FilterSourceFile(
168 white_list=(file_inclusion_pattern, ),
169 black_list=black_list)
172 for f in input_api.AffectedSourceFiles(FilterFile):
173 local_path = f.LocalPath()
174 for line_number, line in f.ChangedContents():
175 if (inclusion_pattern.search(line) and
176 not comment_pattern.search(line) and
177 not exclusion_pattern.search(line)):
179 '%s:%d\n %s' % (local_path, line_number, line.strip()))
182 return [output_api.PresubmitPromptOrNotify(_TEST_ONLY_WARNING, problems)]
187 def _CommonChecks(input_api, output_api):
188 """Checks common to both upload and commit."""
190 results.extend(input_api.canned_checks.CheckOwners(
191 input_api, output_api, source_file_filter=None))
192 results.extend(input_api.canned_checks.CheckPatchFormatted(
193 input_api, output_api))
194 results.extend(_V8PresubmitChecks(input_api, output_api))
195 results.extend(_CheckUnwantedDependencies(input_api, output_api))
197 _CheckNoProductionCodeUsingTestOnlyFunctions(input_api, output_api))
201 def _SkipTreeCheck(input_api, output_api):
202 """Check the env var whether we want to skip tree check.
203 Only skip if include/v8-version.h has been updated."""
204 src_version = 'include/v8-version.h'
205 FilterFile = lambda file: file.LocalPath() == src_version
206 if not input_api.AffectedSourceFiles(
207 lambda file: file.LocalPath() == src_version):
209 return input_api.environ.get('PRESUBMIT_TREE_CHECK') == 'skip'
212 def _CheckChangeLogFlag(input_api, output_api):
213 """Checks usage of LOG= flag in the commit message."""
215 if input_api.change.BUG and not 'LOG' in input_api.change.tags:
216 results.append(output_api.PresubmitError(
217 'An issue reference (BUG=) requires a change log flag (LOG=). '
218 'Use LOG=Y for including this commit message in the change log. '
219 'Use LOG=N or leave blank otherwise.'))
223 def CheckChangeOnUpload(input_api, output_api):
225 results.extend(_CommonChecks(input_api, output_api))
226 results.extend(_CheckChangeLogFlag(input_api, output_api))
230 def CheckChangeOnCommit(input_api, output_api):
232 results.extend(_CommonChecks(input_api, output_api))
233 results.extend(_CheckChangeLogFlag(input_api, output_api))
234 results.extend(input_api.canned_checks.CheckChangeHasDescription(
235 input_api, output_api))
236 if not _SkipTreeCheck(input_api, output_api):
237 results.extend(input_api.canned_checks.CheckTreeIsOpen(
238 input_api, output_api,
239 json_url='http://v8-status.appspot.com/current?format=json'))
243 def GetPreferredTryMasters(project, change):
246 'v8_linux_rel': set(['defaulttests']),
247 'v8_linux_dbg': set(['defaulttests']),
248 'v8_linux_nodcheck_rel': set(['defaulttests']),
249 'v8_linux_gcc_compile_rel': set(['defaulttests']),
250 'v8_linux64_rel': set(['defaulttests']),
251 'v8_linux64_asan_rel': set(['defaulttests']),
252 'v8_linux64_avx2_rel': set(['defaulttests']),
253 'v8_win_rel': set(['defaulttests']),
254 'v8_win_compile_dbg': set(['defaulttests']),
255 'v8_win_nosnap_shared_compile_rel': set(['defaulttests']),
256 'v8_win64_rel': set(['defaulttests']),
257 'v8_mac_rel': set(['defaulttests']),
258 'v8_linux_arm_rel': set(['defaulttests']),
259 'v8_linux_arm64_rel': set(['defaulttests']),
260 'v8_linux_mipsel_compile_rel': set(['defaulttests']),
261 'v8_linux_mips64el_compile_rel': set(['defaulttests']),
262 'v8_android_arm_compile_rel': set(['defaulttests']),
263 'v8_linux_chromium_gn_rel': set(['defaulttests']),