| 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 import os.path | |
| 37 import cPickle as pickle | |
| 38 | |
| 39 from idl_reader import IdlReader | |
| 40 from utilities import write_file | |
| 41 | |
| 42 | |
| 43 def idl_filename_to_interface_name(idl_filename): | |
| 44 basename = os.path.basename(idl_filename) | |
| 45 interface_name, _ = os.path.splitext(basename) | |
| 46 return interface_name | |
| 47 | |
| 48 | |
| 49 class IdlCompiler(object): | |
| 50 """Abstract Base Class for IDL compilers. | |
| 51 | |
| 52 In concrete classes: | |
| 53 * self.code_generator must be set, implementing generate_code() | |
| 54 (returning a list of output code), and | |
| 55 * compile_file() must be implemented (handling output filenames). | |
| 56 """ | |
| 57 __metaclass__ = abc.ABCMeta | |
| 58 | |
| 59 def __init__(self, output_directory, code_generator=None, | |
| 60 interfaces_info=None, interfaces_info_filename='', | |
| 61 only_if_changed=False): | |
| 62 """ | |
| 63 Args: | |
| 64 interfaces_info: | |
| 65 interfaces_info dict | |
| 66 (avoids auxiliary file in run-bindings-tests) | |
| 67 interfaces_info_file: filename of pickled interfaces_info | |
| 68 """ | |
| 69 self.code_generator = code_generator | |
| 70 if interfaces_info_filename: | |
| 71 with open(interfaces_info_filename) as interfaces_info_file: | |
| 72 interfaces_info = pickle.load(interfaces_info_file) | |
| 73 self.interfaces_info = interfaces_info | |
| 74 | |
| 75 self.only_if_changed = only_if_changed | |
| 76 self.output_directory = output_directory | |
| 77 self.reader = IdlReader(interfaces_info, output_directory) | |
| 78 | |
| 79 def compile_and_write(self, idl_filename, output_filenames): | |
| 80 interface_name = idl_filename_to_interface_name(idl_filename) | |
| 81 idl_pickle_filename = os.path.join(self.output_directory, | |
| 82 '%s_globals.pickle' % interface_name) | |
| 83 definitions = self.reader.read_idl_definitions(idl_filename) | |
| 84 output_code_list = self.code_generator.generate_code(definitions, | |
| 85 interface_name, | |
| 86 idl_pickle_filename
, | |
| 87 self.only_if_change
d) | |
| 88 | |
| 89 for output_code, output_filename in zip(output_code_list, output_filenam
es): | |
| 90 write_file(output_code, output_filename, self.only_if_changed) | |
| 91 | |
| 92 def generate_global_and_write(self, global_entries, output_filenames): | |
| 93 output_code_list = self.code_generator.generate_globals(global_entries) | |
| 94 for output_code, output_filename in zip(output_code_list, output_filenam
es): | |
| 95 write_file(output_code, output_filename, self.only_if_changed) | |
| 96 | |
| 97 def generate_dart_blink_and_write(self, global_entries, output_filename): | |
| 98 output_code = self.code_generator.generate_dart_blink(global_entries) | |
| 99 write_file(output_code, output_filename, self.only_if_changed) | |
| 100 | |
| 101 @abc.abstractmethod | |
| 102 def compile_file(self, idl_filename): | |
| 103 pass | |
| OLD | NEW |