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 | |
| 10 | from subprocess import call |
| 11 | |
Jamie Madill | 73397e8 | 2019-01-09 10:33:16 -0500 | [diff] [blame] | 12 | # Fragment of a regular expression that matches C++ and Objective-C++ implementation files. |
| 13 | _IMPLEMENTATION_EXTENSIONS = r'\.(cc|cpp|cxx|mm)$' |
| 14 | |
Jamie Madill | 73397e8 | 2019-01-09 10:33:16 -0500 | [diff] [blame] | 15 | # Fragment of a regular expression that matches C++ and Objective-C++ header files. |
| 16 | _HEADER_EXTENSIONS = r'\.(h|hpp|hxx)$' |
| 17 | |
| 18 | |
Jamie Madill | 9e438ee | 2019-07-05 08:44:23 -0400 | [diff] [blame^] | 19 | def _CheckChangeHasBugField(input_api, output_api): |
| 20 | """Requires that the changelist have a Bug: field.""" |
| 21 | bugs = input_api.change.BugsFromDescription() |
| 22 | if not bugs: |
| 23 | return [ |
| 24 | output_api.PresubmitError( |
| 25 | 'If this change has an associated bug, add Bug: angleproject:[bug number].') |
| 26 | ] |
| 27 | elif not all([' ' not in bug for bug in bugs]): |
| 28 | return [ |
| 29 | output_api.PresubmitError( |
| 30 | 'Check bug tag formatting. Ensure there are no spaces after the colon.') |
| 31 | ] |
| 32 | else: |
| 33 | return [] |
| 34 | |
| 35 | |
Jamie Madill | 73397e8 | 2019-01-09 10:33:16 -0500 | [diff] [blame] | 36 | def _CheckCodeGeneration(input_api, output_api): |
Jamie Madill | 04e9e55 | 2019-04-01 14:40:21 -0400 | [diff] [blame] | 37 | |
| 38 | class Msg(output_api.PresubmitError): |
Geoff Lang | d7d4239 | 2019-05-06 13:15:35 -0400 | [diff] [blame] | 39 | """Specialized error message""" |
| 40 | |
| 41 | def __init__(self, message): |
| 42 | super(output_api.PresubmitError, self).__init__( |
| 43 | message, |
Jamie Madill | 9e438ee | 2019-07-05 08:44:23 -0400 | [diff] [blame^] | 44 | long_text='Please run scripts/run_code_generation.py to refresh generated hashes.\n' |
| 45 | '\n' |
| 46 | 'If that fails, ensure your ANGLE repositiory is synced to tip-of-tree\n' |
| 47 | 'and all ANGLE DEPS are fully up-to-date by running gclient sync.\n' |
| 48 | '\n' |
| 49 | 'If you are building ANGLE inside Chromium you must bootstrap ANGLE\n' |
| 50 | 'before gclient sync. See the DevSetup documentation for more details.\n') |
Jamie Madill | 04e9e55 | 2019-04-01 14:40:21 -0400 | [diff] [blame] | 51 | |
Shahbaz Youssefi | 98a3550 | 2019-01-08 22:09:39 -0500 | [diff] [blame] | 52 | code_gen_path = input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 53 | 'scripts/run_code_generation.py') |
| 54 | cmd_name = 'run_code_generation' |
| 55 | cmd = [input_api.python_executable, code_gen_path, '--verify-no-dirty'] |
Geoff Lang | d7d4239 | 2019-05-06 13:15:35 -0400 | [diff] [blame] | 56 | 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] | 57 | if input_api.verbose: |
| 58 | print('Running ' + cmd_name) |
| 59 | return input_api.RunTests([test_cmd]) |
| 60 | |
Shahbaz Youssefi | 98a3550 | 2019-01-08 22:09:39 -0500 | [diff] [blame] | 61 | |
Jamie Madill | 73397e8 | 2019-01-09 10:33:16 -0500 | [diff] [blame] | 62 | # Taken directly from Chromium's PRESUBMIT.py |
| 63 | def _CheckNewHeaderWithoutGnChange(input_api, output_api): |
Geoff Lang | d7d4239 | 2019-05-06 13:15:35 -0400 | [diff] [blame] | 64 | """Checks that newly added header files have corresponding GN changes. |
Jamie Madill | 73397e8 | 2019-01-09 10:33:16 -0500 | [diff] [blame] | 65 | Note that this is only a heuristic. To be precise, run script: |
| 66 | build/check_gn_headers.py. |
| 67 | """ |
| 68 | |
Geoff Lang | d7d4239 | 2019-05-06 13:15:35 -0400 | [diff] [blame] | 69 | def headers(f): |
| 70 | return input_api.FilterSourceFile(f, white_list=(r'.+%s' % _HEADER_EXTENSIONS,)) |
Jamie Madill | 73397e8 | 2019-01-09 10:33:16 -0500 | [diff] [blame] | 71 | |
Geoff Lang | d7d4239 | 2019-05-06 13:15:35 -0400 | [diff] [blame] | 72 | new_headers = [] |
| 73 | for f in input_api.AffectedSourceFiles(headers): |
| 74 | if f.Action() != 'A': |
| 75 | continue |
| 76 | new_headers.append(f.LocalPath()) |
Jamie Madill | 73397e8 | 2019-01-09 10:33:16 -0500 | [diff] [blame] | 77 | |
Geoff Lang | d7d4239 | 2019-05-06 13:15:35 -0400 | [diff] [blame] | 78 | def gn_files(f): |
| 79 | return input_api.FilterSourceFile(f, white_list=(r'.+\.gn',)) |
Jamie Madill | 73397e8 | 2019-01-09 10:33:16 -0500 | [diff] [blame] | 80 | |
Geoff Lang | d7d4239 | 2019-05-06 13:15:35 -0400 | [diff] [blame] | 81 | all_gn_changed_contents = '' |
| 82 | for f in input_api.AffectedSourceFiles(gn_files): |
| 83 | for _, line in f.ChangedContents(): |
| 84 | all_gn_changed_contents += line |
Jamie Madill | 73397e8 | 2019-01-09 10:33:16 -0500 | [diff] [blame] | 85 | |
Geoff Lang | d7d4239 | 2019-05-06 13:15:35 -0400 | [diff] [blame] | 86 | problems = [] |
| 87 | for header in new_headers: |
| 88 | basename = input_api.os_path.basename(header) |
| 89 | if basename not in all_gn_changed_contents: |
| 90 | problems.append(header) |
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 | if problems: |
| 93 | return [ |
| 94 | output_api.PresubmitPromptWarning( |
| 95 | 'Missing GN changes for new header files', |
| 96 | items=sorted(problems), |
| 97 | long_text='Please double check whether newly added header files need ' |
| 98 | 'corresponding changes in gn or gni files.\nThis checking is only a ' |
| 99 | 'heuristic. Run build/check_gn_headers.py to be precise.\n' |
| 100 | 'Read https://crbug.com/661774 for more info.') |
| 101 | ] |
| 102 | return [] |
Jamie Madill | 73397e8 | 2019-01-09 10:33:16 -0500 | [diff] [blame] | 103 | |
| 104 | |
| 105 | def CheckChangeOnUpload(input_api, output_api): |
| 106 | results = [] |
| 107 | results.extend(_CheckCodeGeneration(input_api, output_api)) |
Jamie Madill | 9e438ee | 2019-07-05 08:44:23 -0400 | [diff] [blame^] | 108 | results.extend(_CheckChangeHasBugField(input_api, output_api)) |
Geoff Lang | d7d4239 | 2019-05-06 13:15:35 -0400 | [diff] [blame] | 109 | results.extend(input_api.canned_checks.CheckChangeHasDescription(input_api, output_api)) |
Jamie Madill | 73397e8 | 2019-01-09 10:33:16 -0500 | [diff] [blame] | 110 | results.extend(_CheckNewHeaderWithoutGnChange(input_api, output_api)) |
Jamie Madill | 9e438ee | 2019-07-05 08:44:23 -0400 | [diff] [blame^] | 111 | results.extend( |
| 112 | input_api.canned_checks.CheckPatchFormatted( |
| 113 | input_api, output_api, result_factory=output_api.PresubmitError)) |
Jamie Madill | 73397e8 | 2019-01-09 10:33:16 -0500 | [diff] [blame] | 114 | return results |
| 115 | |
Shahbaz Youssefi | 98a3550 | 2019-01-08 22:09:39 -0500 | [diff] [blame] | 116 | |
| 117 | def CheckChangeOnCommit(input_api, output_api): |
Jamie Madill | 73397e8 | 2019-01-09 10:33:16 -0500 | [diff] [blame] | 118 | results = [] |
Jamie Madill | 04e9e55 | 2019-04-01 14:40:21 -0400 | [diff] [blame] | 119 | results.extend(_CheckCodeGeneration(input_api, output_api)) |
Jamie Madill | 9e438ee | 2019-07-05 08:44:23 -0400 | [diff] [blame^] | 120 | results.extend( |
| 121 | input_api.canned_checks.CheckPatchFormatted( |
| 122 | input_api, output_api, result_factory=output_api.PresubmitError)) |
| 123 | results.extend(_CheckChangeHasBugField(input_api, output_api)) |
Geoff Lang | d7d4239 | 2019-05-06 13:15:35 -0400 | [diff] [blame] | 124 | results.extend(input_api.canned_checks.CheckChangeHasDescription(input_api, output_api)) |
Jamie Madill | 73397e8 | 2019-01-09 10:33:16 -0500 | [diff] [blame] | 125 | return results |