1 # Copyright 2011 The Chromium Authors
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
5 """Presubmit script for changes affecting chrome/
7 See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
8 for more details about the presubmit API built into depot_tools.
14 INCLUDE_CPP_FILES_ONLY = (
18 INCLUDE_SOURCE_FILES_ONLY = (
19 r'.*\.(c|cc|cpp|h|m|mm)$',
23 # Objective C confuses everything.
27 # All the messages files do weird multiple include trickery
29 # Autogenerated window resources files are off limits
33 # Has safe printf usage that cpplint complains about
34 r'safe_browsing_util\.cc$',
37 def _CheckChangeLintsClean(input_api, output_api):
38 """Makes sure that the chrome/ code is cpplint clean."""
39 files_to_skip = input_api.DEFAULT_FILES_TO_SKIP + EXCLUDE
40 sources = lambda x: input_api.FilterSourceFile(
41 x, files_to_check=INCLUDE_CPP_FILES_ONLY, files_to_skip=files_to_skip)
42 return input_api.canned_checks.CheckChangeLintsClean(
43 input_api, output_api, sources)
46 def _CheckNoContentUnitTestsInChrome(input_api, output_api):
47 """Makes sure that no unit tests from content/ are included in unit_tests."""
49 for f in input_api.AffectedFiles():
50 if not f.LocalPath().endswith('BUILD.gn'):
53 for line_num, line in f.ChangedContents():
54 m = re.search(r"'(.*\/content\/.*unittest.*)'", line)
56 problems.append(m.group(1))
60 return [output_api.PresubmitPromptWarning(
61 'Unit tests located in content/ should be added to the ' +
62 'content_unittests target.',
66 def _CheckNoIsAppleBuildFlagsInChromeFile(input_api, f):
67 """Check for IS_APPLE in a given file in chrome/."""
68 preprocessor_statement = input_api.re.compile(r'^\s*#')
69 apple_buildflag = input_api.re.compile(r'BUILDFLAG\(IS_APPLE\)')
71 for lnum, line in f.ChangedContents():
72 if preprocessor_statement.search(line) and apple_buildflag.search(line):
73 results.append(' %s:%d' % (f.LocalPath(), lnum))
78 def _CheckNoIsAppleBuildFlagsInChrome(input_api, output_api):
79 """Check for IS_APPLE which isn't used in chrome/."""
81 def SourceFilter(affected_file):
82 return input_api.FilterSourceFile(affected_file, INCLUDE_SOURCE_FILES_ONLY,
83 input_api.DEFAULT_FILES_TO_SKIP)
84 for f in input_api.AffectedSourceFiles(SourceFilter):
85 apple_buildflags.extend(_CheckNoIsAppleBuildFlagsInChromeFile(input_api, f))
87 if not apple_buildflags:
90 return [output_api.PresubmitError(
91 'IS_APPLE is not used in chrome/ but found in:\n', apple_buildflags)]
94 def _CheckNoIsIOSBuildFlagsInChromeFile(input_api, f):
95 """Check for IS_IOS in a given file in chrome/."""
96 preprocessor_statement = input_api.re.compile(r'^\s*#')
97 ios_buildflag = input_api.re.compile(r'BUILDFLAG\(IS_IOS\)')
99 for lnum, line in f.ChangedContents():
100 if preprocessor_statement.search(line) and ios_buildflag.search(line):
101 results.append(' %s:%d' % (f.LocalPath(), lnum))
106 def _CheckNoIsIOSBuildFlagsInChrome(input_api, output_api):
107 """Check for IS_IOS which isn't used in chrome/."""
109 def SourceFilter(affected_file):
110 return input_api.FilterSourceFile(affected_file, INCLUDE_SOURCE_FILES_ONLY,
111 input_api.DEFAULT_FILES_TO_SKIP)
112 for f in input_api.AffectedSourceFiles(SourceFilter):
113 ios_buildflags.extend(_CheckNoIsIOSBuildFlagsInChromeFile(input_api, f))
115 if not ios_buildflags:
118 return [output_api.PresubmitError(
119 'IS_IOS is not used in chrome/ but found in:\n', ios_buildflags)]
122 def _CheckBreakingInstallerVersionBumpNeeded(input_api, output_api):
124 breaking_version_installer_updated = False
126 def _FilterFile(affected_file):
127 return input_api.FilterSourceFile(
129 files_to_check=input_api.DEFAULT_FILES_TO_CHECK + (r'.*\.release',))
130 for f in input_api.AffectedSourceFiles(_FilterFile):
131 # Normalize the local path to Linux-style path separators so that the path
132 # comparisons work on Windows as well.
133 local_path = f.LocalPath().replace('\\', '/')
134 breaking_version_installer_updated |= (local_path ==
135 'chrome/installer/setup/last_breaking_installer_version.cc')
136 if (local_path == 'chrome/installer/mini_installer/chrome.release' or
137 local_path.startswith('chrome/test/mini_installer')):
138 files.append(local_path)
140 if files and not breaking_version_installer_updated:
141 return [output_api.PresubmitPromptWarning('''
142 Update chrome/installer/setup/last_breaking_installer_version.cc if the changes
143 found in the following files might break make downgrades not possible beyond
144 this browser's version.''', items=files)]
146 if not files and breaking_version_installer_updated:
147 return [output_api.PresubmitPromptWarning('''
148 No installer breaking changes detected but
149 chrome/installer/setup/last_breaking_installer_version.cc was updated. Please
150 update chrome/installer/PRESUBMIT.py if more files need to be watched for
151 breaking installer changes.''')]
156 def _CommonChecks(input_api, output_api):
157 """Checks common to both upload and commit."""
159 results.extend(_CheckNoContentUnitTestsInChrome(input_api, output_api))
160 results.extend(_CheckNoIsAppleBuildFlagsInChrome(input_api, output_api))
161 results.extend(_CheckNoIsIOSBuildFlagsInChrome(input_api, output_api))
162 results.extend(_CheckBreakingInstallerVersionBumpNeeded(input_api,
167 def CheckChangeOnUpload(input_api, output_api):
169 results.extend(_CommonChecks(input_api, output_api))
170 results.extend(_CheckChangeLintsClean(input_api, output_api))
174 def CheckChangeOnCommit(input_api, output_api):
176 results.extend(_CommonChecks(input_api, output_api))