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 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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 | 81 |
| 82 | 82 |
| 83 # Code generation mode. | |
| 84 MODE_BINDINGS = 1 << 0 | |
| 85 MODE_DICTIONARY_IMPL = 1 << 1 | |
| 86 MODE_ALL = MODE_BINDINGS | MODE_DICTIONARY_IMPL | |
|
haraken
2014/08/01 02:37:44
Shall we simply use a set of strings like set("bin
bashi
2014/08/01 02:49:47
I'm going to remove this flags.
| |
| 87 | |
| 88 | |
| 83 def render_template(interface_info, header_template, cpp_template, | 89 def render_template(interface_info, header_template, cpp_template, |
| 84 template_context): | 90 template_context): |
| 85 template_context['code_generator'] = module_pyname | 91 template_context['code_generator'] = module_pyname |
| 86 | 92 |
| 87 # Add includes for any dependencies | 93 # Add includes for any dependencies |
| 88 template_context['header_includes'] = sorted( | 94 template_context['header_includes'] = sorted( |
| 89 template_context['header_includes']) | 95 template_context['header_includes']) |
| 90 includes.update(interface_info.get('dependencies_include_paths', [])) | 96 includes.update(interface_info.get('dependencies_include_paths', [])) |
| 91 template_context['cpp_includes'] = sorted(includes) | 97 template_context['cpp_includes'] = sorted(includes) |
| 92 | 98 |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 130 v8_types.set_component_dirs(dict( | 136 v8_types.set_component_dirs(dict( |
| 131 (interface_name, interface_info['component_dir']) | 137 (interface_name, interface_info['component_dir']) |
| 132 for interface_name, interface_info in interfaces_info.iteritems())) | 138 for interface_name, interface_info in interfaces_info.iteritems())) |
| 133 | 139 |
| 134 def output_paths_for_bindings(self, definition_name): | 140 def output_paths_for_bindings(self, definition_name): |
| 135 header_path = posixpath.join(self.output_dir, | 141 header_path = posixpath.join(self.output_dir, |
| 136 'V8%s.h' % definition_name) | 142 'V8%s.h' % definition_name) |
| 137 cpp_path = posixpath.join(self.output_dir, 'V8%s.cpp' % definition_name) | 143 cpp_path = posixpath.join(self.output_dir, 'V8%s.cpp' % definition_name) |
| 138 return header_path, cpp_path | 144 return header_path, cpp_path |
| 139 | 145 |
| 140 def output_paths_for_impl(self, definition_name): | 146 def output_paths_for_impl(self, definition_name, relative_dir): |
| 141 header_path = posixpath.join(self.output_dir, '%s.h' % definition_name) | 147 output_dir = posixpath.join(self.output_dir, relative_dir) |
| 142 cpp_path = posixpath.join(self.output_dir, '%s.cpp' % definition_name) | 148 header_path = posixpath.join(output_dir, '%s.h' % definition_name) |
| 149 cpp_path = posixpath.join(output_dir, '%s.cpp' % definition_name) | |
| 143 return header_path, cpp_path | 150 return header_path, cpp_path |
| 144 | 151 |
| 145 def generate_code(self, definitions, definition_name): | 152 def generate_code(self, definitions, definition_name, |
| 153 mode=MODE_ALL): | |
| 146 """Returns .h/.cpp code as (header_text, cpp_text).""" | 154 """Returns .h/.cpp code as (header_text, cpp_text).""" |
| 147 # Set local type info | 155 # Set local type info |
| 148 IdlType.set_callback_functions(definitions.callback_functions.keys()) | 156 IdlType.set_callback_functions(definitions.callback_functions.keys()) |
| 149 IdlType.set_enums((enum.name, enum.values) | 157 IdlType.set_enums((enum.name, enum.values) |
| 150 for enum in definitions.enumerations.values()) | 158 for enum in definitions.enumerations.values()) |
| 151 | 159 |
| 152 if definition_name in definitions.interfaces: | 160 if definition_name in definitions.interfaces: |
| 153 return self.generate_interface_code( | 161 return self.generate_interface_code( |
| 154 definitions, definition_name, | 162 definitions, definition_name, |
| 155 definitions.interfaces[definition_name]) | 163 definitions.interfaces[definition_name]) |
| 156 if definition_name in definitions.dictionaries: | 164 if definition_name in definitions.dictionaries: |
| 157 return self.generate_dictionary_code( | 165 return self.generate_dictionary_code( |
| 158 definitions, definition_name, | 166 definitions, definition_name, |
| 159 definitions.dictionaries[definition_name]) | 167 definitions.dictionaries[definition_name], mode) |
| 160 raise ValueError('%s is not in IDL definitions' % definition_name) | 168 raise ValueError('%s is not in IDL definitions' % definition_name) |
| 161 | 169 |
| 162 def generate_interface_code(self, definitions, interface_name, interface): | 170 def generate_interface_code(self, definitions, interface_name, interface): |
| 163 # Store other interfaces for introspection | 171 # Store other interfaces for introspection |
| 164 interfaces.update(definitions.interfaces) | 172 interfaces.update(definitions.interfaces) |
| 165 | 173 |
| 166 # Select appropriate Jinja template and contents function | 174 # Select appropriate Jinja template and contents function |
| 167 if interface.is_callback: | 175 if interface.is_callback: |
| 168 header_template_filename = 'callback_interface.h' | 176 header_template_filename = 'callback_interface.h' |
| 169 cpp_template_filename = 'callback_interface.cpp' | 177 cpp_template_filename = 'callback_interface.cpp' |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 182 template_context['header_includes'].add(interface_info['include_path']) | 190 template_context['header_includes'].add(interface_info['include_path']) |
| 183 header_text, cpp_text = render_template( | 191 header_text, cpp_text = render_template( |
| 184 interface_info, header_template, cpp_template, template_context) | 192 interface_info, header_template, cpp_template, template_context) |
| 185 header_path, cpp_path = self.output_paths_for_bindings(interface_name) | 193 header_path, cpp_path = self.output_paths_for_bindings(interface_name) |
| 186 return ( | 194 return ( |
| 187 (header_path, header_text), | 195 (header_path, header_text), |
| 188 (cpp_path, cpp_text), | 196 (cpp_path, cpp_text), |
| 189 ) | 197 ) |
| 190 | 198 |
| 191 def generate_dictionary_code(self, definitions, dictionary_name, | 199 def generate_dictionary_code(self, definitions, dictionary_name, |
| 192 dictionary): | 200 dictionary, mode): |
| 193 interface_info = self.interfaces_info[dictionary_name] | 201 interface_info = self.interfaces_info[dictionary_name] |
| 194 bindings_results = self.generate_dictionary_bindings( | 202 results = set() |
| 195 dictionary_name, interface_info, dictionary) | 203 if (mode & MODE_BINDINGS) == MODE_BINDINGS: |
| 196 impl_results = self.generate_dictionary_impl( | 204 results.update(self.generate_dictionary_bindings( |
| 197 dictionary_name, interface_info, dictionary) | 205 dictionary_name, interface_info, dictionary)) |
| 198 return bindings_results + impl_results | 206 if (mode & MODE_DICTIONARY_IMPL) == MODE_DICTIONARY_IMPL: |
| 207 results.update(self.generate_dictionary_impl( | |
| 208 dictionary_name, interface_info, dictionary)) | |
| 209 return results | |
| 199 | 210 |
| 200 def generate_dictionary_bindings(self, dictionary_name, | 211 def generate_dictionary_bindings(self, dictionary_name, |
| 201 interface_info, dictionary): | 212 interface_info, dictionary): |
| 202 header_template = self.jinja_env.get_template('dictionary_v8.h') | 213 header_template = self.jinja_env.get_template('dictionary_v8.h') |
| 203 cpp_template = self.jinja_env.get_template('dictionary_v8.cpp') | 214 cpp_template = self.jinja_env.get_template('dictionary_v8.cpp') |
| 204 template_context = v8_dictionary.dictionary_context(dictionary) | 215 template_context = v8_dictionary.dictionary_context(dictionary) |
| 205 # Add the include for interface itself | 216 # Add the include for interface itself |
| 206 template_context['header_includes'].add(interface_info['include_path']) | 217 template_context['header_includes'].add(interface_info['include_path']) |
| 207 header_text, cpp_text = render_template( | 218 header_text, cpp_text = render_template( |
| 208 interface_info, header_template, cpp_template, template_context) | 219 interface_info, header_template, cpp_template, template_context) |
| 209 header_path, cpp_path = self.output_paths_for_bindings(dictionary_name) | 220 header_path, cpp_path = self.output_paths_for_bindings(dictionary_name) |
| 210 return ( | 221 return ( |
| 211 (header_path, header_text), | 222 (header_path, header_text), |
| 212 (cpp_path, cpp_text), | 223 (cpp_path, cpp_text), |
| 213 ) | 224 ) |
| 214 | 225 |
| 215 def generate_dictionary_impl(self, dictionary_name, | 226 def generate_dictionary_impl(self, dictionary_name, |
| 216 interface_info, dictionary): | 227 interface_info, dictionary): |
| 217 header_template = self.jinja_env.get_template('dictionary_impl.h') | 228 header_template = self.jinja_env.get_template('dictionary_impl.h') |
| 218 cpp_template = self.jinja_env.get_template('dictionary_impl.cpp') | 229 cpp_template = self.jinja_env.get_template('dictionary_impl.cpp') |
| 219 template_context = v8_dictionary.dictionary_impl_context( | 230 template_context = v8_dictionary.dictionary_impl_context( |
| 220 dictionary, self.interfaces_info) | 231 dictionary, self.interfaces_info) |
| 221 header_text, cpp_text = render_template( | 232 header_text, cpp_text = render_template( |
| 222 interface_info, header_template, cpp_template, template_context) | 233 interface_info, header_template, cpp_template, template_context) |
| 223 header_path, cpp_path = self.output_paths_for_impl(dictionary_name) | 234 header_path, cpp_path = self.output_paths_for_impl( |
| 235 dictionary_name, interface_info['relative_dir']) | |
| 224 return ( | 236 return ( |
| 225 (header_path, header_text), | 237 (header_path, header_text), |
| 226 (cpp_path, cpp_text), | 238 (cpp_path, cpp_text), |
| 227 ) | 239 ) |
| 228 | 240 |
| 229 | 241 |
| 230 def initialize_jinja_env(cache_dir): | 242 def initialize_jinja_env(cache_dir): |
| 231 jinja_env = jinja2.Environment( | 243 jinja_env = jinja2.Environment( |
| 232 loader=jinja2.FileSystemLoader(templates_dir), | 244 loader=jinja2.FileSystemLoader(templates_dir), |
| 233 # Bytecode cache is not concurrency-safe unless pre-cached: | 245 # Bytecode cache is not concurrency-safe unless pre-cached: |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 286 | 298 |
| 287 # Create a dummy file as output for the build system, | 299 # Create a dummy file as output for the build system, |
| 288 # since filenames of individual cache files are unpredictable and opaque | 300 # since filenames of individual cache files are unpredictable and opaque |
| 289 # (they are hashes of the template path, which varies based on environment) | 301 # (they are hashes of the template path, which varies based on environment) |
| 290 with open(dummy_filename, 'w') as dummy_file: | 302 with open(dummy_filename, 'w') as dummy_file: |
| 291 pass # |open| creates or touches the file | 303 pass # |open| creates or touches the file |
| 292 | 304 |
| 293 | 305 |
| 294 if __name__ == '__main__': | 306 if __name__ == '__main__': |
| 295 sys.exit(main(sys.argv)) | 307 sys.exit(main(sys.argv)) |
| OLD | NEW |