1 # Copyright 2012 The Chromium Authors
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
12 # In this file `sys.executable` is used instead of
13 # `input_api.python3_executable` because on Windows
14 # `input_api.python3_executable` is `vpython3.bat` whereas `sys.executable` is
15 # `python.exe`. If `input_api.python3_executable` is used, we need to explicitly
16 # pass `shell=True` to `subprocess.Popen()`, which is a security risk
17 # (https://docs.python.org/3/library/subprocess.html#security-considerations).
19 # TODO: Investigate the incompatibility of `input_api.python3_executable` on
20 # Windows, for this particular PRESUBMIT script.
22 def RunCmdAndCheck(cmd, err_string, output_api, cwd=None, warning=False):
24 p = subprocess.Popen(cmd, cwd=cwd,
25 stdout=subprocess.PIPE,
26 stderr=subprocess.PIPE)
27 (_, p_stderr) = p.communicate()
30 results.append(output_api.PresubmitPromptWarning(
31 '%s\n\n%s' % (err_string, p_stderr.decode('utf-8'))))
34 output_api.PresubmitError(err_string,
35 long_text=p_stderr.decode('utf-8')))
39 def RunUnittests(input_api, output_api):
40 # Run some Generator unittests if the generator source was changed.
42 files = input_api.LocalPaths()
44 for filename in files:
45 name_parts = filename.split(os.sep)
46 if name_parts[0:2] == ['ppapi', 'generators']:
47 generator_files.append(filename)
48 if generator_files != []:
49 cmd = [sys.executable, 'idl_tests.py']
50 ppapi_dir = input_api.PresubmitLocalPath()
51 results.extend(RunCmdAndCheck(cmd,
52 'PPAPI IDL unittests failed.',
54 os.path.join(ppapi_dir, 'generators')))
58 # Verify that the files do not contain a 'TODO' in them.
59 RE_TODO = re.compile(r'\WTODO\W', flags=re.I)
60 def CheckTODO(input_api, output_api):
61 live_files = input_api.AffectedFiles(include_deletes=False)
62 files = [f.LocalPath() for f in live_files]
65 for filename in files:
66 name, ext = os.path.splitext(filename)
67 name_parts = name.split(os.sep)
69 # Only check normal build sources.
70 if ext not in ['.h', '.idl']:
73 # Only examine the ppapi directory.
74 if name_parts[0] != 'ppapi':
77 # Only examine public plugin facing directories.
78 if name_parts[1] not in ['api', 'c', 'cpp', 'utility']:
81 # Only examine public stable interfaces.
82 if name_parts[2] in ['dev', 'private', 'trusted']:
85 filepath = os.path.join('..', filename)
86 with io.open(filepath, encoding='utf-8') as f:
87 if RE_TODO.search(f.read()):
91 return [output_api.PresubmitPromptWarning(
92 'TODOs found in stable public PPAPI files:',
93 long_text='\n'.join(todo))]
96 # Verify that no CPP wrappers use un-versioned PPB interface name macros.
97 RE_UNVERSIONED_PPB = re.compile(r'\bPPB_\w+_INTERFACE\b')
98 def CheckUnversionedPPB(input_api, output_api):
99 live_files = input_api.AffectedFiles(include_deletes=False)
100 files = [f.LocalPath() for f in live_files]
103 for filename in files:
104 name, ext = os.path.splitext(filename)
105 name_parts = name.split(os.sep)
107 # Only check C++ sources.
108 if ext not in ['.cc']:
111 # Only examine the public plugin facing ppapi/cpp directory.
112 if name_parts[0:2] != ['ppapi', 'cpp']:
115 # Only examine public stable and trusted interfaces.
116 if name_parts[2] in ['dev', 'private']:
119 filepath = os.path.join('..', filename)
120 with io.open(filepath, encoding='utf-8') as f:
121 if RE_UNVERSIONED_PPB.search(f.read()):
122 todo.append(filename)
125 return [output_api.PresubmitError(
126 'Unversioned PPB interface references found in PPAPI C++ wrappers:',
127 long_text='\n'.join(todo))]
130 # Verify that changes to ppapi headers/sources are also made to NaCl SDK.
131 def CheckUpdatedNaClSDK(input_api, output_api):
132 files = input_api.LocalPaths()
134 # PPAPI files the Native Client SDK cares about.
137 for filename in files:
138 name, ext = os.path.splitext(filename)
139 name_parts = name.split(os.sep)
141 if len(name_parts) <= 2:
144 if name_parts[0] != 'ppapi':
147 if ((name_parts[1] == 'c' and ext == '.h') or
148 (name_parts[1] in ('cpp', 'utility') and ext in ('.h', '.cc'))):
149 if name_parts[2] in ('documentation', 'trusted'):
151 nacl_sdk_files.append(filename)
153 if not nacl_sdk_files:
156 verify_ppapi_py = os.path.join(input_api.change.RepositoryRoot(),
157 'native_client_sdk', 'src', 'build_tools',
159 # When running git cl presubmit --all this presubmit may be asked to check
160 # ~300 files, leading to a command line that is ~9,500 characters, which
161 # exceeds the Windows 8191 character cmd.exe limit and causes cryptic failures
162 # with no context. To avoid these we break the command up into smaller pieces.
164 # The command line is too long.
165 files_per_command = 25 if input_api.is_windows else 1000
167 for i in range(0, len(nacl_sdk_files), files_per_command):
168 cmd = [sys.executable, verify_ppapi_py
169 ] + nacl_sdk_files[i:i + files_per_command]
172 cmd,'PPAPI Interface modified without updating NaCl SDK.\n'
173 '(note that some dev interfaces should not be added '
174 'the NaCl SDK; when in doubt, ask a ppapi OWNER.\n'
175 'To ignore a file, add it to IGNORED_FILES in '
176 'native_client_sdk/src/build_tools/verify_ppapi.py)',
181 # Verify that changes to ppapi/thunk/interfaces_* files have a corresponding
182 # change to tools/metrics/histograms/enums.xml for UMA tracking.
183 def CheckHistogramXml(input_api, output_api):
184 # We can't use input_api.LocalPaths() here because we need to know about
185 # changes outside of ppapi/. See tools/depot_tools/presubmit_support.py for
186 # details on input_api.
187 files = input_api.change.AffectedFiles()
189 INTERFACE_FILES = ('ppapi/thunk/interfaces_legacy.h',
190 'ppapi/thunk/interfaces_ppb_private.h',
191 'ppapi/thunk/interfaces_ppb_private_no_permissions.h',
192 'ppapi/thunk/interfaces_ppb_public_dev_channel.h',
193 'ppapi/thunk/interfaces_ppb_public_dev.h',
194 'ppapi/thunk/interfaces_ppb_public_stable.h',
195 'ppapi/thunk/interfaces_ppb_public_socket.h',
196 'ppapi/thunk/interfaces_ppb_samsung.h')
198 HISTOGRAM_XML_FILE = 'tools/metrics/histograms/enums.xml'
199 interface_changes = []
200 has_histogram_xml_change = False
201 for filename in files:
202 path = filename.LocalPath()
203 if path in INTERFACE_FILES:
204 interface_changes.append(path)
205 if path == HISTOGRAM_XML_FILE:
206 has_histogram_xml_change = True
208 if interface_changes and not has_histogram_xml_change:
209 return [output_api.PresubmitNotifyResult(
210 'Missing change to tools/metrics/histograms/enums.xml.\n' +
211 'Run pepper_hash_for_uma to make get values for new interfaces.\n' +
212 'Interface changes:\n' + '\n'.join(interface_changes))]
215 def CheckChange(input_api, output_api):
218 results.extend(RunUnittests(input_api, output_api))
220 results.extend(CheckTODO(input_api, output_api))
222 results.extend(CheckUnversionedPPB(input_api, output_api))
224 results.extend(CheckUpdatedNaClSDK(input_api, output_api))
226 results.extend(CheckHistogramXml(input_api, output_api))
228 # Verify all modified *.idl have a matching *.h
229 files = input_api.LocalPaths()
232 generators_changed = False
234 # These are autogenerated by the command buffer generator, they don't go
236 whitelist = ['ppb_opengles2', 'ppb_opengles2ext_dev']
238 # Find all relevant .h and .idl files.
239 for filename in files:
240 name, ext = os.path.splitext(filename)
241 name_parts = name.split(os.sep)
242 if name_parts[-1] in whitelist:
244 if name_parts[0:2] == ['ppapi', 'c'] and ext == '.h':
245 h_files.append('/'.join(name_parts[2:]))
246 elif name_parts[0:2] == ['ppapi', 'api'] and ext == '.idl':
247 idl_files.append('/'.join(name_parts[2:]))
248 elif name_parts[0:2] == ['ppapi', 'generators']:
249 generators_changed = True
251 # Generate a list of all appropriate *.h and *.idl changes in this CL.
252 both = h_files + idl_files
254 # If there aren't any, we are done checking.
255 if not both: return results
258 for filename in idl_files:
259 if filename not in set(h_files):
260 missing.append('ppapi/api/%s.idl' % filename)
262 # An IDL change that includes [generate_thunk] doesn't need to have
263 # an update to the corresponding .h file.
265 for filename in missing:
266 lines = input_api.RightHandSideLines(lambda f: f.LocalPath() == filename)
268 if line[2].strip() == '[generate_thunk]':
269 new_thunk_files.append(filename)
270 for filename in new_thunk_files:
271 missing.remove(filename)
275 output_api.PresubmitPromptWarning(
276 'Missing PPAPI header, no change or skipped generation?',
277 long_text='\n '.join(missing)))
282 for filename in h_files:
283 if filename not in set(idl_files):
284 name_parts = filename.split(os.sep)
286 if name_parts[-1] == 'pp_macros':
287 # The C header generator adds a PPAPI_RELEASE macro based on all the
288 # IDL files, so pp_macros.h may change while its IDL does not.
289 lines = input_api.RightHandSideLines(
290 lambda f: f.LocalPath() == 'ppapi/c/%s.h' % filename)
291 releaseChanged = False
293 if line[2].split()[:2] == ['#define', 'PPAPI_RELEASE']:
295 output_api.PresubmitPromptOrNotify(
296 'PPAPI_RELEASE has changed', long_text=line[2]))
297 releaseChanged = True
302 if 'trusted' in name_parts:
303 missing_priv.append(' ppapi/c/%s.h' % filename)
306 if 'private' in name_parts:
307 missing_priv.append(' ppapi/c/%s.h' % filename)
310 if 'dev' in name_parts:
311 missing_dev.append(' ppapi/c/%s.h' % filename)
314 missing_stable.append(' ppapi/c/%s.h' % filename)
318 output_api.PresubmitPromptWarning(
319 'Missing PPAPI IDL for private interface, please generate IDL:',
320 long_text='\n'.join(missing_priv)))
324 output_api.PresubmitPromptWarning(
325 'Missing PPAPI IDL for DEV, required before moving to stable:',
326 long_text='\n'.join(missing_dev)))
329 # It might be okay that the header changed without a corresponding IDL
330 # change. E.g., comment indenting may have been changed. Treat this as a
332 if generators_changed:
334 output_api.PresubmitPromptWarning(
335 'Missing PPAPI IDL for stable interface (due to change in ' +
337 long_text='\n'.join(missing_stable)))
340 output_api.PresubmitError(
341 'Missing PPAPI IDL for stable interface:',
342 long_text='\n'.join(missing_stable)))
344 # Verify all *.h files match *.idl definitions, use:
345 # --test to prevent output to disk
346 # --diff to generate a unified diff
347 # --out to pick which files to examine (only the ones in the CL)
348 ppapi_dir = input_api.PresubmitLocalPath()
349 cmd = [sys.executable, 'generator.py',
350 '--wnone', '--diff', '--test','--cgen', '--range=start,end']
352 # Only generate output for IDL files references (as *.h or *.idl) in this CL
353 cmd.append('--out=' + ','.join([name + '.idl' for name in both]))
354 cmd_results = RunCmdAndCheck(cmd,
355 'PPAPI IDL Diff detected: Run the generator.',
357 os.path.join(ppapi_dir, 'generators'))
359 results.extend(cmd_results)
364 def CheckChangeOnUpload(input_api, output_api):
365 return CheckChange(input_api, output_api)
368 def CheckChangeOnCommit(input_api, output_api):
369 return CheckChange(input_api, output_api)