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