[email protected] | 2c92353 | 2012-03-02 21:16:55 | [diff] [blame] | 1 | # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 0732a49d9 | 2011-03-08 03:37:28 | [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 | |
| 5 | """Chromium presubmit script for src/base. |
| 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 on the presubmit API built into depot_tools. |
[email protected] | 0732a49d9 | 2011-03-08 03:37:28 | [diff] [blame] | 9 | """ |
| 10 | |
[email protected] | 23e6cbc | 2012-06-16 18:51:20 | [diff] [blame] | 11 | def _CheckNoInterfacesInBase(input_api, output_api): |
| 12 | """Checks to make sure no files in libbase.a have |@interface|.""" |
| 13 | pattern = input_api.re.compile(r'^\s*@interface', input_api.re.MULTILINE) |
| 14 | files = [] |
| 15 | for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile): |
| 16 | if (f.LocalPath().startswith('base/') and |
droger | 114a60b | 2014-11-06 11:06:51 | [diff] [blame] | 17 | not "/ios/" in f.LocalPath() and |
[email protected] | a35203a4 | 2012-07-12 15:12:55 | [diff] [blame] | 18 | not "/test/" in f.LocalPath() and |
bttk | ec17659 | 2020-01-23 17:04:17 | [diff] [blame] | 19 | not f.LocalPath().endswith('.java') and |
[email protected] | 43016609 | 2013-05-30 16:09:14 | [diff] [blame] | 20 | not f.LocalPath().endswith('_unittest.mm') and |
| 21 | not f.LocalPath().endswith('mac/sdk_forward_declarations.h')): |
[email protected] | 23e6cbc | 2012-06-16 18:51:20 | [diff] [blame] | 22 | contents = input_api.ReadFile(f) |
| 23 | if pattern.search(contents): |
| 24 | files.append(f) |
| 25 | |
| 26 | if len(files): |
| 27 | return [ output_api.PresubmitError( |
| 28 | 'Objective-C interfaces or categories are forbidden in libbase. ' + |
| 29 | 'See http://groups.google.com/a/chromium.org/group/chromium-dev/' + |
| 30 | 'browse_thread/thread/efb28c10435987fd', |
| 31 | files) ] |
| 32 | return [] |
| 33 | |
| 34 | |
Eric Seckler | 05f7e90 | 2021-02-25 14:47:36 | [diff] [blame^] | 35 | def _FindLocations(input_api, search_regexes, files_to_check, files_to_skip): |
| 36 | """Returns locations matching one of the search_regexes.""" |
| 37 | def FilterFile(affected_file): |
| 38 | return input_api.FilterSourceFile( |
| 39 | affected_file, |
| 40 | files_to_check=files_to_check, |
| 41 | files_to_skip=files_to_skip) |
| 42 | |
| 43 | no_presubmit = r"// no-presubmit-check" |
| 44 | locations = [] |
| 45 | for f in input_api.AffectedSourceFiles(FilterFile): |
| 46 | for line_num, line in f.ChangedContents(): |
| 47 | for search_regex in search_regexes: |
| 48 | if (input_api.re.search(search_regex, line) and |
| 49 | not input_api.re.search(no_presubmit, line)): |
| 50 | locations.append(" %s:%d" % (f.LocalPath(), line_num)) |
| 51 | break |
| 52 | return locations |
| 53 | |
| 54 | |
Eric Seckler | f6c544f | 2020-06-02 10:49:21 | [diff] [blame] | 55 | def _CheckNoTraceEventInclude(input_api, output_api): |
| 56 | """Verify that //base includes base_tracing.h instead of trace event headers. |
| 57 | |
| 58 | Checks that files outside trace event implementation include the |
| 59 | base_tracing.h header instead of specific trace event implementation headers |
| 60 | to maintain compatibility with the gn flag "enable_base_tracing = false". |
| 61 | """ |
| 62 | discouraged_includes = [ |
Eric Seckler | 7d7dd3c | 2020-06-26 09:24:12 | [diff] [blame] | 63 | r'^#include "base/trace_event/(?!base_tracing\.h)', |
Eric Seckler | 05f7e90 | 2021-02-25 14:47:36 | [diff] [blame^] | 64 | r'^#include "third_party/perfetto/include/', |
Eric Seckler | f6c544f | 2020-06-02 10:49:21 | [diff] [blame] | 65 | ] |
| 66 | |
Josip Sokcevic | 8b6cc43 | 2020-08-05 17:45:33 | [diff] [blame] | 67 | files_to_check = [ |
Eric Seckler | f6c544f | 2020-06-02 10:49:21 | [diff] [blame] | 68 | r".*\.(h|cc|mm)$", |
| 69 | ] |
Josip Sokcevic | 8b6cc43 | 2020-08-05 17:45:33 | [diff] [blame] | 70 | files_to_skip = [ |
Eric Seckler | 7d7dd3c | 2020-06-26 09:24:12 | [diff] [blame] | 71 | r".*[\\/]test[\\/].*", |
Eric Seckler | f6c544f | 2020-06-02 10:49:21 | [diff] [blame] | 72 | r".*[\\/]trace_event[\\/].*", |
| 73 | r".*[\\/]tracing[\\/].*", |
| 74 | ] |
Eric Seckler | f6c544f | 2020-06-02 10:49:21 | [diff] [blame] | 75 | |
Eric Seckler | 05f7e90 | 2021-02-25 14:47:36 | [diff] [blame^] | 76 | locations = _FindLocations(input_api, discouraged_includes, files_to_check, |
| 77 | files_to_skip) |
Eric Seckler | f6c544f | 2020-06-02 10:49:21 | [diff] [blame] | 78 | if locations: |
Eric Seckler | 7d7dd3c | 2020-06-26 09:24:12 | [diff] [blame] | 79 | return [ output_api.PresubmitError( |
| 80 | 'Base code should include "base/trace_event/base_tracing.h" instead\n' + |
| 81 | 'of trace_event implementation headers. If you need to include an\n' + |
| 82 | 'implementation header, verify that base_unittests still passes\n' + |
| 83 | 'with gn arg "enable_base_tracing = false" and add\n' + |
| 84 | '"// no-presubmit-check" after the include. \n' + |
| 85 | '\n'.join(locations)) ] |
Eric Seckler | f6c544f | 2020-06-02 10:49:21 | [diff] [blame] | 86 | return [] |
| 87 | |
| 88 | |
Eric Seckler | 05f7e90 | 2021-02-25 14:47:36 | [diff] [blame^] | 89 | def _WarnPbzeroIncludes(input_api, output_api): |
| 90 | """Warn to check enable_base_tracing=false when including a pbzero header. |
| 91 | |
| 92 | Emits a warning when including a perfetto pbzero header, encouraging the |
| 93 | user to verify that //base still builds with enable_base_tracing=false. |
| 94 | """ |
| 95 | warn_includes = [ |
| 96 | r'^#include "third_party/perfetto/protos/', |
| 97 | r'^#include "base/tracing/protos/', |
| 98 | ] |
| 99 | |
| 100 | files_to_check = [ |
| 101 | r".*\.(h|cc|mm)$", |
| 102 | ] |
| 103 | files_to_skip = [ |
| 104 | r".*[\\/]test[\\/].*", |
| 105 | r".*[\\/]trace_event[\\/].*", |
| 106 | r".*[\\/]tracing[\\/].*", |
| 107 | ] |
| 108 | |
| 109 | locations = _FindLocations(input_api, warn_includes, files_to_check, |
| 110 | files_to_skip) |
| 111 | if locations: |
| 112 | return [ output_api.PresubmitPromptWarning( |
| 113 | 'Please verify that base_unittests still builds & passes with gn\n' + |
| 114 | 'arg "enable_base_tracing = false" when adding typed trace events\n' + |
| 115 | 'to //base. You can use "#if BUILDFLAG(ENABLE_BASE_TRACING)" to\n' + |
| 116 | 'exclude pbzero headers and anything not supported by\n' + |
| 117 | '//base/trace_event/trace_event_stub.h.\n' + |
| 118 | '\n'.join(locations)) ] |
| 119 | return [] |
| 120 | |
| 121 | |
[email protected] | 23e6cbc | 2012-06-16 18:51:20 | [diff] [blame] | 122 | def _CommonChecks(input_api, output_api): |
| 123 | """Checks common to both upload and commit.""" |
| 124 | results = [] |
| 125 | results.extend(_CheckNoInterfacesInBase(input_api, output_api)) |
Eric Seckler | f6c544f | 2020-06-02 10:49:21 | [diff] [blame] | 126 | results.extend(_CheckNoTraceEventInclude(input_api, output_api)) |
Eric Seckler | 05f7e90 | 2021-02-25 14:47:36 | [diff] [blame^] | 127 | results.extend(_WarnPbzeroIncludes(input_api, output_api)) |
[email protected] | 23e6cbc | 2012-06-16 18:51:20 | [diff] [blame] | 128 | return results |
| 129 | |
Eric Seckler | f6c544f | 2020-06-02 10:49:21 | [diff] [blame] | 130 | |
[email protected] | 23e6cbc | 2012-06-16 18:51:20 | [diff] [blame] | 131 | def CheckChangeOnUpload(input_api, output_api): |
| 132 | results = [] |
| 133 | results.extend(_CommonChecks(input_api, output_api)) |
| 134 | return results |
| 135 | |
| 136 | |
| 137 | def CheckChangeOnCommit(input_api, output_api): |
| 138 | results = [] |
| 139 | results.extend(_CommonChecks(input_api, output_api)) |
| 140 | return results |