1#!/usr/bin/python
2#
3# Copyright (C) 2009 Google Inc. All rights reserved.
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions are
7# met:
8#
9#     * Redistributions of source code must retain the above copyright
10# notice, this list of conditions and the following disclaimer.
11#     * Redistributions in binary form must reproduce the above
12# copyright notice, this list of conditions and the following disclaimer
13# in the documentation and/or other materials provided with the
14# distribution.
15#     * Neither the name of Google Inc. nor the names of its
16# contributors may be used to endorse or promote products derived from
17# this software without specific prior written permission.
18#
19# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30#
31# Copyright (c) 2009 The Chromium Authors. All rights reserved.
32# Use of this source code is governed by a BSD-style license that can be
33# found in the LICENSE file.
34
35# usage: rule_binding.py INPUT CPPDIR HDIR -- INPUTS -- OPTIONS
36#
37# INPUT is an IDL file, such as Whatever.idl.
38#
39# CPPDIR is the directory into which V8Whatever.cpp will be placed. HDIR is
40# the directory into which V8Whatever.h will be placed.
41#
42# The first item in INPUTS is the path to generate-bindings.pl. Remaining
43# items in INPUTS are used to build the Perl module include path.
44#
45# OPTIONS are passed as-is to generate-bindings.pl as additional arguments.
46
47
48import errno
49import os
50import shlex
51import shutil
52import subprocess
53import sys
54
55
56def SplitArgsIntoSections(args):
57    sections = []
58    while len(args) > 0:
59        if not '--' in args:
60            # If there is no '--' left, everything remaining is an entire section.
61            dashes = len(args)
62        else:
63            dashes = args.index('--')
64
65        sections.append(args[:dashes])
66
67        # Next time through the loop, look at everything after this '--'.
68        if dashes + 1 == len(args):
69            # If the '--' is at the end of the list, we won't come back through the
70            # loop again. Add an empty section now corresponding to the nothingness
71            # following the final '--'.
72            args = []
73            sections.append(args)
74        else:
75            args = args[dashes + 1:]
76
77    return sections
78
79
80def main(args):
81    sections = SplitArgsIntoSections(args[1:])
82    assert len(sections) == 3, sections
83    (base, inputs, options) = sections
84
85    assert len(base) == 3, base
86    (input, cppdir, hdir) = base
87
88    assert len(inputs) > 1, inputs
89    generateBindings = inputs[0]
90    perlModules = inputs[1:]
91
92    includeDirs = []
93    for perlModule in perlModules:
94        includeDir = os.path.dirname(perlModule)
95        if not includeDir in includeDirs:
96            includeDirs.append(includeDir)
97
98    if '--prefix' in options:
99        prefixIndex = options.index('--prefix')
100    else:
101        prefixIndex = options.index('--generator')
102
103    fileName = ''
104    if '--filename' in options:
105        fileName = options[options.index('--filename') + 1]
106
107    if prefixIndex + 1 < len(options):
108        prefix = options[prefixIndex + 1]
109
110    # The defines come in as one flat string. Split it up into distinct arguments.
111    if '--defines' in options:
112        definesIndex = options.index('--defines')
113        if definesIndex + 1 < len(options):
114            splitOptions = shlex.split(options[definesIndex + 1])
115            if splitOptions:
116                options[definesIndex + 1] = ' '.join(splitOptions)
117
118    # Build up the command.
119    command = ['perl', '-w']
120    for includeDir in includeDirs:
121        command.extend(['-I', includeDir])
122    command.append(generateBindings)
123    command.extend(options)
124    command.extend(['--outputHeadersDir', hdir])
125    command.extend(['--outputDir', cppdir, input])
126
127    # Do it. check_call is new in 2.5, so simulate its behavior with call and
128    # assert.
129    returnCode = subprocess.call(command)
130    assert returnCode == 0
131
132    return returnCode
133
134
135if __name__ == '__main__':
136    sys.exit(main(sys.argv))
137