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 |
kjellander@webrtc.org | 8575980 | 2013-10-22 16:47:40 +0000 | [diff] [blame] | 14 | |
| 15 | |
kjellander@webrtc.org | 0fcaf99 | 2015-11-26 15:24:52 +0100 | [diff] [blame] | 16 | # Directories that will be scanned by cpplint by the presubmit script. |
| 17 | CPPLINT_DIRS = [ |
oprypin | 803dc29 | 2017-02-01 01:55:59 -0800 | [diff] [blame] | 18 | 'webrtc/api', |
Fredrik Solenberg | ea07373 | 2015-12-01 11:26:34 +0100 | [diff] [blame] | 19 | 'webrtc/audio', |
| 20 | 'webrtc/call', |
oprypin | 67fdb80 | 2017-03-09 06:25:06 -0800 | [diff] [blame] | 21 | 'webrtc/common_audio', |
jbauch | 0f2e939 | 2015-12-10 03:11:42 -0800 | [diff] [blame] | 22 | 'webrtc/common_video', |
jbauch | 70625e5 | 2015-12-09 14:18:14 -0800 | [diff] [blame] | 23 | 'webrtc/examples', |
aleloi | df9e4d9 | 2016-08-08 10:26:09 -0700 | [diff] [blame] | 24 | 'webrtc/modules/audio_mixer', |
jbauch | f91e6d0 | 2016-01-24 23:05:21 -0800 | [diff] [blame] | 25 | 'webrtc/modules/bitrate_controller', |
Stefan Holmer | 80e1207 | 2016-02-23 13:30:42 +0100 | [diff] [blame] | 26 | 'webrtc/modules/congestion_controller', |
jbauch | d2a2296 | 2016-02-08 23:18:25 -0800 | [diff] [blame] | 27 | 'webrtc/modules/pacing', |
terelius | 8f09f17 | 2015-12-15 00:51:54 -0800 | [diff] [blame] | 28 | 'webrtc/modules/remote_bitrate_estimator', |
danilchap | 377b5e6 | 2015-12-15 04:33:44 -0800 | [diff] [blame] | 29 | 'webrtc/modules/rtp_rtcp', |
philipel | 5908c71 | 2015-12-21 08:23:20 -0800 | [diff] [blame] | 30 | 'webrtc/modules/video_coding', |
mflodman | 88eeac4 | 2015-12-08 09:21:28 +0100 | [diff] [blame] | 31 | 'webrtc/modules/video_processing', |
jbauch | 0f2e939 | 2015-12-10 03:11:42 -0800 | [diff] [blame] | 32 | 'webrtc/tools', |
mflodman | d1590b2 | 2015-12-09 07:07:59 -0800 | [diff] [blame] | 33 | 'webrtc/video', |
kjellander@webrtc.org | 0fcaf99 | 2015-11-26 15:24:52 +0100 | [diff] [blame] | 34 | ] |
| 35 | |
jbauch | c4e3ead | 2016-02-19 00:25:55 -0800 | [diff] [blame] | 36 | # These filters will always be removed, even if the caller specifies a filter |
| 37 | # set, as they are problematic or broken in some way. |
| 38 | # |
| 39 | # Justifications for each filter: |
| 40 | # - build/c++11 : Rvalue ref checks are unreliable (false positives), |
| 41 | # include file and feature blacklists are |
| 42 | # google3-specific. |
kjellander | e5a87a5 | 2016-04-27 02:32:12 -0700 | [diff] [blame] | 43 | # - whitespace/operators: Same as above (doesn't seem sufficient to eliminate |
| 44 | # all move-related errors). |
jbauch | c4e3ead | 2016-02-19 00:25:55 -0800 | [diff] [blame] | 45 | BLACKLIST_LINT_FILTERS = [ |
| 46 | '-build/c++11', |
kjellander | e5a87a5 | 2016-04-27 02:32:12 -0700 | [diff] [blame] | 47 | '-whitespace/operators', |
jbauch | c4e3ead | 2016-02-19 00:25:55 -0800 | [diff] [blame] | 48 | ] |
| 49 | |
kjellander | fd59523 | 2015-12-04 02:44:09 -0800 | [diff] [blame] | 50 | # List of directories of "supported" native APIs. That means changes to headers |
| 51 | # will be done in a compatible way following this scheme: |
| 52 | # 1. Non-breaking changes are made. |
| 53 | # 2. The old APIs as marked as deprecated (with comments). |
| 54 | # 3. Deprecation is announced to discuss-webrtc@googlegroups.com and |
| 55 | # webrtc-users@google.com (internal list). |
| 56 | # 4. (later) The deprecated APIs are removed. |
kjellander | 53047c9 | 2015-12-02 23:56:14 -0800 | [diff] [blame] | 57 | NATIVE_API_DIRS = ( |
kjellander | 53047c9 | 2015-12-02 23:56:14 -0800 | [diff] [blame] | 58 | 'webrtc', |
kjellander | dd70547 | 2016-06-09 11:17:27 -0700 | [diff] [blame] | 59 | 'webrtc/api', |
| 60 | 'webrtc/media', |
kjellander | 53047c9 | 2015-12-02 23:56:14 -0800 | [diff] [blame] | 61 | 'webrtc/modules/audio_device/include', |
kjellander | dd70547 | 2016-06-09 11:17:27 -0700 | [diff] [blame] | 62 | 'webrtc/pc', |
| 63 | ) |
| 64 | # These directories should not be used but are maintained only to avoid breaking |
| 65 | # some legacy downstream code. |
| 66 | LEGACY_API_DIRS = ( |
kjellander | dd70547 | 2016-06-09 11:17:27 -0700 | [diff] [blame] | 67 | 'webrtc/base', |
| 68 | 'webrtc/common_audio/include', |
| 69 | 'webrtc/modules/audio_coding/include', |
| 70 | 'webrtc/modules/audio_conference_mixer/include', |
kjellander | 53047c9 | 2015-12-02 23:56:14 -0800 | [diff] [blame] | 71 | 'webrtc/modules/audio_processing/include', |
| 72 | 'webrtc/modules/bitrate_controller/include', |
Stefan Holmer | 80e1207 | 2016-02-23 13:30:42 +0100 | [diff] [blame] | 73 | 'webrtc/modules/congestion_controller/include', |
kjellander | 53047c9 | 2015-12-02 23:56:14 -0800 | [diff] [blame] | 74 | 'webrtc/modules/include', |
| 75 | 'webrtc/modules/remote_bitrate_estimator/include', |
| 76 | 'webrtc/modules/rtp_rtcp/include', |
kjellander | dd70547 | 2016-06-09 11:17:27 -0700 | [diff] [blame] | 77 | 'webrtc/modules/rtp_rtcp/source', |
kjellander | 53047c9 | 2015-12-02 23:56:14 -0800 | [diff] [blame] | 78 | 'webrtc/modules/utility/include', |
| 79 | 'webrtc/modules/video_coding/codecs/h264/include', |
| 80 | 'webrtc/modules/video_coding/codecs/i420/include', |
| 81 | 'webrtc/modules/video_coding/codecs/vp8/include', |
| 82 | 'webrtc/modules/video_coding/codecs/vp9/include', |
| 83 | 'webrtc/modules/video_coding/include', |
kjellander | dd70547 | 2016-06-09 11:17:27 -0700 | [diff] [blame] | 84 | 'webrtc/system_wrappers/include', |
kjellander | 53047c9 | 2015-12-02 23:56:14 -0800 | [diff] [blame] | 85 | 'webrtc/voice_engine/include', |
| 86 | ) |
kjellander | dd70547 | 2016-06-09 11:17:27 -0700 | [diff] [blame] | 87 | API_DIRS = NATIVE_API_DIRS[:] + LEGACY_API_DIRS[:] |
kjellander | 53047c9 | 2015-12-02 23:56:14 -0800 | [diff] [blame] | 88 | |
| 89 | |
ehmaldonado | 4fb9746 | 2017-01-30 05:27:22 -0800 | [diff] [blame] | 90 | def _RunCommand(command, cwd): |
| 91 | """Runs a command and returns the output from that command.""" |
| 92 | p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, |
| 93 | cwd=cwd) |
| 94 | stdout = p.stdout.read() |
| 95 | stderr = p.stderr.read() |
| 96 | p.wait() |
| 97 | p.stdout.close() |
| 98 | p.stderr.close() |
| 99 | return p.returncode, stdout, stderr |
| 100 | |
| 101 | |
kjellander | 53047c9 | 2015-12-02 23:56:14 -0800 | [diff] [blame] | 102 | def _VerifyNativeApiHeadersListIsValid(input_api, output_api): |
| 103 | """Ensures the list of native API header directories is up to date.""" |
| 104 | non_existing_paths = [] |
| 105 | native_api_full_paths = [ |
| 106 | input_api.os_path.join(input_api.PresubmitLocalPath(), |
kjellander | dd70547 | 2016-06-09 11:17:27 -0700 | [diff] [blame] | 107 | *path.split('/')) for path in API_DIRS] |
kjellander | 53047c9 | 2015-12-02 23:56:14 -0800 | [diff] [blame] | 108 | for path in native_api_full_paths: |
| 109 | if not os.path.isdir(path): |
| 110 | non_existing_paths.append(path) |
| 111 | if non_existing_paths: |
| 112 | return [output_api.PresubmitError( |
| 113 | 'Directories to native API headers have changed which has made the ' |
| 114 | 'list in PRESUBMIT.py outdated.\nPlease update it to the current ' |
| 115 | 'location of our native APIs.', |
| 116 | non_existing_paths)] |
| 117 | return [] |
| 118 | |
kjellander | 94f4d9e | 2017-03-09 06:09:33 -0800 | [diff] [blame] | 119 | api_change_msg = """ |
kwiberg | eb13302 | 2016-04-07 07:41:48 -0700 | [diff] [blame] | 120 | 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] | 121 | 1. Make compatible changes that don't break existing clients. Usually |
| 122 | this is done by keeping the existing method signatures unchanged. |
| 123 | 2. Mark the old stuff as deprecated (see RTC_DEPRECATED macro). |
kwiberg | eb13302 | 2016-04-07 07:41:48 -0700 | [diff] [blame] | 124 | 3. Create a timeline and plan for when the deprecated stuff will be |
| 125 | removed. (The amount of time we give users to change their code |
| 126 | should be informed by how much work it is for them. If they just |
| 127 | need to replace one name with another or something equally |
| 128 | simple, 1-2 weeks might be good; if they need to do serious work, |
| 129 | up to 3 months may be called for.) |
| 130 | 4. Update/inform existing downstream code owners to stop using the |
| 131 | deprecated stuff. (Send announcements to |
| 132 | discuss-webrtc@googlegroups.com and webrtc-users@google.com.) |
| 133 | 5. Remove the deprecated stuff, once the agreed-upon amount of time |
| 134 | has passed. |
| 135 | Related files: |
| 136 | """ |
kjellander | 53047c9 | 2015-12-02 23:56:14 -0800 | [diff] [blame] | 137 | |
| 138 | def _CheckNativeApiHeaderChanges(input_api, output_api): |
| 139 | """Checks to remind proper changing of native APIs.""" |
| 140 | files = [] |
| 141 | for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile): |
| 142 | if f.LocalPath().endswith('.h'): |
kjellander | dd70547 | 2016-06-09 11:17:27 -0700 | [diff] [blame] | 143 | for path in API_DIRS: |
kjellander | 53047c9 | 2015-12-02 23:56:14 -0800 | [diff] [blame] | 144 | if os.path.dirname(f.LocalPath()) == path: |
| 145 | files.append(f) |
| 146 | |
| 147 | if files: |
kjellander | 94f4d9e | 2017-03-09 06:09:33 -0800 | [diff] [blame] | 148 | return [output_api.PresubmitNotifyResult(api_change_msg, files)] |
kjellander | 53047c9 | 2015-12-02 23:56:14 -0800 | [diff] [blame] | 149 | return [] |
| 150 | |
kjellander@webrtc.org | 0fcaf99 | 2015-11-26 15:24:52 +0100 | [diff] [blame] | 151 | |
kjellander@webrtc.org | 51198f1 | 2012-02-21 17:53:46 +0000 | [diff] [blame] | 152 | def _CheckNoIOStreamInHeaders(input_api, output_api): |
| 153 | """Checks to make sure no .h files include <iostream>.""" |
| 154 | files = [] |
| 155 | pattern = input_api.re.compile(r'^#include\s*<iostream>', |
| 156 | input_api.re.MULTILINE) |
| 157 | for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile): |
| 158 | if not f.LocalPath().endswith('.h'): |
| 159 | continue |
| 160 | contents = input_api.ReadFile(f) |
| 161 | if pattern.search(contents): |
| 162 | files.append(f) |
| 163 | |
| 164 | if len(files): |
Henrik Kjellander | 57e5fd2 | 2015-05-25 12:55:39 +0200 | [diff] [blame] | 165 | return [output_api.PresubmitError( |
kjellander@webrtc.org | 51198f1 | 2012-02-21 17:53:46 +0000 | [diff] [blame] | 166 | 'Do not #include <iostream> in header files, since it inserts static ' + |
| 167 | 'initialization into every file including the header. Instead, ' + |
| 168 | '#include <ostream>. See http://crbug.com/94794', |
Henrik Kjellander | 57e5fd2 | 2015-05-25 12:55:39 +0200 | [diff] [blame] | 169 | files)] |
kjellander@webrtc.org | 51198f1 | 2012-02-21 17:53:46 +0000 | [diff] [blame] | 170 | return [] |
| 171 | |
kjellander@webrtc.org | e415864 | 2014-08-06 09:11:18 +0000 | [diff] [blame] | 172 | |
kjellander | 6aeef74 | 2017-02-20 01:13:18 -0800 | [diff] [blame] | 173 | def _CheckNoPragmaOnce(input_api, output_api): |
| 174 | """Make sure that banned functions are not used.""" |
| 175 | files = [] |
| 176 | pattern = input_api.re.compile(r'^#pragma\s+once', |
| 177 | input_api.re.MULTILINE) |
| 178 | for f in input_api.AffectedSourceFiles(input_api.FilterSourceFile): |
| 179 | if not f.LocalPath().endswith('.h'): |
| 180 | continue |
| 181 | contents = input_api.ReadFile(f) |
| 182 | if pattern.search(contents): |
| 183 | files.append(f) |
| 184 | |
| 185 | if files: |
| 186 | return [output_api.PresubmitError( |
| 187 | 'Do not use #pragma once in header files.\n' |
| 188 | 'See http://www.chromium.org/developers/coding-style#TOC-File-headers', |
| 189 | files)] |
| 190 | return [] |
| 191 | |
| 192 | |
kjellander | 94f4d9e | 2017-03-09 06:09:33 -0800 | [diff] [blame] | 193 | def _CheckNoFRIEND_TEST(input_api, output_api): |
kjellander@webrtc.org | 51198f1 | 2012-02-21 17:53:46 +0000 | [diff] [blame] | 194 | """Make sure that gtest's FRIEND_TEST() macro is not used, the |
| 195 | FRIEND_TEST_ALL_PREFIXES() macro from testsupport/gtest_prod_util.h should be |
| 196 | used instead since that allows for FLAKY_, FAILS_ and DISABLED_ prefixes.""" |
| 197 | problems = [] |
| 198 | |
| 199 | file_filter = lambda f: f.LocalPath().endswith(('.cc', '.h')) |
| 200 | for f in input_api.AffectedFiles(file_filter=file_filter): |
| 201 | for line_num, line in f.ChangedContents(): |
| 202 | if 'FRIEND_TEST(' in line: |
| 203 | problems.append(' %s:%d' % (f.LocalPath(), line_num)) |
| 204 | |
| 205 | if not problems: |
| 206 | return [] |
| 207 | return [output_api.PresubmitPromptWarning('WebRTC\'s code should not use ' |
| 208 | 'gtest\'s FRIEND_TEST() macro. Include testsupport/gtest_prod_util.h and ' |
| 209 | 'use FRIEND_TEST_ALL_PREFIXES() instead.\n' + '\n'.join(problems))] |
| 210 | |
kjellander@webrtc.org | e415864 | 2014-08-06 09:11:18 +0000 | [diff] [blame] | 211 | |
kjellander@webrtc.org | 0fcaf99 | 2015-11-26 15:24:52 +0100 | [diff] [blame] | 212 | def _IsLintWhitelisted(whitelist_dirs, file_path): |
| 213 | """ Checks if a file is whitelisted for lint check.""" |
| 214 | for path in whitelist_dirs: |
| 215 | if os.path.dirname(file_path).startswith(path): |
| 216 | return True |
| 217 | return False |
| 218 | |
| 219 | |
mflodman@webrtc.org | 2a45209 | 2012-07-01 05:55:23 +0000 | [diff] [blame] | 220 | def _CheckApprovedFilesLintClean(input_api, output_api, |
| 221 | source_file_filter=None): |
| 222 | """Checks that all new or whitelisted .cc and .h files pass cpplint.py. |
kjellander@webrtc.org | 51198f1 | 2012-02-21 17:53:46 +0000 | [diff] [blame] | 223 | This check is based on _CheckChangeLintsClean in |
| 224 | depot_tools/presubmit_canned_checks.py but has less filters and only checks |
| 225 | added files.""" |
| 226 | result = [] |
| 227 | |
| 228 | # Initialize cpplint. |
| 229 | import cpplint |
| 230 | # Access to a protected member _XX of a client class |
| 231 | # pylint: disable=W0212 |
| 232 | cpplint._cpplint_state.ResetErrorCounts() |
| 233 | |
jbauch | c4e3ead | 2016-02-19 00:25:55 -0800 | [diff] [blame] | 234 | lint_filters = cpplint._Filters() |
| 235 | lint_filters.extend(BLACKLIST_LINT_FILTERS) |
| 236 | cpplint._SetFilters(','.join(lint_filters)) |
| 237 | |
kjellander@webrtc.org | 0fcaf99 | 2015-11-26 15:24:52 +0100 | [diff] [blame] | 238 | # Create a platform independent whitelist for the CPPLINT_DIRS. |
| 239 | whitelist_dirs = [input_api.os_path.join(*path.split('/')) |
| 240 | for path in CPPLINT_DIRS] |
| 241 | |
kjellander@webrtc.org | 51198f1 | 2012-02-21 17:53:46 +0000 | [diff] [blame] | 242 | # Use the strictest verbosity level for cpplint.py (level 1) which is the |
| 243 | # default when running cpplint.py from command line. |
| 244 | # To make it possible to work with not-yet-converted code, we're only applying |
mflodman@webrtc.org | 2a45209 | 2012-07-01 05:55:23 +0000 | [diff] [blame] | 245 | # it to new (or moved/renamed) files and files listed in LINT_FOLDERS. |
kjellander@webrtc.org | 51198f1 | 2012-02-21 17:53:46 +0000 | [diff] [blame] | 246 | verbosity_level = 1 |
| 247 | files = [] |
| 248 | for f in input_api.AffectedSourceFiles(source_file_filter): |
Henrik Kjellander | 57e5fd2 | 2015-05-25 12:55:39 +0200 | [diff] [blame] | 249 | # Note that moved/renamed files also count as added. |
kjellander@webrtc.org | 0fcaf99 | 2015-11-26 15:24:52 +0100 | [diff] [blame] | 250 | if f.Action() == 'A' or _IsLintWhitelisted(whitelist_dirs, f.LocalPath()): |
kjellander@webrtc.org | 51198f1 | 2012-02-21 17:53:46 +0000 | [diff] [blame] | 251 | files.append(f.AbsoluteLocalPath()) |
mflodman@webrtc.org | 2a45209 | 2012-07-01 05:55:23 +0000 | [diff] [blame] | 252 | |
kjellander@webrtc.org | 51198f1 | 2012-02-21 17:53:46 +0000 | [diff] [blame] | 253 | for file_name in files: |
| 254 | cpplint.ProcessFile(file_name, verbosity_level) |
| 255 | |
| 256 | if cpplint._cpplint_state.error_count > 0: |
| 257 | if input_api.is_committing: |
oprypin | 8e58d65 | 2017-03-21 07:52:41 -0700 | [diff] [blame^] | 258 | res_type = output_api.PresubmitError |
kjellander@webrtc.org | 51198f1 | 2012-02-21 17:53:46 +0000 | [diff] [blame] | 259 | else: |
| 260 | res_type = output_api.PresubmitPromptWarning |
| 261 | result = [res_type('Changelist failed cpplint.py check.')] |
| 262 | |
| 263 | return result |
| 264 | |
Henrik Kjellander | b4af3d6 | 2016-11-16 20:11:29 +0100 | [diff] [blame] | 265 | def _CheckNoSourcesAbove(input_api, gn_files, output_api): |
ehmaldonado | 5b1ba08 | 2016-09-02 05:51:08 -0700 | [diff] [blame] | 266 | # Disallow referencing source files with paths above the GN file location. |
| 267 | source_pattern = input_api.re.compile(r' +sources \+?= \[(.*?)\]', |
| 268 | re.MULTILINE | re.DOTALL) |
| 269 | file_pattern = input_api.re.compile(r'"((\.\./.*?)|(//.*?))"') |
| 270 | violating_gn_files = set() |
| 271 | violating_source_entries = [] |
| 272 | for gn_file in gn_files: |
| 273 | contents = input_api.ReadFile(gn_file) |
| 274 | for source_block_match in source_pattern.finditer(contents): |
| 275 | # Find all source list entries starting with ../ in the source block |
| 276 | # (exclude overrides entries). |
| 277 | for file_list_match in file_pattern.finditer(source_block_match.group(1)): |
| 278 | source_file = file_list_match.group(1) |
| 279 | if 'overrides/' not in source_file: |
| 280 | violating_source_entries.append(source_file) |
| 281 | violating_gn_files.add(gn_file) |
| 282 | if violating_gn_files: |
| 283 | return [output_api.PresubmitError( |
| 284 | 'Referencing source files above the directory of the GN file is not ' |
Henrik Kjellander | b4af3d6 | 2016-11-16 20:11:29 +0100 | [diff] [blame] | 285 | 'allowed. Please introduce new GN targets in the proper location ' |
| 286 | 'instead.\n' |
ehmaldonado | 5b1ba08 | 2016-09-02 05:51:08 -0700 | [diff] [blame] | 287 | 'Invalid source entries:\n' |
| 288 | '%s\n' |
| 289 | 'Violating GN files:' % '\n'.join(violating_source_entries), |
| 290 | items=violating_gn_files)] |
| 291 | return [] |
| 292 | |
kjellander | 7439f97 | 2016-12-05 22:47:46 -0800 | [diff] [blame] | 293 | def _CheckNoMixingCAndCCSources(input_api, gn_files, output_api): |
| 294 | # Disallow mixing .c and .cc source files in the same target. |
| 295 | source_pattern = input_api.re.compile(r' +sources \+?= \[(.*?)\]', |
| 296 | re.MULTILINE | re.DOTALL) |
| 297 | file_pattern = input_api.re.compile(r'"(.*)"') |
| 298 | violating_gn_files = dict() |
| 299 | for gn_file in gn_files: |
| 300 | contents = input_api.ReadFile(gn_file) |
| 301 | for source_block_match in source_pattern.finditer(contents): |
| 302 | c_files = [] |
| 303 | cc_files = [] |
| 304 | for file_list_match in file_pattern.finditer(source_block_match.group(1)): |
| 305 | source_file = file_list_match.group(1) |
| 306 | if source_file.endswith('.c'): |
| 307 | c_files.append(source_file) |
| 308 | if source_file.endswith('.cc'): |
| 309 | cc_files.append(source_file) |
| 310 | if c_files and cc_files: |
| 311 | violating_gn_files[gn_file.LocalPath()] = sorted(c_files + cc_files) |
| 312 | if violating_gn_files: |
| 313 | return [output_api.PresubmitError( |
| 314 | 'GN targets cannot mix .cc and .c source files. Please create a ' |
| 315 | 'separate target for each collection of sources.\n' |
| 316 | 'Mixed sources: \n' |
| 317 | '%s\n' |
| 318 | 'Violating GN files:' % json.dumps(violating_gn_files, indent=2), |
| 319 | items=violating_gn_files.keys())] |
| 320 | return [] |
| 321 | |
ehmaldonado | 4fb9746 | 2017-01-30 05:27:22 -0800 | [diff] [blame] | 322 | def _CheckNoPackageBoundaryViolations(input_api, gn_files, output_api): |
| 323 | cwd = input_api.PresubmitLocalPath() |
| 324 | script_path = os.path.join('tools-webrtc', 'check_package_boundaries.py') |
| 325 | webrtc_path = os.path.join('webrtc') |
| 326 | command = [sys.executable, script_path, webrtc_path] |
| 327 | command += [gn_file.LocalPath() for gn_file in gn_files] |
| 328 | returncode, _, stderr = _RunCommand(command, cwd) |
| 329 | if returncode: |
| 330 | return [output_api.PresubmitError( |
| 331 | 'There are package boundary violations in the following GN files:\n\n' |
| 332 | '%s' % stderr)] |
| 333 | return [] |
| 334 | |
ehmaldonado | 5b1ba08 | 2016-09-02 05:51:08 -0700 | [diff] [blame] | 335 | def _CheckGnChanges(input_api, output_api): |
| 336 | source_file_filter = lambda x: input_api.FilterSourceFile( |
| 337 | x, white_list=(r'.+\.(gn|gni)$',)) |
| 338 | |
| 339 | gn_files = [] |
| 340 | for f in input_api.AffectedSourceFiles(source_file_filter): |
| 341 | if f.LocalPath().startswith('webrtc'): |
| 342 | gn_files.append(f) |
| 343 | |
| 344 | result = [] |
| 345 | if gn_files: |
Henrik Kjellander | b4af3d6 | 2016-11-16 20:11:29 +0100 | [diff] [blame] | 346 | result.extend(_CheckNoSourcesAbove(input_api, gn_files, output_api)) |
kjellander | 7439f97 | 2016-12-05 22:47:46 -0800 | [diff] [blame] | 347 | result.extend(_CheckNoMixingCAndCCSources(input_api, gn_files, output_api)) |
ehmaldonado | 4fb9746 | 2017-01-30 05:27:22 -0800 | [diff] [blame] | 348 | result.extend(_CheckNoPackageBoundaryViolations( |
| 349 | input_api, gn_files, output_api)) |
ehmaldonado | 5b1ba08 | 2016-09-02 05:51:08 -0700 | [diff] [blame] | 350 | return result |
| 351 | |
kjellander@webrtc.org | 3bd4156 | 2014-09-01 11:06:37 +0000 | [diff] [blame] | 352 | def _CheckUnwantedDependencies(input_api, output_api): |
| 353 | """Runs checkdeps on #include statements added in this |
| 354 | change. Breaking - rules is an error, breaking ! rules is a |
| 355 | warning. |
| 356 | """ |
| 357 | # Copied from Chromium's src/PRESUBMIT.py. |
| 358 | |
| 359 | # We need to wait until we have an input_api object and use this |
| 360 | # roundabout construct to import checkdeps because this file is |
| 361 | # eval-ed and thus doesn't have __file__. |
| 362 | original_sys_path = sys.path |
| 363 | try: |
kjellander@webrtc.org | aefe61a | 2014-12-08 13:00:30 +0000 | [diff] [blame] | 364 | checkdeps_path = input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 365 | 'buildtools', 'checkdeps') |
| 366 | if not os.path.exists(checkdeps_path): |
| 367 | return [output_api.PresubmitError( |
| 368 | 'Cannot find checkdeps at %s\nHave you run "gclient sync" to ' |
| 369 | 'download Chromium and setup the symlinks?' % checkdeps_path)] |
| 370 | sys.path.append(checkdeps_path) |
kjellander@webrtc.org | 3bd4156 | 2014-09-01 11:06:37 +0000 | [diff] [blame] | 371 | import checkdeps |
| 372 | from cpp_checker import CppChecker |
| 373 | from rules import Rule |
| 374 | finally: |
| 375 | # Restore sys.path to what it was before. |
| 376 | sys.path = original_sys_path |
| 377 | |
| 378 | added_includes = [] |
| 379 | for f in input_api.AffectedFiles(): |
| 380 | if not CppChecker.IsCppFile(f.LocalPath()): |
| 381 | continue |
| 382 | |
Henrik Kjellander | 57e5fd2 | 2015-05-25 12:55:39 +0200 | [diff] [blame] | 383 | changed_lines = [line for _, line in f.ChangedContents()] |
kjellander@webrtc.org | 3bd4156 | 2014-09-01 11:06:37 +0000 | [diff] [blame] | 384 | added_includes.append([f.LocalPath(), changed_lines]) |
| 385 | |
| 386 | deps_checker = checkdeps.DepsChecker(input_api.PresubmitLocalPath()) |
| 387 | |
| 388 | error_descriptions = [] |
| 389 | warning_descriptions = [] |
| 390 | for path, rule_type, rule_description in deps_checker.CheckAddedCppIncludes( |
| 391 | added_includes): |
| 392 | description_with_path = '%s\n %s' % (path, rule_description) |
| 393 | if rule_type == Rule.DISALLOW: |
| 394 | error_descriptions.append(description_with_path) |
| 395 | else: |
| 396 | warning_descriptions.append(description_with_path) |
| 397 | |
| 398 | results = [] |
| 399 | if error_descriptions: |
| 400 | results.append(output_api.PresubmitError( |
| 401 | 'You added one or more #includes that violate checkdeps rules.', |
| 402 | error_descriptions)) |
| 403 | if warning_descriptions: |
| 404 | results.append(output_api.PresubmitPromptOrNotify( |
| 405 | 'You added one or more #includes of files that are temporarily\n' |
| 406 | 'allowed but being removed. Can you avoid introducing the\n' |
| 407 | '#include? See relevant DEPS file(s) for details and contacts.', |
| 408 | warning_descriptions)) |
| 409 | return results |
| 410 | |
kjellander | d1e26a9 | 2016-09-19 08:11:16 -0700 | [diff] [blame] | 411 | def _CheckChangeHasBugField(input_api, output_api): |
| 412 | """Requires that the changelist have a BUG= field. |
| 413 | |
| 414 | This check is stricter than the one in depot_tools/presubmit_canned_checks.py |
| 415 | since it fails the presubmit if the BUG= field is missing or doesn't contain |
| 416 | a bug reference. |
| 417 | """ |
| 418 | if input_api.change.BUG: |
| 419 | return [] |
| 420 | else: |
| 421 | return [output_api.PresubmitError( |
| 422 | 'The BUG=[bug number] field is mandatory. Please create a bug and ' |
| 423 | 'reference it using either of:\n' |
| 424 | ' * https://bugs.webrtc.org - reference it using BUG=webrtc:XXXX\n' |
| 425 | ' * https://crbug.com - reference it using BUG=chromium:XXXXXX')] |
kjellander@webrtc.org | e415864 | 2014-08-06 09:11:18 +0000 | [diff] [blame] | 426 | |
kjellander | 569cf94 | 2016-02-11 05:02:59 -0800 | [diff] [blame] | 427 | def _CheckJSONParseErrors(input_api, output_api): |
| 428 | """Check that JSON files do not contain syntax errors.""" |
| 429 | |
| 430 | def FilterFile(affected_file): |
| 431 | return input_api.os_path.splitext(affected_file.LocalPath())[1] == '.json' |
| 432 | |
| 433 | def GetJSONParseError(input_api, filename): |
| 434 | try: |
| 435 | contents = input_api.ReadFile(filename) |
| 436 | input_api.json.loads(contents) |
| 437 | except ValueError as e: |
| 438 | return e |
| 439 | return None |
| 440 | |
| 441 | results = [] |
| 442 | for affected_file in input_api.AffectedFiles( |
| 443 | file_filter=FilterFile, include_deletes=False): |
| 444 | parse_error = GetJSONParseError(input_api, |
| 445 | affected_file.AbsoluteLocalPath()) |
| 446 | if parse_error: |
| 447 | results.append(output_api.PresubmitError('%s could not be parsed: %s' % |
| 448 | (affected_file.LocalPath(), parse_error))) |
| 449 | return results |
| 450 | |
| 451 | |
Henrik Kjellander | 8d3ad82 | 2015-05-26 19:52:05 +0200 | [diff] [blame] | 452 | def _RunPythonTests(input_api, output_api): |
kjellander | 94f4d9e | 2017-03-09 06:09:33 -0800 | [diff] [blame] | 453 | def join(*args): |
Henrik Kjellander | 8d3ad82 | 2015-05-26 19:52:05 +0200 | [diff] [blame] | 454 | return input_api.os_path.join(input_api.PresubmitLocalPath(), *args) |
| 455 | |
| 456 | test_directories = [ |
kjellander | 94f4d9e | 2017-03-09 06:09:33 -0800 | [diff] [blame] | 457 | join('webrtc', 'tools', 'py_event_log_analyzer') |
ehmaldonado | 4fb9746 | 2017-01-30 05:27:22 -0800 | [diff] [blame] | 458 | ] + [ |
kjellander | 94f4d9e | 2017-03-09 06:09:33 -0800 | [diff] [blame] | 459 | root for root, _, files in os.walk(join('tools-webrtc')) |
ehmaldonado | 4fb9746 | 2017-01-30 05:27:22 -0800 | [diff] [blame] | 460 | if any(f.endswith('_test.py') for f in files) |
Henrik Kjellander | 8d3ad82 | 2015-05-26 19:52:05 +0200 | [diff] [blame] | 461 | ] |
| 462 | |
| 463 | tests = [] |
| 464 | for directory in test_directories: |
| 465 | tests.extend( |
| 466 | input_api.canned_checks.GetUnitTestsInDirectory( |
| 467 | input_api, |
| 468 | output_api, |
| 469 | directory, |
| 470 | whitelist=[r'.+_test\.py$'])) |
| 471 | return input_api.RunTests(tests, parallel=True) |
| 472 | |
| 473 | |
andrew@webrtc.org | 53df136 | 2012-01-26 21:24:23 +0000 | [diff] [blame] | 474 | def _CommonChecks(input_api, output_api): |
| 475 | """Checks common to both upload and commit.""" |
niklase@google.com | da159d6 | 2011-05-30 11:51:34 +0000 | [diff] [blame] | 476 | results = [] |
tkchin | 42f580e | 2015-11-26 23:18:23 -0800 | [diff] [blame] | 477 | # Filter out files that are in objc or ios dirs from being cpplint-ed since |
| 478 | # they do not follow C++ lint rules. |
| 479 | black_list = input_api.DEFAULT_BLACK_LIST + ( |
| 480 | r".*\bobjc[\\\/].*", |
Kári Tristan Helgason | 3fa3517 | 2016-09-09 08:55:05 +0000 | [diff] [blame] | 481 | r".*objc\.[hcm]+$", |
hjon | 65ae2d8 | 2016-08-02 23:55:44 -0700 | [diff] [blame] | 482 | r"webrtc\/build\/ios\/SDK\/.*", |
tkchin | 42f580e | 2015-11-26 23:18:23 -0800 | [diff] [blame] | 483 | ) |
| 484 | source_file_filter = lambda x: input_api.FilterSourceFile(x, None, black_list) |
| 485 | results.extend(_CheckApprovedFilesLintClean( |
| 486 | input_api, output_api, source_file_filter)) |
phoglund@webrtc.org | 5d371393 | 2013-03-07 09:59:43 +0000 | [diff] [blame] | 487 | results.extend(input_api.canned_checks.RunPylint(input_api, output_api, |
kjellander@webrtc.org | 177567c | 2016-12-22 10:40:28 +0100 | [diff] [blame] | 488 | black_list=(r'^base[\\\/].*\.py$', |
Henrik Kjellander | 14771ac | 2015-06-02 13:10:04 +0200 | [diff] [blame] | 489 | r'^build[\\\/].*\.py$', |
| 490 | r'^buildtools[\\\/].*\.py$', |
Henrik Kjellander | 0779e8f | 2016-12-22 12:01:17 +0100 | [diff] [blame] | 491 | r'^ios[\\\/].*\.py$', |
Henrik Kjellander | 14771ac | 2015-06-02 13:10:04 +0200 | [diff] [blame] | 492 | r'^out.*[\\\/].*\.py$', |
| 493 | r'^testing[\\\/].*\.py$', |
| 494 | r'^third_party[\\\/].*\.py$', |
kjellander@webrtc.org | 177567c | 2016-12-22 10:40:28 +0100 | [diff] [blame] | 495 | r'^tools[\\\/].*\.py$', |
kjellander | afd5494 | 2016-12-17 12:21:39 -0800 | [diff] [blame] | 496 | # TODO(phoglund): should arguably be checked. |
Henrik Kjellander | b2d5577 | 2016-12-18 22:14:50 +0100 | [diff] [blame] | 497 | r'^tools-webrtc[\\\/]mb[\\\/].*\.py$', |
kjellander | afd5494 | 2016-12-17 12:21:39 -0800 | [diff] [blame] | 498 | r'^tools-webrtc[\\\/]valgrind[\\\/].*\.py$', |
Henrik Kjellander | 14771ac | 2015-06-02 13:10:04 +0200 | [diff] [blame] | 499 | r'^xcodebuild.*[\\\/].*\.py$',), |
phoglund@webrtc.org | 5d371393 | 2013-03-07 09:59:43 +0000 | [diff] [blame] | 500 | disabled_warnings=['F0401', # Failed to import x |
| 501 | 'E0611', # No package y in x |
| 502 | 'W0232', # Class has no __init__ method |
Henrik Kjellander | 57e5fd2 | 2015-05-25 12:55:39 +0200 | [diff] [blame] | 503 | ], |
| 504 | pylintrc='pylintrc')) |
kjellander | 569cf94 | 2016-02-11 05:02:59 -0800 | [diff] [blame] | 505 | |
nisse | 3d21e23 | 2016-09-02 03:07:06 -0700 | [diff] [blame] | 506 | # TODO(nisse): talk/ is no more, so make below checks simpler? |
Henrik Kjellander | 57e5fd2 | 2015-05-25 12:55:39 +0200 | [diff] [blame] | 507 | # WebRTC can't use the presubmit_canned_checks.PanProjectChecks function since |
| 508 | # we need to have different license checks in talk/ and webrtc/ directories. |
| 509 | # Instead, hand-picked checks are included below. |
Henrik Kjellander | 6322467 | 2015-09-08 08:03:56 +0200 | [diff] [blame] | 510 | |
tkchin | 3cd9a30 | 2016-06-08 12:40:28 -0700 | [diff] [blame] | 511 | # .m and .mm files are ObjC files. For simplicity we will consider .h files in |
| 512 | # ObjC subdirectories ObjC headers. |
| 513 | objc_filter_list = (r'.+\.m$', r'.+\.mm$', r'.+objc\/.+\.h$') |
Henrik Kjellander | b4af3d6 | 2016-11-16 20:11:29 +0100 | [diff] [blame] | 514 | # Skip long-lines check for DEPS and GN files. |
| 515 | build_file_filter_list = (r'.+\.gn$', r'.+\.gni$', 'DEPS') |
tkchin | 3cd9a30 | 2016-06-08 12:40:28 -0700 | [diff] [blame] | 516 | eighty_char_sources = lambda x: input_api.FilterSourceFile(x, |
| 517 | black_list=build_file_filter_list + objc_filter_list) |
| 518 | hundred_char_sources = lambda x: input_api.FilterSourceFile(x, |
| 519 | white_list=objc_filter_list) |
andrew@webrtc.org | 2442de1 | 2012-01-23 17:45:41 +0000 | [diff] [blame] | 520 | results.extend(input_api.canned_checks.CheckLongLines( |
tkchin | 3cd9a30 | 2016-06-08 12:40:28 -0700 | [diff] [blame] | 521 | input_api, output_api, maxlen=80, source_file_filter=eighty_char_sources)) |
| 522 | results.extend(input_api.canned_checks.CheckLongLines( |
| 523 | input_api, output_api, maxlen=100, |
| 524 | source_file_filter=hundred_char_sources)) |
| 525 | |
andrew@webrtc.org | 2442de1 | 2012-01-23 17:45:41 +0000 | [diff] [blame] | 526 | results.extend(input_api.canned_checks.CheckChangeHasNoTabs( |
| 527 | input_api, output_api)) |
andrew@webrtc.org | 53df136 | 2012-01-26 21:24:23 +0000 | [diff] [blame] | 528 | results.extend(input_api.canned_checks.CheckChangeHasNoStrayWhitespace( |
| 529 | input_api, output_api)) |
kjellander | e5dc62a | 2016-12-14 00:16:21 -0800 | [diff] [blame] | 530 | results.extend(input_api.canned_checks.CheckAuthorizedAuthor( |
| 531 | input_api, output_api)) |
andrew@webrtc.org | 53df136 | 2012-01-26 21:24:23 +0000 | [diff] [blame] | 532 | results.extend(input_api.canned_checks.CheckChangeTodoHasOwner( |
| 533 | input_api, output_api)) |
kjellander | 53047c9 | 2015-12-02 23:56:14 -0800 | [diff] [blame] | 534 | results.extend(_CheckNativeApiHeaderChanges(input_api, output_api)) |
kjellander@webrtc.org | 51198f1 | 2012-02-21 17:53:46 +0000 | [diff] [blame] | 535 | results.extend(_CheckNoIOStreamInHeaders(input_api, output_api)) |
kjellander | 6aeef74 | 2017-02-20 01:13:18 -0800 | [diff] [blame] | 536 | results.extend(_CheckNoPragmaOnce(input_api, output_api)) |
kjellander@webrtc.org | 51198f1 | 2012-02-21 17:53:46 +0000 | [diff] [blame] | 537 | results.extend(_CheckNoFRIEND_TEST(input_api, output_api)) |
ehmaldonado | 5b1ba08 | 2016-09-02 05:51:08 -0700 | [diff] [blame] | 538 | results.extend(_CheckGnChanges(input_api, output_api)) |
kjellander@webrtc.org | 3bd4156 | 2014-09-01 11:06:37 +0000 | [diff] [blame] | 539 | results.extend(_CheckUnwantedDependencies(input_api, output_api)) |
kjellander | 569cf94 | 2016-02-11 05:02:59 -0800 | [diff] [blame] | 540 | results.extend(_CheckJSONParseErrors(input_api, output_api)) |
Henrik Kjellander | 8d3ad82 | 2015-05-26 19:52:05 +0200 | [diff] [blame] | 541 | results.extend(_RunPythonTests(input_api, output_api)) |
andrew@webrtc.org | 53df136 | 2012-01-26 21:24:23 +0000 | [diff] [blame] | 542 | return results |
andrew@webrtc.org | 2442de1 | 2012-01-23 17:45:41 +0000 | [diff] [blame] | 543 | |
kjellander@webrtc.org | e415864 | 2014-08-06 09:11:18 +0000 | [diff] [blame] | 544 | |
andrew@webrtc.org | 53df136 | 2012-01-26 21:24:23 +0000 | [diff] [blame] | 545 | def CheckChangeOnUpload(input_api, output_api): |
| 546 | results = [] |
| 547 | results.extend(_CommonChecks(input_api, output_api)) |
Henrik Kjellander | 57e5fd2 | 2015-05-25 12:55:39 +0200 | [diff] [blame] | 548 | results.extend( |
| 549 | input_api.canned_checks.CheckGNFormatted(input_api, output_api)) |
niklase@google.com | da159d6 | 2011-05-30 11:51:34 +0000 | [diff] [blame] | 550 | return results |
| 551 | |
kjellander@webrtc.org | e415864 | 2014-08-06 09:11:18 +0000 | [diff] [blame] | 552 | |
andrew@webrtc.org | 2442de1 | 2012-01-23 17:45:41 +0000 | [diff] [blame] | 553 | def CheckChangeOnCommit(input_api, output_api): |
niklase@google.com | 1198db9 | 2011-06-09 07:07:24 +0000 | [diff] [blame] | 554 | results = [] |
andrew@webrtc.org | 53df136 | 2012-01-26 21:24:23 +0000 | [diff] [blame] | 555 | results.extend(_CommonChecks(input_api, output_api)) |
kjellander | 53047c9 | 2015-12-02 23:56:14 -0800 | [diff] [blame] | 556 | results.extend(_VerifyNativeApiHeadersListIsValid(input_api, output_api)) |
niklase@google.com | 1198db9 | 2011-06-09 07:07:24 +0000 | [diff] [blame] | 557 | results.extend(input_api.canned_checks.CheckOwners(input_api, output_api)) |
andrew@webrtc.org | 53df136 | 2012-01-26 21:24:23 +0000 | [diff] [blame] | 558 | results.extend(input_api.canned_checks.CheckChangeWasUploaded( |
| 559 | input_api, output_api)) |
| 560 | results.extend(input_api.canned_checks.CheckChangeHasDescription( |
| 561 | input_api, output_api)) |
kjellander | d1e26a9 | 2016-09-19 08:11:16 -0700 | [diff] [blame] | 562 | results.extend(_CheckChangeHasBugField(input_api, output_api)) |
kjellander@webrtc.org | 51198f1 | 2012-02-21 17:53:46 +0000 | [diff] [blame] | 563 | results.extend(input_api.canned_checks.CheckChangeHasTestField( |
| 564 | input_api, output_api)) |
kjellander@webrtc.org | 12cb88c | 2014-02-13 11:53:43 +0000 | [diff] [blame] | 565 | results.extend(input_api.canned_checks.CheckTreeIsOpen( |
| 566 | input_api, output_api, |
| 567 | json_url='http://webrtc-status.appspot.com/current?format=json')) |
niklase@google.com | 1198db9 | 2011-06-09 07:07:24 +0000 | [diff] [blame] | 568 | return results |