| OLD | NEW |
| (Empty) |
| 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_bison.py INPUT_FILE OUTPUT_DIR [BISON_EXE] | |
| 36 # INPUT_FILE is a path to either CSSGrammar.y or XPathGrammar.y. | |
| 37 # OUTPUT_DIR is where the bison-generated .cpp and .h files should be placed. | |
| 38 | |
| 39 import errno | |
| 40 import os | |
| 41 import os.path | |
| 42 import subprocess | |
| 43 import sys | |
| 44 | |
| 45 assert len(sys.argv) == 3 or len(sys.argv) == 4 | |
| 46 | |
| 47 inputFile = sys.argv[1] | |
| 48 outputDir = sys.argv[2] | |
| 49 bisonExe = 'bison' | |
| 50 if len(sys.argv) > 3: | |
| 51 bisonExe = sys.argv[3] | |
| 52 | |
| 53 inputName = os.path.basename(inputFile) | |
| 54 assert inputName == 'CSSGrammar.y' or inputName == 'XPathGrammar.y' | |
| 55 prefix = {'CSSGrammar.y': 'cssyy', 'XPathGrammar.y': 'xpathyy'}[inputName] | |
| 56 | |
| 57 (inputRoot, inputExt) = os.path.splitext(inputName) | |
| 58 | |
| 59 # The generated .h will be in a different location depending on the bison | |
| 60 # version. | |
| 61 outputHTries = [ | |
| 62 os.path.join(outputDir, inputRoot + '.cpp.h'), | |
| 63 os.path.join(outputDir, inputRoot + '.hpp'), | |
| 64 ] | |
| 65 | |
| 66 for outputHTry in outputHTries: | |
| 67 try: | |
| 68 os.unlink(outputHTry) | |
| 69 except OSError, e: | |
| 70 if e.errno != errno.ENOENT: | |
| 71 raise | |
| 72 | |
| 73 outputCpp = os.path.join(outputDir, inputRoot + '.cpp') | |
| 74 | |
| 75 returnCode = subprocess.call([bisonExe, '-d', '-p', prefix, inputFile, '-o', out
putCpp]) | |
| 76 assert returnCode == 0 | |
| 77 | |
| 78 # Find the name that bison used for the generated header file. | |
| 79 outputHTmp = None | |
| 80 for outputHTry in outputHTries: | |
| 81 try: | |
| 82 os.stat(outputHTry) | |
| 83 outputHTmp = outputHTry | |
| 84 break | |
| 85 except OSError, e: | |
| 86 if e.errno != errno.ENOENT: | |
| 87 raise | |
| 88 | |
| 89 assert outputHTmp != None | |
| 90 | |
| 91 # Read the header file in under the generated name and remove it. | |
| 92 outputHFile = open(outputHTmp) | |
| 93 outputHContents = outputHFile.read() | |
| 94 outputHFile.close() | |
| 95 os.unlink(outputHTmp) | |
| 96 | |
| 97 # Rewrite the generated header with #include guards. | |
| 98 outputH = os.path.join(outputDir, inputRoot + '.h') | |
| 99 | |
| 100 outputHFile = open(outputH, 'w') | |
| 101 print >>outputHFile, '#ifndef %sH' % inputRoot | |
| 102 print >>outputHFile, '#define %sH' % inputRoot | |
| 103 print >>outputHFile, outputHContents | |
| 104 print >>outputHFile, '#endif' | |
| 105 outputHFile.close() | |
| OLD | NEW |