| 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 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 96 def normalize_and_sort_includes(include_paths): | 96 def normalize_and_sort_includes(include_paths): |
| 97 normalized_include_paths = [] | 97 normalized_include_paths = [] |
| 98 for include_path in include_paths: | 98 for include_path in include_paths: |
| 99 match = re.search(r'/gen/blink/(.*)$', posixpath.abspath(include_path)) | 99 match = re.search(r'/gen/blink/(.*)$', posixpath.abspath(include_path)) |
| 100 if match: | 100 if match: |
| 101 include_path = match.group(1) | 101 include_path = match.group(1) |
| 102 normalized_include_paths.append(include_path) | 102 normalized_include_paths.append(include_path) |
| 103 return sorted(normalized_include_paths) | 103 return sorted(normalized_include_paths) |
| 104 | 104 |
| 105 | 105 |
| 106 def render_template(template, context): |
| 107 filename = str(template.filename) |
| 108 filename = filename[filename.rfind("third_party"):] |
| 109 context["jinja_template_filename"] = filename |
| 110 return template.render(context) |
| 111 |
| 112 |
| 106 class CodeGeneratorBase(object): | 113 class CodeGeneratorBase(object): |
| 107 """Base class for jinja-powered jinja template generation. | 114 """Base class for jinja-powered jinja template generation. |
| 108 """ | 115 """ |
| 109 def __init__(self, generator_name, info_provider, cache_dir, output_dir): | 116 def __init__(self, generator_name, info_provider, cache_dir, output_dir): |
| 110 self.generator_name = generator_name | 117 self.generator_name = generator_name |
| 111 self.info_provider = info_provider | 118 self.info_provider = info_provider |
| 112 self.jinja_env = initialize_jinja_env(cache_dir) | 119 self.jinja_env = initialize_jinja_env(cache_dir) |
| 113 self.output_dir = output_dir | 120 self.output_dir = output_dir |
| 114 self.set_global_type_info() | 121 self.set_global_type_info() |
| 115 | 122 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 136 template_context['header_includes']) | 143 template_context['header_includes']) |
| 137 | 144 |
| 138 for include_path in include_paths: | 145 for include_path in include_paths: |
| 139 if component: | 146 if component: |
| 140 dependency = idl_filename_to_component(include_path) | 147 dependency = idl_filename_to_component(include_path) |
| 141 assert is_valid_component_dependency(component, dependency) | 148 assert is_valid_component_dependency(component, dependency) |
| 142 includes.add(include_path) | 149 includes.add(include_path) |
| 143 | 150 |
| 144 template_context['cpp_includes'] = normalize_and_sort_includes(includes) | 151 template_context['cpp_includes'] = normalize_and_sort_includes(includes) |
| 145 | 152 |
| 146 header_text = header_template.render(template_context) | 153 header_text = render_template(header_template, template_context) |
| 147 cpp_text = cpp_template.render(template_context) | 154 cpp_text = render_template(cpp_template, template_context) |
| 148 return header_text, cpp_text | 155 return header_text, cpp_text |
| 149 | 156 |
| 150 def generate_code(self, definitions, definition_name): | 157 def generate_code(self, definitions, definition_name): |
| 151 """Invokes code generation. The [definitions] argument is a list of defi
nitions, | 158 """Invokes code generation. The [definitions] argument is a list of defi
nitions, |
| 152 and the [definition_name] is the name of the definition | 159 and the [definition_name] is the name of the definition |
| 153 """ | 160 """ |
| 154 # This should be implemented in subclasses. | 161 # This should be implemented in subclasses. |
| 155 raise NotImplementedError() | 162 raise NotImplementedError() |
| 156 | 163 |
| 157 | 164 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 174 | 181 |
| 175 # Create a dummy file as output for the build system, | 182 # Create a dummy file as output for the build system, |
| 176 # since filenames of individual cache files are unpredictable and opaque | 183 # since filenames of individual cache files are unpredictable and opaque |
| 177 # (they are hashes of the template path, which varies based on environment) | 184 # (they are hashes of the template path, which varies based on environment) |
| 178 with open(dummy_filename, 'w') as dummy_file: | 185 with open(dummy_filename, 'w') as dummy_file: |
| 179 pass # |open| creates or touches the file | 186 pass # |open| creates or touches the file |
| 180 | 187 |
| 181 | 188 |
| 182 if __name__ == '__main__': | 189 if __name__ == '__main__': |
| 183 sys.exit(main(sys.argv)) | 190 sys.exit(main(sys.argv)) |
| OLD | NEW |