OLD | NEW |
| (Empty) |
1 # Copyright (C) 2013 Google Inc. All rights reserved. | |
2 # | |
3 # Redistribution and use in source and binary forms, with or without | |
4 # modification, are permitted provided that the following conditions are | |
5 # met: | |
6 # | |
7 # * Redistributions of source code must retain the above copyright | |
8 # notice, this list of conditions and the following disclaimer. | |
9 # * Redistributions in binary form must reproduce the above | |
10 # copyright notice, this list of conditions and the following disclaimer | |
11 # in the documentation and/or other materials provided with the | |
12 # distribution. | |
13 # * Neither the name of Google Inc. nor the names of its | |
14 # contributors may be used to endorse or promote products derived from | |
15 # this software without specific prior written permission. | |
16 # | |
17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
18 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
19 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
20 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
21 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
22 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
23 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
24 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
25 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
27 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
28 | |
29 """Generate Blink C++ bindings (.h and .cpp files) for use by Dart:HTML. | |
30 | |
31 If run itself, caches Jinja templates (and creates dummy file for build, | |
32 since cache filenames are unpredictable and opaque). | |
33 | |
34 This module is *not* concurrency-safe without care: bytecode caching creates | |
35 a race condition on cache *write* (crashes if one process tries to read a | |
36 partially-written cache). However, if you pre-cache the templates (by running | |
37 the module itself), then you can parallelize compiling individual files, since | |
38 cache *reading* is safe. | |
39 | |
40 Input: An object of class IdlDefinitions, containing an IDL interface X | |
41 Output: DartX.h and DartX.cpp | |
42 | |
43 Design doc: http://www.chromium.org/developers/design-documents/idl-compiler | |
44 """ | |
45 | |
46 import os | |
47 import cPickle as pickle | |
48 import re | |
49 import sys | |
50 | |
51 | |
52 # Path handling for libraries and templates | |
53 # Paths have to be normalized because Jinja uses the exact template path to | |
54 # determine the hash used in the cache filename, and we need a pre-caching step | |
55 # to be concurrency-safe. Use absolute path because __file__ is absolute if | |
56 # module is imported, and relative if executed directly. | |
57 # If paths differ between pre-caching and individual file compilation, the cache | |
58 # is regenerated, which causes a race condition and breaks concurrent build, | |
59 # since some compile processes will try to read the partially written cache. | |
60 module_path, module_filename = os.path.split(os.path.realpath(__file__)) | |
61 third_party_dir = os.path.normpath(os.path.join( | |
62 module_path, os.pardir, os.pardir, os.pardir, os.pardir, os.pardir)) | |
63 templates_dir = os.path.normpath(os.path.join(module_path, 'templates')) | |
64 | |
65 # Make sure extension is .py, not .pyc or .pyo, so doesn't depend on caching | |
66 module_pyname = os.path.splitext(module_filename)[0] + '.py' | |
67 | |
68 # jinja2 is in chromium's third_party directory. | |
69 # Insert at 1 so at front to override system libraries, and | |
70 # after path[0] == invoking script dir | |
71 sys.path.insert(1, third_party_dir) | |
72 | |
73 # Add the base compiler scripts to the path here as in compiler.py | |
74 dart_script_path = os.path.dirname(os.path.abspath(__file__)) | |
75 script_path = os.path.join(os.path.dirname(os.path.dirname(dart_script_path)), | |
76 'scripts') | |
77 sys.path.extend([script_path]) | |
78 | |
79 import jinja2 | |
80 | |
81 import idl_types | |
82 from idl_types import IdlType | |
83 import dart_callback_interface | |
84 import dart_interface | |
85 import dart_types | |
86 from dart_utilities import DartUtilities | |
87 from utilities import write_pickle_file, idl_filename_to_interface_name | |
88 import dart_dictionary | |
89 from v8_globals import includes, interfaces | |
90 | |
91 | |
92 def render_template(interface_info, header_template, cpp_template, | |
93 template_context): | |
94 template_context['code_generator'] = module_pyname | |
95 | |
96 # Add includes for any dependencies | |
97 template_context['header_includes'] = sorted( | |
98 template_context['header_includes']) | |
99 includes.update(interface_info.get('dependencies_include_paths', [])) | |
100 template_context['cpp_includes'] = sorted(includes) | |
101 | |
102 header_text = header_template.render(template_context) | |
103 cpp_text = cpp_template.render(template_context) | |
104 return header_text, cpp_text | |
105 | |
106 class CodeGeneratorDart(object): | |
107 def __init__(self, interfaces_info, cache_dir): | |
108 interfaces_info = interfaces_info or {} | |
109 self.interfaces_info = interfaces_info | |
110 self.jinja_env = initialize_jinja_env(cache_dir) | |
111 | |
112 # Set global type info | |
113 if 'ancestors' in interfaces_info: | |
114 idl_types.set_ancestors(interfaces_info['ancestors']) | |
115 if 'callback_interfaces' in interfaces_info: | |
116 IdlType.set_callback_interfaces(interfaces_info['callback_interfaces
']) | |
117 if 'dictionaries' in interfaces_info: | |
118 IdlType.set_dictionaries(interfaces_info['dictionaries']) | |
119 if 'implemented_as_interfaces' in interfaces_info: | |
120 IdlType.set_implemented_as_interfaces(interfaces_info['implemented_a
s_interfaces']) | |
121 if 'garbage_collected_interfaces' in interfaces_info: | |
122 IdlType.set_garbage_collected_types(interfaces_info['garbage_collect
ed_interfaces']) | |
123 if 'will_be_garbage_collected_interfaces' in interfaces_info: | |
124 IdlType.set_will_be_garbage_collected_types(interfaces_info['will_be
_garbage_collected_interfaces']) | |
125 if 'component_dirs' in interfaces_info: | |
126 dart_types.set_component_dirs(interfaces_info['component_dirs']) | |
127 | |
128 def generate_code(self, definitions, interface_name, idl_pickle_filename, | |
129 only_if_changed): | |
130 """Returns .h/.cpp code as (header_text, cpp_text).""" | |
131 | |
132 IdlType.set_enums((enum.name, enum.values) | |
133 for enum in definitions.enumerations.values()) | |
134 | |
135 if interface_name in definitions.interfaces: | |
136 interface = definitions.interfaces[interface_name] | |
137 elif interface_name in definitions.dictionaries: | |
138 output_code_list = self.generate_dictionary_code( | |
139 definitions, interface_name, | |
140 definitions.dictionaries[interface_name]) | |
141 | |
142 # Pickle the dictionary information... | |
143 idl_world = self.load_idl_pickle_file(idl_pickle_filename) | |
144 idl_world['dictionary'] = {'name': interface_name} | |
145 write_pickle_file(idl_pickle_filename, idl_world, only_if_changed) | |
146 | |
147 return output_code_list | |
148 else: | |
149 raise ValueError('%s is not in IDL definitions' % interface_name) | |
150 | |
151 # Store other interfaces for introspection | |
152 interfaces.update(definitions.interfaces) | |
153 | |
154 # Set local type info | |
155 IdlType.set_callback_functions(definitions.callback_functions.keys()) | |
156 | |
157 # Select appropriate Jinja template and contents function | |
158 if interface.is_callback: | |
159 header_template_filename = 'callback_interface_h.template' | |
160 cpp_template_filename = 'callback_interface_cpp.template' | |
161 generate_contents = dart_callback_interface.generate_callback_interf
ace | |
162 else: | |
163 header_template_filename = 'interface_h.template' | |
164 cpp_template_filename = 'interface_cpp.template' | |
165 generate_contents = dart_interface.interface_context | |
166 header_template = self.jinja_env.get_template(header_template_filename) | |
167 cpp_template = self.jinja_env.get_template(cpp_template_filename) | |
168 | |
169 # Generate contents (input parameters for Jinja) | |
170 template_contents = generate_contents(interface) | |
171 template_contents['code_generator'] = module_pyname | |
172 | |
173 # Add includes for interface itself and any dependencies | |
174 interface_info = self.interfaces_info[interface_name] | |
175 template_contents['header_includes'].add(interface_info['include_path']) | |
176 template_contents['header_includes'] = sorted(template_contents['header_
includes']) | |
177 includes.update(interface_info.get('dependencies_include_paths', [])) | |
178 | |
179 # Remove includes that are not needed for Dart and trigger fatal | |
180 # compile warnings if included. These IDL files need to be | |
181 # imported by Dart to generate the list of events but the | |
182 # associated header files do not contain any code used by Dart. | |
183 includes.discard('core/dom/GlobalEventHandlers.h') | |
184 includes.discard('core/frame/DOMWindowEventHandlers.h') | |
185 | |
186 # Remove v8 usages not needed. | |
187 includes.discard('core/frame/UseCounter.h') | |
188 includes.discard('bindings/core/v8/V8ScriptState.h') | |
189 includes.discard('bindings/core/v8/V8DOMActivityLogger.h') | |
190 includes.discard('bindings/core/v8/V8DOMConfiguration.h') | |
191 includes.discard('bindings/core/v8/V8ExceptionState.h') | |
192 includes.discard('bindings/core/v8/V8HiddenValue.h') | |
193 includes.discard('bindings/core/v8/V8ObjectConstructor.h') | |
194 includes.discard('core/dom/ContextFeatures.h') | |
195 includes.discard('core/dom/Document.h') | |
196 includes.discard('platform/RuntimeEnabledFeatures.h') | |
197 includes.discard('platform/TraceEvent.h') | |
198 | |
199 template_contents['cpp_includes'] = sorted(includes) | |
200 | |
201 idl_world = self.load_idl_pickle_file(idl_pickle_filename) | |
202 | |
203 if 'interface_name' in template_contents: | |
204 # interfaces no longer remember there component_dir re-compute based | |
205 # on relative_dir (first directory is the component). | |
206 component_dir = split_path(interface_info['relative_dir'])[0] | |
207 interface_global = {'name': template_contents['interface_name'], | |
208 'parent_interface': template_contents['parent_in
terface'], | |
209 'is_active_dom_object': template_contents['is_ac
tive_dom_object'], | |
210 'is_event_target': template_contents['is_event_t
arget'], | |
211 'has_resolver': template_contents['interface_nam
e'], | |
212 'native_entries': sorted(template_contents['nati
ve_entries'], key=lambda(x): x['blink_entry']), | |
213 'is_node': template_contents['is_node'], | |
214 'conditional_string': template_contents['conditi
onal_string'], | |
215 'component_dir': component_dir, | |
216 } | |
217 idl_world['interface'] = interface_global | |
218 else: | |
219 callback_global = {'name': template_contents['cpp_class']} | |
220 idl_world['callback'] = callback_global | |
221 | |
222 write_pickle_file(idl_pickle_filename, idl_world, only_if_changed) | |
223 | |
224 # Render Jinja templates | |
225 header_text = header_template.render(template_contents) | |
226 cpp_text = cpp_template.render(template_contents) | |
227 return header_text, cpp_text | |
228 | |
229 def load_idl_pickle_file(self, idl_pickle_filename): | |
230 # Pickle the dictionary information... | |
231 idl_world = {'interface': None, 'callback': None, 'dictionary': None} | |
232 | |
233 # Load the pickle file for this IDL. | |
234 if os.path.isfile(idl_pickle_filename): | |
235 with open(idl_pickle_filename) as idl_pickle_file: | |
236 idl_global_data = pickle.load(idl_pickle_file) | |
237 idl_pickle_file.close() | |
238 idl_world['interface'] = idl_global_data['interface'] | |
239 idl_world['callback'] = idl_global_data['callback'] | |
240 idl_world['dictionary'] = idl_global_data['dictionary'] | |
241 | |
242 return idl_world | |
243 | |
244 def generate_dictionary_code(self, definitions, dictionary_name, | |
245 dictionary): | |
246 interface_info = self.interfaces_info[dictionary_name] | |
247 bindings_results = self.generate_dictionary_bindings( | |
248 dictionary_name, interface_info, dictionary) | |
249 impl_results = self.generate_dictionary_impl( | |
250 dictionary_name, interface_info, dictionary) | |
251 return bindings_results + impl_results | |
252 | |
253 def generate_dictionary_bindings(self, dictionary_name, | |
254 interface_info, dictionary): | |
255 header_template = self.jinja_env.get_template('dictionary_dart_h.templat
e') | |
256 cpp_template = self.jinja_env.get_template('dictionary_dart_cpp.template
') | |
257 template_context = dart_dictionary.dictionary_context(dictionary) | |
258 # Add the include for interface itself | |
259 template_context['header_includes'].add(interface_info['include_path']) | |
260 header_text, cpp_text = render_template( | |
261 interface_info, header_template, cpp_template, template_context) | |
262 return (header_text, cpp_text) | |
263 | |
264 def generate_dictionary_impl(self, dictionary_name, | |
265 interface_info, dictionary): | |
266 header_template = self.jinja_env.get_template('dictionary_impl_h.templat
e') | |
267 cpp_template = self.jinja_env.get_template('dictionary_impl_cpp.template
') | |
268 template_context = dart_dictionary.dictionary_impl_context( | |
269 dictionary, self.interfaces_info) | |
270 header_text, cpp_text = render_template( | |
271 interface_info, header_template, cpp_template, template_context) | |
272 return (header_text, cpp_text) | |
273 | |
274 def load_global_pickles(self, global_entries): | |
275 # List of all interfaces and callbacks for global code generation. | |
276 world = {'interfaces': [], 'callbacks': [], 'dictionary': []} | |
277 | |
278 # Load all pickled data for each interface. | |
279 for (directory, file_list) in global_entries: | |
280 for idl_filename in file_list: | |
281 interface_name = idl_filename_to_interface_name(idl_filename) | |
282 idl_pickle_filename = interface_name + "_globals.pickle" | |
283 idl_pickle_filename = os.path.join(directory, idl_pickle_filenam
e) | |
284 with open(idl_pickle_filename) as idl_pickle_file: | |
285 idl_world = pickle.load(idl_pickle_file) | |
286 if 'interface' in idl_world: | |
287 # FIXME: Why are some of these None? | |
288 if idl_world['interface']: | |
289 world['interfaces'].append(idl_world['interface']) | |
290 if 'callbacks' in idl_world: | |
291 # FIXME: Why are some of these None? | |
292 if idl_world['callbacks']: | |
293 world['callbacks'].append(idl_world['callback']) | |
294 if 'dictionary' in idl_world: | |
295 # It's an IDL dictionary | |
296 if idl_world['dictionary']: | |
297 world['dictionary'].append(idl_world['dictionary']) | |
298 world['interfaces'] = sorted(world['interfaces'], key=lambda (x): x['nam
e']) | |
299 world['callbacks'] = sorted(world['callbacks'], key=lambda (x): x['name'
]) | |
300 world['dictionary'] = sorted(world['dictionary'], key=lambda (x): x['nam
e']) | |
301 return world | |
302 | |
303 # Generates global file for all interfaces. | |
304 def generate_globals(self, global_entries): | |
305 template_contents = self.load_global_pickles(global_entries) | |
306 template_contents['code_generator'] = module_pyname | |
307 | |
308 header_template_filename = 'global_h.template' | |
309 header_template = self.jinja_env.get_template(header_template_filename) | |
310 header_text = header_template.render(template_contents) | |
311 | |
312 cpp_template_filename = 'global_cpp.template' | |
313 cpp_template = self.jinja_env.get_template(cpp_template_filename) | |
314 cpp_text = cpp_template.render(template_contents) | |
315 | |
316 return header_text, cpp_text | |
317 | |
318 # Generates global dart blink file for all interfaces. | |
319 def generate_dart_blink(self, global_entries): | |
320 template_contents = self.load_global_pickles(global_entries) | |
321 template_contents['code_generator'] = module_pyname | |
322 | |
323 template_filename = 'dart_blink.template' | |
324 template = self.jinja_env.get_template(template_filename) | |
325 | |
326 text = template.render(template_contents) | |
327 return text | |
328 | |
329 | |
330 def initialize_jinja_env(cache_dir): | |
331 jinja_env = jinja2.Environment( | |
332 loader=jinja2.FileSystemLoader(templates_dir), | |
333 # Bytecode cache is not concurrency-safe unless pre-cached: | |
334 # if pre-cached this is read-only, but writing creates a race condition. | |
335 # bytecode_cache=jinja2.FileSystemBytecodeCache(cache_dir), | |
336 keep_trailing_newline=True, # newline-terminate generated files | |
337 lstrip_blocks=True, # so can indent control flow tags | |
338 trim_blocks=True) | |
339 jinja_env.filters.update({ | |
340 'blink_capitalize': DartUtilities.capitalize, | |
341 'conditional': conditional_if_endif, | |
342 'runtime_enabled': runtime_enabled_if, | |
343 }) | |
344 return jinja_env | |
345 | |
346 | |
347 # [Conditional] | |
348 def conditional_if_endif(code, conditional_string): | |
349 # Jinja2 filter to generate if/endif directive blocks | |
350 if not conditional_string: | |
351 return code | |
352 return ('#if %s\n' % conditional_string + | |
353 code + | |
354 '#endif // %s\n' % conditional_string) | |
355 | |
356 | |
357 # [RuntimeEnabled] | |
358 def runtime_enabled_if(code, runtime_enabled_function_name): | |
359 if not runtime_enabled_function_name: | |
360 return code | |
361 # Indent if statement to level of original code | |
362 indent = re.match(' *', code).group(0) | |
363 return ('%sif (%s())\n' % (indent, runtime_enabled_function_name) + | |
364 ' %s' % code) | |
365 | |
366 | |
367 def split_path(path): | |
368 path_list = [] | |
369 while os.path.basename(path): | |
370 path_list.append(os.path.basename(path)) | |
371 path = os.path.dirname(path) | |
372 path_list.reverse() | |
373 return path_list | |
374 | |
375 | |
376 ################################################################################ | |
377 | |
378 def main(argv): | |
379 # If file itself executed, cache templates | |
380 try: | |
381 cache_dir = argv[1] | |
382 dummy_filename = argv[2] | |
383 except IndexError as err: | |
384 print 'Usage: %s OUTPUT_DIR DUMMY_FILENAME' % argv[0] | |
385 return 1 | |
386 | |
387 # Cache templates | |
388 jinja_env = initialize_jinja_env(cache_dir) | |
389 template_filenames = [filename for filename in os.listdir(templates_dir) | |
390 # Skip .svn, directories, etc. | |
391 if filename.endswith(('.cpp', '.h', '.template'))] | |
392 for template_filename in template_filenames: | |
393 jinja_env.get_template(template_filename) | |
394 | |
395 # Create a dummy file as output for the build system, | |
396 # since filenames of individual cache files are unpredictable and opaque | |
397 # (they are hashes of the template path, which varies based on environment) | |
398 with open(dummy_filename, 'w') as dummy_file: | |
399 pass # |open| creates or touches the file | |
400 | |
401 | |
402 if __name__ == '__main__': | |
403 sys.exit(main(sys.argv)) | |
OLD | NEW |