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 48 import errno 49 import os 50 import shlex 51 import shutil 52 import subprocess 53 import sys 54 55 56 def 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 80 def 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 # The defines come in as one flat string. Split it up into distinct arguments. 99 if '--defines' in options: 100 definesIndex = options.index('--defines') 101 if definesIndex + 1 < len(options): 102 splitOptions = shlex.split(options[definesIndex + 1]) 103 if splitOptions: 104 options[definesIndex + 1] = ' '.join(splitOptions) 105 106 # Build up the command. 107 command = ['perl', '-w'] 108 for includeDir in includeDirs: 109 command.extend(['-I', includeDir]) 110 command.append(generateBindings) 111 command.extend(options) 112 command.extend(['--outputDir', cppdir, input]) 113 114 # Do it. check_call is new in 2.5, so simulate its behavior with call and 115 # assert. 116 returnCode = subprocess.call(command) 117 assert returnCode == 0 118 119 # Both the .cpp and .h were generated in cppdir, but if hdir is different, 120 # the .h needs to move. Copy it instead of using os.rename for maximum 121 # portability in all cases. 122 if cppdir != hdir: 123 inputBasename = os.path.basename(input) 124 (root, ext) = os.path.splitext(inputBasename) 125 hname = 'V8%s.h' % root 126 hsrc = os.path.join(cppdir, hname) 127 hdst = os.path.join(hdir, hname) 128 shutil.copyfile(hsrc, hdst) 129 os.unlink(hsrc) 130 131 return returnCode 132 133 134 if __name__ == '__main__': 135 sys.exit(main(sys.argv)) 136