| 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 # Insert at front to override system libraries, and after path[0] == script dir | |
| 43 sys.path.insert(1, third_party) | |
| 44 import jinja2 | |
| 45 | |
| 46 templates_dir = os.path.join(module_path, os.pardir, 'templates') | |
| 47 | |
| 48 import v8_callback_interface | |
| 49 import v8_interface | |
| 50 from v8_utilities import cpp_class_name, generate_conditional_string, v8_class_n
ame | |
| 51 | |
| 52 | |
| 53 class CodeGeneratorV8: | |
| 54 def __init__(self, definitions, interface_name, output_directory, relative_d
ir_posix, idl_directories, verbose=False): | |
| 55 self.idl_definitions = definitions | |
| 56 self.interface_name = interface_name | |
| 57 self.idl_directories = idl_directories | |
| 58 self.output_directory = output_directory | |
| 59 self.verbose = verbose | |
| 60 # FIXME: remove definitions check when remove write_dummy_header_and_cpp | |
| 61 if not definitions: | |
| 62 return | |
| 63 try: | |
| 64 self.interface = definitions.interfaces[interface_name] | |
| 65 except KeyError: | |
| 66 raise Exception('%s not in IDL definitions' % interface_name) | |
| 67 if self.interface.is_callback: | |
| 68 header_template_filename = 'callback_interface.h' | |
| 69 cpp_template_filename = 'callback_interface.cpp' | |
| 70 self.generate_contents = v8_callback_interface.generate_callback_int
erface | |
| 71 else: | |
| 72 header_template_filename = 'interface.h' | |
| 73 cpp_template_filename = 'interface.cpp' | |
| 74 self.generate_contents = v8_interface.generate_interface | |
| 75 # FIXME: update to Jinja 2.7 and use: | |
| 76 # keep_trailing_newline=True, # newline-terminate generated files | |
| 77 # lstrip_blocks=True, # so can indent control flow tags | |
| 78 jinja_env = jinja2.Environment( | |
| 79 loader=jinja2.FileSystemLoader(templates_dir), | |
| 80 trim_blocks=True) | |
| 81 self.header_template = jinja_env.get_template(header_template_filename) | |
| 82 self.cpp_template = jinja_env.get_template(cpp_template_filename) | |
| 83 | |
| 84 class_name = cpp_class_name(self.interface) | |
| 85 self.include_for_cpp_class = posixpath.join(relative_dir_posix, class_na
me + '.h') | |
| 86 | |
| 87 def write_dummy_header_and_cpp(self): | |
| 88 # FIXME: fix GYP so these files aren't needed and remove this method | |
| 89 target_interface_name = self.interface_name | |
| 90 header_basename = 'V8%s.h' % target_interface_name | |
| 91 cpp_basename = 'V8%s.cpp' % target_interface_name | |
| 92 contents = """/* | |
| 93 This file is generated just to tell build scripts that {header_basename} and | |
| 94 {cpp_basename} are created for {target_interface_name}.idl, and thus | |
| 95 prevent the build scripts from trying to generate {header_basename} and | |
| 96 {cpp_basename} at every build. This file must not be tried to compile. | |
| 97 */ | |
| 98 """.format(**locals()) | |
| 99 self.write_file(header_basename, contents) | |
| 100 self.write_file(cpp_basename, contents) | |
| 101 | |
| 102 def write_header_and_cpp(self): | |
| 103 interface = self.interface | |
| 104 template_contents = self.generate_contents(interface) | |
| 105 template_contents['conditional_string'] = generate_conditional_string(in
terface) | |
| 106 template_contents['header_includes'].add(self.include_for_cpp_class) | |
| 107 template_contents['header_includes'] = sorted(template_contents['header_
includes']) | |
| 108 template_contents['cpp_includes'] = sorted(template_contents['cpp_includ
es']) | |
| 109 | |
| 110 header_basename = v8_class_name(interface) + '.h' | |
| 111 header_file_text = self.header_template.render(template_contents) | |
| 112 self.write_file(header_basename, header_file_text) | |
| 113 | |
| 114 cpp_basename = v8_class_name(interface) + '.cpp' | |
| 115 cpp_file_text = self.cpp_template.render(template_contents) | |
| 116 self.write_file(cpp_basename, cpp_file_text) | |
| 117 | |
| 118 def write_file(self, basename, file_text): | |
| 119 filename = os.path.join(self.output_directory, basename) | |
| 120 with open(filename, 'w') as output_file: | |
| 121 output_file.write(file_text) | |
| OLD | NEW |