OLD | NEW |
| (Empty) |
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 C++ bindings (.h and .cpp files) for Dart:HTML. | |
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 | |
40 from idl_reader import IdlReader | |
41 from utilities import write_file | |
42 | |
43 | |
44 # TODO(terry): Temporary whitelist of IDL files to skip code generating. e.g., | |
45 # adding 'Animation.idl' to this list will skip that IDL file. | |
46 SKIP_IDL_FILES = [''] | |
47 | |
48 | |
49 def parse_options(): | |
50 parser = OptionParser() | |
51 parser.add_option('--idl-attributes-file', | |
52 help="location of bindings/IDLExtendedAttributes.txt") | |
53 parser.add_option('--output-directory') | |
54 parser.add_option('--interfaces-info-file') | |
55 parser.add_option('--write-file-only-if-changed', type='int') | |
56 # ensure output comes last, so command line easy to parse via regexes | |
57 parser.disable_interspersed_args() | |
58 | |
59 options, args = parser.parse_args() | |
60 if options.output_directory is None: | |
61 parser.error('Must specify output directory using --output-directory.') | |
62 options.write_file_only_if_changed = bool(options.write_file_only_if_changed
) | |
63 if len(args) != 1: | |
64 parser.error('Must specify exactly 1 input file as argument, but %d give
n.' % len(args)) | |
65 idl_filename = os.path.realpath(args[0]) | |
66 return options, idl_filename | |
67 | |
68 | |
69 def idl_filename_to_interface_name(idl_filename): | |
70 basename = os.path.basename(idl_filename) | |
71 interface_name, _ = os.path.splitext(basename) | |
72 return interface_name | |
73 | |
74 | |
75 class IdlCompiler(object): | |
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, code_generator=None, | |
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 = code_generator | |
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 | |
101 self.only_if_changed = only_if_changed | |
102 self.output_directory = output_directory | |
103 self.reader = IdlReader(interfaces_info, output_directory, True) | |
104 | |
105 def compile_and_write(self, idl_filename, output_filenames): | |
106 # Only compile the IDL file and return the AST. | |
107 definitions = self.reader.read_idl_definitions(idl_filename) | |
108 return definitions | |
109 | |
110 def generate_global_and_write(self, output_filenames): | |
111 pass | |
112 | |
113 @abc.abstractmethod | |
114 def compile_file(self, idl_filename): | |
115 pass | |
OLD | NEW |