Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / bindings / scripts / unstable / 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 FIXME: Not currently used in build.
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.
35 Until then, please work on the Perl IDL compiler.
36 For details, see bug http://crbug.com/239771
37 """
38
39 import optparse
40 import os
41 import cPickle as pickle
42 import sys
43
44 import code_generator_v8
45 import idl_reader
46
47 def parse_options():
48     parser = optparse.OptionParser()
49     # FIXME: The --dump-json-and-pickle option is only for debugging and will
50     # be removed once we complete migrating all IDL files from the Perl flow to
51     # the Python flow.
52     parser.add_option('--dump-json-and-pickle', action='store_true', default=False)
53     parser.add_option('--idl-attributes-file')
54     parser.add_option('--output-directory')
55     parser.add_option('--interfaces-info-file')
56     parser.add_option('--write-file-only-if-changed', type='int')
57     # ensure output comes last, so command line easy to parse via regexes
58     parser.disable_interspersed_args()
59
60     options, args = parser.parse_args()
61     if options.output_directory is None:
62         parser.error('Must specify output directory using --output-directory.')
63     if len(args) != 1:
64         parser.error('Must specify exactly 1 input file as argument, but %d given.' % len(args))
65     options.idl_filename = os.path.realpath(args[0])
66     return options
67
68
69 def write_json_and_pickle(definitions, interface_name, output_directory):
70     json_string = definitions.to_json()
71     json_basename = interface_name + '.json'
72     json_filename = os.path.join(output_directory, json_basename)
73     with open(json_filename, 'w') as json_file:
74         json_file.write(json_string)
75     pickle_basename = interface_name + '.pkl'
76     pickle_filename = os.path.join(output_directory, pickle_basename)
77     with open(pickle_filename, 'wb') as pickle_file:
78         pickle.dump(definitions, pickle_file)
79
80
81 def main():
82     options = parse_options()
83     idl_filename = options.idl_filename
84     basename = os.path.basename(idl_filename)
85     interface_name, _ = os.path.splitext(basename)
86     output_directory = options.output_directory
87
88     interfaces_info_filename = options.interfaces_info_file
89     if interfaces_info_filename:
90         with open(interfaces_info_filename) as interfaces_info_file:
91             interfaces_info = pickle.load(interfaces_info_file)
92     else:
93         interfaces_info = None
94
95     reader = idl_reader.IdlReader(interfaces_info, options.idl_attributes_file, output_directory)
96     definitions = reader.read_idl_definitions(idl_filename)
97     if options.dump_json_and_pickle:
98         write_json_and_pickle(definitions, interface_name, output_directory)
99         return
100     code_generator_v8.write_header_and_cpp(definitions, interface_name, interfaces_info, options.output_directory)
101
102
103 if __name__ == '__main__':
104     sys.exit(main())