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 18 matching lines...) Expand all Loading... | |
29 | 29 |
30 """Compile an .idl file to Blink V8 bindings (.h and .cpp files). | 30 """Compile an .idl file to Blink V8 bindings (.h and .cpp files). |
31 | 31 |
32 FIXME: Not currently used in build. | 32 FIXME: Not currently used in build. |
33 This is a rewrite of the Perl IDL compiler in Python, but is not complete. | 33 This is a rewrite of the Perl IDL compiler in Python, but is not complete. |
34 Once it is complete, we will switch all IDL files over to Python at once. | 34 Once it is complete, we will switch all IDL files over to Python at once. |
35 Until then, please work on the Perl IDL compiler. | 35 Until then, please work on the Perl IDL compiler. |
36 For details, see bug http://crbug.com/239771 | 36 For details, see bug http://crbug.com/239771 |
37 """ | 37 """ |
38 | 38 |
39 import abc | |
39 from optparse import OptionParser | 40 from optparse import OptionParser |
40 import os | 41 import os |
41 import cPickle as pickle | 42 import cPickle as pickle |
42 import sys | 43 import sys |
43 | 44 |
44 from code_generator_v8 import CodeGeneratorV8 | 45 from code_generator_v8 import CodeGeneratorV8 |
45 from idl_reader import IdlReader | 46 from idl_reader import IdlReader |
46 # from utilities import write_file # FIXME: import once in same directory | 47 from utilities import write_file |
47 | 48 |
48 | 49 |
49 def parse_options(): | 50 def parse_options(): |
50 parser = OptionParser() | 51 parser = OptionParser() |
51 parser.add_option('--idl-attributes-file') | 52 parser.add_option('--idl-attributes-file') |
52 parser.add_option('--output-directory') | 53 parser.add_option('--output-directory') |
53 parser.add_option('--interfaces-info-file') | 54 parser.add_option('--interfaces-info-file') |
54 parser.add_option('--write-file-only-if-changed', type='int') | 55 parser.add_option('--write-file-only-if-changed', type='int') |
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]) |
65 return options, idl_filename | 66 return options, idl_filename |
66 | 67 |
67 | 68 |
68 # FIXME: import from utilities once moved into same directory (lines vs. text) | 69 def idl_filename_to_interface_name(idl_filename): |
69 def write_file(new_text, destination_filename, only_if_changed): | 70 basename = os.path.basename(idl_filename) |
70 if only_if_changed and os.path.isfile(destination_filename): | 71 interface_name, _ = os.path.splitext(basename) |
71 with open(destination_filename) as destination_file: | 72 return interface_name |
72 if destination_file.read() == new_text: | |
73 return | |
74 with open(destination_filename, 'w') as destination_file: | |
75 destination_file.write(new_text) | |
76 | 73 |
77 | 74 |
78 class IdlCompiler(object): | 75 class IdlCompiler(object): |
79 def __init__(self, output_directory, interfaces_info, idl_attributes_file, o nly_if_changed=False): | 76 """Abstract Base Class for IDL compilers. |
77 | |
78 In concrete classes: | |
79 * self.code_generator must be set, implementing generate_code() | |
80 (returning a list of output code), and | |
81 * compile_file() must be implemented (handling output filenames). | |
82 """ | |
83 __metaclass__ = abc.ABCMeta | |
84 | |
85 def __init__(self, output_directory, idl_attributes_file, | |
86 interfaces_info=None, interfaces_info_filename='', | |
87 only_if_changed=False): | |
88 """ | |
89 Args: | |
90 interfaces_info: | |
91 interfaces_info dict | |
92 (avoids auxiliary file in run-bindings-tests) | |
93 interfaces_info_file: filename of pickled interfaces_info | |
94 """ | |
95 self.code_generator = None # Set in derived class | |
kouhei (in TOK)
2014/03/10 06:01:52
optional nit: make this an arg of __init__ to enfo
Nils Barth (inactive)
2014/03/10 06:31:02
Done.
Thanks, that's a bit clearer!
In this case w
| |
96 if interfaces_info_filename: | |
97 with open(interfaces_info_filename) as interfaces_info_file: | |
98 interfaces_info = pickle.load(interfaces_info_file) | |
99 self.interfaces_info = interfaces_info | |
100 self.only_if_changed = only_if_changed | |
80 self.output_directory = output_directory | 101 self.output_directory = output_directory |
81 self.only_if_changed = only_if_changed | 102 self.reader = IdlReader(interfaces_info, idl_attributes_file, |
82 self.reader = IdlReader(interfaces_info, idl_attributes_file, output_dir ectory) | 103 output_directory) |
83 self.code_generator = CodeGeneratorV8(interfaces_info, output_directory) | |
84 | 104 |
85 def compile(self, idl_filename): | 105 def compile_and_write(self, idl_filename, output_filenames): |
86 basename = os.path.basename(idl_filename) | 106 interface_name = idl_filename_to_interface_name(idl_filename) |
87 interface_name, _ = os.path.splitext(basename) | 107 definitions = self.reader.read_idl_definitions(idl_filename) |
108 output_code_list = self.code_generator.generate_code( | |
109 definitions, interface_name) | |
110 for output_code, output_filename in zip(output_code_list, | |
111 output_filenames): | |
112 write_file(output_code, output_filename, self.only_if_changed) | |
88 | 113 |
89 definitions = self.reader.read_idl_definitions(idl_filename) | 114 @abc.abstractmethod |
90 header_text, cpp_text = self.code_generator.generate_code(definitions, i nterface_name) | 115 def compile_file(self, idl_filename): |
116 pass | |
91 | 117 |
118 | |
119 class IdlCompilerV8(IdlCompiler): | |
120 def __init__(self, *args, **kwargs): | |
121 IdlCompiler.__init__(self, *args, **kwargs) | |
122 self.code_generator = CodeGeneratorV8(self.interfaces_info, | |
123 self.output_directory) | |
124 | |
125 def compile_file(self, idl_filename): | |
126 interface_name = idl_filename_to_interface_name(idl_filename) | |
92 header_filename = os.path.join(self.output_directory, | 127 header_filename = os.path.join(self.output_directory, |
93 'V8%s.h' % interface_name) | 128 'V8%s.h' % interface_name) |
94 cpp_filename = os.path.join(self.output_directory, | 129 cpp_filename = os.path.join(self.output_directory, |
95 'V8%s.cpp' % interface_name) | 130 'V8%s.cpp' % interface_name) |
96 write_file(header_text, header_filename, self.only_if_changed) | 131 self.compile_and_write(idl_filename, (header_filename, cpp_filename)) |
97 write_file(cpp_text, cpp_filename, self.only_if_changed) | |
98 | 132 |
99 | 133 |
100 def main(): | 134 def main(): |
101 options, idl_filename = parse_options() | 135 options, idl_filename = parse_options() |
136 idl_compiler = IdlCompilerV8(options.output_directory, | |
137 options.idl_attributes_file, | |
138 interfaces_info_filename=options.interfaces_inf o_file, | |
139 only_if_changed=options.write_file_only_if_chan ged) | |
140 idl_compiler.compile_file(idl_filename) | |
102 | 141 |
103 interfaces_info_filename = options.interfaces_info_file | |
104 if interfaces_info_filename: | |
105 with open(interfaces_info_filename) as interfaces_info_file: | |
106 interfaces_info = pickle.load(interfaces_info_file) | |
107 else: | |
108 interfaces_info = None | |
109 | |
110 idl_compiler = IdlCompiler(options.output_directory, | |
111 interfaces_info, | |
112 options.idl_attributes_file, | |
113 options.write_file_only_if_changed) | |
114 idl_compiler.compile(idl_filename) | |
115 | 142 |
116 if __name__ == '__main__': | 143 if __name__ == '__main__': |
117 sys.exit(main()) | 144 sys.exit(main()) |
OLD | NEW |