andrew@webrtc.org | 2442de1 | 2012-01-23 17:45:41 +0000 | [diff] [blame] | 1 | # Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
| 2 | # |
| 3 | # Use of this source code is governed by a BSD-style license |
| 4 | # that can be found in the LICENSE file in the root of the source |
| 5 | # tree. An additional intellectual property rights grant can be found |
| 6 | # in the file PATENTS. All contributing project authors may |
| 7 | # be found in the AUTHORS file in the root of the source tree. |
niklase@google.com | da159d6 | 2011-05-30 11:51:34 +0000 | [diff] [blame] | 8 | |
kjellander | 7439f97 | 2016-12-05 22:47:46 -0800 | [diff] [blame] | 9 | import json |
kjellander@webrtc.org | aefe61a | 2014-12-08 13:00:30 +0000 | [diff] [blame] | 10 | import os |
kjellander@webrtc.org | 8575980 | 2013-10-22 16:47:40 +0000 | [diff] [blame] | 11 | import re |
ehmaldonado | 4fb9746 | 2017-01-30 05:27:22 -0800 | [diff] [blame] | 12 | import subprocess |
kjellander@webrtc.org | 3bd4156 | 2014-09-01 11:06:37 +0000 | [diff] [blame] | 13 | import sys |
Mirko Bonadei | 4dc4e25 | 2017-09-19 13:49:16 +0200 | [diff] [blame] | 14 | from collections import defaultdict |
kjellander@webrtc.org | 8575980 | 2013-10-22 16:47:40 +0000 | [diff] [blame] | 15 | |
| 16 | |
oprypin | 2aa463f | 2017-03-23 03:17:02 -0700 | [diff] [blame] | 17 | # Files and directories that are *skipped* by cpplint in the presubmit script. |
| 18 | CPPLINT_BLACKLIST = [ |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 19 | 'api/video_codecs/video_decoder.h', |
| 20 | 'common_types.cc', |
| 21 | 'common_types.h', |
| 22 | 'examples/objc', |
| 23 | 'media', |
| 24 | 'modules/audio_coding', |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 25 | 'modules/audio_device', |
| 26 | 'modules/audio_processing', |
| 27 | 'modules/desktop_capture', |
| 28 | 'modules/include/module_common_types.h', |
| 29 | 'modules/media_file', |
| 30 | 'modules/utility', |
| 31 | 'modules/video_capture', |
| 32 | 'p2p', |
| 33 | 'pc', |
| 34 | 'rtc_base', |
| 35 | 'sdk/android/src/jni', |
| 36 | 'sdk/objc', |
| 37 | 'system_wrappers', |
| 38 | 'test', |
Henrik Kjellander | 90fd7d8 | 2017-05-09 08:30:10 +0200 | [diff] [blame] | 39 | 'tools_webrtc', |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 40 | 'voice_engine', |
kjellander@webrtc.org | 0fcaf99 | 2015-11-26 15:24:52 +0100 | [diff] [blame] | 41 | ] |
| 42 | |
jbauch | c4e3ead | 2016-02-19 00:25:55 -0800 | [diff] [blame] | 43 | # These filters will always be removed, even if the caller specifies a filter |
| 44 | # set, as they are problematic or broken in some way. |
| 45 | # |
| 46 | # Justifications for each filter: |
| 47 | # - build/c++11 : Rvalue ref checks are unreliable (false positives), |
| 48 | # include file and feature blacklists are |
| 49 | # google3-specific. |
kjellander | e5a87a5 | 2016-04-27 02:32:12 -0700 | [diff] [blame] | 50 | # - whitespace/operators: Same as above (doesn't seem sufficient to eliminate |
| 51 | # all move-related errors). |
jbauch | c4e3ead | 2016-02-19 00:25:55 -0800 | [diff] [blame] | 52 | BLACKLIST_LINT_FILTERS = [ |
| 53 | '-build/c++11', |
kjellander | e5a87a5 | 2016-04-27 02:32:12 -0700 | [diff] [blame] | 54 | '-whitespace/operators', |
jbauch | c4e3ead | 2016-02-19 00:25:55 -0800 | [diff] [blame] | 55 | ] |
| 56 | |
kjellander | fd59523 | 2015-12-04 02:44:09 -0800 | [diff] [blame] | 57 | # List of directories of "supported" native APIs. That means changes to headers |
| 58 | # will be done in a compatible way following this scheme: |
| 59 | # 1. Non-breaking changes are made. |
| 60 | # 2. The old APIs as marked as deprecated (with comments). |
| 61 | # 3. Deprecation is announced to discuss-webrtc@googlegroups.com and |
| 62 | # webrtc-users@google.com (internal list). |
| 63 | # 4. (later) The deprecated APIs are removed. |
kjellander | 53047c9 | 2015-12-02 23:56:14 -0800 | [diff] [blame] | 64 | NATIVE_API_DIRS = ( |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 65 | 'api', |
| 66 | 'media', |
| 67 | 'modules/audio_device/include', |
| 68 | 'pc', |
kjellander | dd70547 | 2016-06-09 11:17:27 -0700 | [diff] [blame] | 69 | ) |
Mirko Bonadei | 4dc4e25 | 2017-09-19 13:49:16 +0200 | [diff] [blame] | 70 | |
kjellander | dd70547 | 2016-06-09 11:17:27 -0700 | [diff] [blame] | 71 | # These directories should not be used but are maintained only to avoid breaking |
| 72 | # some legacy downstream code. |
| 73 | LEGACY_API_DIRS = ( |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 74 | 'common_audio/include', |
| 75 | 'modules/audio_coding/include', |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 76 | 'modules/audio_processing/include', |
| 77 | 'modules/bitrate_controller/include', |
| 78 | 'modules/congestion_controller/include', |
| 79 | 'modules/include', |
| 80 | 'modules/remote_bitrate_estimator/include', |
| 81 | 'modules/rtp_rtcp/include', |
| 82 | 'modules/rtp_rtcp/source', |
| 83 | 'modules/utility/include', |
| 84 | 'modules/video_coding/codecs/h264/include', |
| 85 | 'modules/video_coding/codecs/i420/include', |
| 86 | 'modules/video_coding/codecs/vp8/include', |
| 87 | 'modules/video_coding/codecs/vp9/include', |
| 88 | 'modules/video_coding/include', |
| 89 | 'rtc_base', |
| 90 | 'system_wrappers/include', |
| 91 | 'voice_engine/include', |
kjellander | 53047c9 | 2015-12-02 23:56:14 -0800 | [diff] [blame] | 92 | ) |
Mirko Bonadei | 4dc4e25 | 2017-09-19 13:49:16 +0200 | [diff] [blame] | 93 | |
kjellander | dd70547 | 2016-06-09 11:17:27 -0700 | [diff] [blame] | 94 | API_DIRS = NATIVE_API_DIRS[:] + LEGACY_API_DIRS[:] |
kjellander | 53047c9 | 2015-12-02 23:56:14 -0800 | [diff] [blame] | 95 | |
Mirko Bonadei | 4dc4e25 | 2017-09-19 13:49:16 +0200 | [diff] [blame] | 96 | # TARGET_RE matches a GN target, and extracts the target name and the contents. |
| 97 | TARGET_RE = re.compile(r'(?P<indent>\s*)\w+\("(?P<target_name>\w+)"\) {' |
| 98 | r'(?P<target_contents>.*?)' |
| 99 | r'(?P=indent)}', |
| 100 | re.MULTILINE | re.DOTALL) |
| 101 | |
| 102 | # SOURCES_RE matches a block of sources inside a GN target. |
| 103 | SOURCES_RE = re.compile(r'sources \+?= \[(?P<sources>.*?)\]', |
| 104 | re.MULTILINE | re.DOTALL) |
| 105 | |
| 106 | # FILE_PATH_RE matchies a file path. |
| 107 | FILE_PATH_RE = re.compile(r'"(?P<file_path>(\w|\/)+)(?P<extension>\.\w+)"') |
| 108 | |
kjellander | 53047c9 | 2015-12-02 23:56:14 -0800 | [diff] [blame] | 109 | |
ehmaldonado | 4fb9746 | 2017-01-30 05:27:22 -0800 | [diff] [blame] | 110 | def _RunCommand(command, cwd): |
| 111 | """Runs a command and returns the output from that command.""" |
| 112 | p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, |
| 113 | cwd=cwd) |
| 114 | stdout = p.stdout.read() |
| 115 | stderr = p.stderr.read() |
| 116 | p.wait() |
| 117 | p.stdout.close() |
| 118 | p.stderr.close() |
| 119 | return p.returncode, stdout, stderr |
| 120 | |
| 121 | |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 122 | def VerifyNativeApiHeadersListIsValid(input_api, output_api): |
kjellander | 53047c9 | 2015-12-02 23:56:14 -0800 | [diff] [blame] | 123 | """Ensures the list of native API header directories is up to date.""" |
| 124 | non_existing_paths = [] |
| 125 | native_api_full_paths = [ |
| 126 | input_api.os_path.join(input_api.PresubmitLocalPath(), |
kjellander | dd70547 | 2016-06-09 11:17:27 -0700 | [diff] [blame] | 127 | *path.split('/')) for path in API_DIRS] |
kjellander | 53047c9 | 2015-12-02 23:56:14 -0800 | [diff] [blame] | 128 | for path in native_api_full_paths: |
| 129 | if not os.path.isdir(path): |
| 130 | non_existing_paths.append(path) |
| 131 | if non_existing_paths: |
| 132 | return [output_api.PresubmitError( |
| 133 | 'Directories to native API headers have changed which has made the ' |
| 134 | 'list in PRESUBMIT.py outdated.\nPlease update it to the current ' |
| 135 | 'location of our native APIs.', |
| 136 | non_existing_paths)] |
| 137 | return [] |
| 138 | |
kjellander | c88b5d5 | 2017-04-05 06:42:43 -0700 | [diff] [blame] | 139 | API_CHANGE_MSG = """ |
kwiberg | eb13302 | 2016-04-07 07:41:48 -0700 | [diff] [blame] | 140 | You seem to be changing native API header files. Please make sure that you: |
oprypin | 375b9ac | 2017-02-13 04:13:23 -0800 | [diff] [blame] | 141 | 1. Make compatible changes that don't break existing clients. Usually |
| 142 | this is done by keeping the existing method signatures unchanged. |
| 143 | 2. Mark the old stuff as deprecated (see RTC_DEPRECATED macro). |
kwiberg | eb13302 | 2016-04-07 07:41:48 -0700 | [diff] [blame] | 144 | 3. Create a timeline and plan for when the deprecated stuff will be |
| 145 | removed. (The amount of time we give users to change their code |
| 146 | should be informed by how much work it is for them. If they just |
| 147 | need to replace one name with another or something equally |
| 148 | simple, 1-2 weeks might be good; if they need to do serious work, |
| 149 | up to 3 months may be called for.) |
| 150 | 4. Update/inform existing downstream code owners to stop using the |
| 151 | deprecated stuff. (Send announcements to |
| 152 | discuss-webrtc@googlegroups.com and webrtc-users@google.com.) |
| 153 | 5. Remove the deprecated stuff, once the agreed-upon amount of time |
| 154 | has passed. |
| 155 | Related files: |
| 156 | """ |
kjellander | 53047c9 | 2015-12-02 23:56:14 -0800 | [diff] [blame] | 157 | |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 158 | def CheckNativeApiHeaderChanges(input_api, output_api): |
kjellander | 53047c9 | 2015-12-02 23:56:14 -0800 | [diff] [blame] | 159 | """Checks to remind proper changing of native APIs.""" |
| 160 | files = [] |
| 161 | for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile): |
| 162 | if f.LocalPath().endswith('.h'): |
kjellander | dd70547 | 2016-06-09 11:17:27 -0700 | [diff] [blame] | 163 | for path in API_DIRS: |
kjellander | 53047c9 | 2015-12-02 23:56:14 -0800 | [diff] [blame] | 164 | if os.path.dirname(f.LocalPath()) == path: |
| 165 | files.append(f) |
| 166 | |
| 167 | if files: |
kjellander | c88b5d5 | 2017-04-05 06:42:43 -0700 | [diff] [blame] | 168 | return [output_api.PresubmitNotifyResult(API_CHANGE_MSG, files)] |
kjellander | 53047c9 | 2015-12-02 23:56:14 -0800 | [diff] [blame] | 169 | return [] |
| 170 | |
kjellander@webrtc.org | 0fcaf99 | 2015-11-26 15:24:52 +0100 | [diff] [blame] | 171 | |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 172 | def CheckNoIOStreamInHeaders(input_api, output_api): |
kjellander@webrtc.org | 51198f1 | 2012-02-21 17:53:46 +0000 | [diff] [blame] | 173 | """Checks to make sure no .h files include <iostream>.""" |
| 174 | files = [] |
| 175 | pattern = input_api.re.compile(r'^#include\s*<iostream>', |
| 176 | input_api.re.MULTILINE) |
| 177 | for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile): |
| 178 | if not f.LocalPath().endswith('.h'): |
| 179 | continue |
| 180 | contents = input_api.ReadFile(f) |
| 181 | if pattern.search(contents): |
| 182 | files.append(f) |
| 183 | |
| 184 | if len(files): |
Henrik Kjellander | 57e5fd2 | 2015-05-25 12:55:39 +0200 | [diff] [blame] | 185 | return [output_api.PresubmitError( |
kjellander@webrtc.org | 51198f1 | 2012-02-21 17:53:46 +0000 | [diff] [blame] | 186 | 'Do not #include <iostream> in header files, since it inserts static ' + |
| 187 | 'initialization into every file including the header. Instead, ' + |
| 188 | '#include <ostream>. See http://crbug.com/94794', |
Henrik Kjellander | 57e5fd2 | 2015-05-25 12:55:39 +0200 | [diff] [blame] | 189 | files)] |
kjellander@webrtc.org | 51198f1 | 2012-02-21 17:53:46 +0000 | [diff] [blame] | 190 | return [] |
| 191 | |
kjellander@webrtc.org | e415864 | 2014-08-06 09:11:18 +0000 | [diff] [blame] | 192 | |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 193 | def CheckNoPragmaOnce(input_api, output_api): |
kjellander | 6aeef74 | 2017-02-20 01:13:18 -0800 | [diff] [blame] | 194 | """Make sure that banned functions are not used.""" |
| 195 | files = [] |
| 196 | pattern = input_api.re.compile(r'^#pragma\s+once', |
| 197 | input_api.re.MULTILINE) |
| 198 | for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile): |
| 199 | if not f.LocalPath().endswith('.h'): |
| 200 | continue |
| 201 | contents = input_api.ReadFile(f) |
| 202 | if pattern.search(contents): |
| 203 | files.append(f) |
| 204 | |
| 205 | if files: |
| 206 | return [output_api.PresubmitError( |
| 207 | 'Do not use #pragma once in header files.\n' |
| 208 | 'See http://www.chromium.org/developers/coding-style#TOC-File-headers', |
| 209 | files)] |
| 210 | return [] |
| 211 | |
| 212 | |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 213 | def CheckNoFRIEND_TEST(input_api, output_api): # pylint: disable=invalid-name |
kjellander@webrtc.org | 51198f1 | 2012-02-21 17:53:46 +0000 | [diff] [blame] | 214 | """Make sure that gtest's FRIEND_TEST() macro is not used, the |
| 215 | FRIEND_TEST_ALL_PREFIXES() macro from testsupport/gtest_prod_util.h should be |
| 216 | used instead since that allows for FLAKY_, FAILS_ and DISABLED_ prefixes.""" |
| 217 | problems = [] |
| 218 | |
| 219 | file_filter = lambda f: f.LocalPath().endswith(('.cc', '.h')) |
| 220 | for f in input_api.AffectedFiles(file_filter=file_filter): |
| 221 | for line_num, line in f.ChangedContents(): |
| 222 | if 'FRIEND_TEST(' in line: |
| 223 | problems.append(' %s:%d' % (f.LocalPath(), line_num)) |
| 224 | |
| 225 | if not problems: |
| 226 | return [] |
| 227 | return [output_api.PresubmitPromptWarning('WebRTC\'s code should not use ' |
| 228 | 'gtest\'s FRIEND_TEST() macro. Include testsupport/gtest_prod_util.h and ' |
| 229 | 'use FRIEND_TEST_ALL_PREFIXES() instead.\n' + '\n'.join(problems))] |
| 230 | |
kjellander@webrtc.org | e415864 | 2014-08-06 09:11:18 +0000 | [diff] [blame] | 231 | |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 232 | def IsLintBlacklisted(blacklist_paths, file_path): |
oprypin | 2aa463f | 2017-03-23 03:17:02 -0700 | [diff] [blame] | 233 | """ Checks if a file is blacklisted for lint check.""" |
| 234 | for path in blacklist_paths: |
| 235 | if file_path == path or os.path.dirname(file_path).startswith(path): |
kjellander@webrtc.org | 0fcaf99 | 2015-11-26 15:24:52 +0100 | [diff] [blame] | 236 | return True |
| 237 | return False |
| 238 | |
| 239 | |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 240 | def CheckApprovedFilesLintClean(input_api, output_api, |
mflodman@webrtc.org | 2a45209 | 2012-07-01 05:55:23 +0000 | [diff] [blame] | 241 | source_file_filter=None): |
oprypin | 2aa463f | 2017-03-23 03:17:02 -0700 | [diff] [blame] | 242 | """Checks that all new or non-blacklisted .cc and .h files pass cpplint.py. |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 243 | This check is based on CheckChangeLintsClean in |
kjellander@webrtc.org | 51198f1 | 2012-02-21 17:53:46 +0000 | [diff] [blame] | 244 | depot_tools/presubmit_canned_checks.py but has less filters and only checks |
| 245 | added files.""" |
| 246 | result = [] |
| 247 | |
| 248 | # Initialize cpplint. |
| 249 | import cpplint |
| 250 | # Access to a protected member _XX of a client class |
| 251 | # pylint: disable=W0212 |
| 252 | cpplint._cpplint_state.ResetErrorCounts() |
| 253 | |
jbauch | c4e3ead | 2016-02-19 00:25:55 -0800 | [diff] [blame] | 254 | lint_filters = cpplint._Filters() |
| 255 | lint_filters.extend(BLACKLIST_LINT_FILTERS) |
| 256 | cpplint._SetFilters(','.join(lint_filters)) |
| 257 | |
oprypin | 2aa463f | 2017-03-23 03:17:02 -0700 | [diff] [blame] | 258 | # Create a platform independent blacklist for cpplint. |
| 259 | blacklist_paths = [input_api.os_path.join(*path.split('/')) |
| 260 | for path in CPPLINT_BLACKLIST] |
kjellander@webrtc.org | 0fcaf99 | 2015-11-26 15:24:52 +0100 | [diff] [blame] | 261 | |
kjellander@webrtc.org | 51198f1 | 2012-02-21 17:53:46 +0000 | [diff] [blame] | 262 | # Use the strictest verbosity level for cpplint.py (level 1) which is the |
oprypin | 2aa463f | 2017-03-23 03:17:02 -0700 | [diff] [blame] | 263 | # default when running cpplint.py from command line. To make it possible to |
| 264 | # work with not-yet-converted code, we're only applying it to new (or |
| 265 | # moved/renamed) files and files not listed in CPPLINT_BLACKLIST. |
kjellander@webrtc.org | 51198f1 | 2012-02-21 17:53:46 +0000 | [diff] [blame] | 266 | verbosity_level = 1 |
| 267 | files = [] |
| 268 | for f in input_api.AffectedSourceFiles(source_file_filter): |
Henrik Kjellander | 57e5fd2 | 2015-05-25 12:55:39 +0200 | [diff] [blame] | 269 | # Note that moved/renamed files also count as added. |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 270 | if f.Action() == 'A' or not IsLintBlacklisted(blacklist_paths, |
oprypin | 2aa463f | 2017-03-23 03:17:02 -0700 | [diff] [blame] | 271 | f.LocalPath()): |
kjellander@webrtc.org | 51198f1 | 2012-02-21 17:53:46 +0000 | [diff] [blame] | 272 | files.append(f.AbsoluteLocalPath()) |
mflodman@webrtc.org | 2a45209 | 2012-07-01 05:55:23 +0000 | [diff] [blame] | 273 | |
kjellander@webrtc.org | 51198f1 | 2012-02-21 17:53:46 +0000 | [diff] [blame] | 274 | for file_name in files: |
| 275 | cpplint.ProcessFile(file_name, verbosity_level) |
| 276 | |
| 277 | if cpplint._cpplint_state.error_count > 0: |
| 278 | if input_api.is_committing: |
oprypin | 8e58d65 | 2017-03-21 07:52:41 -0700 | [diff] [blame] | 279 | res_type = output_api.PresubmitError |
kjellander@webrtc.org | 51198f1 | 2012-02-21 17:53:46 +0000 | [diff] [blame] | 280 | else: |
| 281 | res_type = output_api.PresubmitPromptWarning |
| 282 | result = [res_type('Changelist failed cpplint.py check.')] |
| 283 | |
| 284 | return result |
| 285 | |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 286 | def CheckNoSourcesAbove(input_api, gn_files, output_api): |
ehmaldonado | 5b1ba08 | 2016-09-02 05:51:08 -0700 | [diff] [blame] | 287 | # Disallow referencing source files with paths above the GN file location. |
| 288 | source_pattern = input_api.re.compile(r' +sources \+?= \[(.*?)\]', |
| 289 | re.MULTILINE | re.DOTALL) |
| 290 | file_pattern = input_api.re.compile(r'"((\.\./.*?)|(//.*?))"') |
| 291 | violating_gn_files = set() |
| 292 | violating_source_entries = [] |
| 293 | for gn_file in gn_files: |
| 294 | contents = input_api.ReadFile(gn_file) |
| 295 | for source_block_match in source_pattern.finditer(contents): |
| 296 | # Find all source list entries starting with ../ in the source block |
| 297 | # (exclude overrides entries). |
| 298 | for file_list_match in file_pattern.finditer(source_block_match.group(1)): |
| 299 | source_file = file_list_match.group(1) |
| 300 | if 'overrides/' not in source_file: |
| 301 | violating_source_entries.append(source_file) |
| 302 | violating_gn_files.add(gn_file) |
| 303 | if violating_gn_files: |
| 304 | return [output_api.PresubmitError( |
| 305 | 'Referencing source files above the directory of the GN file is not ' |
Henrik Kjellander | b4af3d6 | 2016-11-16 20:11:29 +0100 | [diff] [blame] | 306 | 'allowed. Please introduce new GN targets in the proper location ' |
| 307 | 'instead.\n' |
ehmaldonado | 5b1ba08 | 2016-09-02 05:51:08 -0700 | [diff] [blame] | 308 | 'Invalid source entries:\n' |
| 309 | '%s\n' |
| 310 | 'Violating GN files:' % '\n'.join(violating_source_entries), |
| 311 | items=violating_gn_files)] |
| 312 | return [] |
| 313 | |
Mirko Bonadei | 4dc4e25 | 2017-09-19 13:49:16 +0200 | [diff] [blame] | 314 | def CheckNoMixingSources(input_api, gn_files, output_api): |
| 315 | """Disallow mixing C, C++ and Obj-C/Obj-C++ in the same target. |
| 316 | |
| 317 | See bugs.webrtc.org/7743 for more context. |
| 318 | """ |
| 319 | def _MoreThanOneSourceUsed(*sources_lists): |
| 320 | sources_used = 0 |
| 321 | for source_list in sources_lists: |
| 322 | if len(source_list): |
| 323 | sources_used += 1 |
| 324 | return sources_used > 1 |
| 325 | |
| 326 | errors = defaultdict(lambda: []) |
kjellander | 7439f97 | 2016-12-05 22:47:46 -0800 | [diff] [blame] | 327 | for gn_file in gn_files: |
Mirko Bonadei | 4dc4e25 | 2017-09-19 13:49:16 +0200 | [diff] [blame] | 328 | gn_file_content = input_api.ReadFile(gn_file) |
| 329 | for target_match in TARGET_RE.finditer(gn_file_content): |
| 330 | # list_of_sources is a list of tuples of the form |
| 331 | # (c_files, cc_files, objc_files) that keeps track of all the sources |
| 332 | # defined in a target. A GN target can have more that on definition of |
| 333 | # sources (since it supports if/else statements). |
| 334 | # E.g.: |
| 335 | # rtc_static_library("foo") { |
| 336 | # if (is_win) { |
| 337 | # sources = [ "foo.cc" ] |
| 338 | # } else { |
| 339 | # sources = [ "foo.mm" ] |
| 340 | # } |
| 341 | # } |
| 342 | # This is allowed and the presubmit check should support this case. |
| 343 | list_of_sources = [] |
kjellander | 7439f97 | 2016-12-05 22:47:46 -0800 | [diff] [blame] | 344 | c_files = [] |
| 345 | cc_files = [] |
Mirko Bonadei | 4dc4e25 | 2017-09-19 13:49:16 +0200 | [diff] [blame] | 346 | objc_files = [] |
| 347 | target_name = target_match.group('target_name') |
| 348 | target_contents = target_match.group('target_contents') |
| 349 | for sources_match in SOURCES_RE.finditer(target_contents): |
| 350 | if '+=' not in sources_match.group(0): |
| 351 | if c_files or cc_files or objc_files: |
| 352 | list_of_sources.append((c_files, cc_files, objc_files)) |
| 353 | c_files = [] |
| 354 | cc_files = [] |
| 355 | objc_files = [] |
| 356 | for file_match in FILE_PATH_RE.finditer(sources_match.group(1)): |
| 357 | file_path = file_match.group('file_path') |
| 358 | extension = file_match.group('extension') |
| 359 | if extension == '.c': |
| 360 | c_files.append(file_path + extension) |
| 361 | if extension == '.cc': |
| 362 | cc_files.append(file_path + extension) |
| 363 | if extension in ['.m', '.mm']: |
| 364 | objc_files.append(file_path + extension) |
| 365 | list_of_sources.append((c_files, cc_files, objc_files)) |
| 366 | for c_files_list, cc_files_list, objc_files_list in list_of_sources: |
| 367 | if _MoreThanOneSourceUsed(c_files_list, cc_files_list, objc_files_list): |
| 368 | all_sources = sorted(c_files_list + cc_files_list + objc_files_list) |
| 369 | errors[gn_file.LocalPath()].append((target_name, all_sources)) |
| 370 | if errors: |
kjellander | 7439f97 | 2016-12-05 22:47:46 -0800 | [diff] [blame] | 371 | return [output_api.PresubmitError( |
Mirko Bonadei | 4dc4e25 | 2017-09-19 13:49:16 +0200 | [diff] [blame] | 372 | 'GN targets cannot mix .c, .cc and .m (or .mm) source files.\n' |
| 373 | 'Please create a separate target for each collection of sources.\n' |
kjellander | 7439f97 | 2016-12-05 22:47:46 -0800 | [diff] [blame] | 374 | 'Mixed sources: \n' |
| 375 | '%s\n' |
Mirko Bonadei | 4dc4e25 | 2017-09-19 13:49:16 +0200 | [diff] [blame] | 376 | 'Violating GN files:\n%s\n' % (json.dumps(errors, indent=2), |
| 377 | '\n'.join(errors.keys())))] |
kjellander | 7439f97 | 2016-12-05 22:47:46 -0800 | [diff] [blame] | 378 | return [] |
| 379 | |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 380 | def CheckNoPackageBoundaryViolations(input_api, gn_files, output_api): |
ehmaldonado | 4fb9746 | 2017-01-30 05:27:22 -0800 | [diff] [blame] | 381 | cwd = input_api.PresubmitLocalPath() |
mbonadei | ab587dc | 2017-05-12 04:13:31 -0700 | [diff] [blame] | 382 | script_path = os.path.join('tools_webrtc', 'presubmit_checks_lib', |
| 383 | 'check_package_boundaries.py') |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 384 | command = [sys.executable, script_path] |
ehmaldonado | 4fb9746 | 2017-01-30 05:27:22 -0800 | [diff] [blame] | 385 | command += [gn_file.LocalPath() for gn_file in gn_files] |
| 386 | returncode, _, stderr = _RunCommand(command, cwd) |
| 387 | if returncode: |
| 388 | return [output_api.PresubmitError( |
| 389 | 'There are package boundary violations in the following GN files:\n\n' |
| 390 | '%s' % stderr)] |
| 391 | return [] |
| 392 | |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 393 | def CheckGnChanges(input_api, output_api): |
ehmaldonado | 5b1ba08 | 2016-09-02 05:51:08 -0700 | [diff] [blame] | 394 | source_file_filter = lambda x: input_api.FilterSourceFile( |
| 395 | x, white_list=(r'.+\.(gn|gni)$',)) |
| 396 | |
| 397 | gn_files = [] |
| 398 | for f in input_api.AffectedSourceFiles(source_file_filter): |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 399 | gn_files.append(f) |
ehmaldonado | 5b1ba08 | 2016-09-02 05:51:08 -0700 | [diff] [blame] | 400 | |
| 401 | result = [] |
| 402 | if gn_files: |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 403 | result.extend(CheckNoSourcesAbove(input_api, gn_files, output_api)) |
Mirko Bonadei | 4dc4e25 | 2017-09-19 13:49:16 +0200 | [diff] [blame] | 404 | result.extend(CheckNoMixingSources(input_api, gn_files, output_api)) |
| 405 | result.extend(CheckNoPackageBoundaryViolations(input_api, gn_files, |
| 406 | output_api)) |
ehmaldonado | 5b1ba08 | 2016-09-02 05:51:08 -0700 | [diff] [blame] | 407 | return result |
| 408 | |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 409 | def CheckUnwantedDependencies(input_api, output_api): |
kjellander@webrtc.org | 3bd4156 | 2014-09-01 11:06:37 +0000 | [diff] [blame] | 410 | """Runs checkdeps on #include statements added in this |
| 411 | change. Breaking - rules is an error, breaking ! rules is a |
| 412 | warning. |
| 413 | """ |
| 414 | # Copied from Chromium's src/PRESUBMIT.py. |
| 415 | |
| 416 | # We need to wait until we have an input_api object and use this |
| 417 | # roundabout construct to import checkdeps because this file is |
| 418 | # eval-ed and thus doesn't have __file__. |
| 419 | original_sys_path = sys.path |
| 420 | try: |
kjellander@webrtc.org | aefe61a | 2014-12-08 13:00:30 +0000 | [diff] [blame] | 421 | checkdeps_path = input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 422 | 'buildtools', 'checkdeps') |
| 423 | if not os.path.exists(checkdeps_path): |
| 424 | return [output_api.PresubmitError( |
| 425 | 'Cannot find checkdeps at %s\nHave you run "gclient sync" to ' |
| 426 | 'download Chromium and setup the symlinks?' % checkdeps_path)] |
| 427 | sys.path.append(checkdeps_path) |
kjellander@webrtc.org | 3bd4156 | 2014-09-01 11:06:37 +0000 | [diff] [blame] | 428 | import checkdeps |
| 429 | from cpp_checker import CppChecker |
| 430 | from rules import Rule |
| 431 | finally: |
| 432 | # Restore sys.path to what it was before. |
| 433 | sys.path = original_sys_path |
| 434 | |
| 435 | added_includes = [] |
| 436 | for f in input_api.AffectedFiles(): |
| 437 | if not CppChecker.IsCppFile(f.LocalPath()): |
| 438 | continue |
| 439 | |
Henrik Kjellander | 57e5fd2 | 2015-05-25 12:55:39 +0200 | [diff] [blame] | 440 | changed_lines = [line for _, line in f.ChangedContents()] |
kjellander@webrtc.org | 3bd4156 | 2014-09-01 11:06:37 +0000 | [diff] [blame] | 441 | added_includes.append([f.LocalPath(), changed_lines]) |
| 442 | |
| 443 | deps_checker = checkdeps.DepsChecker(input_api.PresubmitLocalPath()) |
| 444 | |
| 445 | error_descriptions = [] |
| 446 | warning_descriptions = [] |
| 447 | for path, rule_type, rule_description in deps_checker.CheckAddedCppIncludes( |
| 448 | added_includes): |
| 449 | description_with_path = '%s\n %s' % (path, rule_description) |
| 450 | if rule_type == Rule.DISALLOW: |
| 451 | error_descriptions.append(description_with_path) |
| 452 | else: |
| 453 | warning_descriptions.append(description_with_path) |
| 454 | |
| 455 | results = [] |
| 456 | if error_descriptions: |
| 457 | results.append(output_api.PresubmitError( |
kjellander | a7066a3 | 2017-03-23 03:47:05 -0700 | [diff] [blame] | 458 | 'You added one or more #includes that violate checkdeps rules.\n' |
| 459 | 'Check that the DEPS files in these locations contain valid rules.\n' |
| 460 | 'See https://cs.chromium.org/chromium/src/buildtools/checkdeps/ for ' |
| 461 | 'more details about checkdeps.', |
kjellander@webrtc.org | 3bd4156 | 2014-09-01 11:06:37 +0000 | [diff] [blame] | 462 | error_descriptions)) |
| 463 | if warning_descriptions: |
| 464 | results.append(output_api.PresubmitPromptOrNotify( |
| 465 | 'You added one or more #includes of files that are temporarily\n' |
| 466 | 'allowed but being removed. Can you avoid introducing the\n' |
kjellander | a7066a3 | 2017-03-23 03:47:05 -0700 | [diff] [blame] | 467 | '#include? See relevant DEPS file(s) for details and contacts.\n' |
| 468 | 'See https://cs.chromium.org/chromium/src/buildtools/checkdeps/ for ' |
| 469 | 'more details about checkdeps.', |
kjellander@webrtc.org | 3bd4156 | 2014-09-01 11:06:37 +0000 | [diff] [blame] | 470 | warning_descriptions)) |
| 471 | return results |
| 472 | |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 473 | def CheckCommitMessageBugEntry(input_api, output_api): |
| 474 | """Check that bug entries are well-formed in commit message.""" |
| 475 | bogus_bug_msg = ( |
| 476 | 'Bogus BUG entry: %s. Please specify the issue tracker prefix and the ' |
| 477 | 'issue number, separated by a colon, e.g. webrtc:123 or chromium:12345.') |
| 478 | results = [] |
| 479 | for bug in (input_api.change.BUG or '').split(','): |
| 480 | bug = bug.strip() |
| 481 | if bug.lower() == 'none': |
| 482 | continue |
| 483 | if ':' not in bug: |
| 484 | try: |
| 485 | if int(bug) > 100000: |
| 486 | # Rough indicator for current chromium bugs. |
| 487 | prefix_guess = 'chromium' |
| 488 | else: |
| 489 | prefix_guess = 'webrtc' |
| 490 | results.append('BUG entry requires issue tracker prefix, e.g. %s:%s' % |
| 491 | (prefix_guess, bug)) |
| 492 | except ValueError: |
| 493 | results.append(bogus_bug_msg % bug) |
| 494 | elif not re.match(r'\w+:\d+', bug): |
| 495 | results.append(bogus_bug_msg % bug) |
| 496 | return [output_api.PresubmitError(r) for r in results] |
| 497 | |
| 498 | def CheckChangeHasBugField(input_api, output_api): |
kjellander | d1e26a9 | 2016-09-19 08:11:16 -0700 | [diff] [blame] | 499 | """Requires that the changelist have a BUG= field. |
| 500 | |
| 501 | This check is stricter than the one in depot_tools/presubmit_canned_checks.py |
| 502 | since it fails the presubmit if the BUG= field is missing or doesn't contain |
| 503 | a bug reference. |
| 504 | """ |
| 505 | if input_api.change.BUG: |
| 506 | return [] |
| 507 | else: |
| 508 | return [output_api.PresubmitError( |
| 509 | 'The BUG=[bug number] field is mandatory. Please create a bug and ' |
| 510 | 'reference it using either of:\n' |
| 511 | ' * https://bugs.webrtc.org - reference it using BUG=webrtc:XXXX\n' |
| 512 | ' * https://crbug.com - reference it using BUG=chromium:XXXXXX')] |
kjellander@webrtc.org | e415864 | 2014-08-06 09:11:18 +0000 | [diff] [blame] | 513 | |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 514 | def CheckJSONParseErrors(input_api, output_api): |
kjellander | 569cf94 | 2016-02-11 05:02:59 -0800 | [diff] [blame] | 515 | """Check that JSON files do not contain syntax errors.""" |
| 516 | |
| 517 | def FilterFile(affected_file): |
| 518 | return input_api.os_path.splitext(affected_file.LocalPath())[1] == '.json' |
| 519 | |
| 520 | def GetJSONParseError(input_api, filename): |
| 521 | try: |
| 522 | contents = input_api.ReadFile(filename) |
| 523 | input_api.json.loads(contents) |
| 524 | except ValueError as e: |
| 525 | return e |
| 526 | return None |
| 527 | |
| 528 | results = [] |
| 529 | for affected_file in input_api.AffectedFiles( |
| 530 | file_filter=FilterFile, include_deletes=False): |
| 531 | parse_error = GetJSONParseError(input_api, |
| 532 | affected_file.AbsoluteLocalPath()) |
| 533 | if parse_error: |
| 534 | results.append(output_api.PresubmitError('%s could not be parsed: %s' % |
| 535 | (affected_file.LocalPath(), parse_error))) |
| 536 | return results |
| 537 | |
| 538 | |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 539 | def RunPythonTests(input_api, output_api): |
kjellander | c88b5d5 | 2017-04-05 06:42:43 -0700 | [diff] [blame] | 540 | def Join(*args): |
Henrik Kjellander | 8d3ad82 | 2015-05-26 19:52:05 +0200 | [diff] [blame] | 541 | return input_api.os_path.join(input_api.PresubmitLocalPath(), *args) |
| 542 | |
| 543 | test_directories = [ |
Edward Lemur | 6d01f6d | 2017-09-14 17:02:01 +0200 | [diff] [blame] | 544 | input_api.PresubmitLocalPath(), |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 545 | Join('rtc_tools', 'py_event_log_analyzer'), |
| 546 | Join('rtc_tools'), |
| 547 | Join('audio', 'test', 'unittests'), |
ehmaldonado | 4fb9746 | 2017-01-30 05:27:22 -0800 | [diff] [blame] | 548 | ] + [ |
Henrik Kjellander | 90fd7d8 | 2017-05-09 08:30:10 +0200 | [diff] [blame] | 549 | root for root, _, files in os.walk(Join('tools_webrtc')) |
ehmaldonado | 4fb9746 | 2017-01-30 05:27:22 -0800 | [diff] [blame] | 550 | if any(f.endswith('_test.py') for f in files) |
Henrik Kjellander | 8d3ad82 | 2015-05-26 19:52:05 +0200 | [diff] [blame] | 551 | ] |
| 552 | |
| 553 | tests = [] |
| 554 | for directory in test_directories: |
| 555 | tests.extend( |
| 556 | input_api.canned_checks.GetUnitTestsInDirectory( |
| 557 | input_api, |
| 558 | output_api, |
| 559 | directory, |
| 560 | whitelist=[r'.+_test\.py$'])) |
| 561 | return input_api.RunTests(tests, parallel=True) |
| 562 | |
| 563 | |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 564 | def CheckUsageOfGoogleProtobufNamespace(input_api, output_api): |
mbonadei | 38415b2 | 2017-04-07 05:38:01 -0700 | [diff] [blame] | 565 | """Checks that the namespace google::protobuf has not been used.""" |
| 566 | files = [] |
| 567 | pattern = input_api.re.compile(r'google::protobuf') |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 568 | proto_utils_path = os.path.join('rtc_base', 'protobuf_utils.h') |
mbonadei | 38415b2 | 2017-04-07 05:38:01 -0700 | [diff] [blame] | 569 | for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile): |
| 570 | if f.LocalPath() in [proto_utils_path, 'PRESUBMIT.py']: |
| 571 | continue |
| 572 | contents = input_api.ReadFile(f) |
| 573 | if pattern.search(contents): |
| 574 | files.append(f) |
| 575 | |
| 576 | if files: |
| 577 | return [output_api.PresubmitError( |
| 578 | 'Please avoid to use namespace `google::protobuf` directly.\n' |
| 579 | 'Add a using directive in `%s` and include that header instead.' |
| 580 | % proto_utils_path, files)] |
| 581 | return [] |
| 582 | |
| 583 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 584 | def _LicenseHeader(input_api): |
| 585 | """Returns the license header regexp.""" |
| 586 | # Accept any year number from 2003 to the current year |
| 587 | current_year = int(input_api.time.strftime('%Y')) |
| 588 | allowed_years = (str(s) for s in reversed(xrange(2003, current_year + 1))) |
| 589 | years_re = '(' + '|'.join(allowed_years) + ')' |
| 590 | license_header = ( |
| 591 | r'.*? Copyright( \(c\))? %(year)s The WebRTC [Pp]roject [Aa]uthors\. ' |
| 592 | r'All [Rr]ights [Rr]eserved\.\n' |
| 593 | r'.*?\n' |
| 594 | r'.*? Use of this source code is governed by a BSD-style license\n' |
| 595 | r'.*? that can be found in the LICENSE file in the root of the source\n' |
| 596 | r'.*? tree\. An additional intellectual property rights grant can be ' |
| 597 | r'found\n' |
| 598 | r'.*? in the file PATENTS\. All contributing project authors may\n' |
| 599 | r'.*? be found in the AUTHORS file in the root of the source tree\.\n' |
| 600 | ) % { |
| 601 | 'year': years_re, |
| 602 | } |
| 603 | return license_header |
| 604 | |
| 605 | |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 606 | def CommonChecks(input_api, output_api): |
andrew@webrtc.org | 53df136 | 2012-01-26 21:24:23 +0000 | [diff] [blame] | 607 | """Checks common to both upload and commit.""" |
niklase@google.com | da159d6 | 2011-05-30 11:51:34 +0000 | [diff] [blame] | 608 | results = [] |
tkchin | 42f580e | 2015-11-26 23:18:23 -0800 | [diff] [blame] | 609 | # Filter out files that are in objc or ios dirs from being cpplint-ed since |
| 610 | # they do not follow C++ lint rules. |
| 611 | black_list = input_api.DEFAULT_BLACK_LIST + ( |
| 612 | r".*\bobjc[\\\/].*", |
Kári Tristan Helgason | 3fa3517 | 2016-09-09 08:55:05 +0000 | [diff] [blame] | 613 | r".*objc\.[hcm]+$", |
tkchin | 42f580e | 2015-11-26 23:18:23 -0800 | [diff] [blame] | 614 | ) |
| 615 | source_file_filter = lambda x: input_api.FilterSourceFile(x, None, black_list) |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 616 | results.extend(CheckApprovedFilesLintClean( |
tkchin | 42f580e | 2015-11-26 23:18:23 -0800 | [diff] [blame] | 617 | input_api, output_api, source_file_filter)) |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 618 | results.extend(input_api.canned_checks.CheckLicense( |
| 619 | input_api, output_api, _LicenseHeader(input_api))) |
phoglund@webrtc.org | 5d371393 | 2013-03-07 09:59:43 +0000 | [diff] [blame] | 620 | results.extend(input_api.canned_checks.RunPylint(input_api, output_api, |
kjellander@webrtc.org | 177567c | 2016-12-22 10:40:28 +0100 | [diff] [blame] | 621 | black_list=(r'^base[\\\/].*\.py$', |
Henrik Kjellander | 14771ac | 2015-06-02 13:10:04 +0200 | [diff] [blame] | 622 | r'^build[\\\/].*\.py$', |
| 623 | r'^buildtools[\\\/].*\.py$', |
kjellander | 38c65c8 | 2017-04-12 22:43:38 -0700 | [diff] [blame] | 624 | r'^infra[\\\/].*\.py$', |
Henrik Kjellander | 0779e8f | 2016-12-22 12:01:17 +0100 | [diff] [blame] | 625 | r'^ios[\\\/].*\.py$', |
Henrik Kjellander | 14771ac | 2015-06-02 13:10:04 +0200 | [diff] [blame] | 626 | r'^out.*[\\\/].*\.py$', |
| 627 | r'^testing[\\\/].*\.py$', |
| 628 | r'^third_party[\\\/].*\.py$', |
kjellander@webrtc.org | 177567c | 2016-12-22 10:40:28 +0100 | [diff] [blame] | 629 | r'^tools[\\\/].*\.py$', |
kjellander | afd5494 | 2016-12-17 12:21:39 -0800 | [diff] [blame] | 630 | # TODO(phoglund): should arguably be checked. |
Henrik Kjellander | 90fd7d8 | 2017-05-09 08:30:10 +0200 | [diff] [blame] | 631 | r'^tools_webrtc[\\\/]mb[\\\/].*\.py$', |
| 632 | r'^tools_webrtc[\\\/]valgrind[\\\/].*\.py$', |
Henrik Kjellander | 14771ac | 2015-06-02 13:10:04 +0200 | [diff] [blame] | 633 | r'^xcodebuild.*[\\\/].*\.py$',), |
Henrik Kjellander | 57e5fd2 | 2015-05-25 12:55:39 +0200 | [diff] [blame] | 634 | pylintrc='pylintrc')) |
kjellander | 569cf94 | 2016-02-11 05:02:59 -0800 | [diff] [blame] | 635 | |
nisse | 3d21e23 | 2016-09-02 03:07:06 -0700 | [diff] [blame] | 636 | # TODO(nisse): talk/ is no more, so make below checks simpler? |
Henrik Kjellander | 57e5fd2 | 2015-05-25 12:55:39 +0200 | [diff] [blame] | 637 | # WebRTC can't use the presubmit_canned_checks.PanProjectChecks function since |
| 638 | # we need to have different license checks in talk/ and webrtc/ directories. |
| 639 | # Instead, hand-picked checks are included below. |
Henrik Kjellander | 6322467 | 2015-09-08 08:03:56 +0200 | [diff] [blame] | 640 | |
tkchin | 3cd9a30 | 2016-06-08 12:40:28 -0700 | [diff] [blame] | 641 | # .m and .mm files are ObjC files. For simplicity we will consider .h files in |
| 642 | # ObjC subdirectories ObjC headers. |
| 643 | objc_filter_list = (r'.+\.m$', r'.+\.mm$', r'.+objc\/.+\.h$') |
Henrik Kjellander | b4af3d6 | 2016-11-16 20:11:29 +0100 | [diff] [blame] | 644 | # Skip long-lines check for DEPS and GN files. |
| 645 | build_file_filter_list = (r'.+\.gn$', r'.+\.gni$', 'DEPS') |
tkchin | 3cd9a30 | 2016-06-08 12:40:28 -0700 | [diff] [blame] | 646 | eighty_char_sources = lambda x: input_api.FilterSourceFile(x, |
| 647 | black_list=build_file_filter_list + objc_filter_list) |
| 648 | hundred_char_sources = lambda x: input_api.FilterSourceFile(x, |
| 649 | white_list=objc_filter_list) |
andrew@webrtc.org | 2442de1 | 2012-01-23 17:45:41 +0000 | [diff] [blame] | 650 | results.extend(input_api.canned_checks.CheckLongLines( |
tkchin | 3cd9a30 | 2016-06-08 12:40:28 -0700 | [diff] [blame] | 651 | input_api, output_api, maxlen=80, source_file_filter=eighty_char_sources)) |
| 652 | results.extend(input_api.canned_checks.CheckLongLines( |
| 653 | input_api, output_api, maxlen=100, |
| 654 | source_file_filter=hundred_char_sources)) |
| 655 | |
andrew@webrtc.org | 2442de1 | 2012-01-23 17:45:41 +0000 | [diff] [blame] | 656 | results.extend(input_api.canned_checks.CheckChangeHasNoTabs( |
| 657 | input_api, output_api)) |
andrew@webrtc.org | 53df136 | 2012-01-26 21:24:23 +0000 | [diff] [blame] | 658 | results.extend(input_api.canned_checks.CheckChangeHasNoStrayWhitespace( |
| 659 | input_api, output_api)) |
kjellander | e5dc62a | 2016-12-14 00:16:21 -0800 | [diff] [blame] | 660 | results.extend(input_api.canned_checks.CheckAuthorizedAuthor( |
| 661 | input_api, output_api)) |
andrew@webrtc.org | 53df136 | 2012-01-26 21:24:23 +0000 | [diff] [blame] | 662 | results.extend(input_api.canned_checks.CheckChangeTodoHasOwner( |
| 663 | input_api, output_api)) |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 664 | results.extend(CheckNativeApiHeaderChanges(input_api, output_api)) |
| 665 | results.extend(CheckNoIOStreamInHeaders(input_api, output_api)) |
| 666 | results.extend(CheckNoPragmaOnce(input_api, output_api)) |
| 667 | results.extend(CheckNoFRIEND_TEST(input_api, output_api)) |
| 668 | results.extend(CheckGnChanges(input_api, output_api)) |
| 669 | results.extend(CheckUnwantedDependencies(input_api, output_api)) |
| 670 | results.extend(CheckJSONParseErrors(input_api, output_api)) |
| 671 | results.extend(RunPythonTests(input_api, output_api)) |
| 672 | results.extend(CheckUsageOfGoogleProtobufNamespace(input_api, output_api)) |
Mirko Bonadei | 866d337 | 2017-09-15 12:35:26 +0200 | [diff] [blame] | 673 | results.extend(CheckOrphanHeaders(input_api, output_api)) |
Mirko Bonadei | a730c1c | 2017-09-18 11:33:13 +0200 | [diff] [blame] | 674 | results.extend(CheckNewlineAtTheEndOfProtoFiles(input_api, output_api)) |
andrew@webrtc.org | 53df136 | 2012-01-26 21:24:23 +0000 | [diff] [blame] | 675 | return results |
andrew@webrtc.org | 2442de1 | 2012-01-23 17:45:41 +0000 | [diff] [blame] | 676 | |
kjellander@webrtc.org | e415864 | 2014-08-06 09:11:18 +0000 | [diff] [blame] | 677 | |
andrew@webrtc.org | 53df136 | 2012-01-26 21:24:23 +0000 | [diff] [blame] | 678 | def CheckChangeOnUpload(input_api, output_api): |
| 679 | results = [] |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 680 | results.extend(CommonChecks(input_api, output_api)) |
Henrik Kjellander | 57e5fd2 | 2015-05-25 12:55:39 +0200 | [diff] [blame] | 681 | results.extend( |
| 682 | input_api.canned_checks.CheckGNFormatted(input_api, output_api)) |
niklase@google.com | da159d6 | 2011-05-30 11:51:34 +0000 | [diff] [blame] | 683 | return results |
| 684 | |
kjellander@webrtc.org | e415864 | 2014-08-06 09:11:18 +0000 | [diff] [blame] | 685 | |
andrew@webrtc.org | 2442de1 | 2012-01-23 17:45:41 +0000 | [diff] [blame] | 686 | def CheckChangeOnCommit(input_api, output_api): |
niklase@google.com | 1198db9 | 2011-06-09 07:07:24 +0000 | [diff] [blame] | 687 | results = [] |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 688 | results.extend(CommonChecks(input_api, output_api)) |
| 689 | results.extend(VerifyNativeApiHeadersListIsValid(input_api, output_api)) |
niklase@google.com | 1198db9 | 2011-06-09 07:07:24 +0000 | [diff] [blame] | 690 | results.extend(input_api.canned_checks.CheckOwners(input_api, output_api)) |
andrew@webrtc.org | 53df136 | 2012-01-26 21:24:23 +0000 | [diff] [blame] | 691 | results.extend(input_api.canned_checks.CheckChangeWasUploaded( |
| 692 | input_api, output_api)) |
| 693 | results.extend(input_api.canned_checks.CheckChangeHasDescription( |
| 694 | input_api, output_api)) |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 695 | results.extend(CheckChangeHasBugField(input_api, output_api)) |
| 696 | results.extend(CheckCommitMessageBugEntry(input_api, output_api)) |
kjellander@webrtc.org | 12cb88c | 2014-02-13 11:53:43 +0000 | [diff] [blame] | 697 | results.extend(input_api.canned_checks.CheckTreeIsOpen( |
| 698 | input_api, output_api, |
| 699 | json_url='http://webrtc-status.appspot.com/current?format=json')) |
niklase@google.com | 1198db9 | 2011-06-09 07:07:24 +0000 | [diff] [blame] | 700 | return results |
mbonadei | 74973ed | 2017-05-09 07:58:05 -0700 | [diff] [blame] | 701 | |
| 702 | |
charujain | 9893e25 | 2017-09-14 13:33:22 +0200 | [diff] [blame] | 703 | def CheckOrphanHeaders(input_api, output_api): |
mbonadei | 74973ed | 2017-05-09 07:58:05 -0700 | [diff] [blame] | 704 | # We need to wait until we have an input_api object and use this |
| 705 | # roundabout construct to import prebubmit_checks_lib because this file is |
| 706 | # eval-ed and thus doesn't have __file__. |
| 707 | error_msg = """Header file {} is not listed in any GN target. |
| 708 | Please create a target or add it to an existing one in {}""" |
| 709 | results = [] |
| 710 | original_sys_path = sys.path |
| 711 | try: |
| 712 | sys.path = sys.path + [input_api.os_path.join( |
| 713 | input_api.PresubmitLocalPath(), 'tools_webrtc', 'presubmit_checks_lib')] |
| 714 | from check_orphan_headers import GetBuildGnPathFromFilePath |
| 715 | from check_orphan_headers import IsHeaderInBuildGn |
| 716 | finally: |
| 717 | # Restore sys.path to what it was before. |
| 718 | sys.path = original_sys_path |
| 719 | |
| 720 | for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile): |
mbonadei | a644ad3 | 2017-05-10 05:21:55 -0700 | [diff] [blame] | 721 | if f.LocalPath().endswith('.h') and f.Action() == 'A': |
mbonadei | 74973ed | 2017-05-09 07:58:05 -0700 | [diff] [blame] | 722 | file_path = os.path.abspath(f.LocalPath()) |
| 723 | root_dir = os.getcwd() |
| 724 | gn_file_path = GetBuildGnPathFromFilePath(file_path, os.path.exists, |
| 725 | root_dir) |
| 726 | in_build_gn = IsHeaderInBuildGn(file_path, gn_file_path) |
| 727 | if not in_build_gn: |
| 728 | results.append(output_api.PresubmitError(error_msg.format( |
| 729 | file_path, gn_file_path))) |
| 730 | return results |
Mirko Bonadei | 960fd5b | 2017-06-29 14:59:36 +0200 | [diff] [blame] | 731 | |
| 732 | |
Mirko Bonadei | a730c1c | 2017-09-18 11:33:13 +0200 | [diff] [blame] | 733 | def CheckNewlineAtTheEndOfProtoFiles(input_api, output_api): |
Mirko Bonadei | 960fd5b | 2017-06-29 14:59:36 +0200 | [diff] [blame] | 734 | """Checks that all .proto files are terminated with a newline.""" |
| 735 | error_msg = 'File {} must end with exactly one newline.' |
| 736 | results = [] |
| 737 | source_file_filter = lambda x: input_api.FilterSourceFile( |
| 738 | x, white_list=(r'.+\.proto$',)) |
| 739 | for f in input_api.AffectedSourceFiles(source_file_filter): |
| 740 | file_path = f.LocalPath() |
| 741 | with open(file_path) as f: |
| 742 | lines = f.readlines() |
Mirko Bonadei | a730c1c | 2017-09-18 11:33:13 +0200 | [diff] [blame] | 743 | if len(lines) > 0 and not lines[-1].endswith('\n'): |
Mirko Bonadei | 960fd5b | 2017-06-29 14:59:36 +0200 | [diff] [blame] | 744 | results.append(output_api.PresubmitError(error_msg.format(file_path))) |
| 745 | return results |