• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
31If run itself, caches Jinja templates (and creates dummy file for build,
32since cache filenames are unpredictable and opaque).
33
34This module is *not* concurrency-safe without care: bytecode caching creates
35a race condition on cache *write* (crashes if one process tries to read a
36partially-written cache). However, if you pre-cache the templates (by running
37the module itself), then you can parallelize compiling individual files, since
38cache *reading* is safe.
39
40Input: An object of class IdlDefinitions, containing an IDL interface X
41Output: V8X.h and V8X.cpp
42
43Design doc: http://www.chromium.org/developers/design-documents/idl-compiler
44"""
45
46import os
47import posixpath
48import re
49import sys
50
51# Path handling for libraries and templates
52# Paths have to be normalized because Jinja uses the exact template path to
53# determine the hash used in the cache filename, and we need a pre-caching step
54# to be concurrency-safe. Use absolute path because __file__ is absolute if
55# module is imported, and relative if executed directly.
56# If paths differ between pre-caching and individual file compilation, the cache
57# is regenerated, which causes a race condition and breaks concurrent build,
58# since some compile processes will try to read the partially written cache.
59module_path, module_filename = os.path.split(os.path.realpath(__file__))
60third_party_dir = os.path.normpath(os.path.join(
61    module_path, os.pardir, os.pardir, os.pardir, os.pardir))
62templates_dir = os.path.normpath(os.path.join(
63    module_path, os.pardir, 'templates'))
64# Make sure extension is .py, not .pyc or .pyo, so doesn't depend on caching
65module_pyname = os.path.splitext(module_filename)[0] + '.py'
66
67# jinja2 is in chromium's third_party directory.
68# Insert at 1 so at front to override system libraries, and
69# after path[0] == invoking script dir
70sys.path.insert(1, third_party_dir)
71import jinja2
72
73import idl_types
74from idl_types import IdlType
75import v8_callback_interface
76from v8_globals import includes, interfaces
77import v8_interface
78import v8_types
79from v8_utilities import capitalize, cpp_name, conditional_string, v8_class_name
80
81
82class CodeGeneratorV8(object):
83    def __init__(self, interfaces_info, cache_dir):
84        interfaces_info = interfaces_info or {}
85        self.interfaces_info = interfaces_info
86        self.jinja_env = initialize_jinja_env(cache_dir)
87
88        # Set global type info
89        idl_types.set_ancestors(dict(
90            (interface_name, interface_info['ancestors'])
91            for interface_name, interface_info in interfaces_info.iteritems()
92            if interface_info['ancestors']))
93        IdlType.set_callback_interfaces(set(
94            interface_name
95            for interface_name, interface_info in interfaces_info.iteritems()
96            if interface_info['is_callback_interface']))
97        IdlType.set_implemented_as_interfaces(dict(
98            (interface_name, interface_info['implemented_as'])
99            for interface_name, interface_info in interfaces_info.iteritems()
100            if interface_info['implemented_as']))
101        IdlType.set_garbage_collected_types(set(
102            interface_name
103            for interface_name, interface_info in interfaces_info.iteritems()
104            if 'GarbageCollected' in interface_info['inherited_extended_attributes']))
105        IdlType.set_will_be_garbage_collected_types(set(
106            interface_name
107            for interface_name, interface_info in interfaces_info.iteritems()
108            if 'WillBeGarbageCollected' in interface_info['inherited_extended_attributes']))
109        v8_types.set_component_dirs(dict(
110            (interface_name, interface_info['component_dir'])
111            for interface_name, interface_info in interfaces_info.iteritems()))
112
113    def generate_code(self, definitions, interface_name):
114        """Returns .h/.cpp code as (header_text, cpp_text)."""
115        try:
116            interface = definitions.interfaces[interface_name]
117        except KeyError:
118            raise Exception('%s not in IDL definitions' % interface_name)
119
120        # Store other interfaces for introspection
121        interfaces.update(definitions.interfaces)
122
123        # Set local type info
124        IdlType.set_callback_functions(definitions.callback_functions.keys())
125        IdlType.set_enums((enum.name, enum.values)
126                          for enum in definitions.enumerations.values())
127
128        # Select appropriate Jinja template and contents function
129        if interface.is_callback:
130            header_template_filename = 'callback_interface.h'
131            cpp_template_filename = 'callback_interface.cpp'
132            generate_contents = v8_callback_interface.generate_callback_interface
133        else:
134            header_template_filename = 'interface.h'
135            cpp_template_filename = 'interface.cpp'
136            generate_contents = v8_interface.generate_interface
137        header_template = self.jinja_env.get_template(header_template_filename)
138        cpp_template = self.jinja_env.get_template(cpp_template_filename)
139
140        # Generate contents (input parameters for Jinja)
141        template_contents = generate_contents(interface)
142        template_contents['code_generator'] = module_pyname
143
144        # Add includes for interface itself and any dependencies
145        interface_info = self.interfaces_info[interface_name]
146        template_contents['header_includes'].add(interface_info['include_path'])
147        template_contents['header_includes'] = sorted(template_contents['header_includes'])
148        includes.update(interface_info.get('dependencies_include_paths', []))
149        template_contents['cpp_includes'] = sorted(includes)
150
151        # Render Jinja templates
152        header_text = header_template.render(template_contents)
153        cpp_text = cpp_template.render(template_contents)
154        return header_text, cpp_text
155
156
157def initialize_jinja_env(cache_dir):
158    jinja_env = jinja2.Environment(
159        loader=jinja2.FileSystemLoader(templates_dir),
160        # Bytecode cache is not concurrency-safe unless pre-cached:
161        # if pre-cached this is read-only, but writing creates a race condition.
162        bytecode_cache=jinja2.FileSystemBytecodeCache(cache_dir),
163        keep_trailing_newline=True,  # newline-terminate generated files
164        lstrip_blocks=True,  # so can indent control flow tags
165        trim_blocks=True)
166    jinja_env.filters.update({
167        'blink_capitalize': capitalize,
168        'conditional': conditional_if_endif,
169        'runtime_enabled': runtime_enabled_if,
170        })
171    return jinja_env
172
173
174# [Conditional]
175def conditional_if_endif(code, conditional_string):
176    # Jinja2 filter to generate if/endif directive blocks
177    if not conditional_string:
178        return code
179    return ('#if %s\n' % conditional_string +
180            code +
181            '#endif // %s\n' % conditional_string)
182
183
184# [RuntimeEnabled]
185def runtime_enabled_if(code, runtime_enabled_function_name):
186    if not runtime_enabled_function_name:
187        return code
188    # Indent if statement to level of original code
189    indent = re.match(' *', code).group(0)
190    return ('%sif (%s()) {\n' % (indent, runtime_enabled_function_name) +
191            '    %s\n' % '\n    '.join(code.splitlines()) +
192            '%s}\n' % indent)
193
194
195################################################################################
196
197def main(argv):
198    # If file itself executed, cache templates
199    try:
200        cache_dir = argv[1]
201        dummy_filename = argv[2]
202    except IndexError as err:
203        print 'Usage: %s CACHE_DIR DUMMY_FILENAME' % argv[0]
204        return 1
205
206    # Cache templates
207    jinja_env = initialize_jinja_env(cache_dir)
208    template_filenames = [filename for filename in os.listdir(templates_dir)
209                          # Skip .svn, directories, etc.
210                          if filename.endswith(('.cpp', '.h'))]
211    for template_filename in template_filenames:
212        jinja_env.get_template(template_filename)
213
214    # Create a dummy file as output for the build system,
215    # since filenames of individual cache files are unpredictable and opaque
216    # (they are hashes of the template path, which varies based on environment)
217    with open(dummy_filename, 'w') as dummy_file:
218        pass  # |open| creates or touches the file
219
220
221if __name__ == '__main__':
222    sys.exit(main(sys.argv))
223