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 27 matching lines...) Expand all Loading... |
38 import cPickle as pickle | 38 import cPickle as pickle |
39 import sys | 39 import sys |
40 | 40 |
41 from code_generator_v8 import CodeGeneratorV8 | 41 from code_generator_v8 import CodeGeneratorV8 |
42 from idl_reader import IdlReader | 42 from idl_reader import IdlReader |
43 from utilities import write_file | 43 from utilities import write_file |
44 | 44 |
45 | 45 |
46 def parse_options(): | 46 def parse_options(): |
47 parser = OptionParser() | 47 parser = OptionParser() |
| 48 parser.add_option('--cache-directory', |
| 49 help='cache directory, defaults to output directory') |
48 parser.add_option('--output-directory') | 50 parser.add_option('--output-directory') |
49 parser.add_option('--interfaces-info-file') | 51 parser.add_option('--interfaces-info-file') |
50 parser.add_option('--write-file-only-if-changed', type='int') | 52 parser.add_option('--write-file-only-if-changed', type='int') |
51 # ensure output comes last, so command line easy to parse via regexes | 53 # ensure output comes last, so command line easy to parse via regexes |
52 parser.disable_interspersed_args() | 54 parser.disable_interspersed_args() |
53 | 55 |
54 options, args = parser.parse_args() | 56 options, args = parser.parse_args() |
55 if options.output_directory is None: | 57 if options.output_directory is None: |
56 parser.error('Must specify output directory using --output-directory.') | 58 parser.error('Must specify output directory using --output-directory.') |
57 options.write_file_only_if_changed = bool(options.write_file_only_if_changed
) | 59 options.write_file_only_if_changed = bool(options.write_file_only_if_changed
) |
(...skipping 12 matching lines...) Expand all Loading... |
70 class IdlCompiler(object): | 72 class IdlCompiler(object): |
71 """Abstract Base Class for IDL compilers. | 73 """Abstract Base Class for IDL compilers. |
72 | 74 |
73 In concrete classes: | 75 In concrete classes: |
74 * self.code_generator must be set, implementing generate_code() | 76 * self.code_generator must be set, implementing generate_code() |
75 (returning a list of output code), and | 77 (returning a list of output code), and |
76 * compile_file() must be implemented (handling output filenames). | 78 * compile_file() must be implemented (handling output filenames). |
77 """ | 79 """ |
78 __metaclass__ = abc.ABCMeta | 80 __metaclass__ = abc.ABCMeta |
79 | 81 |
80 def __init__(self, output_directory, code_generator=None, | 82 def __init__(self, output_directory, cache_directory='', |
81 interfaces_info=None, interfaces_info_filename='', | 83 code_generator=None, interfaces_info=None, |
82 only_if_changed=False): | 84 interfaces_info_filename='', only_if_changed=False): |
83 """ | 85 """ |
84 Args: | 86 Args: |
85 interfaces_info: | 87 interfaces_info: |
86 interfaces_info dict | 88 interfaces_info dict |
87 (avoids auxiliary file in run-bindings-tests) | 89 (avoids auxiliary file in run-bindings-tests) |
88 interfaces_info_file: filename of pickled interfaces_info | 90 interfaces_info_file: filename of pickled interfaces_info |
89 """ | 91 """ |
| 92 cache_directory = cache_directory or output_directory |
| 93 self.cache_directory = cache_directory |
90 self.code_generator = code_generator | 94 self.code_generator = code_generator |
91 if interfaces_info_filename: | 95 if interfaces_info_filename: |
92 with open(interfaces_info_filename) as interfaces_info_file: | 96 with open(interfaces_info_filename) as interfaces_info_file: |
93 interfaces_info = pickle.load(interfaces_info_file) | 97 interfaces_info = pickle.load(interfaces_info_file) |
94 self.interfaces_info = interfaces_info | 98 self.interfaces_info = interfaces_info |
95 self.only_if_changed = only_if_changed | 99 self.only_if_changed = only_if_changed |
96 self.output_directory = output_directory | 100 self.output_directory = output_directory |
97 self.reader = IdlReader(interfaces_info, output_directory) | 101 self.reader = IdlReader(interfaces_info, cache_directory) |
98 | 102 |
99 def compile_and_write(self, idl_filename, output_filenames): | 103 def compile_and_write(self, idl_filename, output_filenames): |
100 interface_name = idl_filename_to_interface_name(idl_filename) | 104 interface_name = idl_filename_to_interface_name(idl_filename) |
101 definitions = self.reader.read_idl_definitions(idl_filename) | 105 definitions = self.reader.read_idl_definitions(idl_filename) |
102 output_code_list = self.code_generator.generate_code( | 106 output_code_list = self.code_generator.generate_code( |
103 definitions, interface_name) | 107 definitions, interface_name) |
104 for output_code, output_filename in zip(output_code_list, | 108 for output_code, output_filename in zip(output_code_list, |
105 output_filenames): | 109 output_filenames): |
106 write_file(output_code, output_filename, self.only_if_changed) | 110 write_file(output_code, output_filename, self.only_if_changed) |
107 | 111 |
108 @abc.abstractmethod | 112 @abc.abstractmethod |
109 def compile_file(self, idl_filename): | 113 def compile_file(self, idl_filename): |
110 pass | 114 pass |
111 | 115 |
112 | 116 |
113 class IdlCompilerV8(IdlCompiler): | 117 class IdlCompilerV8(IdlCompiler): |
114 def __init__(self, *args, **kwargs): | 118 def __init__(self, *args, **kwargs): |
115 IdlCompiler.__init__(self, *args, **kwargs) | 119 IdlCompiler.__init__(self, *args, **kwargs) |
116 self.code_generator = CodeGeneratorV8(self.interfaces_info, | 120 self.code_generator = CodeGeneratorV8(self.interfaces_info, |
117 self.output_directory) | 121 self.cache_directory) |
118 | 122 |
119 def compile_file(self, idl_filename): | 123 def compile_file(self, idl_filename): |
120 interface_name = idl_filename_to_interface_name(idl_filename) | 124 interface_name = idl_filename_to_interface_name(idl_filename) |
121 header_filename = os.path.join(self.output_directory, | 125 header_filename = os.path.join(self.output_directory, |
122 'V8%s.h' % interface_name) | 126 'V8%s.h' % interface_name) |
123 cpp_filename = os.path.join(self.output_directory, | 127 cpp_filename = os.path.join(self.output_directory, |
124 'V8%s.cpp' % interface_name) | 128 'V8%s.cpp' % interface_name) |
125 self.compile_and_write(idl_filename, (header_filename, cpp_filename)) | 129 self.compile_and_write(idl_filename, (header_filename, cpp_filename)) |
126 | 130 |
127 | 131 |
128 def main(): | 132 def main(): |
129 options, idl_filename = parse_options() | 133 options, idl_filename = parse_options() |
130 idl_compiler = IdlCompilerV8( | 134 idl_compiler = IdlCompilerV8( |
131 options.output_directory, | 135 options.output_directory, |
| 136 cache_directory=options.cache_directory, |
132 interfaces_info_filename=options.interfaces_info_file, | 137 interfaces_info_filename=options.interfaces_info_file, |
133 only_if_changed=options.write_file_only_if_changed) | 138 only_if_changed=options.write_file_only_if_changed) |
134 idl_compiler.compile_file(idl_filename) | 139 idl_compiler.compile_file(idl_filename) |
135 | 140 |
136 | 141 |
137 if __name__ == '__main__': | 142 if __name__ == '__main__': |
138 sys.exit(main()) | 143 sys.exit(main()) |
OLD | NEW |