| OLD | NEW |
| (Empty) | |
| 1 #!/usr/bin/python |
| 2 # Copyright (C) 2014 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 Dart bindings (.h and .cpp files). |
| 31 |
| 32 Design doc: ?????? |
| 33 """ |
| 34 |
| 35 from optparse import OptionParser |
| 36 import os |
| 37 import sys |
| 38 |
| 39 from dart_compiler import IdlCompiler |
| 40 from code_generator_dart import CodeGeneratorDart |
| 41 |
| 42 |
| 43 def parse_options(): |
| 44 parser = OptionParser() |
| 45 parser.add_option('--output-directory') |
| 46 parser.add_option('--interfaces-info-file') |
| 47 parser.add_option('--write-file-only-if-changed', type='int', default='1') |
| 48 parser.add_option('--generate-dart-blink', |
| 49 action='append', |
| 50 type='string', |
| 51 dest='blink_global_entries', |
| 52 nargs=2, |
| 53 help="Pickle file directory and idl file list (dart:blink)
") |
| 54 |
| 55 parser.add_option('--generate-globals', |
| 56 action='append', |
| 57 type='string', |
| 58 dest='global_entries', |
| 59 nargs=2, |
| 60 help="Pickle file directory and idl file list (global clas
s table)") |
| 61 |
| 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 bool(options.global_entries) or bool(options.blink_global_entries): |
| 70 return options, None |
| 71 if len(args) != 1: |
| 72 parser.error('Must specify exactly 1 input file as argument, but %d give
n.' % len(args)) |
| 73 filename = os.path.realpath(args[0]) |
| 74 return options, filename |
| 75 |
| 76 |
| 77 def idl_filename_to_interface_name(idl_filename): |
| 78 basename = os.path.basename(idl_filename) |
| 79 interface_name, _ = os.path.splitext(basename) |
| 80 return interface_name |
| 81 |
| 82 |
| 83 class IdlCompilerDart(IdlCompiler): |
| 84 def __init__(self, *args, **kwargs): |
| 85 IdlCompiler.__init__(self, *args, **kwargs) |
| 86 |
| 87 interfaces_info = self.interfaces_info |
| 88 self.output_directory = self.output_directory |
| 89 |
| 90 self.code_generator = CodeGeneratorDart(interfaces_info, self.output_dir
ectory) |
| 91 |
| 92 def compile_file(self, idl_filename): |
| 93 interface_name = idl_filename_to_interface_name(idl_filename) |
| 94 header_filename = os.path.join(self.output_directory, |
| 95 'Dart%s.h' % interface_name) |
| 96 cpp_filename = os.path.join(self.output_directory, |
| 97 'Dart%s.cpp' % interface_name) |
| 98 dart_filename = os.path.join(self.output_directory, |
| 99 '%s.dart' % interface_name) |
| 100 output_paths = (header_filename, cpp_filename, dart_filename) |
| 101 self.compile_and_write(idl_filename, output_paths) |
| 102 |
| 103 def generate_global(self, global_entries): |
| 104 expanded_global_entries = [] |
| 105 for (directory, file_list_file) in global_entries: |
| 106 with open(file_list_file) as input_file: |
| 107 idl_file_list = sorted([line.rstrip('\n') |
| 108 for line in input_file]) |
| 109 expanded_global_entries.append((directory, idl_file_list)) |
| 110 global_header_filename = os.path.join(self.output_directory, 'DartGlobal
.h') |
| 111 global_cpp_filename = os.path.join(self.output_directory, 'DartGlobal.cp
p') |
| 112 self.generate_global_and_write(expanded_global_entries, |
| 113 (global_header_filename, global_cpp_filen
ame)) |
| 114 |
| 115 def generate_dart_blink(self, global_entries): |
| 116 global_dart_blink_filename = os.path.join(self.output_directory, |
| 117 'sky_core.dart') |
| 118 expanded_global_entries = [] |
| 119 for (directory, file_list_file) in global_entries: |
| 120 with open(file_list_file) as input_file: |
| 121 idl_file_list = sorted([line.rstrip('\n') |
| 122 for line in input_file]) |
| 123 expanded_global_entries.append((directory, idl_file_list)) |
| 124 self.generate_dart_blink_and_write(expanded_global_entries, |
| 125 global_dart_blink_filename) |
| 126 |
| 127 |
| 128 def main(): |
| 129 options, filename = parse_options() |
| 130 idl_compiler = IdlCompilerDart(options.output_directory, |
| 131 interfaces_info_filename=options.interfaces_i
nfo_file, |
| 132 only_if_changed=options.write_file_only_if_ch
anged) |
| 133 if bool(options.global_entries): |
| 134 idl_compiler.generate_global(options.global_entries) |
| 135 elif bool(options.blink_global_entries): |
| 136 idl_compiler.generate_dart_blink(options.blink_global_entries) |
| 137 else: |
| 138 idl_compiler.compile_file(filename) |
| 139 |
| 140 |
| 141 if __name__ == '__main__': |
| 142 sys.exit(main()) |
| OLD | NEW |