Shahbaz Youssefi | 98a3550 | 2019-01-08 22:09:39 -0500 | [diff] [blame] | 1 | # Copyright 2019 The ANGLE Project Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
Shahbaz Youssefi | 98a3550 | 2019-01-08 22:09:39 -0500 | [diff] [blame] | 4 | """Top-level presubmit script for code generation. |
| 5 | |
| 6 | See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts |
| 7 | for more details on the presubmit API built into depot_tools. |
| 8 | """ |
| 9 | |
Brian Sheedy | cc4d833 | 2019-09-11 17:26:00 -0700 | [diff] [blame] | 10 | import os |
Shahbaz Youssefi | 634ee43 | 2019-12-16 11:39:10 -0500 | [diff] [blame] | 11 | import re |
Brian Sheedy | cc4d833 | 2019-09-11 17:26:00 -0700 | [diff] [blame] | 12 | import shutil |
| 13 | import subprocess |
| 14 | import sys |
| 15 | import tempfile |
Shahbaz Youssefi | 98a3550 | 2019-01-08 22:09:39 -0500 | [diff] [blame] | 16 | |
James Darpinian | 4dff995 | 2019-12-10 17:05:37 -0800 | [diff] [blame] | 17 | # Fragment of a regular expression that matches C++ and Objective-C++ implementation files and headers. |
| 18 | _IMPLEMENTATION_AND_HEADER_EXTENSIONS = r'\.(cc|cpp|cxx|mm|h|hpp|hxx)$' |
Jamie Madill | 73397e8 | 2019-01-09 10:33:16 -0500 | [diff] [blame] | 19 | |
Jamie Madill | 73397e8 | 2019-01-09 10:33:16 -0500 | [diff] [blame] | 20 | # Fragment of a regular expression that matches C++ and Objective-C++ header files. |
| 21 | _HEADER_EXTENSIONS = r'\.(h|hpp|hxx)$' |
| 22 | |
Brian Sheedy | cc4d833 | 2019-09-11 17:26:00 -0700 | [diff] [blame] | 23 | _PRIMARY_EXPORT_TARGETS = [ |
| 24 | '//:libEGL', |
| 25 | '//:libGLESv1_CM', |
| 26 | '//:libGLESv2', |
| 27 | '//:translator', |
| 28 | ] |
| 29 | |
Jamie Madill | 73397e8 | 2019-01-09 10:33:16 -0500 | [diff] [blame] | 30 | |
Jamie Madill | 9e438ee | 2019-07-05 08:44:23 -0400 | [diff] [blame] | 31 | def _CheckChangeHasBugField(input_api, output_api): |
Shahbaz Youssefi | 634ee43 | 2019-12-16 11:39:10 -0500 | [diff] [blame] | 32 | """Requires that the changelist have a Bug: field from a known project.""" |
Jamie Madill | 9e438ee | 2019-07-05 08:44:23 -0400 | [diff] [blame] | 33 | bugs = input_api.change.BugsFromDescription() |
| 34 | if not bugs: |
| 35 | return [ |
Alexis Hetu | 4210e49 | 2019-10-10 14:22:03 -0400 | [diff] [blame] | 36 | output_api.PresubmitError('Please ensure that your description contains:\n' |
| 37 | '"Bug: angleproject:[bug number]"\n' |
| 38 | 'directly above the Change-Id tag.') |
Jamie Madill | 9e438ee | 2019-07-05 08:44:23 -0400 | [diff] [blame] | 39 | ] |
Shahbaz Youssefi | 634ee43 | 2019-12-16 11:39:10 -0500 | [diff] [blame] | 40 | |
| 41 | # The bug must be in the form of "project:number". None is also accepted, which is used by |
| 42 | # rollers as well as in very minor changes. |
| 43 | if len(bugs) == 1 and bugs[0] == 'None': |
Jamie Madill | 9e438ee | 2019-07-05 08:44:23 -0400 | [diff] [blame] | 44 | return [] |
| 45 | |
Geoff Lang | 13e4cdb | 2020-04-16 14:35:54 -0400 | [diff] [blame] | 46 | projects = ['angleproject:', 'chromium:', 'dawn:', 'fuchsia:', 'skia:', 'swiftshader:', 'b/'] |
| 47 | bug_regex = re.compile(r"([a-z]+[:/])(\d+)") |
Shahbaz Youssefi | 634ee43 | 2019-12-16 11:39:10 -0500 | [diff] [blame] | 48 | errors = [] |
| 49 | extra_help = None |
| 50 | |
| 51 | for bug in bugs: |
| 52 | if bug == 'None': |
| 53 | errors.append( |
| 54 | output_api.PresubmitError('Invalid bug tag "None" in presence of other bug tags.')) |
| 55 | continue |
| 56 | |
| 57 | match = re.match(bug_regex, bug) |
| 58 | if match == None or bug != match.group(0) or match.group(1) not in projects: |
| 59 | errors.append(output_api.PresubmitError('Incorrect bug tag "' + bug + '".')) |
| 60 | if not extra_help: |
| 61 | extra_help = output_api.PresubmitError('Acceptable format is:\n\n' |
| 62 | ' Bug: project:bugnumber\n\n' |
| 63 | 'Acceptable projects are:\n\n ' + |
| 64 | '\n '.join(projects)) |
| 65 | |
| 66 | if extra_help: |
| 67 | errors.append(extra_help) |
| 68 | |
| 69 | return errors |
| 70 | |
Jamie Madill | 9e438ee | 2019-07-05 08:44:23 -0400 | [diff] [blame] | 71 | |
Jamie Madill | 73397e8 | 2019-01-09 10:33:16 -0500 | [diff] [blame] | 72 | def _CheckCodeGeneration(input_api, output_api): |
Jamie Madill | 04e9e55 | 2019-04-01 14:40:21 -0400 | [diff] [blame] | 73 | |
| 74 | class Msg(output_api.PresubmitError): |
Geoff Lang | d7d4239 | 2019-05-06 13:15:35 -0400 | [diff] [blame] | 75 | """Specialized error message""" |
| 76 | |
| 77 | def __init__(self, message): |
| 78 | super(output_api.PresubmitError, self).__init__( |
| 79 | message, |
Jamie Madill | 8dfc2b0 | 2019-11-25 15:12:58 -0500 | [diff] [blame] | 80 | long_text='Please ensure your ANGLE repositiory is synced to tip-of-tree\n' |
Jamie Madill | 9e438ee | 2019-07-05 08:44:23 -0400 | [diff] [blame] | 81 | 'and all ANGLE DEPS are fully up-to-date by running gclient sync.\n' |
| 82 | '\n' |
Jamie Madill | 8dfc2b0 | 2019-11-25 15:12:58 -0500 | [diff] [blame] | 83 | 'If that fails, run scripts/run_code_generation.py to refresh generated hashes.\n' |
| 84 | '\n' |
Jamie Madill | 9e438ee | 2019-07-05 08:44:23 -0400 | [diff] [blame] | 85 | 'If you are building ANGLE inside Chromium you must bootstrap ANGLE\n' |
| 86 | 'before gclient sync. See the DevSetup documentation for more details.\n') |
Jamie Madill | 04e9e55 | 2019-04-01 14:40:21 -0400 | [diff] [blame] | 87 | |
Shahbaz Youssefi | 98a3550 | 2019-01-08 22:09:39 -0500 | [diff] [blame] | 88 | code_gen_path = input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 89 | 'scripts/run_code_generation.py') |
| 90 | cmd_name = 'run_code_generation' |
| 91 | cmd = [input_api.python_executable, code_gen_path, '--verify-no-dirty'] |
Geoff Lang | d7d4239 | 2019-05-06 13:15:35 -0400 | [diff] [blame] | 92 | test_cmd = input_api.Command(name=cmd_name, cmd=cmd, kwargs={}, message=Msg) |
Shahbaz Youssefi | 98a3550 | 2019-01-08 22:09:39 -0500 | [diff] [blame] | 93 | if input_api.verbose: |
| 94 | print('Running ' + cmd_name) |
| 95 | return input_api.RunTests([test_cmd]) |
| 96 | |
Shahbaz Youssefi | 98a3550 | 2019-01-08 22:09:39 -0500 | [diff] [blame] | 97 | |
Jamie Madill | 73397e8 | 2019-01-09 10:33:16 -0500 | [diff] [blame] | 98 | # Taken directly from Chromium's PRESUBMIT.py |
| 99 | def _CheckNewHeaderWithoutGnChange(input_api, output_api): |
Geoff Lang | d7d4239 | 2019-05-06 13:15:35 -0400 | [diff] [blame] | 100 | """Checks that newly added header files have corresponding GN changes. |
Jamie Madill | 73397e8 | 2019-01-09 10:33:16 -0500 | [diff] [blame] | 101 | Note that this is only a heuristic. To be precise, run script: |
| 102 | build/check_gn_headers.py. |
| 103 | """ |
| 104 | |
Geoff Lang | d7d4239 | 2019-05-06 13:15:35 -0400 | [diff] [blame] | 105 | def headers(f): |
| 106 | return input_api.FilterSourceFile(f, white_list=(r'.+%s' % _HEADER_EXTENSIONS,)) |
Jamie Madill | 73397e8 | 2019-01-09 10:33:16 -0500 | [diff] [blame] | 107 | |
Geoff Lang | d7d4239 | 2019-05-06 13:15:35 -0400 | [diff] [blame] | 108 | new_headers = [] |
| 109 | for f in input_api.AffectedSourceFiles(headers): |
| 110 | if f.Action() != 'A': |
| 111 | continue |
| 112 | new_headers.append(f.LocalPath()) |
Jamie Madill | 73397e8 | 2019-01-09 10:33:16 -0500 | [diff] [blame] | 113 | |
Geoff Lang | d7d4239 | 2019-05-06 13:15:35 -0400 | [diff] [blame] | 114 | def gn_files(f): |
| 115 | return input_api.FilterSourceFile(f, white_list=(r'.+\.gn',)) |
Jamie Madill | 73397e8 | 2019-01-09 10:33:16 -0500 | [diff] [blame] | 116 | |
Geoff Lang | d7d4239 | 2019-05-06 13:15:35 -0400 | [diff] [blame] | 117 | all_gn_changed_contents = '' |
| 118 | for f in input_api.AffectedSourceFiles(gn_files): |
| 119 | for _, line in f.ChangedContents(): |
| 120 | all_gn_changed_contents += line |
Jamie Madill | 73397e8 | 2019-01-09 10:33:16 -0500 | [diff] [blame] | 121 | |
Geoff Lang | d7d4239 | 2019-05-06 13:15:35 -0400 | [diff] [blame] | 122 | problems = [] |
| 123 | for header in new_headers: |
| 124 | basename = input_api.os_path.basename(header) |
| 125 | if basename not in all_gn_changed_contents: |
| 126 | problems.append(header) |
Jamie Madill | 73397e8 | 2019-01-09 10:33:16 -0500 | [diff] [blame] | 127 | |
Geoff Lang | d7d4239 | 2019-05-06 13:15:35 -0400 | [diff] [blame] | 128 | if problems: |
| 129 | return [ |
| 130 | output_api.PresubmitPromptWarning( |
| 131 | 'Missing GN changes for new header files', |
| 132 | items=sorted(problems), |
| 133 | long_text='Please double check whether newly added header files need ' |
| 134 | 'corresponding changes in gn or gni files.\nThis checking is only a ' |
| 135 | 'heuristic. Run build/check_gn_headers.py to be precise.\n' |
| 136 | 'Read https://crbug.com/661774 for more info.') |
| 137 | ] |
| 138 | return [] |
Jamie Madill | 73397e8 | 2019-01-09 10:33:16 -0500 | [diff] [blame] | 139 | |
| 140 | |
Brian Sheedy | cc4d833 | 2019-09-11 17:26:00 -0700 | [diff] [blame] | 141 | def _CheckExportValidity(input_api, output_api): |
| 142 | outdir = tempfile.mkdtemp() |
| 143 | # shell=True is necessary on Windows, as otherwise subprocess fails to find |
| 144 | # either 'gn' or 'vpython3' even if they are findable via PATH. |
| 145 | use_shell = input_api.is_windows |
| 146 | try: |
| 147 | try: |
| 148 | subprocess.check_output(['gn', 'gen', outdir], shell=use_shell) |
| 149 | except subprocess.CalledProcessError as e: |
| 150 | return [ |
| 151 | output_api.PresubmitError( |
| 152 | 'Unable to run gn gen for export_targets.py: %s' % e.output) |
| 153 | ] |
| 154 | export_target_script = os.path.join(input_api.PresubmitLocalPath(), 'scripts', |
| 155 | 'export_targets.py') |
| 156 | try: |
| 157 | subprocess.check_output( |
| 158 | ['vpython3', export_target_script, outdir] + _PRIMARY_EXPORT_TARGETS, |
| 159 | stderr=subprocess.STDOUT, |
| 160 | shell=use_shell) |
| 161 | except subprocess.CalledProcessError as e: |
| 162 | if input_api.is_committing: |
| 163 | return [output_api.PresubmitError('export_targets.py failed: %s' % e.output)] |
| 164 | return [ |
| 165 | output_api.PresubmitPromptWarning( |
| 166 | 'export_targets.py failed, this may just be due to your local checkout: %s' % |
| 167 | e.output) |
| 168 | ] |
| 169 | return [] |
| 170 | finally: |
| 171 | shutil.rmtree(outdir) |
| 172 | |
| 173 | |
James Darpinian | 4dff995 | 2019-12-10 17:05:37 -0800 | [diff] [blame] | 174 | def _CheckTabsInSourceFiles(input_api, output_api): |
| 175 | """Forbids tab characters in source files due to a WebKit repo requirement. """ |
| 176 | |
| 177 | def implementation_and_headers(f): |
| 178 | return input_api.FilterSourceFile( |
| 179 | f, white_list=(r'.+%s' % _IMPLEMENTATION_AND_HEADER_EXTENSIONS,)) |
| 180 | |
| 181 | files_with_tabs = [] |
| 182 | for f in input_api.AffectedSourceFiles(implementation_and_headers): |
| 183 | for (num, line) in f.ChangedContents(): |
| 184 | if '\t' in line: |
| 185 | files_with_tabs.append(f) |
| 186 | break |
| 187 | |
| 188 | if files_with_tabs: |
| 189 | return [ |
| 190 | output_api.PresubmitError( |
| 191 | 'Tab characters in source files.', |
| 192 | items=sorted(files_with_tabs), |
| 193 | long_text= |
| 194 | 'Tab characters are forbidden in ANGLE source files because WebKit\'s Subversion\n' |
| 195 | 'repository does not allow tab characters in source files.\n' |
| 196 | 'Please remove tab characters from these files.') |
| 197 | ] |
| 198 | return [] |
| 199 | |
| 200 | |
Jamie Madill | 73397e8 | 2019-01-09 10:33:16 -0500 | [diff] [blame] | 201 | def CheckChangeOnUpload(input_api, output_api): |
| 202 | results = [] |
James Darpinian | 4dff995 | 2019-12-10 17:05:37 -0800 | [diff] [blame] | 203 | results.extend(_CheckTabsInSourceFiles(input_api, output_api)) |
Jamie Madill | 73397e8 | 2019-01-09 10:33:16 -0500 | [diff] [blame] | 204 | results.extend(_CheckCodeGeneration(input_api, output_api)) |
Jamie Madill | 9e438ee | 2019-07-05 08:44:23 -0400 | [diff] [blame] | 205 | results.extend(_CheckChangeHasBugField(input_api, output_api)) |
Geoff Lang | d7d4239 | 2019-05-06 13:15:35 -0400 | [diff] [blame] | 206 | results.extend(input_api.canned_checks.CheckChangeHasDescription(input_api, output_api)) |
Jamie Madill | 73397e8 | 2019-01-09 10:33:16 -0500 | [diff] [blame] | 207 | results.extend(_CheckNewHeaderWithoutGnChange(input_api, output_api)) |
Brian Sheedy | cc4d833 | 2019-09-11 17:26:00 -0700 | [diff] [blame] | 208 | results.extend(_CheckExportValidity(input_api, output_api)) |
Jamie Madill | 9e438ee | 2019-07-05 08:44:23 -0400 | [diff] [blame] | 209 | results.extend( |
| 210 | input_api.canned_checks.CheckPatchFormatted( |
| 211 | input_api, output_api, result_factory=output_api.PresubmitError)) |
Jamie Madill | 73397e8 | 2019-01-09 10:33:16 -0500 | [diff] [blame] | 212 | return results |
| 213 | |
Shahbaz Youssefi | 98a3550 | 2019-01-08 22:09:39 -0500 | [diff] [blame] | 214 | |
| 215 | def CheckChangeOnCommit(input_api, output_api): |
Jamie Madill | 73397e8 | 2019-01-09 10:33:16 -0500 | [diff] [blame] | 216 | results = [] |
Jamie Madill | 04e9e55 | 2019-04-01 14:40:21 -0400 | [diff] [blame] | 217 | results.extend(_CheckCodeGeneration(input_api, output_api)) |
Jamie Madill | 9e438ee | 2019-07-05 08:44:23 -0400 | [diff] [blame] | 218 | results.extend( |
| 219 | input_api.canned_checks.CheckPatchFormatted( |
| 220 | input_api, output_api, result_factory=output_api.PresubmitError)) |
| 221 | results.extend(_CheckChangeHasBugField(input_api, output_api)) |
Brian Sheedy | cc4d833 | 2019-09-11 17:26:00 -0700 | [diff] [blame] | 222 | results.extend(_CheckExportValidity(input_api, output_api)) |
Geoff Lang | d7d4239 | 2019-05-06 13:15:35 -0400 | [diff] [blame] | 223 | results.extend(input_api.canned_checks.CheckChangeHasDescription(input_api, output_api)) |
Jamie Madill | 73397e8 | 2019-01-09 10:33:16 -0500 | [diff] [blame] | 224 | return results |