Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(7)

Side by Side Diff: tools/dom/new_scripts/code_generator_dart.py

Issue 444743002: Use Blink IDL parser for dart libraries. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Merged Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « tools/dom/idl/dart/dart.idl ('k') | tools/dom/new_scripts/compiler.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 from utilities import write_pickle_file
84 from v8_globals import includes, interfaces
85 from dart_utilities import DartUtilities
86
87
88 # TODO(jacobr): remove this hacked together list.
89 INTERFACES_WITHOUT_RESOLVERS = frozenset([
90 'TypeConversions',
91 'GCObservation',
92 'InternalProfilers',
93 'InternalRuntimeFlags',
94 'InternalSettings',
95 'InternalSettingsGenerated',
96 'Internals',
97 'LayerRect',
98 'LayerRectList',
99 'MallocStatistics',
100 'TypeConversions'])
101
102 class CodeGeneratorDart(object):
103 def __init__(self, interfaces_info, cache_dir):
104 interfaces_info = interfaces_info or {}
105 self.interfaces_info = interfaces_info
106 self.jinja_env = initialize_jinja_env(cache_dir)
107
108 # Set global type info
109 idl_types.set_ancestors(dict(
110 (interface_name, interface_info['ancestors'])
111 for interface_name, interface_info in interfaces_info.iteritems()
112 if interface_info['ancestors']))
113 IdlType.set_callback_interfaces(set(
114 interface_name
115 for interface_name, interface_info in interfaces_info.iteritems()
116 if interface_info['is_callback_interface']))
117 IdlType.set_implemented_as_interfaces(dict(
118 (interface_name, interface_info['implemented_as'])
119 for interface_name, interface_info in interfaces_info.iteritems()
120 if interface_info['implemented_as']))
121 IdlType.set_garbage_collected_types(set(
122 interface_name
123 for interface_name, interface_info in interfaces_info.iteritems()
124 if 'GarbageCollected' in interface_info['inherited_extended_attribut es']))
125 IdlType.set_will_be_garbage_collected_types(set(
126 interface_name
127 for interface_name, interface_info in interfaces_info.iteritems()
128 if 'WillBeGarbageCollected' in interface_info['inherited_extended_at tributes']))
129
130 def generate_code(self, definitions, interface_name, idl_pickle_filename,
131 only_if_changed):
132 """Returns .h/.cpp code as (header_text, cpp_text)."""
133 try:
134 interface = definitions.interfaces[interface_name]
135 except KeyError:
136 raise Exception('%s not in IDL definitions' % interface_name)
137
138 # Store other interfaces for introspection
139 interfaces.update(definitions.interfaces)
140
141 # Set local type info
142 IdlType.set_callback_functions(definitions.callback_functions.keys())
143 IdlType.set_enums((enum.name, enum.values)
144 for enum in definitions.enumerations.values())
145
146 # Select appropriate Jinja template and contents function
147 if interface.is_callback:
148 header_template_filename = 'callback_interface_h.template'
149 cpp_template_filename = 'callback_interface_cpp.template'
150 generate_contents = dart_callback_interface.generate_callback_interf ace
151 else:
152 header_template_filename = 'interface_h.template'
153 cpp_template_filename = 'interface_cpp.template'
154 generate_contents = dart_interface.generate_interface
155 header_template = self.jinja_env.get_template(header_template_filename)
156 cpp_template = self.jinja_env.get_template(cpp_template_filename)
157
158 # Generate contents (input parameters for Jinja)
159 template_contents = generate_contents(interface)
160 template_contents['code_generator'] = module_pyname
161
162 # Add includes for interface itself and any dependencies
163 interface_info = self.interfaces_info[interface_name]
164 template_contents['header_includes'].add(interface_info['include_path'])
165 template_contents['header_includes'] = sorted(template_contents['header_ includes'])
166 includes.update(interface_info.get('dependencies_include_paths', []))
167
168 # Remove includes that are not needed for Dart and trigger fatal
169 # compile warnings if included. These IDL files need to be
170 # imported by Dart to generate the list of events but the
171 # associated header files do not contain any code used by Dart.
172 includes.discard('core/dom/GlobalEventHandlers.h')
173 includes.discard('core/frame/DOMWindowEventHandlers.h')
174
175 template_contents['cpp_includes'] = sorted(includes)
176
177 idl_world = {'interface': None, 'callback': None}
178
179 # Load the pickle file for this IDL.
180 if os.path.isfile(idl_pickle_filename):
181 with open(idl_pickle_filename) as idl_pickle_file:
182 idl_global_data = pickle.load(idl_pickle_file)
183 idl_pickle_file.close()
184 idl_world['interface'] = idl_global_data['interface']
185 idl_world['callback'] = idl_global_data['callback']
186
187 if 'interface_name' in template_contents:
188 interface_global = {'name': template_contents['interface_name'],
189 'parent_interface': template_contents['parent_in terface'],
190 'is_active_dom_object': template_contents['is_ac tive_dom_object'],
191 'is_event_target': template_contents['is_event_t arget'],
192 'has_resolver': template_contents['interface_nam e'] not in INTERFACES_WITHOUT_RESOLVERS,
193 'is_node': template_contents['is_node'],
194 'conditional_string': template_contents['conditi onal_string'],
195 }
196 idl_world['interface'] = interface_global
197 else:
198 callback_global = {'name': template_contents['cpp_class']}
199 idl_world['callback'] = callback_global
200
201 write_pickle_file(idl_pickle_filename, idl_world, only_if_changed)
202
203 # Render Jinja templates
204 header_text = header_template.render(template_contents)
205 cpp_text = cpp_template.render(template_contents)
206 return header_text, cpp_text
207
208 # Generates global file for all interfaces.
209 def generate_globals(self, output_directory):
210 header_template_filename = 'global_h.template'
211 cpp_template_filename = 'global_cpp.template'
212
213 # Delete the global pickle file we'll rebuild from each pickle generated
214 # for each IDL file '(%s_globals.pickle) % interface_name'.
215 global_pickle_filename = os.path.join(output_directory, 'global.pickle')
216 if os.path.isfile(global_pickle_filename):
217 os.remove(global_pickle_filename)
218
219 # List of all interfaces and callbacks for global code generation.
220 world = {'interfaces': [], 'callbacks': []}
221
222 # Load all pickled data for each interface.
223 listing = os.listdir(output_directory)
224 for filename in listing:
225 if filename.endswith('_globals.pickle'):
226 idl_filename = os.path.join(output_directory, filename)
227 with open(idl_filename) as idl_pickle_file:
228 idl_world = pickle.load(idl_pickle_file)
229 if 'interface' in idl_world:
230 # FIXME: Why are some of these None?
231 if idl_world['interface']:
232 world['interfaces'].append(idl_world['interface'])
233 if 'callbacks' in idl_world:
234 # FIXME: Why are some of these None?
235 if idl_world['callbacks']:
236 world['callbacks'].append(idl_world['callback'])
237 idl_pickle_file.close()
238
239 world['interfaces'] = sorted(world['interfaces'], key=lambda (x): x['nam e'])
240 world['callbacks'] = sorted(world['callbacks'], key=lambda (x): x['name' ])
241
242 template_contents = world
243 template_contents['code_generator'] = module_pyname
244
245 header_template = self.jinja_env.get_template(header_template_filename)
246 header_text = header_template.render(template_contents)
247
248 cpp_template = self.jinja_env.get_template(cpp_template_filename)
249 cpp_text = cpp_template.render(template_contents)
250 return header_text, cpp_text
251
252
253 def initialize_jinja_env(cache_dir):
254 jinja_env = jinja2.Environment(
255 loader=jinja2.FileSystemLoader(templates_dir),
256 # Bytecode cache is not concurrency-safe unless pre-cached:
257 # if pre-cached this is read-only, but writing creates a race condition.
258 bytecode_cache=jinja2.FileSystemBytecodeCache(cache_dir),
259 keep_trailing_newline=True, # newline-terminate generated files
260 lstrip_blocks=True, # so can indent control flow tags
261 trim_blocks=True)
262 jinja_env.filters.update({
263 'blink_capitalize': DartUtilities.capitalize,
264 'conditional': conditional_if_endif,
265 'runtime_enabled': runtime_enabled_if,
266 })
267 return jinja_env
268
269
270 # [Conditional]
271 def conditional_if_endif(code, conditional_string):
272 # Jinja2 filter to generate if/endif directive blocks
273 if not conditional_string:
274 return code
275 return ('#if %s\n' % conditional_string +
276 code +
277 '#endif // %s\n' % conditional_string)
278
279
280 # [RuntimeEnabled]
281 def runtime_enabled_if(code, runtime_enabled_function_name):
282 if not runtime_enabled_function_name:
283 return code
284 # Indent if statement to level of original code
285 indent = re.match(' *', code).group(0)
286 return ('%sif (%s())\n' % (indent, runtime_enabled_function_name) +
287 ' %s' % code)
288
289
290 ################################################################################
291
292 def main(argv):
293 # If file itself executed, cache templates
294 try:
295 cache_dir = argv[1]
296 dummy_filename = argv[2]
297 except IndexError as err:
298 print 'Usage: %s OUTPUT_DIR DUMMY_FILENAME' % argv[0]
299 return 1
300
301 # Cache templates
302 jinja_env = initialize_jinja_env(cache_dir)
303 template_filenames = [filename for filename in os.listdir(templates_dir)
304 # Skip .svn, directories, etc.
305 if filename.endswith(('.cpp', '.h', '.template'))]
306 for template_filename in template_filenames:
307 jinja_env.get_template(template_filename)
308
309 # Create a dummy file as output for the build system,
310 # since filenames of individual cache files are unpredictable and opaque
311 # (they are hashes of the template path, which varies based on environment)
312 with open(dummy_filename, 'w') as dummy_file:
313 pass # |open| creates or touches the file
314
315
316 if __name__ == '__main__':
317 sys.exit(main(sys.argv))
OLDNEW
« no previous file with comments | « tools/dom/idl/dart/dart.idl ('k') | tools/dom/new_scripts/compiler.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698