Chromium Code Reviews| 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 if not is_valid_component_dependency(component, dependency): | |
| 96 continue | |
| 97 includes.add(include_path) | |
| 98 | |
| 92 template_context['cpp_includes'] = sorted(includes) | 99 template_context['cpp_includes'] = sorted(includes) |
| 93 | 100 |
| 94 header_text = header_template.render(template_context) | 101 header_text = header_template.render(template_context) |
| 95 cpp_text = cpp_template.render(template_context) | 102 cpp_text = cpp_template.render(template_context) |
| 96 return header_text, cpp_text | 103 return header_text, cpp_text |
| 97 | 104 |
| 98 | 105 |
| 99 class CodeGeneratorBase(object): | 106 class CodeGeneratorBase(object): |
| 100 """Base class for v8 bindings generator and IDL dictionary impl generator""" | 107 """Base class for v8 bindings generator and IDL dictionary impl generator""" |
| 101 | 108 |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 139 | 146 |
| 140 def generate_code_internal(self, definitions, definition_name): | 147 def generate_code_internal(self, definitions, definition_name): |
| 141 if definition_name in definitions.interfaces: | 148 if definition_name in definitions.interfaces: |
| 142 return self.generate_interface_code( | 149 return self.generate_interface_code( |
| 143 definitions, definition_name, | 150 definitions, definition_name, |
| 144 definitions.interfaces[definition_name]) | 151 definitions.interfaces[definition_name]) |
| 145 if definition_name in definitions.dictionaries: | 152 if definition_name in definitions.dictionaries: |
| 146 return self.generate_dictionary_code( | 153 return self.generate_dictionary_code( |
| 147 definitions, definition_name, | 154 definitions, definition_name, |
| 148 definitions.dictionaries[definition_name]) | 155 definitions.dictionaries[definition_name]) |
| 149 raise ValueError('%s is not in IDL definitions' % definition_name) | 156 |
| 157 for method in definitions.interfaces.values(): | |
|
bashi
2014/10/10 09:48:11
Could you explain why this change is needed?
tasak
2014/10/10 11:43:40
This is for avoiding exceptions when --generate-pa
| |
| 158 if not method.is_partial: | |
| 159 raise ValueError('%s is not in IDL definitions' % definition_nam e) | |
| 150 | 160 |
| 151 def generate_interface_code(self, definitions, interface_name, interface): | 161 def generate_interface_code(self, definitions, interface_name, interface): |
| 152 # Store other interfaces for introspection | 162 # Store other interfaces for introspection |
| 153 interfaces.update(definitions.interfaces) | 163 interfaces.update(definitions.interfaces) |
| 154 | 164 |
| 165 interface_info = self.interfaces_info[interface_name] | |
| 166 component = idl_filename_to_component( | |
| 167 interface_info.get('full_path')) | |
| 168 include_paths = interface_info.get('dependencies_include_paths') | |
| 169 | |
| 155 # Select appropriate Jinja template and contents function | 170 # Select appropriate Jinja template and contents function |
| 171 definition_name = interface_name | |
| 156 if interface.is_callback: | 172 if interface.is_callback: |
| 157 header_template_filename = 'callback_interface.h' | 173 header_template_filename = 'callback_interface.h' |
| 158 cpp_template_filename = 'callback_interface.cpp' | 174 cpp_template_filename = 'callback_interface.cpp' |
| 159 interface_context = v8_callback_interface.callback_interface_context | 175 interface_context = v8_callback_interface.callback_interface_context |
| 176 elif interface.is_partial: | |
| 177 interface_context = v8_interface.interface_context | |
| 178 header_template_filename = 'partial_interface.h' | |
| 179 cpp_template_filename = 'partial_interface.cpp' | |
| 180 definition_name = ''.join([interface_name, 'Partial']) | |
| 181 assert component == 'core' | |
| 182 component = 'modules' | |
| 183 include_paths = interface_info.get('partial_interface_dependencies_i nclude_paths') | |
| 160 else: | 184 else: |
| 161 header_template_filename = 'interface.h' | 185 header_template_filename = 'interface.h' |
| 162 cpp_template_filename = 'interface.cpp' | 186 cpp_template_filename = 'interface.cpp' |
| 163 interface_context = v8_interface.interface_context | 187 interface_context = v8_interface.interface_context |
| 164 header_template = self.jinja_env.get_template(header_template_filename) | 188 header_template = self.jinja_env.get_template(header_template_filename) |
| 165 cpp_template = self.jinja_env.get_template(cpp_template_filename) | 189 cpp_template = self.jinja_env.get_template(cpp_template_filename) |
| 166 | 190 |
| 167 interface_info = self.interfaces_info[interface_name] | |
| 168 | |
| 169 template_context = interface_context(interface) | 191 template_context = interface_context(interface) |
| 170 # Add the include for interface itself | 192 # Add the include for interface itself |
| 171 template_context['header_includes'].add(interface_info['include_path']) | 193 template_context['header_includes'].add(interface_info['include_path']) |
| 172 header_text, cpp_text = render_template( | 194 header_text, cpp_text = render_template( |
| 173 interface_info, header_template, cpp_template, template_context) | 195 include_paths, header_template, cpp_template, template_context, |
| 174 header_path, cpp_path = self.output_paths(interface_name) | 196 component) |
| 197 header_path, cpp_path = self.output_paths(definition_name) | |
| 175 return ( | 198 return ( |
| 176 (header_path, header_text), | 199 (header_path, header_text), |
| 177 (cpp_path, cpp_text), | 200 (cpp_path, cpp_text), |
| 178 ) | 201 ) |
| 179 | 202 |
| 180 def generate_dictionary_code(self, definitions, dictionary_name, | 203 def generate_dictionary_code(self, definitions, dictionary_name, |
| 181 dictionary): | 204 dictionary): |
| 182 header_template = self.jinja_env.get_template('dictionary_v8.h') | 205 header_template = self.jinja_env.get_template('dictionary_v8.h') |
| 183 cpp_template = self.jinja_env.get_template('dictionary_v8.cpp') | 206 cpp_template = self.jinja_env.get_template('dictionary_v8.cpp') |
| 184 template_context = v8_dictionary.dictionary_context(dictionary) | 207 template_context = v8_dictionary.dictionary_context(dictionary) |
| 185 interface_info = self.interfaces_info[dictionary_name] | 208 interface_info = self.interfaces_info[dictionary_name] |
| 209 include_paths = interface_info.get('dependencies_include_paths') | |
| 186 # Add the include for interface itself | 210 # Add the include for interface itself |
| 187 template_context['header_includes'].add(interface_info['include_path']) | 211 template_context['header_includes'].add(interface_info['include_path']) |
| 188 header_text, cpp_text = render_template( | 212 header_text, cpp_text = render_template( |
| 189 interface_info, header_template, cpp_template, template_context) | 213 include_paths, header_template, cpp_template, template_context) |
| 190 header_path, cpp_path = self.output_paths(dictionary_name) | 214 header_path, cpp_path = self.output_paths(dictionary_name) |
| 191 return ( | 215 return ( |
| 192 (header_path, header_text), | 216 (header_path, header_text), |
| 193 (cpp_path, cpp_text), | 217 (cpp_path, cpp_text), |
| 194 ) | 218 ) |
| 195 | 219 |
| 196 | 220 |
| 197 class CodeGeneratorDictionaryImpl(CodeGeneratorBase): | 221 class CodeGeneratorDictionaryImpl(CodeGeneratorBase): |
| 198 def __init__(self, interfaces_info, cache_dir, output_dir): | 222 def __init__(self, interfaces_info, cache_dir, output_dir): |
| 199 CodeGeneratorBase.__init__(self, interfaces_info, cache_dir, output_dir) | 223 CodeGeneratorBase.__init__(self, interfaces_info, cache_dir, output_dir) |
| 200 | 224 |
| 201 def output_paths(self, definition_name, interface_info): | 225 def output_paths(self, definition_name, interface_info): |
| 202 output_dir = posixpath.join(self.output_dir, | 226 output_dir = posixpath.join(self.output_dir, |
| 203 interface_info['relative_dir']) | 227 interface_info['relative_dir']) |
| 204 header_path = posixpath.join(output_dir, '%s.h' % definition_name) | 228 header_path = posixpath.join(output_dir, '%s.h' % definition_name) |
| 205 cpp_path = posixpath.join(output_dir, '%s.cpp' % definition_name) | 229 cpp_path = posixpath.join(output_dir, '%s.cpp' % definition_name) |
| 206 return header_path, cpp_path | 230 return header_path, cpp_path |
| 207 | 231 |
| 208 def generate_code_internal(self, definitions, definition_name): | 232 def generate_code_internal(self, definitions, definition_name): |
| 209 if not definition_name in definitions.dictionaries: | 233 if not definition_name in definitions.dictionaries: |
| 210 raise ValueError('%s is not an IDL dictionary') | 234 raise ValueError('%s is not an IDL dictionary') |
| 211 dictionary = definitions.dictionaries[definition_name] | 235 dictionary = definitions.dictionaries[definition_name] |
| 212 interface_info = self.interfaces_info[definition_name] | 236 interface_info = self.interfaces_info[definition_name] |
| 213 header_template = self.jinja_env.get_template('dictionary_impl.h') | 237 header_template = self.jinja_env.get_template('dictionary_impl.h') |
| 214 cpp_template = self.jinja_env.get_template('dictionary_impl.cpp') | 238 cpp_template = self.jinja_env.get_template('dictionary_impl.cpp') |
| 215 template_context = v8_dictionary.dictionary_impl_context( | 239 template_context = v8_dictionary.dictionary_impl_context( |
| 216 dictionary, self.interfaces_info) | 240 dictionary, self.interfaces_info) |
| 241 include_paths = interface_info.get('dependencies_include_paths') | |
| 217 header_text, cpp_text = render_template( | 242 header_text, cpp_text = render_template( |
| 218 interface_info, header_template, cpp_template, template_context) | 243 include_paths, header_template, cpp_template, template_context) |
| 219 header_path, cpp_path = self.output_paths( | 244 header_path, cpp_path = self.output_paths( |
| 220 definition_name, interface_info) | 245 definition_name, interface_info) |
| 221 return ( | 246 return ( |
| 222 (header_path, header_text), | 247 (header_path, header_text), |
| 223 (cpp_path, cpp_text), | 248 (cpp_path, cpp_text), |
| 224 ) | 249 ) |
| 225 | 250 |
| 226 | 251 |
| 227 def initialize_jinja_env(cache_dir): | 252 def initialize_jinja_env(cache_dir): |
| 228 jinja_env = jinja2.Environment( | 253 jinja_env = jinja2.Environment( |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 303 | 328 |
| 304 # Create a dummy file as output for the build system, | 329 # Create a dummy file as output for the build system, |
| 305 # since filenames of individual cache files are unpredictable and opaque | 330 # since filenames of individual cache files are unpredictable and opaque |
| 306 # (they are hashes of the template path, which varies based on environment) | 331 # (they are hashes of the template path, which varies based on environment) |
| 307 with open(dummy_filename, 'w') as dummy_file: | 332 with open(dummy_filename, 'w') as dummy_file: |
| 308 pass # |open| creates or touches the file | 333 pass # |open| creates or touches the file |
| 309 | 334 |
| 310 | 335 |
| 311 if __name__ == '__main__': | 336 if __name__ == '__main__': |
| 312 sys.exit(main(sys.argv)) | 337 sys.exit(main(sys.argv)) |
| OLD | NEW |