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 |
| 11 | import shutil |
| 12 | import subprocess |
| 13 | import sys |
| 14 | import tempfile |
Shahbaz Youssefi | 98a3550 | 2019-01-08 22:09:39 -0500 | [diff] [blame] | 15 | |
Jamie Madill | 73397e8 | 2019-01-09 10:33:16 -0500 | [diff] [blame] | 16 | # Fragment of a regular expression that matches C++ and Objective-C++ implementation files. |
| 17 | _IMPLEMENTATION_EXTENSIONS = r'\.(cc|cpp|cxx|mm)$' |
| 18 | |
Jamie Madill | 73397e8 | 2019-01-09 10:33:16 -0500 | [diff] [blame] | 19 | # Fragment of a regular expression that matches C++ and Objective-C++ header files. |
| 20 | _HEADER_EXTENSIONS = r'\.(h|hpp|hxx)$' |
| 21 | |
Brian Sheedy | cc4d833 | 2019-09-11 17:26:00 -0700 | [diff] [blame^] | 22 | _PRIMARY_EXPORT_TARGETS = [ |
| 23 | '//:libEGL', |
| 24 | '//:libGLESv1_CM', |
| 25 | '//:libGLESv2', |
| 26 | '//:translator', |
| 27 | ] |
| 28 | |
Jamie Madill | 73397e8 | 2019-01-09 10:33:16 -0500 | [diff] [blame] | 29 | |
Jamie Madill | 9e438ee | 2019-07-05 08:44:23 -0400 | [diff] [blame] | 30 | def _CheckChangeHasBugField(input_api, output_api): |
| 31 | """Requires that the changelist have a Bug: field.""" |
| 32 | bugs = input_api.change.BugsFromDescription() |
| 33 | if not bugs: |
| 34 | return [ |
| 35 | output_api.PresubmitError( |
| 36 | 'If this change has an associated bug, add Bug: angleproject:[bug number].') |
| 37 | ] |
| 38 | elif not all([' ' not in bug for bug in bugs]): |
| 39 | return [ |
| 40 | output_api.PresubmitError( |
| 41 | 'Check bug tag formatting. Ensure there are no spaces after the colon.') |
| 42 | ] |
| 43 | else: |
| 44 | return [] |
| 45 | |
| 46 | |
Jamie Madill | 73397e8 | 2019-01-09 10:33:16 -0500 | [diff] [blame] | 47 | def _CheckCodeGeneration(input_api, output_api): |
Jamie Madill | 04e9e55 | 2019-04-01 14:40:21 -0400 | [diff] [blame] | 48 | |
| 49 | class Msg(output_api.PresubmitError): |
Geoff Lang | d7d4239 | 2019-05-06 13:15:35 -0400 | [diff] [blame] | 50 | """Specialized error message""" |
| 51 | |
| 52 | def __init__(self, message): |
| 53 | super(output_api.PresubmitError, self).__init__( |
| 54 | message, |
Jamie Madill | 9e438ee | 2019-07-05 08:44:23 -0400 | [diff] [blame] | 55 | long_text='Please run scripts/run_code_generation.py to refresh generated hashes.\n' |
| 56 | '\n' |
| 57 | 'If that fails, ensure your ANGLE repositiory is synced to tip-of-tree\n' |
| 58 | 'and all ANGLE DEPS are fully up-to-date by running gclient sync.\n' |
| 59 | '\n' |
| 60 | 'If you are building ANGLE inside Chromium you must bootstrap ANGLE\n' |
| 61 | 'before gclient sync. See the DevSetup documentation for more details.\n') |
Jamie Madill | 04e9e55 | 2019-04-01 14:40:21 -0400 | [diff] [blame] | 62 | |
Shahbaz Youssefi | 98a3550 | 2019-01-08 22:09:39 -0500 | [diff] [blame] | 63 | code_gen_path = input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 64 | 'scripts/run_code_generation.py') |
| 65 | cmd_name = 'run_code_generation' |
| 66 | cmd = [input_api.python_executable, code_gen_path, '--verify-no-dirty'] |
Geoff Lang | d7d4239 | 2019-05-06 13:15:35 -0400 | [diff] [blame] | 67 | 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] | 68 | if input_api.verbose: |
| 69 | print('Running ' + cmd_name) |
| 70 | return input_api.RunTests([test_cmd]) |
| 71 | |
Shahbaz Youssefi | 98a3550 | 2019-01-08 22:09:39 -0500 | [diff] [blame] | 72 | |
Jamie Madill | 73397e8 | 2019-01-09 10:33:16 -0500 | [diff] [blame] | 73 | # Taken directly from Chromium's PRESUBMIT.py |
| 74 | def _CheckNewHeaderWithoutGnChange(input_api, output_api): |
Geoff Lang | d7d4239 | 2019-05-06 13:15:35 -0400 | [diff] [blame] | 75 | """Checks that newly added header files have corresponding GN changes. |
Jamie Madill | 73397e8 | 2019-01-09 10:33:16 -0500 | [diff] [blame] | 76 | Note that this is only a heuristic. To be precise, run script: |
| 77 | build/check_gn_headers.py. |
| 78 | """ |
| 79 | |
Geoff Lang | d7d4239 | 2019-05-06 13:15:35 -0400 | [diff] [blame] | 80 | def headers(f): |
| 81 | return input_api.FilterSourceFile(f, white_list=(r'.+%s' % _HEADER_EXTENSIONS,)) |
Jamie Madill | 73397e8 | 2019-01-09 10:33:16 -0500 | [diff] [blame] | 82 | |
Geoff Lang | d7d4239 | 2019-05-06 13:15:35 -0400 | [diff] [blame] | 83 | new_headers = [] |
| 84 | for f in input_api.AffectedSourceFiles(headers): |
| 85 | if f.Action() != 'A': |
| 86 | continue |
| 87 | new_headers.append(f.LocalPath()) |
Jamie Madill | 73397e8 | 2019-01-09 10:33:16 -0500 | [diff] [blame] | 88 | |
Geoff Lang | d7d4239 | 2019-05-06 13:15:35 -0400 | [diff] [blame] | 89 | def gn_files(f): |
| 90 | return input_api.FilterSourceFile(f, white_list=(r'.+\.gn',)) |
Jamie Madill | 73397e8 | 2019-01-09 10:33:16 -0500 | [diff] [blame] | 91 | |
Geoff Lang | d7d4239 | 2019-05-06 13:15:35 -0400 | [diff] [blame] | 92 | all_gn_changed_contents = '' |
| 93 | for f in input_api.AffectedSourceFiles(gn_files): |
| 94 | for _, line in f.ChangedContents(): |
| 95 | all_gn_changed_contents += line |
Jamie Madill | 73397e8 | 2019-01-09 10:33:16 -0500 | [diff] [blame] | 96 | |
Geoff Lang | d7d4239 | 2019-05-06 13:15:35 -0400 | [diff] [blame] | 97 | problems = [] |
| 98 | for header in new_headers: |
| 99 | basename = input_api.os_path.basename(header) |
| 100 | if basename not in all_gn_changed_contents: |
| 101 | problems.append(header) |
Jamie Madill | 73397e8 | 2019-01-09 10:33:16 -0500 | [diff] [blame] | 102 | |
Geoff Lang | d7d4239 | 2019-05-06 13:15:35 -0400 | [diff] [blame] | 103 | if problems: |
| 104 | return [ |
| 105 | output_api.PresubmitPromptWarning( |
| 106 | 'Missing GN changes for new header files', |
| 107 | items=sorted(problems), |
| 108 | long_text='Please double check whether newly added header files need ' |
| 109 | 'corresponding changes in gn or gni files.\nThis checking is only a ' |
| 110 | 'heuristic. Run build/check_gn_headers.py to be precise.\n' |
| 111 | 'Read https://crbug.com/661774 for more info.') |
| 112 | ] |
| 113 | return [] |
Jamie Madill | 73397e8 | 2019-01-09 10:33:16 -0500 | [diff] [blame] | 114 | |
| 115 | |
Brian Sheedy | cc4d833 | 2019-09-11 17:26:00 -0700 | [diff] [blame^] | 116 | def _CheckExportValidity(input_api, output_api): |
| 117 | outdir = tempfile.mkdtemp() |
| 118 | # shell=True is necessary on Windows, as otherwise subprocess fails to find |
| 119 | # either 'gn' or 'vpython3' even if they are findable via PATH. |
| 120 | use_shell = input_api.is_windows |
| 121 | try: |
| 122 | try: |
| 123 | subprocess.check_output(['gn', 'gen', outdir], shell=use_shell) |
| 124 | except subprocess.CalledProcessError as e: |
| 125 | return [ |
| 126 | output_api.PresubmitError( |
| 127 | 'Unable to run gn gen for export_targets.py: %s' % e.output) |
| 128 | ] |
| 129 | export_target_script = os.path.join(input_api.PresubmitLocalPath(), 'scripts', |
| 130 | 'export_targets.py') |
| 131 | try: |
| 132 | subprocess.check_output( |
| 133 | ['vpython3', export_target_script, outdir] + _PRIMARY_EXPORT_TARGETS, |
| 134 | stderr=subprocess.STDOUT, |
| 135 | shell=use_shell) |
| 136 | except subprocess.CalledProcessError as e: |
| 137 | if input_api.is_committing: |
| 138 | return [output_api.PresubmitError('export_targets.py failed: %s' % e.output)] |
| 139 | return [ |
| 140 | output_api.PresubmitPromptWarning( |
| 141 | 'export_targets.py failed, this may just be due to your local checkout: %s' % |
| 142 | e.output) |
| 143 | ] |
| 144 | return [] |
| 145 | finally: |
| 146 | shutil.rmtree(outdir) |
| 147 | |
| 148 | |
Jamie Madill | 73397e8 | 2019-01-09 10:33:16 -0500 | [diff] [blame] | 149 | def CheckChangeOnUpload(input_api, output_api): |
| 150 | results = [] |
| 151 | results.extend(_CheckCodeGeneration(input_api, output_api)) |
Jamie Madill | 9e438ee | 2019-07-05 08:44:23 -0400 | [diff] [blame] | 152 | results.extend(_CheckChangeHasBugField(input_api, output_api)) |
Geoff Lang | d7d4239 | 2019-05-06 13:15:35 -0400 | [diff] [blame] | 153 | results.extend(input_api.canned_checks.CheckChangeHasDescription(input_api, output_api)) |
Jamie Madill | 73397e8 | 2019-01-09 10:33:16 -0500 | [diff] [blame] | 154 | results.extend(_CheckNewHeaderWithoutGnChange(input_api, output_api)) |
Brian Sheedy | cc4d833 | 2019-09-11 17:26:00 -0700 | [diff] [blame^] | 155 | results.extend(_CheckExportValidity(input_api, output_api)) |
Jamie Madill | 9e438ee | 2019-07-05 08:44:23 -0400 | [diff] [blame] | 156 | results.extend( |
| 157 | input_api.canned_checks.CheckPatchFormatted( |
| 158 | input_api, output_api, result_factory=output_api.PresubmitError)) |
Jamie Madill | 73397e8 | 2019-01-09 10:33:16 -0500 | [diff] [blame] | 159 | return results |
| 160 | |
Shahbaz Youssefi | 98a3550 | 2019-01-08 22:09:39 -0500 | [diff] [blame] | 161 | |
| 162 | def CheckChangeOnCommit(input_api, output_api): |
Jamie Madill | 73397e8 | 2019-01-09 10:33:16 -0500 | [diff] [blame] | 163 | results = [] |
Jamie Madill | 04e9e55 | 2019-04-01 14:40:21 -0400 | [diff] [blame] | 164 | results.extend(_CheckCodeGeneration(input_api, output_api)) |
Jamie Madill | 9e438ee | 2019-07-05 08:44:23 -0400 | [diff] [blame] | 165 | results.extend( |
| 166 | input_api.canned_checks.CheckPatchFormatted( |
| 167 | input_api, output_api, result_factory=output_api.PresubmitError)) |
| 168 | results.extend(_CheckChangeHasBugField(input_api, output_api)) |
Brian Sheedy | cc4d833 | 2019-09-11 17:26:00 -0700 | [diff] [blame^] | 169 | results.extend(_CheckExportValidity(input_api, output_api)) |
Geoff Lang | d7d4239 | 2019-05-06 13:15:35 -0400 | [diff] [blame] | 170 | results.extend(input_api.canned_checks.CheckChangeHasDescription(input_api, output_api)) |
Jamie Madill | 73397e8 | 2019-01-09 10:33:16 -0500 | [diff] [blame] | 171 | return results |