Index: Source/bindings/scripts/code_generator_v8.py |
diff --git a/Source/bindings/scripts/code_generator_v8.py b/Source/bindings/scripts/code_generator_v8.py |
new file mode 100644 |
index 0000000000000000000000000000000000000000..72144485d0a77187b5e9eea0a721a45287d382c2 |
--- /dev/null |
+++ b/Source/bindings/scripts/code_generator_v8.py |
@@ -0,0 +1,151 @@ |
+# Copyright (C) 2013 Google Inc. All rights reserved. |
+# |
+# Redistribution and use in source and binary forms, with or without |
+# modification, are permitted provided that the following conditions are |
+# met: |
+# |
+# * Redistributions of source code must retain the above copyright |
+# notice, this list of conditions and the following disclaimer. |
+# * Redistributions in binary form must reproduce the above |
+# copyright notice, this list of conditions and the following disclaimer |
+# in the documentation and/or other materials provided with the |
+# distribution. |
+# * Neither the name of Google Inc. nor the names of its |
+# contributors may be used to endorse or promote products derived from |
+# this software without specific prior written permission. |
+# |
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
+# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
+# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
+# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
+# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
+# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
+# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
+# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
+ |
+import os.path |
+ |
+ |
+def generate_empty_header_and_cpp(target_interface_name, output_headers_directory, output_directory): |
+ header_basename = 'V8%s.h' % target_interface_name |
+ cpp_basename = 'V8%s.cpp' % target_interface_name |
+ header_fullname = os.path.join(output_headers_directory, header_basename) |
+ cpp_fullname = os.path.join(output_directory, cpp_basename) |
+ contents = """/* |
+ This file is generated just to tell build scripts that {header_basename} and |
+ {cpp_basename} are created for {target_interface_name}.idl, and thus |
+ prevent the build scripts from trying to generate {header_basename} and |
+ {cpp_basename} at every build. This file must not be tried to compile. |
+*/ |
+""".format(**locals) |
+ # Better wording: |
+ # contents = """/* |
+ # This file is generated to tell build scripts that %(header_basename) and |
+ # %(cpp_basename) have been created for %(target_interface_name).idl, and thus |
+ # prevent the build scripts from trying to generate them on every build. |
+ # This file must not be compiled. |
+ # */ |
+ # """ % locals() |
+ with open(header_fullname, 'w') as header_file: |
+ header_file.write(contents) |
+ with open(cpp_fullname, 'w') as cpp_file: |
+ cpp_file.write(contents) |
+ |
+ |
+def write_file(filename, contents, only_if_changed): |
+ if only_if_changed and os.path.isfile(filename): |
+ with open(filename) as existing_file: |
+ old_contents = ''.join(list(existing_file)) |
+ if contents == old_contents: |
+ return |
+ with open(filename, 'w') as new_file: |
+ new_file.write(contents) |
+ |
+ |
+class Block(): |
+ """Block of code, separated as header + contents + footer. |
+ |
+ header, contents, and footer are each a list, |
+ consisting of strings (separate lines, embedded newlines ok) and Blocks. |
+ """ |
+ def __init__(self, name=None, header=None, footer=None): |
+ self.name = name or 'Anonymous block' |
+ self.header = [] |
+ self.contents = [] |
+ self.footer = [] |
+ if header is not None: |
+ self.header.append(header) |
+ if footer is not None: |
+ self.footer.append(footer) |
+ |
+ def add_header(self, header=None): |
+ if header is not None: |
+ self.header.append(header) |
+ |
+ def add(self, content=None): |
+ if content is not None: |
+ self.contents.append(content) |
+ |
+ def add_footer(self, footer=None): |
+ if footer is not None: |
+ self.footer.append(footer) |
+ |
+ def __str__(self): |
+ # FIXME: fill in! |
+ return '' |
+ |
+ def verbose_string(self): |
+ # FIXME: fill in! |
+ return ''.join(self.header) |
+ |
+ |
+class CodeGenerator(): |
+ def __init__(self, document, dependent_idl_files, options): |
+ self.document = document |
+ self.dependent_idl_files = dependent_idl_files |
+ self.verbose = options.verbose |
+ self.write_file_only_if_changed = options.write_file_only_if_changed |
+ |
+ self.header = {} |
+ self.implementation = {} |
+ self.header_includes = {} |
+ self.implied_includes = set() |
+ |
+ def write_interfaces(self, output_directory, output_headers_directory): |
+ interface_list = self.document.interfaces |
+ for interface in interface_list: |
+ if self.verbose: |
+ print 'Generating bindings code for IDL interface "%s"...' % interface['name'] |
+ self.generate_interface(interface) |
+ self.write_data(interface, output_directory, output_headers_directory) |
+ |
+ def generate_interface(self, interface): |
+ # FIXME: fill in! |
+ self.header['root'] = Block('ROOT') |
+ self.implementation['root'] = Block('ROOT') |
+ self.implementation['includes'] = Block('Includes') |
+ self.make_implied_includes_explicit(interface) |
+ |
+ def make_implied_includes_explicit(self, interface): |
+ """Make implied includes into explicit include lines. |
+ |
+ Clears implied_includes, as now explicit.""" |
+ # FIXME: do not treat main header specially |
+ main_include = 'V8%s.h' % interface.name |
+ for include in sorted(self.implied_includes): |
+ if include == main_include: |
+ continue |
+ self.implementation['includes'].add('#include "%s"\n' % include) |
+ if not interface.is_callback: |
+ self.implementation['includes'].add('\n') |
+ self.implied_includes = set() |
+ |
+ def write_data(self, interface, output_directory, output_headers_directory): |
+ name = interface.name |
+ header_filename = '%s/V8%s.h' % (output_headers_directory, name) |
+ cpp_filename = '%s/V8%s.cpp' % (output_directory, name) |
+ write_file(header_filename, str(self.header['root']), self.write_file_only_if_changed) |
+ write_file(cpp_filename, str(self.implementation['root']), self.write_file_only_if_changed) |