| 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 template_context['header_includes'].add(interface_info['include_path']) | 188 template_context['header_includes'].add(interface_info['include_path']) |
| 172 header_text, cpp_text = render_template( | 189 header_text, cpp_text = render_template( |
| 173 interface_info, header_template, cpp_template, template_context) | 190 include_paths, header_template, cpp_template, template_context, |
| 191 component) |
| 174 header_path, cpp_path = self.output_paths(interface_name) | 192 header_path, cpp_path = self.output_paths(interface_name) |
| 175 return ( | 193 return ( |
| 176 (header_path, header_text), | 194 (header_path, header_text), |
| 177 (cpp_path, cpp_text), | 195 (cpp_path, cpp_text), |
| 178 ) | 196 ) |
| 179 | 197 |
| 180 def generate_dictionary_code(self, definitions, dictionary_name, | 198 def generate_dictionary_code(self, definitions, dictionary_name, |
| 181 dictionary): | 199 dictionary): |
| 182 header_template = self.jinja_env.get_template('dictionary_v8.h') | 200 header_template = self.jinja_env.get_template('dictionary_v8.h') |
| 183 cpp_template = self.jinja_env.get_template('dictionary_v8.cpp') | 201 cpp_template = self.jinja_env.get_template('dictionary_v8.cpp') |
| 184 template_context = v8_dictionary.dictionary_context(dictionary) | 202 template_context = v8_dictionary.dictionary_context(dictionary) |
| 185 interface_info = self.interfaces_info[dictionary_name] | 203 interface_info = self.interfaces_info[dictionary_name] |
| 204 include_paths = interface_info.get('dependencies_include_paths') |
| 186 # Add the include for interface itself | 205 # Add the include for interface itself |
| 187 template_context['header_includes'].add(interface_info['include_path']) | 206 template_context['header_includes'].add(interface_info['include_path']) |
| 188 header_text, cpp_text = render_template( | 207 header_text, cpp_text = render_template( |
| 189 interface_info, header_template, cpp_template, template_context) | 208 include_paths, header_template, cpp_template, template_context) |
| 190 header_path, cpp_path = self.output_paths(dictionary_name) | 209 header_path, cpp_path = self.output_paths(dictionary_name) |
| 191 return ( | 210 return ( |
| 192 (header_path, header_text), | 211 (header_path, header_text), |
| 193 (cpp_path, cpp_text), | 212 (cpp_path, cpp_text), |
| 194 ) | 213 ) |
| 195 | 214 |
| 196 | 215 |
| 197 class CodeGeneratorDictionaryImpl(CodeGeneratorBase): | 216 class CodeGeneratorDictionaryImpl(CodeGeneratorBase): |
| 198 def __init__(self, interfaces_info, cache_dir, output_dir): | 217 def __init__(self, interfaces_info, cache_dir, output_dir): |
| 199 CodeGeneratorBase.__init__(self, interfaces_info, cache_dir, output_dir) | 218 CodeGeneratorBase.__init__(self, interfaces_info, cache_dir, output_dir) |
| 200 | 219 |
| 201 def output_paths(self, definition_name, interface_info): | 220 def output_paths(self, definition_name, interface_info): |
| 202 output_dir = posixpath.join(self.output_dir, | 221 output_dir = posixpath.join(self.output_dir, |
| 203 interface_info['relative_dir']) | 222 interface_info['relative_dir']) |
| 204 header_path = posixpath.join(output_dir, '%s.h' % definition_name) | 223 header_path = posixpath.join(output_dir, '%s.h' % definition_name) |
| 205 cpp_path = posixpath.join(output_dir, '%s.cpp' % definition_name) | 224 cpp_path = posixpath.join(output_dir, '%s.cpp' % definition_name) |
| 206 return header_path, cpp_path | 225 return header_path, cpp_path |
| 207 | 226 |
| 208 def generate_code_internal(self, definitions, definition_name): | 227 def generate_code_internal(self, definitions, definition_name): |
| 209 if not definition_name in definitions.dictionaries: | 228 if not definition_name in definitions.dictionaries: |
| 210 raise ValueError('%s is not an IDL dictionary') | 229 raise ValueError('%s is not an IDL dictionary') |
| 211 dictionary = definitions.dictionaries[definition_name] | 230 dictionary = definitions.dictionaries[definition_name] |
| 212 interface_info = self.interfaces_info[definition_name] | 231 interface_info = self.interfaces_info[definition_name] |
| 213 header_template = self.jinja_env.get_template('dictionary_impl.h') | 232 header_template = self.jinja_env.get_template('dictionary_impl.h') |
| 214 cpp_template = self.jinja_env.get_template('dictionary_impl.cpp') | 233 cpp_template = self.jinja_env.get_template('dictionary_impl.cpp') |
| 215 template_context = v8_dictionary.dictionary_impl_context( | 234 template_context = v8_dictionary.dictionary_impl_context( |
| 216 dictionary, self.interfaces_info) | 235 dictionary, self.interfaces_info) |
| 236 include_paths = interface_info.get('dependencies_include_paths') |
| 217 header_text, cpp_text = render_template( | 237 header_text, cpp_text = render_template( |
| 218 interface_info, header_template, cpp_template, template_context) | 238 include_paths, header_template, cpp_template, template_context) |
| 219 header_path, cpp_path = self.output_paths( | 239 header_path, cpp_path = self.output_paths( |
| 220 definition_name, interface_info) | 240 definition_name, interface_info) |
| 221 return ( | 241 return ( |
| 222 (header_path, header_text), | 242 (header_path, header_text), |
| 223 (cpp_path, cpp_text), | 243 (cpp_path, cpp_text), |
| 224 ) | 244 ) |
| 225 | 245 |
| 226 | 246 |
| 227 def initialize_jinja_env(cache_dir): | 247 def initialize_jinja_env(cache_dir): |
| 228 jinja_env = jinja2.Environment( | 248 jinja_env = jinja2.Environment( |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 303 | 323 |
| 304 # Create a dummy file as output for the build system, | 324 # Create a dummy file as output for the build system, |
| 305 # since filenames of individual cache files are unpredictable and opaque | 325 # since filenames of individual cache files are unpredictable and opaque |
| 306 # (they are hashes of the template path, which varies based on environment) | 326 # (they are hashes of the template path, which varies based on environment) |
| 307 with open(dummy_filename, 'w') as dummy_file: | 327 with open(dummy_filename, 'w') as dummy_file: |
| 308 pass # |open| creates or touches the file | 328 pass # |open| creates or touches the file |
| 309 | 329 |
| 310 | 330 |
| 311 if __name__ == '__main__': | 331 if __name__ == '__main__': |
| 312 sys.exit(main(sys.argv)) | 332 sys.exit(main(sys.argv)) |
| OLD | NEW |