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