blob: 1c3a7bc881ec22f7a2ea79a8c598535e0e53fe18 [file] [log] [blame]
[email protected]2fac3752011-11-27 20:56:511# Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]93b2d7372009-11-17 18:54:532# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
[email protected]5a0114502011-11-29 13:01:245"""Presubmit script for changes affecting chrome/
6
7See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
tfarina78bb92f42015-01-31 00:20:488for more details about the presubmit API built into depot_tools.
[email protected]5a0114502011-11-29 13:01:249"""
10
Dan Harrington0e54c592021-06-02 16:41:2911USE_PYTHON3 = True
12
[email protected]5a0114502011-11-29 13:01:2413import re
[email protected]93b2d7372009-11-17 18:54:5314
15INCLUDE_CPP_FILES_ONLY = (
tfarina6aed9ae2015-03-08 03:54:0716 r'.*\.(cc|h)$',
[email protected]93b2d7372009-11-17 18:54:5317)
18
Lei Zhang29278b42017-11-10 22:34:1719INCLUDE_SOURCE_FILES_ONLY = (
20 r'.*\.(c|cc|cpp|h|m|mm)$',
21)
22
[email protected]93b2d7372009-11-17 18:54:5323EXCLUDE = (
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]3139e9f2013-10-10 21:29:4729 r'.*_messages.*\.h$',
[email protected]93b2d7372009-11-17 18:54:5330 # Autogenerated window resources files are off limits
31 r'.*resource.h$',
[email protected]93b2d7372009-11-17 18:54:5332 # Header trickery
33 r'.*-inl\.h$',
[email protected]93b2d7372009-11-17 18:54:5334 # Has safe printf usage that cpplint complains about
35 r'safe_browsing_util\.cc$',
[email protected]93b2d7372009-11-17 18:54:5336)
37
[email protected]5a0114502011-11-29 13:01:2438def _CheckChangeLintsClean(input_api, output_api):
39 """Makes sure that the chrome/ code is cpplint clean."""
Josip Sokcevic8b6cc432020-08-05 17:45:3340 files_to_skip = input_api.DEFAULT_FILES_TO_SKIP + EXCLUDE
[email protected]93b2d7372009-11-17 18:54:5341 sources = lambda x: input_api.FilterSourceFile(
Josip Sokcevic8b6cc432020-08-05 17:45:3342 x, files_to_check=INCLUDE_CPP_FILES_ONLY, files_to_skip=files_to_skip)
[email protected]5a0114502011-11-29 13:01:2443 return input_api.canned_checks.CheckChangeLintsClean(
44 input_api, output_api, sources)
45
Lei Zhang29278b42017-11-10 22:34:1746
[email protected]5a0114502011-11-29 13:01:2447def _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 Zhang421a7ac2017-11-13 07:16:2751 if not f.LocalPath().endswith('BUILD.gn'):
[email protected]5a0114502011-11-29 13:01:2452 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 Zhang421a7ac2017-11-13 07:16:2763 'content_unittests target.',
[email protected]5a0114502011-11-29 13:01:2464 items=problems)]
65
Lei Zhang29278b42017-11-10 22:34:1766
Bruce Dawson42c62cf2022-04-27 15:29:0267def _CheckNoIsAppleBuildFlagsInChromeFile(input_api, f):
68 """Check for IS_APPLE in a given file in chrome/."""
Lei Zhanga4561e702020-08-20 17:09:1569 preprocessor_statement = input_api.re.compile(r'^\s*#')
Bruce Dawson42c62cf2022-04-27 15:29:0270 apple_buildflag = input_api.re.compile(r'BUILDFLAG\(IS_APPLE\)')
Lei Zhanga4561e702020-08-20 17:09:1571 results = []
72 for lnum, line in f.ChangedContents():
Bruce Dawson42c62cf2022-04-27 15:29:0273 if preprocessor_statement.search(line) and apple_buildflag.search(line):
Lei Zhanga4561e702020-08-20 17:09:1574 results.append(' %s:%d' % (f.LocalPath(), lnum))
75
76 return results
77
78
Bruce Dawson42c62cf2022-04-27 15:29:0279def _CheckNoIsAppleBuildFlagsInChrome(input_api, output_api):
80 """Check for IS_APPLE which isn't used in chrome/."""
81 apple_buildflags = []
Lei Zhanga4561e702020-08-20 17:09:1582 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 Dawson42c62cf2022-04-27 15:29:0286 apple_buildflags.extend(_CheckNoIsAppleBuildFlagsInChromeFile(input_api, f))
Lei Zhanga4561e702020-08-20 17:09:1587
Bruce Dawson42c62cf2022-04-27 15:29:0288 if not apple_buildflags:
Lei Zhanga4561e702020-08-20 17:09:1589 return []
90
91 return [output_api.PresubmitError(
Bruce Dawson42c62cf2022-04-27 15:29:0292 'IS_APPLE is not used in chrome/ but found in:\n', apple_buildflags)]
Lei Zhanga4561e702020-08-20 17:09:1593
94
Bruce Dawson42c62cf2022-04-27 15:29:0295def _CheckNoIsIOSBuildFlagsInChromeFile(input_api, f):
96 """Check for IS_IOS in a given file in chrome/."""
Lei Zhang29278b42017-11-10 22:34:1797 preprocessor_statement = input_api.re.compile(r'^\s*#')
Bruce Dawson42c62cf2022-04-27 15:29:0298 ios_buildflag = input_api.re.compile(r'BUILDFLAG\(IS_IOS\)')
Lei Zhang29278b42017-11-10 22:34:1799 results = []
100 for lnum, line in f.ChangedContents():
Bruce Dawson42c62cf2022-04-27 15:29:02101 if preprocessor_statement.search(line) and ios_buildflag.search(line):
Lei Zhang29278b42017-11-10 22:34:17102 results.append(' %s:%d' % (f.LocalPath(), lnum))
103
104 return results
105
106
Bruce Dawson42c62cf2022-04-27 15:29:02107def _CheckNoIsIOSBuildFlagsInChrome(input_api, output_api):
108 """Check for IS_IOS which isn't used in chrome/."""
109 ios_buildflags = []
Lei Zhang29278b42017-11-10 22:34:17110 def SourceFilter(affected_file):
111 return input_api.FilterSourceFile(affected_file, INCLUDE_SOURCE_FILES_ONLY,
Josip Sokcevic8b6cc432020-08-05 17:45:33112 input_api.DEFAULT_FILES_TO_SKIP)
Lei Zhang29278b42017-11-10 22:34:17113 for f in input_api.AffectedSourceFiles(SourceFilter):
Bruce Dawson42c62cf2022-04-27 15:29:02114 ios_buildflags.extend(_CheckNoIsIOSBuildFlagsInChromeFile(input_api, f))
Lei Zhang29278b42017-11-10 22:34:17115
Bruce Dawson42c62cf2022-04-27 15:29:02116 if not ios_buildflags:
Lei Zhang29278b42017-11-10 22:34:17117 return []
118
119 return [output_api.PresubmitError(
Bruce Dawson42c62cf2022-04-27 15:29:02120 'IS_IOS is not used in chrome/ but found in:\n', ios_buildflags)]
Lei Zhang29278b42017-11-10 22:34:17121
122
[email protected]5a0114502011-11-29 13:01:24123def _CommonChecks(input_api, output_api):
124 """Checks common to both upload and commit."""
125 results = []
126 results.extend(_CheckNoContentUnitTestsInChrome(input_api, output_api))
Bruce Dawson42c62cf2022-04-27 15:29:02127 results.extend(_CheckNoIsAppleBuildFlagsInChrome(input_api, output_api))
128 results.extend(_CheckNoIsIOSBuildFlagsInChrome(input_api, output_api))
[email protected]5a0114502011-11-29 13:01:24129 return results
130
Lei Zhang29278b42017-11-10 22:34:17131
[email protected]5a0114502011-11-29 13:01:24132def 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 Zhang29278b42017-11-10 22:34:17138
[email protected]5a0114502011-11-29 13:01:24139def CheckChangeOnCommit(input_api, output_api):
140 results = []
141 results.extend(_CommonChecks(input_api, output_api))
[email protected]93b2d7372009-11-17 18:54:53142 return results