| 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 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 45 | 45 |
| 46 def parse_options(): | 46 def parse_options(): |
| 47 parser = OptionParser() | 47 parser = OptionParser() |
| 48 parser.add_option('--cache-directory', | 48 parser.add_option('--cache-directory', |
| 49 help='cache directory, defaults to output directory') | 49 help='cache directory, defaults to output directory') |
| 50 parser.add_option('--generate-dictionary-impl', | 50 parser.add_option('--generate-dictionary-impl', |
| 51 action="store_true", default=False) | 51 action="store_true", default=False) |
| 52 parser.add_option('--output-directory') | 52 parser.add_option('--output-directory') |
| 53 parser.add_option('--interfaces-info-file') | 53 parser.add_option('--interfaces-info-file') |
| 54 parser.add_option('--write-file-only-if-changed', type='int') | 54 parser.add_option('--write-file-only-if-changed', type='int') |
| 55 parser.add_option('--generate-partial', action='store_true') |
| 55 # ensure output comes last, so command line easy to parse via regexes | 56 # ensure output comes last, so command line easy to parse via regexes |
| 56 parser.disable_interspersed_args() | 57 parser.disable_interspersed_args() |
| 57 | 58 |
| 58 options, args = parser.parse_args() | 59 options, args = parser.parse_args() |
| 59 if options.output_directory is None: | 60 if options.output_directory is None: |
| 60 parser.error('Must specify output directory using --output-directory.') | 61 parser.error('Must specify output directory using --output-directory.') |
| 61 options.write_file_only_if_changed = bool(options.write_file_only_if_changed
) | 62 options.write_file_only_if_changed = bool(options.write_file_only_if_changed
) |
| 62 if len(args) != 1: | 63 if len(args) != 1: |
| 63 parser.error('Must specify exactly 1 input file as argument, but %d give
n.' % len(args)) | 64 parser.error('Must specify exactly 1 input file as argument, but %d give
n.' % len(args)) |
| 64 idl_filename = os.path.realpath(args[0]) | 65 idl_filename = os.path.realpath(args[0]) |
| (...skipping 11 matching lines...) Expand all Loading... |
| 76 | 77 |
| 77 In concrete classes: | 78 In concrete classes: |
| 78 * self.code_generator must be set, implementing generate_code() | 79 * self.code_generator must be set, implementing generate_code() |
| 79 (returning a list of output code), and | 80 (returning a list of output code), and |
| 80 * compile_file() must be implemented (handling output filenames). | 81 * compile_file() must be implemented (handling output filenames). |
| 81 """ | 82 """ |
| 82 __metaclass__ = abc.ABCMeta | 83 __metaclass__ = abc.ABCMeta |
| 83 | 84 |
| 84 def __init__(self, output_directory, cache_directory='', | 85 def __init__(self, output_directory, cache_directory='', |
| 85 code_generator=None, interfaces_info=None, | 86 code_generator=None, interfaces_info=None, |
| 86 interfaces_info_filename='', only_if_changed=False): | 87 interfaces_info_filename='', only_if_changed=False, |
| 88 generate_partial=False): |
| 87 """ | 89 """ |
| 88 Args: | 90 Args: |
| 89 interfaces_info: | 91 interfaces_info: |
| 90 interfaces_info dict | 92 interfaces_info dict |
| 91 (avoids auxiliary file in run-bindings-tests) | 93 (avoids auxiliary file in run-bindings-tests) |
| 92 interfaces_info_file: filename of pickled interfaces_info | 94 interfaces_info_file: filename of pickled interfaces_info |
| 93 """ | 95 """ |
| 94 cache_directory = cache_directory or output_directory | 96 cache_directory = cache_directory or output_directory |
| 95 self.cache_directory = cache_directory | 97 self.cache_directory = cache_directory |
| 96 self.code_generator = code_generator | 98 self.code_generator = code_generator |
| 97 if interfaces_info_filename: | 99 if interfaces_info_filename: |
| 98 with open(interfaces_info_filename) as interfaces_info_file: | 100 with open(interfaces_info_filename) as interfaces_info_file: |
| 99 interfaces_info = pickle.load(interfaces_info_file) | 101 interfaces_info = pickle.load(interfaces_info_file) |
| 100 self.interfaces_info = interfaces_info | 102 self.interfaces_info = interfaces_info |
| 101 self.only_if_changed = only_if_changed | 103 self.only_if_changed = only_if_changed |
| 102 self.output_directory = output_directory | 104 self.output_directory = output_directory |
| 105 self.generate_partial = generate_partial |
| 103 self.reader = IdlReader(interfaces_info, cache_directory) | 106 self.reader = IdlReader(interfaces_info, cache_directory) |
| 104 | 107 |
| 105 def compile_and_write(self, idl_filename): | 108 def compile_and_write(self, idl_filename): |
| 106 interface_name = idl_filename_to_interface_name(idl_filename) | 109 interface_name = idl_filename_to_interface_name(idl_filename) |
| 107 component = idl_filename_to_component(idl_filename) | 110 component = idl_filename_to_component(idl_filename) |
| 108 definitions = self.reader.read_idl_definitions(idl_filename) | 111 definitions = self.reader.read_idl_definitions(idl_filename) |
| 112 if self.generate_partial and 'modules' in definitions: |
| 113 assert component == 'core' |
| 114 target_definitions = definitions['modules'] |
| 115 else: |
| 116 target_definitions = definitions[component] |
| 109 output_code_list = self.code_generator.generate_code( | 117 output_code_list = self.code_generator.generate_code( |
| 110 definitions[component], interface_name) | 118 target_definitions, interface_name) |
| 119 if output_code_list is None: |
| 120 return |
| 111 for output_path, output_code in output_code_list: | 121 for output_path, output_code in output_code_list: |
| 112 write_file(output_code, output_path, self.only_if_changed) | 122 if output_code and output_path: |
| 123 write_file(output_code, output_path, self.only_if_changed) |
| 113 | 124 |
| 114 @abc.abstractmethod | 125 @abc.abstractmethod |
| 115 def compile_file(self, idl_filename): | 126 def compile_file(self, idl_filename): |
| 116 pass | 127 pass |
| 117 | 128 |
| 118 | 129 |
| 119 class IdlCompilerV8(IdlCompiler): | 130 class IdlCompilerV8(IdlCompiler): |
| 120 def __init__(self, *args, **kwargs): | 131 def __init__(self, *args, **kwargs): |
| 121 IdlCompiler.__init__(self, *args, **kwargs) | 132 IdlCompiler.__init__(self, *args, **kwargs) |
| 122 self.code_generator = CodeGeneratorV8(self.interfaces_info, | 133 self.code_generator = CodeGeneratorV8(self.interfaces_info, |
| (...skipping 12 matching lines...) Expand all Loading... |
| 135 | 146 |
| 136 def compile_file(self, idl_filename): | 147 def compile_file(self, idl_filename): |
| 137 self.compile_and_write(idl_filename) | 148 self.compile_and_write(idl_filename) |
| 138 | 149 |
| 139 | 150 |
| 140 def generate_bindings(options, input_filename): | 151 def generate_bindings(options, input_filename): |
| 141 idl_compiler = IdlCompilerV8( | 152 idl_compiler = IdlCompilerV8( |
| 142 options.output_directory, | 153 options.output_directory, |
| 143 cache_directory=options.cache_directory, | 154 cache_directory=options.cache_directory, |
| 144 interfaces_info_filename=options.interfaces_info_file, | 155 interfaces_info_filename=options.interfaces_info_file, |
| 145 only_if_changed=options.write_file_only_if_changed) | 156 only_if_changed=options.write_file_only_if_changed, |
| 157 generate_partial=options.generate_partial) |
| 146 idl_compiler.compile_file(input_filename) | 158 idl_compiler.compile_file(input_filename) |
| 147 | 159 |
| 148 | 160 |
| 149 def generate_dictionary_impl(options, input_filename): | 161 def generate_dictionary_impl(options, input_filename): |
| 150 idl_compiler = IdlCompilerDictionaryImpl( | 162 idl_compiler = IdlCompilerDictionaryImpl( |
| 151 options.output_directory, | 163 options.output_directory, |
| 152 cache_directory=options.cache_directory, | 164 cache_directory=options.cache_directory, |
| 153 interfaces_info_filename=options.interfaces_info_file, | 165 interfaces_info_filename=options.interfaces_info_file, |
| 154 only_if_changed=options.write_file_only_if_changed) | 166 only_if_changed=options.write_file_only_if_changed) |
| 155 | 167 |
| 156 idl_filenames = read_idl_files_list_from_file(input_filename) | 168 idl_filenames = read_idl_files_list_from_file(input_filename) |
| 157 for idl_filename in idl_filenames: | 169 for idl_filename in idl_filenames: |
| 158 idl_compiler.compile_file(idl_filename) | 170 idl_compiler.compile_file(idl_filename) |
| 159 | 171 |
| 160 | 172 |
| 161 def main(): | 173 def main(): |
| 162 options, input_filename = parse_options() | 174 options, input_filename = parse_options() |
| 163 if options.generate_dictionary_impl: | 175 if options.generate_dictionary_impl: |
| 164 # |input_filename| should be a file which contains a list of IDL | 176 # |input_filename| should be a file which contains a list of IDL |
| 165 # dictionary paths. | 177 # dictionary paths. |
| 166 generate_dictionary_impl(options, input_filename) | 178 generate_dictionary_impl(options, input_filename) |
| 167 else: | 179 else: |
| 168 # |input_filename| should be a path of an IDL file. | 180 # |input_filename| should be a path of an IDL file. |
| 169 generate_bindings(options, input_filename) | 181 generate_bindings(options, input_filename) |
| 170 | 182 |
| 171 | 183 |
| 172 if __name__ == '__main__': | 184 if __name__ == '__main__': |
| 173 sys.exit(main()) | 185 sys.exit(main()) |
| OLD | NEW |