OLD | NEW |
1 # Copyright 2016 The Chromium Authors. All rights reserved. | 1 # Copyright 2016 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 # pylint: disable=import-error,print-statement,relative-import | 5 # pylint: disable=import-error,print-statement,relative-import |
6 | 6 |
7 """Plumbing for a Jinja-based code generator, including CodeGeneratorBase, a bas
e class for all generators.""" | 7 """Plumbing for a Jinja-based code generator, including CodeGeneratorBase, a bas
e class for all generators.""" |
8 | 8 |
9 import os | 9 import os |
10 import posixpath | 10 import posixpath |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
114 | 114 |
115 | 115 |
116 class CodeGeneratorBase(object): | 116 class CodeGeneratorBase(object): |
117 """Base class for jinja-powered jinja template generation. | 117 """Base class for jinja-powered jinja template generation. |
118 """ | 118 """ |
119 def __init__(self, generator_name, info_provider, cache_dir, output_dir): | 119 def __init__(self, generator_name, info_provider, cache_dir, output_dir): |
120 self.generator_name = generator_name | 120 self.generator_name = generator_name |
121 self.info_provider = info_provider | 121 self.info_provider = info_provider |
122 self.jinja_env = initialize_jinja_env(cache_dir) | 122 self.jinja_env = initialize_jinja_env(cache_dir) |
123 self.output_dir = output_dir | 123 self.output_dir = output_dir |
124 self.set_global_type_info() | |
125 | |
126 def should_generate_code(self, definitions): | |
127 return definitions.interfaces or definitions.dictionaries | |
128 | |
129 def set_global_type_info(self): | |
130 interfaces_info = self.info_provider.interfaces_info | |
131 set_ancestors(interfaces_info['ancestors']) | |
132 IdlType.set_callback_interfaces(interfaces_info['callback_interfaces']) | |
133 IdlType.set_dictionaries(interfaces_info['dictionaries']) | |
134 IdlType.set_enums(self.info_provider.enumerations) | |
135 IdlType.set_callback_functions(self.info_provider.callback_functions) | |
136 IdlType.set_implemented_as_interfaces(interfaces_info['implemented_as_in
terfaces']) | |
137 IdlType.set_garbage_collected_types(interfaces_info['garbage_collected_i
nterfaces']) | |
138 set_component_dirs(interfaces_info['component_dirs']) | |
139 | 124 |
140 def render_template(self, include_paths, header_template, cpp_template, | 125 def render_template(self, include_paths, header_template, cpp_template, |
141 template_context, component=None): | 126 template_context, component=None): |
142 template_context['code_generator'] = self.generator_name | 127 template_context['code_generator'] = self.generator_name |
143 | 128 |
144 # Add includes for any dependencies | 129 # Add includes for any dependencies |
145 template_context['header_includes'] = normalize_and_sort_includes( | 130 template_context['header_includes'] = normalize_and_sort_includes( |
146 template_context['header_includes']) | 131 template_context['header_includes']) |
147 | 132 |
148 for include_path in include_paths: | 133 for include_path in include_paths: |
149 if component: | 134 if component: |
150 dependency = idl_filename_to_component(include_path) | 135 dependency = idl_filename_to_component(include_path) |
151 assert is_valid_component_dependency(component, dependency) | 136 assert is_valid_component_dependency(component, dependency) |
152 includes.add(include_path) | 137 includes.add(include_path) |
153 | 138 |
154 template_context['cpp_includes'] = normalize_and_sort_includes(includes) | 139 template_context['cpp_includes'] = normalize_and_sort_includes(includes) |
155 | 140 |
156 header_text = render_template(header_template, template_context) | 141 header_text = render_template(header_template, template_context) |
157 cpp_text = render_template(cpp_template, template_context) | 142 cpp_text = render_template(cpp_template, template_context) |
158 return header_text, cpp_text | 143 return header_text, cpp_text |
159 | 144 |
| 145 |
| 146 class IDLCodeGeneratorBase(CodeGeneratorBase): |
| 147 """Base class for code generators which create bindings implementations from |
| 148 IDL files. |
| 149 """ |
| 150 def __init__(self, generator_name, info_provider, cache_dir, output_dir): |
| 151 super(IDLCodeGeneratorBase, self).__init__(generator_name, info_provider
, cache_dir, output_dir) |
| 152 self.set_global_type_info() |
| 153 |
| 154 def should_generate_code(self, definitions): |
| 155 return definitions.interfaces or definitions.dictionaries |
| 156 |
| 157 def set_global_type_info(self): |
| 158 interfaces_info = self.info_provider.interfaces_info |
| 159 set_ancestors(interfaces_info['ancestors']) |
| 160 IdlType.set_callback_interfaces(interfaces_info['callback_interfaces']) |
| 161 IdlType.set_dictionaries(interfaces_info['dictionaries']) |
| 162 IdlType.set_enums(self.info_provider.enumerations) |
| 163 IdlType.set_callback_functions(self.info_provider.callback_functions) |
| 164 IdlType.set_implemented_as_interfaces(interfaces_info['implemented_as_in
terfaces']) |
| 165 IdlType.set_garbage_collected_types(interfaces_info['garbage_collected_i
nterfaces']) |
| 166 set_component_dirs(interfaces_info['component_dirs']) |
| 167 |
160 def generate_code(self, definitions, definition_name): | 168 def generate_code(self, definitions, definition_name): |
161 """Invokes code generation. The [definitions] argument is a list of defi
nitions, | 169 """Invokes code generation. The [definitions] argument is a list of defi
nitions, |
162 and the [definition_name] is the name of the definition | 170 and the [definition_name] is the name of the definition |
163 """ | 171 """ |
164 # This should be implemented in subclasses. | 172 # This should be implemented in subclasses. |
165 raise NotImplementedError() | 173 raise NotImplementedError() |
166 | 174 |
167 | 175 |
168 def main(argv): | 176 def main(argv): |
169 # If file itself executed, cache templates | 177 # If file itself executed, cache templates |
(...skipping 14 matching lines...) Expand all Loading... |
184 | 192 |
185 # Create a dummy file as output for the build system, | 193 # Create a dummy file as output for the build system, |
186 # since filenames of individual cache files are unpredictable and opaque | 194 # since filenames of individual cache files are unpredictable and opaque |
187 # (they are hashes of the template path, which varies based on environment) | 195 # (they are hashes of the template path, which varies based on environment) |
188 with open(dummy_filename, 'w') as dummy_file: | 196 with open(dummy_filename, 'w') as dummy_file: |
189 pass # |open| creates or touches the file | 197 pass # |open| creates or touches the file |
190 | 198 |
191 | 199 |
192 if __name__ == '__main__': | 200 if __name__ == '__main__': |
193 sys.exit(main(sys.argv)) | 201 sys.exit(main(sys.argv)) |
OLD | NEW |