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 | |
| 19 | def _CheckCodeGeneration(input_api, output_api): |
Jamie Madill | 04e9e55 | 2019-04-01 14:40:21 -0400 | [diff] [blame] | 20 | |
| 21 | class Msg(output_api.PresubmitError): |
Geoff Lang | d7d4239 | 2019-05-06 13:15:35 -0400 | [diff] [blame^] | 22 | """Specialized error message""" |
| 23 | |
| 24 | def __init__(self, message): |
| 25 | super(output_api.PresubmitError, self).__init__( |
| 26 | message, |
| 27 | long_text='Please ensure your ANGLE repositiory is synced to tip-of-tree\n' |
| 28 | 'and you have an up-to-date checkout of all ANGLE dependencies.\n' |
| 29 | 'If you are using ANGLE inside Chromium you may need to bootstrap ANGLE \n' |
| 30 | 'and run gclient sync. See the DevSetup documentation for details.\n') |
Jamie Madill | 04e9e55 | 2019-04-01 14:40:21 -0400 | [diff] [blame] | 31 | |
Shahbaz Youssefi | 98a3550 | 2019-01-08 22:09:39 -0500 | [diff] [blame] | 32 | code_gen_path = input_api.os_path.join(input_api.PresubmitLocalPath(), |
| 33 | 'scripts/run_code_generation.py') |
| 34 | cmd_name = 'run_code_generation' |
| 35 | cmd = [input_api.python_executable, code_gen_path, '--verify-no-dirty'] |
Geoff Lang | d7d4239 | 2019-05-06 13:15:35 -0400 | [diff] [blame^] | 36 | 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] | 37 | if input_api.verbose: |
| 38 | print('Running ' + cmd_name) |
| 39 | return input_api.RunTests([test_cmd]) |
| 40 | |
Shahbaz Youssefi | 98a3550 | 2019-01-08 22:09:39 -0500 | [diff] [blame] | 41 | |
Jamie Madill | 73397e8 | 2019-01-09 10:33:16 -0500 | [diff] [blame] | 42 | # Taken directly from Chromium's PRESUBMIT.py |
| 43 | def _CheckNewHeaderWithoutGnChange(input_api, output_api): |
Geoff Lang | d7d4239 | 2019-05-06 13:15:35 -0400 | [diff] [blame^] | 44 | """Checks that newly added header files have corresponding GN changes. |
Jamie Madill | 73397e8 | 2019-01-09 10:33:16 -0500 | [diff] [blame] | 45 | Note that this is only a heuristic. To be precise, run script: |
| 46 | build/check_gn_headers.py. |
| 47 | """ |
| 48 | |
Geoff Lang | d7d4239 | 2019-05-06 13:15:35 -0400 | [diff] [blame^] | 49 | def headers(f): |
| 50 | return input_api.FilterSourceFile(f, white_list=(r'.+%s' % _HEADER_EXTENSIONS,)) |
Jamie Madill | 73397e8 | 2019-01-09 10:33:16 -0500 | [diff] [blame] | 51 | |
Geoff Lang | d7d4239 | 2019-05-06 13:15:35 -0400 | [diff] [blame^] | 52 | new_headers = [] |
| 53 | for f in input_api.AffectedSourceFiles(headers): |
| 54 | if f.Action() != 'A': |
| 55 | continue |
| 56 | new_headers.append(f.LocalPath()) |
Jamie Madill | 73397e8 | 2019-01-09 10:33:16 -0500 | [diff] [blame] | 57 | |
Geoff Lang | d7d4239 | 2019-05-06 13:15:35 -0400 | [diff] [blame^] | 58 | def gn_files(f): |
| 59 | return input_api.FilterSourceFile(f, white_list=(r'.+\.gn',)) |
Jamie Madill | 73397e8 | 2019-01-09 10:33:16 -0500 | [diff] [blame] | 60 | |
Geoff Lang | d7d4239 | 2019-05-06 13:15:35 -0400 | [diff] [blame^] | 61 | all_gn_changed_contents = '' |
| 62 | for f in input_api.AffectedSourceFiles(gn_files): |
| 63 | for _, line in f.ChangedContents(): |
| 64 | all_gn_changed_contents += line |
Jamie Madill | 73397e8 | 2019-01-09 10:33:16 -0500 | [diff] [blame] | 65 | |
Geoff Lang | d7d4239 | 2019-05-06 13:15:35 -0400 | [diff] [blame^] | 66 | problems = [] |
| 67 | for header in new_headers: |
| 68 | basename = input_api.os_path.basename(header) |
| 69 | if basename not in all_gn_changed_contents: |
| 70 | problems.append(header) |
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 | if problems: |
| 73 | return [ |
| 74 | output_api.PresubmitPromptWarning( |
| 75 | 'Missing GN changes for new header files', |
| 76 | items=sorted(problems), |
| 77 | long_text='Please double check whether newly added header files need ' |
| 78 | 'corresponding changes in gn or gni files.\nThis checking is only a ' |
| 79 | 'heuristic. Run build/check_gn_headers.py to be precise.\n' |
| 80 | 'Read https://crbug.com/661774 for more info.') |
| 81 | ] |
| 82 | return [] |
Jamie Madill | 73397e8 | 2019-01-09 10:33:16 -0500 | [diff] [blame] | 83 | |
| 84 | |
| 85 | def CheckChangeOnUpload(input_api, output_api): |
| 86 | results = [] |
| 87 | results.extend(_CheckCodeGeneration(input_api, output_api)) |
Geoff Lang | d7d4239 | 2019-05-06 13:15:35 -0400 | [diff] [blame^] | 88 | results.extend(input_api.canned_checks.CheckChangeHasBugField(input_api, output_api)) |
| 89 | results.extend(input_api.canned_checks.CheckChangeHasDescription(input_api, output_api)) |
Jamie Madill | 73397e8 | 2019-01-09 10:33:16 -0500 | [diff] [blame] | 90 | results.extend(_CheckNewHeaderWithoutGnChange(input_api, output_api)) |
Geoff Lang | d7d4239 | 2019-05-06 13:15:35 -0400 | [diff] [blame^] | 91 | results.extend(input_api.canned_checks.CheckPatchFormatted(input_api, output_api)) |
Jamie Madill | 73397e8 | 2019-01-09 10:33:16 -0500 | [diff] [blame] | 92 | return results |
| 93 | |
Shahbaz Youssefi | 98a3550 | 2019-01-08 22:09:39 -0500 | [diff] [blame] | 94 | |
| 95 | def CheckChangeOnCommit(input_api, output_api): |
Jamie Madill | 73397e8 | 2019-01-09 10:33:16 -0500 | [diff] [blame] | 96 | results = [] |
Jamie Madill | 04e9e55 | 2019-04-01 14:40:21 -0400 | [diff] [blame] | 97 | results.extend(_CheckCodeGeneration(input_api, output_api)) |
Geoff Lang | d7d4239 | 2019-05-06 13:15:35 -0400 | [diff] [blame^] | 98 | results.extend(input_api.canned_checks.CheckPatchFormatted(input_api, output_api)) |
| 99 | results.extend(input_api.canned_checks.CheckChangeHasBugField(input_api, output_api)) |
| 100 | results.extend(input_api.canned_checks.CheckChangeHasDescription(input_api, output_api)) |
Jamie Madill | 73397e8 | 2019-01-09 10:33:16 -0500 | [diff] [blame] | 101 | return results |