blob: a530728f7597f83ac158107d994b7337137f6e28 [file] [log] [blame]
Shahbaz Youssefi98a35502019-01-08 22:09:39 -05001# 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 Youssefi98a35502019-01-08 22:09:39 -05004"""Top-level presubmit script for code generation.
5
6See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
7for more details on the presubmit API built into depot_tools.
8"""
9
10from subprocess import call
11
Jamie Madill73397e82019-01-09 10:33:16 -050012# Fragment of a regular expression that matches C++ and Objective-C++ implementation files.
13_IMPLEMENTATION_EXTENSIONS = r'\.(cc|cpp|cxx|mm)$'
14
Jamie Madill73397e82019-01-09 10:33:16 -050015# Fragment of a regular expression that matches C++ and Objective-C++ header files.
16_HEADER_EXTENSIONS = r'\.(h|hpp|hxx)$'
17
18
19def _CheckCodeGeneration(input_api, output_api):
Jamie Madill04e9e552019-04-01 14:40:21 -040020
21 class Msg(output_api.PresubmitError):
Geoff Langd7d42392019-05-06 13:15:35 -040022 """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 Madill04e9e552019-04-01 14:40:21 -040031
Shahbaz Youssefi98a35502019-01-08 22:09:39 -050032 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 Langd7d42392019-05-06 13:15:35 -040036 test_cmd = input_api.Command(name=cmd_name, cmd=cmd, kwargs={}, message=Msg)
Shahbaz Youssefi98a35502019-01-08 22:09:39 -050037 if input_api.verbose:
38 print('Running ' + cmd_name)
39 return input_api.RunTests([test_cmd])
40
Shahbaz Youssefi98a35502019-01-08 22:09:39 -050041
Jamie Madill73397e82019-01-09 10:33:16 -050042# Taken directly from Chromium's PRESUBMIT.py
43def _CheckNewHeaderWithoutGnChange(input_api, output_api):
Geoff Langd7d42392019-05-06 13:15:35 -040044 """Checks that newly added header files have corresponding GN changes.
Jamie Madill73397e82019-01-09 10:33:16 -050045 Note that this is only a heuristic. To be precise, run script:
46 build/check_gn_headers.py.
47 """
48
Geoff Langd7d42392019-05-06 13:15:35 -040049 def headers(f):
50 return input_api.FilterSourceFile(f, white_list=(r'.+%s' % _HEADER_EXTENSIONS,))
Jamie Madill73397e82019-01-09 10:33:16 -050051
Geoff Langd7d42392019-05-06 13:15:35 -040052 new_headers = []
53 for f in input_api.AffectedSourceFiles(headers):
54 if f.Action() != 'A':
55 continue
56 new_headers.append(f.LocalPath())
Jamie Madill73397e82019-01-09 10:33:16 -050057
Geoff Langd7d42392019-05-06 13:15:35 -040058 def gn_files(f):
59 return input_api.FilterSourceFile(f, white_list=(r'.+\.gn',))
Jamie Madill73397e82019-01-09 10:33:16 -050060
Geoff Langd7d42392019-05-06 13:15:35 -040061 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 Madill73397e82019-01-09 10:33:16 -050065
Geoff Langd7d42392019-05-06 13:15:35 -040066 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 Madill73397e82019-01-09 10:33:16 -050071
Geoff Langd7d42392019-05-06 13:15:35 -040072 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 Madill73397e82019-01-09 10:33:16 -050083
84
85def CheckChangeOnUpload(input_api, output_api):
86 results = []
87 results.extend(_CheckCodeGeneration(input_api, output_api))
Geoff Langd7d42392019-05-06 13:15:35 -040088 results.extend(input_api.canned_checks.CheckChangeHasBugField(input_api, output_api))
89 results.extend(input_api.canned_checks.CheckChangeHasDescription(input_api, output_api))
Jamie Madill73397e82019-01-09 10:33:16 -050090 results.extend(_CheckNewHeaderWithoutGnChange(input_api, output_api))
Geoff Langd7d42392019-05-06 13:15:35 -040091 results.extend(input_api.canned_checks.CheckPatchFormatted(input_api, output_api))
Jamie Madill73397e82019-01-09 10:33:16 -050092 return results
93
Shahbaz Youssefi98a35502019-01-08 22:09:39 -050094
95def CheckChangeOnCommit(input_api, output_api):
Jamie Madill73397e82019-01-09 10:33:16 -050096 results = []
Jamie Madill04e9e552019-04-01 14:40:21 -040097 results.extend(_CheckCodeGeneration(input_api, output_api))
Geoff Langd7d42392019-05-06 13:15:35 -040098 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 Madill73397e82019-01-09 10:33:16 -0500101 return results