OLD | NEW |
1 # Copyright (C) 2013 Google Inc. All rights reserved. | 1 # Copyright (C) 2013 Google Inc. All rights reserved. |
2 # | 2 # |
3 # Redistribution and use in source and binary forms, with or without | 3 # Redistribution and use in source and binary forms, with or without |
4 # modification, are permitted provided that the following conditions are | 4 # modification, are permitted provided that the following conditions are |
5 # met: | 5 # met: |
6 # | 6 # |
7 # * Redistributions of source code must retain the above copyright | 7 # * Redistributions of source code must retain the above copyright |
8 # notice, this list of conditions and the following disclaimer. | 8 # notice, this list of conditions and the following disclaimer. |
9 # * Redistributions in binary form must reproduce the above | 9 # * Redistributions in binary form must reproduce the above |
10 # copyright notice, this list of conditions and the following disclaimer | 10 # copyright notice, this list of conditions and the following disclaimer |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
71 import jinja2 | 71 import jinja2 |
72 | 72 |
73 import idl_types | 73 import idl_types |
74 from idl_types import IdlType | 74 from idl_types import IdlType |
75 import v8_callback_interface | 75 import v8_callback_interface |
76 import v8_dictionary | 76 import v8_dictionary |
77 from v8_globals import includes, interfaces | 77 from v8_globals import includes, interfaces |
78 import v8_interface | 78 import v8_interface |
79 import v8_types | 79 import v8_types |
80 from v8_utilities import capitalize, cpp_name, conditional_string, v8_class_name | 80 from v8_utilities import capitalize, cpp_name, conditional_string, v8_class_name |
81 from utilities import KNOWN_COMPONENTS | 81 from utilities import KNOWN_COMPONENTS, idl_filename_to_component, is_valid_comp
onent_dependency |
82 | 82 |
83 | 83 |
84 def render_template(interface_info, header_template, cpp_template, | 84 def render_template(include_paths, header_template, cpp_template, |
85 template_context): | 85 template_context, component=None): |
86 template_context['code_generator'] = module_pyname | 86 template_context['code_generator'] = module_pyname |
87 | 87 |
88 # Add includes for any dependencies | 88 # Add includes for any dependencies |
89 template_context['header_includes'] = sorted( | 89 template_context['header_includes'] = sorted( |
90 template_context['header_includes']) | 90 template_context['header_includes']) |
91 includes.update(interface_info.get('dependencies_include_paths', [])) | 91 |
| 92 for include_path in include_paths: |
| 93 if component: |
| 94 dependency = idl_filename_to_component(include_path) |
| 95 assert is_valid_component_dependency(component, dependency) |
| 96 includes.add(include_path) |
| 97 |
92 template_context['cpp_includes'] = sorted(includes) | 98 template_context['cpp_includes'] = sorted(includes) |
93 | 99 |
94 header_text = header_template.render(template_context) | 100 header_text = header_template.render(template_context) |
95 cpp_text = cpp_template.render(template_context) | 101 cpp_text = cpp_template.render(template_context) |
96 return header_text, cpp_text | 102 return header_text, cpp_text |
97 | 103 |
98 | 104 |
99 class CodeGeneratorBase(object): | 105 class CodeGeneratorBase(object): |
100 """Base class for v8 bindings generator and IDL dictionary impl generator""" | 106 """Base class for v8 bindings generator and IDL dictionary impl generator""" |
101 | 107 |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
145 if definition_name in definitions.dictionaries: | 151 if definition_name in definitions.dictionaries: |
146 return self.generate_dictionary_code( | 152 return self.generate_dictionary_code( |
147 definitions, definition_name, | 153 definitions, definition_name, |
148 definitions.dictionaries[definition_name]) | 154 definitions.dictionaries[definition_name]) |
149 raise ValueError('%s is not in IDL definitions' % definition_name) | 155 raise ValueError('%s is not in IDL definitions' % definition_name) |
150 | 156 |
151 def generate_interface_code(self, definitions, interface_name, interface): | 157 def generate_interface_code(self, definitions, interface_name, interface): |
152 # Store other interfaces for introspection | 158 # Store other interfaces for introspection |
153 interfaces.update(definitions.interfaces) | 159 interfaces.update(definitions.interfaces) |
154 | 160 |
| 161 interface_info = self.interfaces_info[interface_name] |
| 162 component = idl_filename_to_component( |
| 163 interface_info.get('full_path')) |
| 164 include_paths = interface_info.get('dependencies_include_paths') |
| 165 |
155 # Select appropriate Jinja template and contents function | 166 # Select appropriate Jinja template and contents function |
156 if interface.is_callback: | 167 if interface.is_callback: |
157 header_template_filename = 'callback_interface.h' | 168 header_template_filename = 'callback_interface.h' |
158 cpp_template_filename = 'callback_interface.cpp' | 169 cpp_template_filename = 'callback_interface.cpp' |
159 interface_context = v8_callback_interface.callback_interface_context | 170 interface_context = v8_callback_interface.callback_interface_context |
| 171 elif interface.is_partial: |
| 172 interface_context = v8_interface.interface_context |
| 173 header_template_filename = 'partial_interface.h' |
| 174 cpp_template_filename = 'partial_interface.cpp' |
| 175 interface_name += 'Partial' |
| 176 assert component == 'core' |
| 177 component = 'modules' |
| 178 include_paths = interface_info.get('dependencies_other_component_inc
lude_paths') |
160 else: | 179 else: |
161 header_template_filename = 'interface.h' | 180 header_template_filename = 'interface.h' |
162 cpp_template_filename = 'interface.cpp' | 181 cpp_template_filename = 'interface.cpp' |
163 interface_context = v8_interface.interface_context | 182 interface_context = v8_interface.interface_context |
164 header_template = self.jinja_env.get_template(header_template_filename) | 183 header_template = self.jinja_env.get_template(header_template_filename) |
165 cpp_template = self.jinja_env.get_template(cpp_template_filename) | 184 cpp_template = self.jinja_env.get_template(cpp_template_filename) |
166 | 185 |
167 interface_info = self.interfaces_info[interface_name] | |
168 | |
169 template_context = interface_context(interface) | 186 template_context = interface_context(interface) |
170 # Add the include for interface itself | 187 # Add the include for interface itself |
171 if IdlType(interface_name).is_typed_array: | 188 if IdlType(interface_name).is_typed_array: |
172 template_context['header_includes'].add('core/dom/DOMTypedArray.h') | 189 template_context['header_includes'].add('core/dom/DOMTypedArray.h') |
173 else: | 190 else: |
174 template_context['header_includes'].add(interface_info['include_path
']) | 191 template_context['header_includes'].add(interface_info['include_path
']) |
175 header_text, cpp_text = render_template( | 192 header_text, cpp_text = render_template( |
176 interface_info, header_template, cpp_template, template_context) | 193 include_paths, header_template, cpp_template, template_context, |
| 194 component) |
177 header_path, cpp_path = self.output_paths(interface_name) | 195 header_path, cpp_path = self.output_paths(interface_name) |
178 return ( | 196 return ( |
179 (header_path, header_text), | 197 (header_path, header_text), |
180 (cpp_path, cpp_text), | 198 (cpp_path, cpp_text), |
181 ) | 199 ) |
182 | 200 |
183 def generate_dictionary_code(self, definitions, dictionary_name, | 201 def generate_dictionary_code(self, definitions, dictionary_name, |
184 dictionary): | 202 dictionary): |
185 header_template = self.jinja_env.get_template('dictionary_v8.h') | 203 header_template = self.jinja_env.get_template('dictionary_v8.h') |
186 cpp_template = self.jinja_env.get_template('dictionary_v8.cpp') | 204 cpp_template = self.jinja_env.get_template('dictionary_v8.cpp') |
187 template_context = v8_dictionary.dictionary_context(dictionary) | 205 template_context = v8_dictionary.dictionary_context(dictionary) |
188 interface_info = self.interfaces_info[dictionary_name] | 206 interface_info = self.interfaces_info[dictionary_name] |
| 207 include_paths = interface_info.get('dependencies_include_paths') |
189 # Add the include for interface itself | 208 # Add the include for interface itself |
190 template_context['header_includes'].add(interface_info['include_path']) | 209 template_context['header_includes'].add(interface_info['include_path']) |
191 header_text, cpp_text = render_template( | 210 header_text, cpp_text = render_template( |
192 interface_info, header_template, cpp_template, template_context) | 211 include_paths, header_template, cpp_template, template_context) |
193 header_path, cpp_path = self.output_paths(dictionary_name) | 212 header_path, cpp_path = self.output_paths(dictionary_name) |
194 return ( | 213 return ( |
195 (header_path, header_text), | 214 (header_path, header_text), |
196 (cpp_path, cpp_text), | 215 (cpp_path, cpp_text), |
197 ) | 216 ) |
198 | 217 |
199 | 218 |
200 class CodeGeneratorDictionaryImpl(CodeGeneratorBase): | 219 class CodeGeneratorDictionaryImpl(CodeGeneratorBase): |
201 def __init__(self, interfaces_info, cache_dir, output_dir): | 220 def __init__(self, interfaces_info, cache_dir, output_dir): |
202 CodeGeneratorBase.__init__(self, interfaces_info, cache_dir, output_dir) | 221 CodeGeneratorBase.__init__(self, interfaces_info, cache_dir, output_dir) |
203 | 222 |
204 def output_paths(self, definition_name, interface_info): | 223 def output_paths(self, definition_name, interface_info): |
205 output_dir = posixpath.join(self.output_dir, | 224 output_dir = posixpath.join(self.output_dir, |
206 interface_info['relative_dir']) | 225 interface_info['relative_dir']) |
207 header_path = posixpath.join(output_dir, '%s.h' % definition_name) | 226 header_path = posixpath.join(output_dir, '%s.h' % definition_name) |
208 cpp_path = posixpath.join(output_dir, '%s.cpp' % definition_name) | 227 cpp_path = posixpath.join(output_dir, '%s.cpp' % definition_name) |
209 return header_path, cpp_path | 228 return header_path, cpp_path |
210 | 229 |
211 def generate_code_internal(self, definitions, definition_name): | 230 def generate_code_internal(self, definitions, definition_name): |
212 if not definition_name in definitions.dictionaries: | 231 if not definition_name in definitions.dictionaries: |
213 raise ValueError('%s is not an IDL dictionary') | 232 raise ValueError('%s is not an IDL dictionary') |
214 dictionary = definitions.dictionaries[definition_name] | 233 dictionary = definitions.dictionaries[definition_name] |
215 interface_info = self.interfaces_info[definition_name] | 234 interface_info = self.interfaces_info[definition_name] |
216 header_template = self.jinja_env.get_template('dictionary_impl.h') | 235 header_template = self.jinja_env.get_template('dictionary_impl.h') |
217 cpp_template = self.jinja_env.get_template('dictionary_impl.cpp') | 236 cpp_template = self.jinja_env.get_template('dictionary_impl.cpp') |
218 template_context = v8_dictionary.dictionary_impl_context( | 237 template_context = v8_dictionary.dictionary_impl_context( |
219 dictionary, self.interfaces_info) | 238 dictionary, self.interfaces_info) |
| 239 include_paths = interface_info.get('dependencies_include_paths') |
220 header_text, cpp_text = render_template( | 240 header_text, cpp_text = render_template( |
221 interface_info, header_template, cpp_template, template_context) | 241 include_paths, header_template, cpp_template, template_context) |
222 header_path, cpp_path = self.output_paths( | 242 header_path, cpp_path = self.output_paths( |
223 definition_name, interface_info) | 243 definition_name, interface_info) |
224 return ( | 244 return ( |
225 (header_path, header_text), | 245 (header_path, header_text), |
226 (cpp_path, cpp_text), | 246 (cpp_path, cpp_text), |
227 ) | 247 ) |
228 | 248 |
229 | 249 |
230 def initialize_jinja_env(cache_dir): | 250 def initialize_jinja_env(cache_dir): |
231 jinja_env = jinja2.Environment( | 251 jinja_env = jinja2.Environment( |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
306 | 326 |
307 # Create a dummy file as output for the build system, | 327 # Create a dummy file as output for the build system, |
308 # since filenames of individual cache files are unpredictable and opaque | 328 # since filenames of individual cache files are unpredictable and opaque |
309 # (they are hashes of the template path, which varies based on environment) | 329 # (they are hashes of the template path, which varies based on environment) |
310 with open(dummy_filename, 'w') as dummy_file: | 330 with open(dummy_filename, 'w') as dummy_file: |
311 pass # |open| creates or touches the file | 331 pass # |open| creates or touches the file |
312 | 332 |
313 | 333 |
314 if __name__ == '__main__': | 334 if __name__ == '__main__': |
315 sys.exit(main(sys.argv)) | 335 sys.exit(main(sys.argv)) |
OLD | NEW |