| OLD | NEW |
| 1 #!/usr/bin/python | 1 #!/usr/bin/python |
| 2 # Copyright (C) 2013 Google Inc. All rights reserved. | 2 # Copyright (C) 2013 Google Inc. All rights reserved. |
| 3 # | 3 # |
| 4 # Redistribution and use in source and binary forms, with or without | 4 # Redistribution and use in source and binary forms, with or without |
| 5 # modification, are permitted provided that the following conditions are | 5 # modification, are permitted provided that the following conditions are |
| 6 # met: | 6 # met: |
| 7 # | 7 # |
| 8 # * Redistributions of source code must retain the above copyright | 8 # * Redistributions of source code must retain the above copyright |
| 9 # notice, this list of conditions and the following disclaimer. | 9 # notice, this list of conditions and the following disclaimer. |
| 10 # * Redistributions in binary form must reproduce the above | 10 # * Redistributions in binary form must reproduce the above |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 78 return options, idl_filename | 78 return options, idl_filename |
| 79 | 79 |
| 80 | 80 |
| 81 def idl_filename_to_interface_name(idl_filename): | 81 def idl_filename_to_interface_name(idl_filename): |
| 82 basename = os.path.basename(idl_filename) | 82 basename = os.path.basename(idl_filename) |
| 83 interface_name, _ = os.path.splitext(basename) | 83 interface_name, _ = os.path.splitext(basename) |
| 84 return interface_name | 84 return interface_name |
| 85 | 85 |
| 86 | 86 |
| 87 class IdlCompiler(object): | 87 class IdlCompiler(object): |
| 88 """Abstract Base Class for IDL compilers. | 88 """The IDL Compiler. |
| 89 | 89 |
| 90 In concrete classes: | |
| 91 * self.code_generator must be set, implementing generate_code() | |
| 92 (returning a list of output code), and | |
| 93 * compile_file() must be implemented (handling output filenames). | |
| 94 """ | 90 """ |
| 95 __metaclass__ = abc.ABCMeta | 91 __metaclass__ = abc.ABCMeta |
| 96 | 92 |
| 97 def __init__(self, output_directory, cache_directory=None, | 93 def __init__(self, output_directory, cache_directory=None, |
| 98 code_generator=None, info_provider=None, | 94 code_generator_class=None, info_provider=None, |
| 99 target_component=None): | 95 target_component=None): |
| 100 """ | 96 """ |
| 101 Args: | 97 Args: |
| 102 output_directory: directory to put output files. | 98 output_directory: directory to put output files. |
| 103 cache_directory: directory which contains PLY caches. | 99 cache_directory: directory which contains PLY caches. |
| 104 code_generator: code generator to be used. | 100 code_generator_class: code generator class to be used. |
| 105 info_provider: component-specific information provider. | 101 info_provider: component-specific information provider. |
| 106 target_component: component to be processed. | 102 target_component: component to be processed. |
| 107 """ | 103 """ |
| 108 self.cache_directory = cache_directory | 104 self.cache_directory = cache_directory |
| 109 self.code_generator = code_generator | |
| 110 self.info_provider = info_provider | 105 self.info_provider = info_provider |
| 111 self.output_directory = output_directory | 106 self.output_directory = output_directory |
| 112 self.target_component = target_component | 107 self.target_component = target_component |
| 113 self.reader = IdlReader(info_provider.interfaces_info, cache_directory) | 108 self.reader = IdlReader(info_provider.interfaces_info, cache_directory) |
| 109 self.code_generator = code_generator_class(self.info_provider, |
| 110 self.cache_directory, |
| 111 self.output_directory) |
| 114 | 112 |
| 115 def compile_and_write(self, idl_filename): | 113 def compile_and_write(self, idl_filename): |
| 116 interface_name = idl_filename_to_interface_name(idl_filename) | 114 interface_name = idl_filename_to_interface_name(idl_filename) |
| 117 definitions = self.reader.read_idl_definitions(idl_filename) | 115 definitions = self.reader.read_idl_definitions(idl_filename) |
| 118 target_definitions = definitions[self.target_component] | 116 target_definitions = definitions[self.target_component] |
| 119 output_code_list = self.code_generator.generate_code( | 117 output_code_list = self.code_generator.generate_code( |
| 120 target_definitions, interface_name) | 118 target_definitions, interface_name) |
| 121 for output_path, output_code in output_code_list: | 119 for output_path, output_code in output_code_list: |
| 122 write_file(output_code, output_path) | 120 write_file(output_code, output_path) |
| 123 | 121 |
| 124 @abc.abstractmethod | |
| 125 def compile_file(self, idl_filename): | |
| 126 pass | |
| 127 | |
| 128 | |
| 129 class IdlCompilerV8(IdlCompiler): | |
| 130 def __init__(self, *args, **kwargs): | |
| 131 IdlCompiler.__init__(self, *args, **kwargs) | |
| 132 self.code_generator = CodeGeneratorV8(self.info_provider, | |
| 133 self.cache_directory, | |
| 134 self.output_directory) | |
| 135 | |
| 136 def compile_file(self, idl_filename): | 122 def compile_file(self, idl_filename): |
| 137 self.compile_and_write(idl_filename) | 123 self.compile_and_write(idl_filename) |
| 138 | 124 |
| 139 | 125 |
| 140 class IdlCompilerDictionaryImpl(IdlCompiler): | 126 def generate_bindings(code_generator_class, options, input_filename): |
| 141 def __init__(self, *args, **kwargs): | |
| 142 IdlCompiler.__init__(self, *args, **kwargs) | |
| 143 self.code_generator = CodeGeneratorDictionaryImpl( | |
| 144 self.info_provider, self.cache_directory, self.output_directory) | |
| 145 | |
| 146 def compile_file(self, idl_filename): | |
| 147 self.compile_and_write(idl_filename) | |
| 148 | |
| 149 | |
| 150 def generate_bindings(options, input_filename): | |
| 151 info_provider = create_component_info_provider( | 127 info_provider = create_component_info_provider( |
| 152 options.info_dir, options.target_component) | 128 options.info_dir, options.target_component) |
| 153 idl_compiler = IdlCompilerV8( | 129 idl_compiler = IdlCompiler( |
| 154 options.output_directory, | 130 output_directory=options.output_directory, |
| 155 cache_directory=options.cache_directory, | 131 cache_directory=options.cache_directory, |
| 132 code_generator_class=code_generator_class, |
| 156 info_provider=info_provider, | 133 info_provider=info_provider, |
| 157 target_component=options.target_component) | 134 target_component=options.target_component) |
| 158 idl_compiler.compile_file(input_filename) | 135 idl_compiler.compile_file(input_filename) |
| 159 | 136 |
| 160 | 137 |
| 161 def generate_dictionary_impl(options, input_filename): | 138 def generate_dictionary_impl(code_generator_class, options, input_filename): |
| 162 info_provider = create_component_info_provider( | 139 info_provider = create_component_info_provider( |
| 163 options.info_dir, options.target_component) | 140 options.info_dir, options.target_component) |
| 164 idl_compiler = IdlCompilerDictionaryImpl( | 141 idl_compiler = IdlCompiler( |
| 165 options.impl_output_directory, | 142 output_directory=options.impl_output_directory, |
| 166 cache_directory=options.cache_directory, | 143 cache_directory=options.cache_directory, |
| 144 code_generator_class=code_generator_class, |
| 167 info_provider=info_provider, | 145 info_provider=info_provider, |
| 168 target_component=options.target_component) | 146 target_component=options.target_component) |
| 169 | 147 |
| 170 idl_filenames = read_idl_files_list_from_file(input_filename, | 148 idl_filenames = read_idl_files_list_from_file(input_filename, |
| 171 is_gyp_format=True) | 149 is_gyp_format=True) |
| 172 for idl_filename in idl_filenames: | 150 for idl_filename in idl_filenames: |
| 173 idl_compiler.compile_file(idl_filename) | 151 idl_compiler.compile_file(idl_filename) |
| 174 | 152 |
| 175 | 153 |
| 176 def generate_union_type_containers(options): | 154 def generate_union_type_containers(code_generator_class, options): |
| 177 info_provider = create_component_info_provider( | 155 info_provider = create_component_info_provider( |
| 178 options.info_dir, options.target_component) | 156 options.info_dir, options.target_component) |
| 179 if not info_provider.interfaces_info: | 157 if not info_provider.interfaces_info: |
| 180 raise Exception('Interfaces info is required to generate ' | 158 raise Exception('Interfaces info is required to generate ' |
| 181 'union types containers') | 159 'union types containers') |
| 182 generator = CodeGeneratorUnionType( | 160 generator = code_generator_class( |
| 183 info_provider, | 161 info_provider, |
| 184 options.cache_directory, | 162 options.cache_directory, |
| 185 options.output_directory, | 163 options.output_directory, |
| 186 options.target_component) | 164 options.target_component) |
| 187 output_code_list = generator.generate_code() | 165 output_code_list = generator.generate_code() |
| 188 for output_path, output_code in output_code_list: | 166 for output_path, output_code in output_code_list: |
| 189 write_file(output_code, output_path) | 167 write_file(output_code, output_path) |
| 190 | 168 |
| 191 | 169 |
| 192 def generate_callback_function_impl(options): | 170 def generate_callback_function_impl(code_generator_class, options): |
| 193 info_provider = create_component_info_provider( | 171 info_provider = create_component_info_provider( |
| 194 options.info_dir, options.target_component) | 172 options.info_dir, options.target_component) |
| 195 generator = CodeGeneratorCallbackFunction( | 173 generator = code_generator_class( |
| 196 info_provider, | 174 info_provider, |
| 197 options.cache_directory, | 175 options.cache_directory, |
| 198 options.output_directory, | 176 options.output_directory, |
| 199 options.target_component) | 177 options.target_component) |
| 200 output_code_list = generator.generate_code() | 178 output_code_list = generator.generate_code() |
| 201 for output_path, output_code in output_code_list: | 179 for output_path, output_code in output_code_list: |
| 202 write_file(output_code, output_path) | 180 write_file(output_code, output_path) |
| 203 | 181 |
| 204 | 182 |
| 205 def main(): | 183 def main(): |
| 206 options, input_filename = parse_options() | 184 options, input_filename = parse_options() |
| 207 if options.generate_impl: | 185 if options.generate_impl: |
| 208 # |input_filename| should be a file which contains a list of IDL | 186 # |input_filename| should be a file which contains a list of IDL |
| 209 # dictionary paths. | 187 # dictionary paths. |
| 210 generate_dictionary_impl(options, input_filename) | 188 generate_dictionary_impl(CodeGeneratorDictionaryImpl, options, |
| 211 generate_union_type_containers(options) | 189 input_filename) |
| 212 generate_callback_function_impl(options) | 190 generate_union_type_containers(CodeGeneratorUnionType, options) |
| 191 generate_callback_function_impl(CodeGeneratorCallbackFunction, options) |
| 213 else: | 192 else: |
| 214 # |input_filename| should be a path of an IDL file. | 193 # |input_filename| should be a path of an IDL file. |
| 215 generate_bindings(options, input_filename) | 194 generate_bindings(CodeGeneratorV8, options, input_filename) |
| 216 | 195 |
| 217 | 196 |
| 218 if __name__ == '__main__': | 197 if __name__ == '__main__': |
| 219 sys.exit(main()) | 198 sys.exit(main()) |
| OLD | NEW |