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 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 34import sys 35 36 37_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 63def _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 70 from presubmit import CheckRuntimeVsNativesNameClashes 71 from presubmit import CheckExternalReferenceRegistration 72 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( 78 "Copyright header, trailing whitespaces and two empty lines " \ 79 "between declarations check failed")) 80 if not CheckRuntimeVsNativesNameClashes(input_api.PresubmitLocalPath()): 81 results.append(output_api.PresubmitError( 82 "Runtime/natives name clash check failed")) 83 if not CheckExternalReferenceRegistration(input_api.PresubmitLocalPath()): 84 results.append(output_api.PresubmitError( 85 "External references registration check failed")) 86 return results 87 88 89def _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 142def _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 185def _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)) 190 results.extend(input_api.canned_checks.CheckPatchFormatted( 191 input_api, output_api)) 192 results.extend(_V8PresubmitChecks(input_api, output_api)) 193 results.extend(_CheckUnwantedDependencies(input_api, output_api)) 194 results.extend( 195 _CheckNoProductionCodeUsingTestOnlyFunctions(input_api, output_api)) 196 return results 197 198 199def _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' 203 FilterFile = lambda file: file.LocalPath() == src_version 204 if not input_api.AffectedSourceFiles( 205 lambda file: file.LocalPath() == src_version): 206 return False 207 return input_api.environ.get('PRESUBMIT_TREE_CHECK') == 'skip' 208 209 210def _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 221def CheckChangeOnUpload(input_api, output_api): 222 results = [] 223 results.extend(_CommonChecks(input_api, output_api)) 224 results.extend(_CheckChangeLogFlag(input_api, output_api)) 225 return results 226 227 228def CheckChangeOnCommit(input_api, output_api): 229 results = [] 230 results.extend(_CommonChecks(input_api, output_api)) 231 results.extend(_CheckChangeLogFlag(input_api, output_api)) 232 results.extend(input_api.canned_checks.CheckChangeHasDescription( 233 input_api, output_api)) 234 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')) 238 return results 239 240 241def GetPreferredTryMasters(project, change): 242 return { 243 'tryserver.v8': { 244 'v8_linux_rel': set(['defaulttests']), 245 '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']), 251 'v8_linux_layout_dbg': set(['defaulttests']), 252 'v8_mac_rel': set(['defaulttests']), 253 'v8_win_rel': set(['defaulttests']), 254 'v8_win64_compile_rel': set(['defaulttests']), 255 }, 256 } 257