[email protected] | 2fac375 | 2011-11-27 20:56:51 | [diff] [blame] | 1 | # Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 93b2d737 | 2009-11-17 18:54:53 | [diff] [blame] | 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
[email protected] | 5a011450 | 2011-11-29 13:01:24 | [diff] [blame] | 5 | """Presubmit script for changes affecting chrome/ |
| 6 | |
| 7 | See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts |
tfarina | 78bb92f4 | 2015-01-31 00:20:48 | [diff] [blame] | 8 | for more details about the presubmit API built into depot_tools. |
[email protected] | 5a011450 | 2011-11-29 13:01:24 | [diff] [blame] | 9 | """ |
| 10 | |
Dan Harrington | 0e54c59 | 2021-06-02 16:41:29 | [diff] [blame] | 11 | USE_PYTHON3 = True |
| 12 | |
[email protected] | 5a011450 | 2011-11-29 13:01:24 | [diff] [blame] | 13 | import re |
[email protected] | 93b2d737 | 2009-11-17 18:54:53 | [diff] [blame] | 14 | |
| 15 | INCLUDE_CPP_FILES_ONLY = ( |
tfarina | 6aed9ae | 2015-03-08 03:54:07 | [diff] [blame] | 16 | r'.*\.(cc|h)$', |
[email protected] | 93b2d737 | 2009-11-17 18:54:53 | [diff] [blame] | 17 | ) |
| 18 | |
Lei Zhang | 29278b4 | 2017-11-10 22:34:17 | [diff] [blame] | 19 | INCLUDE_SOURCE_FILES_ONLY = ( |
| 20 | r'.*\.(c|cc|cpp|h|m|mm)$', |
| 21 | ) |
| 22 | |
[email protected] | 93b2d737 | 2009-11-17 18:54:53 | [diff] [blame] | 23 | EXCLUDE = ( |
| 24 | # Objective C confuses everything. |
| 25 | r'.*cocoa.*', |
| 26 | r'.*_mac\.(cc|h)$', |
| 27 | r'.*_mac_.*', |
| 28 | # All the messages files do weird multiple include trickery |
[email protected] | 3139e9f | 2013-10-10 21:29:47 | [diff] [blame] | 29 | r'.*_messages.*\.h$', |
[email protected] | 93b2d737 | 2009-11-17 18:54:53 | [diff] [blame] | 30 | # Autogenerated window resources files are off limits |
| 31 | r'.*resource.h$', |
[email protected] | 93b2d737 | 2009-11-17 18:54:53 | [diff] [blame] | 32 | # Header trickery |
| 33 | r'.*-inl\.h$', |
[email protected] | 93b2d737 | 2009-11-17 18:54:53 | [diff] [blame] | 34 | # Has safe printf usage that cpplint complains about |
| 35 | r'safe_browsing_util\.cc$', |
[email protected] | 93b2d737 | 2009-11-17 18:54:53 | [diff] [blame] | 36 | ) |
| 37 | |
[email protected] | 5a011450 | 2011-11-29 13:01:24 | [diff] [blame] | 38 | def _CheckChangeLintsClean(input_api, output_api): |
| 39 | """Makes sure that the chrome/ code is cpplint clean.""" |
Josip Sokcevic | 8b6cc43 | 2020-08-05 17:45:33 | [diff] [blame] | 40 | files_to_skip = input_api.DEFAULT_FILES_TO_SKIP + EXCLUDE |
[email protected] | 93b2d737 | 2009-11-17 18:54:53 | [diff] [blame] | 41 | sources = lambda x: input_api.FilterSourceFile( |
Josip Sokcevic | 8b6cc43 | 2020-08-05 17:45:33 | [diff] [blame] | 42 | x, files_to_check=INCLUDE_CPP_FILES_ONLY, files_to_skip=files_to_skip) |
[email protected] | 5a011450 | 2011-11-29 13:01:24 | [diff] [blame] | 43 | return input_api.canned_checks.CheckChangeLintsClean( |
| 44 | input_api, output_api, sources) |
| 45 | |
Lei Zhang | 29278b4 | 2017-11-10 22:34:17 | [diff] [blame] | 46 | |
[email protected] | 5a011450 | 2011-11-29 13:01:24 | [diff] [blame] | 47 | def _CheckNoContentUnitTestsInChrome(input_api, output_api): |
| 48 | """Makes sure that no unit tests from content/ are included in unit_tests.""" |
| 49 | problems = [] |
| 50 | for f in input_api.AffectedFiles(): |
Lei Zhang | 421a7ac | 2017-11-13 07:16:27 | [diff] [blame] | 51 | if not f.LocalPath().endswith('BUILD.gn'): |
[email protected] | 5a011450 | 2011-11-29 13:01:24 | [diff] [blame] | 52 | continue |
| 53 | |
| 54 | for line_num, line in f.ChangedContents(): |
| 55 | m = re.search(r"'(.*\/content\/.*unittest.*)'", line) |
| 56 | if m: |
| 57 | problems.append(m.group(1)) |
| 58 | |
| 59 | if not problems: |
| 60 | return [] |
| 61 | return [output_api.PresubmitPromptWarning( |
| 62 | 'Unit tests located in content/ should be added to the ' + |
Lei Zhang | 421a7ac | 2017-11-13 07:16:27 | [diff] [blame] | 63 | 'content_unittests target.', |
[email protected] | 5a011450 | 2011-11-29 13:01:24 | [diff] [blame] | 64 | items=problems)] |
| 65 | |
Lei Zhang | 29278b4 | 2017-11-10 22:34:17 | [diff] [blame] | 66 | |
Bruce Dawson | 42c62cf | 2022-04-27 15:29:02 | [diff] [blame] | 67 | def _CheckNoIsAppleBuildFlagsInChromeFile(input_api, f): |
| 68 | """Check for IS_APPLE in a given file in chrome/.""" |
Lei Zhang | a4561e70 | 2020-08-20 17:09:15 | [diff] [blame] | 69 | preprocessor_statement = input_api.re.compile(r'^\s*#') |
Bruce Dawson | 42c62cf | 2022-04-27 15:29:02 | [diff] [blame] | 70 | apple_buildflag = input_api.re.compile(r'BUILDFLAG\(IS_APPLE\)') |
Lei Zhang | a4561e70 | 2020-08-20 17:09:15 | [diff] [blame] | 71 | results = [] |
| 72 | for lnum, line in f.ChangedContents(): |
Bruce Dawson | 42c62cf | 2022-04-27 15:29:02 | [diff] [blame] | 73 | if preprocessor_statement.search(line) and apple_buildflag.search(line): |
Lei Zhang | a4561e70 | 2020-08-20 17:09:15 | [diff] [blame] | 74 | results.append(' %s:%d' % (f.LocalPath(), lnum)) |
| 75 | |
| 76 | return results |
| 77 | |
| 78 | |
Bruce Dawson | 42c62cf | 2022-04-27 15:29:02 | [diff] [blame] | 79 | def _CheckNoIsAppleBuildFlagsInChrome(input_api, output_api): |
| 80 | """Check for IS_APPLE which isn't used in chrome/.""" |
| 81 | apple_buildflags = [] |
Lei Zhang | a4561e70 | 2020-08-20 17:09:15 | [diff] [blame] | 82 | def SourceFilter(affected_file): |
| 83 | return input_api.FilterSourceFile(affected_file, INCLUDE_SOURCE_FILES_ONLY, |
| 84 | input_api.DEFAULT_FILES_TO_SKIP) |
| 85 | for f in input_api.AffectedSourceFiles(SourceFilter): |
Bruce Dawson | 42c62cf | 2022-04-27 15:29:02 | [diff] [blame] | 86 | apple_buildflags.extend(_CheckNoIsAppleBuildFlagsInChromeFile(input_api, f)) |
Lei Zhang | a4561e70 | 2020-08-20 17:09:15 | [diff] [blame] | 87 | |
Bruce Dawson | 42c62cf | 2022-04-27 15:29:02 | [diff] [blame] | 88 | if not apple_buildflags: |
Lei Zhang | a4561e70 | 2020-08-20 17:09:15 | [diff] [blame] | 89 | return [] |
| 90 | |
| 91 | return [output_api.PresubmitError( |
Bruce Dawson | 42c62cf | 2022-04-27 15:29:02 | [diff] [blame] | 92 | 'IS_APPLE is not used in chrome/ but found in:\n', apple_buildflags)] |
Lei Zhang | a4561e70 | 2020-08-20 17:09:15 | [diff] [blame] | 93 | |
| 94 | |
Bruce Dawson | 42c62cf | 2022-04-27 15:29:02 | [diff] [blame] | 95 | def _CheckNoIsIOSBuildFlagsInChromeFile(input_api, f): |
| 96 | """Check for IS_IOS in a given file in chrome/.""" |
Lei Zhang | 29278b4 | 2017-11-10 22:34:17 | [diff] [blame] | 97 | preprocessor_statement = input_api.re.compile(r'^\s*#') |
Bruce Dawson | 42c62cf | 2022-04-27 15:29:02 | [diff] [blame] | 98 | ios_buildflag = input_api.re.compile(r'BUILDFLAG\(IS_IOS\)') |
Lei Zhang | 29278b4 | 2017-11-10 22:34:17 | [diff] [blame] | 99 | results = [] |
| 100 | for lnum, line in f.ChangedContents(): |
Bruce Dawson | 42c62cf | 2022-04-27 15:29:02 | [diff] [blame] | 101 | if preprocessor_statement.search(line) and ios_buildflag.search(line): |
Lei Zhang | 29278b4 | 2017-11-10 22:34:17 | [diff] [blame] | 102 | results.append(' %s:%d' % (f.LocalPath(), lnum)) |
| 103 | |
| 104 | return results |
| 105 | |
| 106 | |
Bruce Dawson | 42c62cf | 2022-04-27 15:29:02 | [diff] [blame] | 107 | def _CheckNoIsIOSBuildFlagsInChrome(input_api, output_api): |
| 108 | """Check for IS_IOS which isn't used in chrome/.""" |
| 109 | ios_buildflags = [] |
Lei Zhang | 29278b4 | 2017-11-10 22:34:17 | [diff] [blame] | 110 | def SourceFilter(affected_file): |
| 111 | return input_api.FilterSourceFile(affected_file, INCLUDE_SOURCE_FILES_ONLY, |
Josip Sokcevic | 8b6cc43 | 2020-08-05 17:45:33 | [diff] [blame] | 112 | input_api.DEFAULT_FILES_TO_SKIP) |
Lei Zhang | 29278b4 | 2017-11-10 22:34:17 | [diff] [blame] | 113 | for f in input_api.AffectedSourceFiles(SourceFilter): |
Bruce Dawson | 42c62cf | 2022-04-27 15:29:02 | [diff] [blame] | 114 | ios_buildflags.extend(_CheckNoIsIOSBuildFlagsInChromeFile(input_api, f)) |
Lei Zhang | 29278b4 | 2017-11-10 22:34:17 | [diff] [blame] | 115 | |
Bruce Dawson | 42c62cf | 2022-04-27 15:29:02 | [diff] [blame] | 116 | if not ios_buildflags: |
Lei Zhang | 29278b4 | 2017-11-10 22:34:17 | [diff] [blame] | 117 | return [] |
| 118 | |
| 119 | return [output_api.PresubmitError( |
Bruce Dawson | 42c62cf | 2022-04-27 15:29:02 | [diff] [blame] | 120 | 'IS_IOS is not used in chrome/ but found in:\n', ios_buildflags)] |
Lei Zhang | 29278b4 | 2017-11-10 22:34:17 | [diff] [blame] | 121 | |
| 122 | |
[email protected] | 5a011450 | 2011-11-29 13:01:24 | [diff] [blame] | 123 | def _CommonChecks(input_api, output_api): |
| 124 | """Checks common to both upload and commit.""" |
| 125 | results = [] |
| 126 | results.extend(_CheckNoContentUnitTestsInChrome(input_api, output_api)) |
Bruce Dawson | 42c62cf | 2022-04-27 15:29:02 | [diff] [blame] | 127 | results.extend(_CheckNoIsAppleBuildFlagsInChrome(input_api, output_api)) |
| 128 | results.extend(_CheckNoIsIOSBuildFlagsInChrome(input_api, output_api)) |
[email protected] | 5a011450 | 2011-11-29 13:01:24 | [diff] [blame] | 129 | return results |
| 130 | |
Lei Zhang | 29278b4 | 2017-11-10 22:34:17 | [diff] [blame] | 131 | |
[email protected] | 5a011450 | 2011-11-29 13:01:24 | [diff] [blame] | 132 | def CheckChangeOnUpload(input_api, output_api): |
| 133 | results = [] |
| 134 | results.extend(_CommonChecks(input_api, output_api)) |
| 135 | results.extend(_CheckChangeLintsClean(input_api, output_api)) |
| 136 | return results |
| 137 | |
Lei Zhang | 29278b4 | 2017-11-10 22:34:17 | [diff] [blame] | 138 | |
[email protected] | 5a011450 | 2011-11-29 13:01:24 | [diff] [blame] | 139 | def CheckChangeOnCommit(input_api, output_api): |
| 140 | results = [] |
| 141 | results.extend(_CommonChecks(input_api, output_api)) |
[email protected] | 93b2d737 | 2009-11-17 18:54:53 | [diff] [blame] | 142 | return results |