Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / bindings / scripts / idl_compiler.py
1 #!/usr/bin/python
2 # Copyright (C) 2013 Google Inc. All rights reserved.
3 #
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are
6 # met:
7 #
8 #     * Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer.
10 #     * Redistributions in binary form must reproduce the above
11 # copyright notice, this list of conditions and the following disclaimer
12 # in the documentation and/or other materials provided with the
13 # distribution.
14 #     * Neither the name of Google Inc. nor the names of its
15 # contributors may be used to endorse or promote products derived from
16 # this software without specific prior written permission.
17 #
18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 """Compile an .idl file to Blink V8 bindings (.h and .cpp files).
31
32 Design doc: http://www.chromium.org/developers/design-documents/idl-compiler
33 """
34
35 import abc
36 from optparse import OptionParser
37 import os
38 import cPickle as pickle
39 import sys
40
41 from code_generator_v8 import CodeGeneratorDictionaryImpl, CodeGeneratorV8, CodeGeneratorUnionType
42 from idl_reader import IdlReader
43 from utilities import read_idl_files_list_from_file, write_file, idl_filename_to_component
44
45
46 def parse_options():
47     parser = OptionParser()
48     parser.add_option('--cache-directory',
49                       help='cache directory, defaults to output directory')
50     parser.add_option('--generate-impl',
51                       action="store_true", default=False)
52     parser.add_option('--output-directory')
53     parser.add_option('--impl-output-directory')
54     parser.add_option('--interfaces-info-file')
55     parser.add_option('--component-info-file')
56     parser.add_option('--write-file-only-if-changed', type='int')
57     # FIXME: We should always explicitly specify --target-component and
58     # remove the default behavior.
59     parser.add_option('--target-component',
60                       help='target component to generate code, defaults to '
61                       'component of input idl file')
62     # ensure output comes last, so command line easy to parse via regexes
63     parser.disable_interspersed_args()
64
65     options, args = parser.parse_args()
66     if options.output_directory is None:
67         parser.error('Must specify output directory using --output-directory.')
68     options.write_file_only_if_changed = bool(options.write_file_only_if_changed)
69     if len(args) != 1:
70         parser.error('Must specify exactly 1 input file as argument, but %d given.' % len(args))
71     idl_filename = os.path.realpath(args[0])
72     return options, idl_filename
73
74
75 def idl_filename_to_interface_name(idl_filename):
76     basename = os.path.basename(idl_filename)
77     interface_name, _ = os.path.splitext(basename)
78     return interface_name
79
80
81 class IdlCompiler(object):
82     """Abstract Base Class for IDL compilers.
83
84     In concrete classes:
85     * self.code_generator must be set, implementing generate_code()
86       (returning a list of output code), and
87     * compile_file() must be implemented (handling output filenames).
88     """
89     __metaclass__ = abc.ABCMeta
90
91     def __init__(self, output_directory, cache_directory=None,
92                  code_generator=None, interfaces_info=None,
93                  interfaces_info_filename='', only_if_changed=False,
94                  target_component=None):
95         """
96         Args:
97             interfaces_info:
98                 interfaces_info dict
99                 (avoids auxiliary file in run-bindings-tests)
100             interfaces_info_file: filename of pickled interfaces_info
101         """
102         self.cache_directory = cache_directory
103         self.code_generator = code_generator
104         if interfaces_info_filename:
105             with open(interfaces_info_filename) as interfaces_info_file:
106                 interfaces_info = pickle.load(interfaces_info_file)
107         self.interfaces_info = interfaces_info
108         self.only_if_changed = only_if_changed
109         self.output_directory = output_directory
110         self.target_component = target_component
111         self.reader = IdlReader(interfaces_info, cache_directory)
112
113     def compile_and_write(self, idl_filename):
114         interface_name = idl_filename_to_interface_name(idl_filename)
115         definitions = self.reader.read_idl_definitions(idl_filename)
116         target_component = self.target_component or idl_filename_to_component(idl_filename)
117         target_definitions = definitions[target_component]
118         output_code_list = self.code_generator.generate_code(
119             target_definitions, interface_name)
120         for output_path, output_code in output_code_list:
121             write_file(output_code, output_path, self.only_if_changed)
122
123     @abc.abstractmethod
124     def compile_file(self, idl_filename):
125         pass
126
127
128 class IdlCompilerV8(IdlCompiler):
129     def __init__(self, *args, **kwargs):
130         IdlCompiler.__init__(self, *args, **kwargs)
131         self.code_generator = CodeGeneratorV8(self.interfaces_info,
132                                               self.cache_directory,
133                                               self.output_directory)
134
135     def compile_file(self, idl_filename):
136         self.compile_and_write(idl_filename)
137
138
139 class IdlCompilerDictionaryImpl(IdlCompiler):
140     def __init__(self, *args, **kwargs):
141         IdlCompiler.__init__(self, *args, **kwargs)
142         self.code_generator = CodeGeneratorDictionaryImpl(
143             self.interfaces_info, self.cache_directory, self.output_directory)
144
145     def compile_file(self, idl_filename):
146         self.compile_and_write(idl_filename)
147
148
149 def generate_bindings(options, input_filename):
150     idl_compiler = IdlCompilerV8(
151         options.output_directory,
152         cache_directory=options.cache_directory,
153         interfaces_info_filename=options.interfaces_info_file,
154         only_if_changed=options.write_file_only_if_changed,
155         target_component=options.target_component)
156     idl_compiler.compile_file(input_filename)
157
158
159 def generate_dictionary_impl(options, input_filename):
160     idl_compiler = IdlCompilerDictionaryImpl(
161         options.impl_output_directory,
162         cache_directory=options.cache_directory,
163         interfaces_info_filename=options.interfaces_info_file,
164         only_if_changed=options.write_file_only_if_changed)
165
166     idl_filenames = read_idl_files_list_from_file(input_filename)
167     for idl_filename in idl_filenames:
168         idl_compiler.compile_file(idl_filename)
169
170
171 def generate_union_type_containers(options):
172     if not (options.interfaces_info_file and options.component_info_file):
173         raise Exception('Interfaces info is required to generate '
174                         'union types containers')
175     with open(options.interfaces_info_file) as interfaces_info_file:
176         interfaces_info = pickle.load(interfaces_info_file)
177     with open(options.component_info_file) as component_info_file:
178         component_info = pickle.load(component_info_file)
179     generator = CodeGeneratorUnionType(
180         interfaces_info,
181         options.cache_directory,
182         options.output_directory,
183         options.target_component)
184     output_code_list = generator.generate_code(component_info['union_types'])
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
189 def main():
190     options, input_filename = parse_options()
191     if options.generate_impl:
192         # |input_filename| should be a file which contains a list of IDL
193         # dictionary paths.
194         generate_dictionary_impl(options, input_filename)
195         generate_union_type_containers(options)
196     else:
197         # |input_filename| should be a path of an IDL file.
198         generate_bindings(options, input_filename)
199
200
201 if __name__ == '__main__':
202     sys.exit(main())