blob: 25b7d872ba47a5e09e96e33ae888c1deb7ae4154 [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
Brian Sheedycc4d8332019-09-11 17:26:00 -070010import os
11import shutil
12import subprocess
13import sys
14import tempfile
Shahbaz Youssefi98a35502019-01-08 22:09:39 -050015
Jamie Madill73397e82019-01-09 10:33:16 -050016# Fragment of a regular expression that matches C++ and Objective-C++ implementation files.
17_IMPLEMENTATION_EXTENSIONS = r'\.(cc|cpp|cxx|mm)$'
18
Jamie Madill73397e82019-01-09 10:33:16 -050019# Fragment of a regular expression that matches C++ and Objective-C++ header files.
20_HEADER_EXTENSIONS = r'\.(h|hpp|hxx)$'
21
Brian Sheedycc4d8332019-09-11 17:26:00 -070022_PRIMARY_EXPORT_TARGETS = [
23 '//:libEGL',
24 '//:libGLESv1_CM',
25 '//:libGLESv2',
26 '//:translator',
27]
28
Jamie Madill73397e82019-01-09 10:33:16 -050029
Jamie Madill9e438ee2019-07-05 08:44:23 -040030def _CheckChangeHasBugField(input_api, output_api):
31 """Requires that the changelist have a Bug: field."""
32 bugs = input_api.change.BugsFromDescription()
33 if not bugs:
34 return [
35 output_api.PresubmitError(
36 'If this change has an associated bug, add Bug: angleproject:[bug number].')
37 ]
38 elif not all([' ' not in bug for bug in bugs]):
39 return [
40 output_api.PresubmitError(
41 'Check bug tag formatting. Ensure there are no spaces after the colon.')
42 ]
43 else:
44 return []
45
46
Jamie Madill73397e82019-01-09 10:33:16 -050047def _CheckCodeGeneration(input_api, output_api):
Jamie Madill04e9e552019-04-01 14:40:21 -040048
49 class Msg(output_api.PresubmitError):
Geoff Langd7d42392019-05-06 13:15:35 -040050 """Specialized error message"""
51
52 def __init__(self, message):
53 super(output_api.PresubmitError, self).__init__(
54 message,
Jamie Madill9e438ee2019-07-05 08:44:23 -040055 long_text='Please run scripts/run_code_generation.py to refresh generated hashes.\n'
56 '\n'
57 'If that fails, ensure your ANGLE repositiory is synced to tip-of-tree\n'
58 'and all ANGLE DEPS are fully up-to-date by running gclient sync.\n'
59 '\n'
60 'If you are building ANGLE inside Chromium you must bootstrap ANGLE\n'
61 'before gclient sync. See the DevSetup documentation for more details.\n')
Jamie Madill04e9e552019-04-01 14:40:21 -040062
Shahbaz Youssefi98a35502019-01-08 22:09:39 -050063 code_gen_path = input_api.os_path.join(input_api.PresubmitLocalPath(),
64 'scripts/run_code_generation.py')
65 cmd_name = 'run_code_generation'
66 cmd = [input_api.python_executable, code_gen_path, '--verify-no-dirty']
Geoff Langd7d42392019-05-06 13:15:35 -040067 test_cmd = input_api.Command(name=cmd_name, cmd=cmd, kwargs={}, message=Msg)
Shahbaz Youssefi98a35502019-01-08 22:09:39 -050068 if input_api.verbose:
69 print('Running ' + cmd_name)
70 return input_api.RunTests([test_cmd])
71
Shahbaz Youssefi98a35502019-01-08 22:09:39 -050072
Jamie Madill73397e82019-01-09 10:33:16 -050073# Taken directly from Chromium's PRESUBMIT.py
74def _CheckNewHeaderWithoutGnChange(input_api, output_api):
Geoff Langd7d42392019-05-06 13:15:35 -040075 """Checks that newly added header files have corresponding GN changes.
Jamie Madill73397e82019-01-09 10:33:16 -050076 Note that this is only a heuristic. To be precise, run script:
77 build/check_gn_headers.py.
78 """
79
Geoff Langd7d42392019-05-06 13:15:35 -040080 def headers(f):
81 return input_api.FilterSourceFile(f, white_list=(r'.+%s' % _HEADER_EXTENSIONS,))
Jamie Madill73397e82019-01-09 10:33:16 -050082
Geoff Langd7d42392019-05-06 13:15:35 -040083 new_headers = []
84 for f in input_api.AffectedSourceFiles(headers):
85 if f.Action() != 'A':
86 continue
87 new_headers.append(f.LocalPath())
Jamie Madill73397e82019-01-09 10:33:16 -050088
Geoff Langd7d42392019-05-06 13:15:35 -040089 def gn_files(f):
90 return input_api.FilterSourceFile(f, white_list=(r'.+\.gn',))
Jamie Madill73397e82019-01-09 10:33:16 -050091
Geoff Langd7d42392019-05-06 13:15:35 -040092 all_gn_changed_contents = ''
93 for f in input_api.AffectedSourceFiles(gn_files):
94 for _, line in f.ChangedContents():
95 all_gn_changed_contents += line
Jamie Madill73397e82019-01-09 10:33:16 -050096
Geoff Langd7d42392019-05-06 13:15:35 -040097 problems = []
98 for header in new_headers:
99 basename = input_api.os_path.basename(header)
100 if basename not in all_gn_changed_contents:
101 problems.append(header)
Jamie Madill73397e82019-01-09 10:33:16 -0500102
Geoff Langd7d42392019-05-06 13:15:35 -0400103 if problems:
104 return [
105 output_api.PresubmitPromptWarning(
106 'Missing GN changes for new header files',
107 items=sorted(problems),
108 long_text='Please double check whether newly added header files need '
109 'corresponding changes in gn or gni files.\nThis checking is only a '
110 'heuristic. Run build/check_gn_headers.py to be precise.\n'
111 'Read https://crbug.com/661774 for more info.')
112 ]
113 return []
Jamie Madill73397e82019-01-09 10:33:16 -0500114
115
Brian Sheedycc4d8332019-09-11 17:26:00 -0700116def _CheckExportValidity(input_api, output_api):
117 outdir = tempfile.mkdtemp()
118 # shell=True is necessary on Windows, as otherwise subprocess fails to find
119 # either 'gn' or 'vpython3' even if they are findable via PATH.
120 use_shell = input_api.is_windows
121 try:
122 try:
123 subprocess.check_output(['gn', 'gen', outdir], shell=use_shell)
124 except subprocess.CalledProcessError as e:
125 return [
126 output_api.PresubmitError(
127 'Unable to run gn gen for export_targets.py: %s' % e.output)
128 ]
129 export_target_script = os.path.join(input_api.PresubmitLocalPath(), 'scripts',
130 'export_targets.py')
131 try:
132 subprocess.check_output(
133 ['vpython3', export_target_script, outdir] + _PRIMARY_EXPORT_TARGETS,
134 stderr=subprocess.STDOUT,
135 shell=use_shell)
136 except subprocess.CalledProcessError as e:
137 if input_api.is_committing:
138 return [output_api.PresubmitError('export_targets.py failed: %s' % e.output)]
139 return [
140 output_api.PresubmitPromptWarning(
141 'export_targets.py failed, this may just be due to your local checkout: %s' %
142 e.output)
143 ]
144 return []
145 finally:
146 shutil.rmtree(outdir)
147
148
Jamie Madill73397e82019-01-09 10:33:16 -0500149def CheckChangeOnUpload(input_api, output_api):
150 results = []
151 results.extend(_CheckCodeGeneration(input_api, output_api))
Jamie Madill9e438ee2019-07-05 08:44:23 -0400152 results.extend(_CheckChangeHasBugField(input_api, output_api))
Geoff Langd7d42392019-05-06 13:15:35 -0400153 results.extend(input_api.canned_checks.CheckChangeHasDescription(input_api, output_api))
Jamie Madill73397e82019-01-09 10:33:16 -0500154 results.extend(_CheckNewHeaderWithoutGnChange(input_api, output_api))
Brian Sheedycc4d8332019-09-11 17:26:00 -0700155 results.extend(_CheckExportValidity(input_api, output_api))
Jamie Madill9e438ee2019-07-05 08:44:23 -0400156 results.extend(
157 input_api.canned_checks.CheckPatchFormatted(
158 input_api, output_api, result_factory=output_api.PresubmitError))
Jamie Madill73397e82019-01-09 10:33:16 -0500159 return results
160
Shahbaz Youssefi98a35502019-01-08 22:09:39 -0500161
162def CheckChangeOnCommit(input_api, output_api):
Jamie Madill73397e82019-01-09 10:33:16 -0500163 results = []
Jamie Madill04e9e552019-04-01 14:40:21 -0400164 results.extend(_CheckCodeGeneration(input_api, output_api))
Jamie Madill9e438ee2019-07-05 08:44:23 -0400165 results.extend(
166 input_api.canned_checks.CheckPatchFormatted(
167 input_api, output_api, result_factory=output_api.PresubmitError))
168 results.extend(_CheckChangeHasBugField(input_api, output_api))
Brian Sheedycc4d8332019-09-11 17:26:00 -0700169 results.extend(_CheckExportValidity(input_api, output_api))
Geoff Langd7d42392019-05-06 13:15:35 -0400170 results.extend(input_api.canned_checks.CheckChangeHasDescription(input_api, output_api))
Jamie Madill73397e82019-01-09 10:33:16 -0500171 return results