blob: 8a9d8747d8ee23c78f693c4a6b0be2d8103fd506 [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.
4
5"""Top-level presubmit script for code generation.
6
7See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
8for more details on the presubmit API built into depot_tools.
9"""
10
11from subprocess import call
12
Jamie Madill73397e82019-01-09 10:33:16 -050013
14# Fragment of a regular expression that matches C++ and Objective-C++ implementation files.
15_IMPLEMENTATION_EXTENSIONS = r'\.(cc|cpp|cxx|mm)$'
16
17
18# Fragment of a regular expression that matches C++ and Objective-C++ header files.
19_HEADER_EXTENSIONS = r'\.(h|hpp|hxx)$'
20
21
22def _CheckCodeGeneration(input_api, output_api):
Shahbaz Youssefi98a35502019-01-08 22:09:39 -050023 code_gen_path = input_api.os_path.join(input_api.PresubmitLocalPath(),
24 'scripts/run_code_generation.py')
25 cmd_name = 'run_code_generation'
26 cmd = [input_api.python_executable, code_gen_path, '--verify-no-dirty']
27 test_cmd = input_api.Command(
28 name=cmd_name,
29 cmd=cmd,
30 kwargs={},
31 message=output_api.PresubmitError)
32 if input_api.verbose:
33 print('Running ' + cmd_name)
34 return input_api.RunTests([test_cmd])
35
Shahbaz Youssefi98a35502019-01-08 22:09:39 -050036
Jamie Madill73397e82019-01-09 10:33:16 -050037# Taken directly from Chromium's PRESUBMIT.py
38def _CheckNewHeaderWithoutGnChange(input_api, output_api):
39 """Checks that newly added header files have corresponding GN changes.
40 Note that this is only a heuristic. To be precise, run script:
41 build/check_gn_headers.py.
42 """
43
44 def headers(f):
45 return input_api.FilterSourceFile(
46 f, white_list=(r'.+%s' % _HEADER_EXTENSIONS, ))
47
48 new_headers = []
49 for f in input_api.AffectedSourceFiles(headers):
50 if f.Action() != 'A':
51 continue
52 new_headers.append(f.LocalPath())
53
54 def gn_files(f):
55 return input_api.FilterSourceFile(f, white_list=(r'.+\.gn', ))
56
57 all_gn_changed_contents = ''
58 for f in input_api.AffectedSourceFiles(gn_files):
59 for _, line in f.ChangedContents():
60 all_gn_changed_contents += line
61
62 problems = []
63 for header in new_headers:
64 basename = input_api.os_path.basename(header)
65 if basename not in all_gn_changed_contents:
66 problems.append(header)
67
68 if problems:
69 return [output_api.PresubmitPromptWarning(
70 'Missing GN changes for new header files', items=sorted(problems),
71 long_text='Please double check whether newly added header files need '
72 'corresponding changes in gn or gni files.\nThis checking is only a '
73 'heuristic. Run build/check_gn_headers.py to be precise.\n'
74 'Read https://crbug.com/661774 for more info.')]
75 return []
76
77
78def CheckChangeOnUpload(input_api, output_api):
79 results = []
80 results.extend(_CheckCodeGeneration(input_api, output_api))
81 results.extend(input_api.canned_checks.CheckChangeHasBugField(
82 input_api, output_api))
83 results.extend(input_api.canned_checks.CheckChangeHasDescription(
84 input_api, output_api))
85 results.extend(_CheckNewHeaderWithoutGnChange(input_api, output_api))
86 results.extend(
87 input_api.canned_checks.CheckPatchFormatted(input_api, output_api))
88 return results
89
Shahbaz Youssefi98a35502019-01-08 22:09:39 -050090
91def CheckChangeOnCommit(input_api, output_api):
Jamie Madill73397e82019-01-09 10:33:16 -050092 results = []
93 results.extend(
94 input_api.canned_checks.CheckPatchFormatted(input_api, output_api))
95 results.extend(input_api.canned_checks.CheckChangeHasBugField(
96 input_api, output_api))
97 results.extend(input_api.canned_checks.CheckChangeHasDescription(
98 input_api, output_api))
99 return results