| OLD | NEW |
| (Empty) |
| 1 # Copyright (C) 2013 Google Inc. All rights reserved. | |
| 2 # | |
| 3 # Redistribution and use in source and binary forms, with or without | |
| 4 # modification, are permitted provided that the following conditions are | |
| 5 # met: | |
| 6 # | |
| 7 # * Redistributions of source code must retain the above copyright | |
| 8 # notice, this list of conditions and the following disclaimer. | |
| 9 # * Redistributions in binary form must reproduce the above | |
| 10 # copyright notice, this list of conditions and the following disclaimer | |
| 11 # in the documentation and/or other materials provided with the | |
| 12 # distribution. | |
| 13 # * Neither the name of Google Inc. nor the names of its | |
| 14 # contributors may be used to endorse or promote products derived from | |
| 15 # this software without specific prior written permission. | |
| 16 # | |
| 17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 18 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 19 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| 20 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
| 21 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
| 22 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
| 23 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
| 24 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
| 25 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
| 27 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 28 | |
| 29 """Generate Blink V8 bindings (.h and .cpp files). | |
| 30 | |
| 31 Input: An object of class IdlDefinitions, containing an IDL interface X | |
| 32 Output: V8X.h and V8X.cpp | |
| 33 """ | |
| 34 | |
| 35 import os | |
| 36 import posixpath | |
| 37 import sys | |
| 38 | |
| 39 # jinja2 is in chromium's third_party directory. | |
| 40 module_path, module_name = os.path.split(__file__) | |
| 41 third_party = os.path.join(module_path, os.pardir, os.pardir, os.pardir, os.pard
ir) | |
| 42 sys.path.append(third_party) | |
| 43 import jinja2 | |
| 44 | |
| 45 templates_dir = os.path.join(module_path, os.pardir, 'templates') | |
| 46 | |
| 47 import v8_callback_interface | |
| 48 import v8_interface | |
| 49 from v8_utilities import cpp_class_name, generate_conditional_string, v8_class_n
ame | |
| 50 | |
| 51 | |
| 52 class CodeGeneratorV8: | |
| 53 def __init__(self, definitions, interface_name, output_directory, relative_d
ir_posix, idl_directories, verbose=False): | |
| 54 self.idl_definitions = definitions | |
| 55 self.interface_name = interface_name | |
| 56 self.idl_directories = idl_directories | |
| 57 self.output_directory = output_directory | |
| 58 self.verbose = verbose | |
| 59 # FIXME: remove definitions check when remove write_dummy_header_and_cpp | |
| 60 if not definitions: | |
| 61 return | |
| 62 try: | |
| 63 self.interface = definitions.interfaces[interface_name] | |
| 64 except KeyError: | |
| 65 raise Exception('%s not in IDL definitions' % interface_name) | |
| 66 if self.interface.is_callback: | |
| 67 header_template_filename = 'callback_interface.h' | |
| 68 cpp_template_filename = 'callback_interface.cpp' | |
| 69 self.generate_contents = v8_callback_interface.generate_callback_int
erface | |
| 70 else: | |
| 71 header_template_filename = 'interface.h' | |
| 72 cpp_template_filename = 'interface.cpp' | |
| 73 self.generate_contents = v8_interface.generate_interface | |
| 74 # FIXME: update to Jinja 2.7 and use: | |
| 75 # keep_trailing_newline=True, # newline-terminate generated files | |
| 76 # lstrip_blocks=True, # so can indent control flow tags | |
| 77 jinja_env = jinja2.Environment( | |
| 78 loader=jinja2.FileSystemLoader(templates_dir), | |
| 79 trim_blocks=True) | |
| 80 self.header_template = jinja_env.get_template(header_template_filename) | |
| 81 self.cpp_template = jinja_env.get_template(cpp_template_filename) | |
| 82 | |
| 83 class_name = cpp_class_name(self.interface) | |
| 84 self.include_for_cpp_class = posixpath.join(relative_dir_posix, class_na
me + '.h') | |
| 85 | |
| 86 def write_dummy_header_and_cpp(self): | |
| 87 # FIXME: fix GYP so these files aren't needed and remove this method | |
| 88 target_interface_name = self.interface_name | |
| 89 header_basename = 'V8%s.h' % target_interface_name | |
| 90 cpp_basename = 'V8%s.cpp' % target_interface_name | |
| 91 contents = """/* | |
| 92 This file is generated just to tell build scripts that {header_basename} and | |
| 93 {cpp_basename} are created for {target_interface_name}.idl, and thus | |
| 94 prevent the build scripts from trying to generate {header_basename} and | |
| 95 {cpp_basename} at every build. This file must not be tried to compile. | |
| 96 */ | |
| 97 """.format(**locals()) | |
| 98 self.write_file(header_basename, contents) | |
| 99 self.write_file(cpp_basename, contents) | |
| 100 | |
| 101 def write_header_and_cpp(self): | |
| 102 interface = self.interface | |
| 103 template_contents = self.generate_contents(interface) | |
| 104 template_contents['conditional_string'] = generate_conditional_string(in
terface) | |
| 105 template_contents['header_includes'].add(self.include_for_cpp_class) | |
| 106 template_contents['header_includes'] = sorted(template_contents['header_
includes']) | |
| 107 template_contents['cpp_includes'] = sorted(template_contents['cpp_includ
es']) | |
| 108 | |
| 109 header_basename = v8_class_name(interface) + '.h' | |
| 110 header_file_text = self.header_template.render(template_contents) | |
| 111 self.write_file(header_basename, header_file_text) | |
| 112 | |
| 113 cpp_basename = v8_class_name(interface) + '.cpp' | |
| 114 cpp_file_text = self.cpp_template.render(template_contents) | |
| 115 self.write_file(cpp_basename, cpp_file_text) | |
| 116 | |
| 117 def write_file(self, basename, file_text): | |
| 118 filename = os.path.join(self.output_directory, basename) | |
| 119 with open(filename, 'w') as output_file: | |
| 120 output_file.write(file_text) | |
| OLD | NEW |