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 20 matching lines...) Expand all Loading... |
31 | 31 |
32 Design doc: http://www.chromium.org/developers/design-documents/idl-compiler | 32 Design doc: http://www.chromium.org/developers/design-documents/idl-compiler |
33 """ | 33 """ |
34 | 34 |
35 import abc | 35 import abc |
36 from optparse import OptionParser | 36 from optparse import OptionParser |
37 import os | 37 import os |
38 import cPickle as pickle | 38 import cPickle as pickle |
39 import sys | 39 import sys |
40 | 40 |
41 from code_generator_v8 import CodeGeneratorDictionaryImpl, CodeGeneratorV8 | 41 from code_generator_v8 import CodeGeneratorDictionaryImpl, CodeGeneratorV8, Code
GeneratorUnionType |
42 from idl_reader import IdlReader | 42 from idl_reader import IdlReader |
43 from utilities import read_idl_files_list_from_file, write_file, idl_filename_to
_component | 43 from utilities import create_component_info_provider, read_idl_files_list_from_f
ile, write_file, idl_filename_to_component |
44 | 44 |
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-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('--impl-output-directory') |
| 54 parser.add_option('--info-dir') |
54 parser.add_option('--write-file-only-if-changed', type='int') | 55 parser.add_option('--write-file-only-if-changed', type='int') |
| 56 # FIXME: We should always explicitly specify --target-component and |
| 57 # remove the default behavior. |
| 58 parser.add_option('--target-component', |
| 59 help='target component to generate code, defaults to ' |
| 60 'component of input idl file') |
55 # ensure output comes last, so command line easy to parse via regexes | 61 # ensure output comes last, so command line easy to parse via regexes |
56 parser.disable_interspersed_args() | 62 parser.disable_interspersed_args() |
57 | 63 |
58 options, args = parser.parse_args() | 64 options, args = parser.parse_args() |
59 if options.output_directory is None: | 65 if options.output_directory is None: |
60 parser.error('Must specify output directory using --output-directory.') | 66 parser.error('Must specify output directory using --output-directory.') |
61 options.write_file_only_if_changed = bool(options.write_file_only_if_changed
) | 67 options.write_file_only_if_changed = bool(options.write_file_only_if_changed
) |
62 if len(args) != 1: | 68 if len(args) != 1: |
63 parser.error('Must specify exactly 1 input file as argument, but %d give
n.' % len(args)) | 69 parser.error('Must specify exactly 1 input file as argument, but %d give
n.' % len(args)) |
64 idl_filename = os.path.realpath(args[0]) | 70 idl_filename = os.path.realpath(args[0]) |
65 return options, idl_filename | 71 return options, idl_filename |
66 | 72 |
67 | 73 |
68 def idl_filename_to_interface_name(idl_filename): | 74 def idl_filename_to_interface_name(idl_filename): |
69 basename = os.path.basename(idl_filename) | 75 basename = os.path.basename(idl_filename) |
70 interface_name, _ = os.path.splitext(basename) | 76 interface_name, _ = os.path.splitext(basename) |
71 return interface_name | 77 return interface_name |
72 | 78 |
73 | 79 |
74 class IdlCompiler(object): | 80 class IdlCompiler(object): |
75 """Abstract Base Class for IDL compilers. | 81 """Abstract Base Class for IDL compilers. |
76 | 82 |
77 In concrete classes: | 83 In concrete classes: |
78 * self.code_generator must be set, implementing generate_code() | 84 * self.code_generator must be set, implementing generate_code() |
79 (returning a list of output code), and | 85 (returning a list of output code), and |
80 * compile_file() must be implemented (handling output filenames). | 86 * compile_file() must be implemented (handling output filenames). |
81 """ | 87 """ |
82 __metaclass__ = abc.ABCMeta | 88 __metaclass__ = abc.ABCMeta |
83 | 89 |
84 def __init__(self, output_directory, cache_directory='', | 90 def __init__(self, output_directory, cache_directory=None, |
85 code_generator=None, interfaces_info=None, | 91 code_generator=None, info_provider=None, |
86 interfaces_info_filename='', only_if_changed=False): | 92 only_if_changed=False, target_component=None): |
87 """ | 93 """ |
88 Args: | 94 Args: |
89 interfaces_info: | 95 output_directory: directory to put output files. |
90 interfaces_info dict | 96 cache_directory: directory which contains PLY caches. |
91 (avoids auxiliary file in run-bindings-tests) | 97 code_generator: code generator to be used. |
92 interfaces_info_file: filename of pickled interfaces_info | 98 info_provider: component-specific information provider. |
| 99 only_if_changed: True when the compiler should only write output files |
| 100 when the contents are changed. |
| 101 target_component: component to be processed. |
93 """ | 102 """ |
94 cache_directory = cache_directory or output_directory | |
95 self.cache_directory = cache_directory | 103 self.cache_directory = cache_directory |
96 self.code_generator = code_generator | 104 self.code_generator = code_generator |
97 if interfaces_info_filename: | 105 self.info_provider = info_provider |
98 with open(interfaces_info_filename) as interfaces_info_file: | |
99 interfaces_info = pickle.load(interfaces_info_file) | |
100 self.interfaces_info = interfaces_info | |
101 self.only_if_changed = only_if_changed | 106 self.only_if_changed = only_if_changed |
102 self.output_directory = output_directory | 107 self.output_directory = output_directory |
103 self.reader = IdlReader(interfaces_info, cache_directory) | 108 self.target_component = target_component |
| 109 self.reader = IdlReader(info_provider.interfaces_info, cache_directory) |
104 | 110 |
105 def compile_and_write(self, idl_filename): | 111 def compile_and_write(self, idl_filename): |
106 interface_name = idl_filename_to_interface_name(idl_filename) | 112 interface_name = idl_filename_to_interface_name(idl_filename) |
107 component = idl_filename_to_component(idl_filename) | |
108 definitions = self.reader.read_idl_definitions(idl_filename) | 113 definitions = self.reader.read_idl_definitions(idl_filename) |
| 114 target_component = self.target_component or idl_filename_to_component(id
l_filename) |
| 115 target_definitions = definitions[target_component] |
109 output_code_list = self.code_generator.generate_code( | 116 output_code_list = self.code_generator.generate_code( |
110 definitions[component], interface_name) | 117 target_definitions, interface_name) |
111 for output_path, output_code in output_code_list: | 118 for output_path, output_code in output_code_list: |
112 write_file(output_code, output_path, self.only_if_changed) | 119 write_file(output_code, output_path, self.only_if_changed) |
113 | 120 |
114 @abc.abstractmethod | 121 @abc.abstractmethod |
115 def compile_file(self, idl_filename): | 122 def compile_file(self, idl_filename): |
116 pass | 123 pass |
117 | 124 |
118 | 125 |
119 class IdlCompilerV8(IdlCompiler): | 126 class IdlCompilerV8(IdlCompiler): |
120 def __init__(self, *args, **kwargs): | 127 def __init__(self, *args, **kwargs): |
121 IdlCompiler.__init__(self, *args, **kwargs) | 128 IdlCompiler.__init__(self, *args, **kwargs) |
122 self.code_generator = CodeGeneratorV8(self.interfaces_info, | 129 self.code_generator = CodeGeneratorV8(self.info_provider, |
123 self.cache_directory, | 130 self.cache_directory, |
124 self.output_directory) | 131 self.output_directory) |
125 | 132 |
126 def compile_file(self, idl_filename): | 133 def compile_file(self, idl_filename): |
127 self.compile_and_write(idl_filename) | 134 self.compile_and_write(idl_filename) |
128 | 135 |
129 | 136 |
130 class IdlCompilerDictionaryImpl(IdlCompiler): | 137 class IdlCompilerDictionaryImpl(IdlCompiler): |
131 def __init__(self, *args, **kwargs): | 138 def __init__(self, *args, **kwargs): |
132 IdlCompiler.__init__(self, *args, **kwargs) | 139 IdlCompiler.__init__(self, *args, **kwargs) |
133 self.code_generator = CodeGeneratorDictionaryImpl( | 140 self.code_generator = CodeGeneratorDictionaryImpl( |
134 self.interfaces_info, self.cache_directory, self.output_directory) | 141 self.info_provider, self.cache_directory, self.output_directory) |
135 | 142 |
136 def compile_file(self, idl_filename): | 143 def compile_file(self, idl_filename): |
137 self.compile_and_write(idl_filename) | 144 self.compile_and_write(idl_filename) |
138 | 145 |
139 | 146 |
140 def generate_bindings(options, input_filename): | 147 def generate_bindings(options, input_filename): |
| 148 info_provider = create_component_info_provider( |
| 149 options.info_dir, options.target_component) |
141 idl_compiler = IdlCompilerV8( | 150 idl_compiler = IdlCompilerV8( |
142 options.output_directory, | 151 options.output_directory, |
143 cache_directory=options.cache_directory, | 152 cache_directory=options.cache_directory, |
144 interfaces_info_filename=options.interfaces_info_file, | 153 info_provider=info_provider, |
145 only_if_changed=options.write_file_only_if_changed) | 154 only_if_changed=options.write_file_only_if_changed, |
| 155 target_component=options.target_component) |
146 idl_compiler.compile_file(input_filename) | 156 idl_compiler.compile_file(input_filename) |
147 | 157 |
148 | 158 |
149 def generate_dictionary_impl(options, input_filename): | 159 def generate_dictionary_impl(options, input_filename): |
| 160 info_provider = create_component_info_provider( |
| 161 options.info_dir, options.target_component) |
150 idl_compiler = IdlCompilerDictionaryImpl( | 162 idl_compiler = IdlCompilerDictionaryImpl( |
151 options.output_directory, | 163 options.impl_output_directory, |
152 cache_directory=options.cache_directory, | 164 cache_directory=options.cache_directory, |
153 interfaces_info_filename=options.interfaces_info_file, | 165 info_provider=info_provider, |
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 |
| 173 def generate_union_type_containers(options): |
| 174 info_provider = create_component_info_provider( |
| 175 options.info_dir, options.target_component) |
| 176 if not info_provider.interfaces_info: |
| 177 raise Exception('Interfaces info is required to generate ' |
| 178 'union types containers') |
| 179 generator = CodeGeneratorUnionType( |
| 180 info_provider, |
| 181 options.cache_directory, |
| 182 options.output_directory, |
| 183 options.target_component) |
| 184 output_code_list = generator.generate_code() |
| 185 for output_path, output_code in output_code_list: |
| 186 write_file(output_code, output_path, options.write_file_only_if_changed) |
| 187 |
| 188 |
161 def main(): | 189 def main(): |
162 options, input_filename = parse_options() | 190 options, input_filename = parse_options() |
163 if options.generate_dictionary_impl: | 191 if options.generate_impl: |
164 # |input_filename| should be a file which contains a list of IDL | 192 # |input_filename| should be a file which contains a list of IDL |
165 # dictionary paths. | 193 # dictionary paths. |
166 generate_dictionary_impl(options, input_filename) | 194 generate_dictionary_impl(options, input_filename) |
| 195 generate_union_type_containers(options) |
167 else: | 196 else: |
168 # |input_filename| should be a path of an IDL file. | 197 # |input_filename| should be a path of an IDL file. |
169 generate_bindings(options, input_filename) | 198 generate_bindings(options, input_filename) |
170 | 199 |
171 | 200 |
172 if __name__ == '__main__': | 201 if __name__ == '__main__': |
173 sys.exit(main()) | 202 sys.exit(main()) |
OLD | NEW |