Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / build / scripts / rule_bison.py
blob91037072c26667bddb26fefcefe60c1b133855b4
1 #!/usr/bin/python
3 # Copyright (C) 2009 Google Inc. All rights reserved.
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions are
7 # met:
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.
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.
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.
35 # usage: rule_bison.py INPUT_FILE OUTPUT_DIR [BISON_EXE]
36 # INPUT_FILE is a path to either XPathGrammar.y.
37 # OUTPUT_DIR is where the bison-generated .cpp and .h files should be placed.
39 import errno
40 import os
41 import os.path
42 import subprocess
43 import sys
45 assert len(sys.argv) == 3 or len(sys.argv) == 4
47 inputFile = sys.argv[1]
48 outputDir = sys.argv[2]
49 bisonExe = 'bison'
50 if len(sys.argv) > 3:
51 bisonExe = sys.argv[3]
53 pathToBison = os.path.split(bisonExe)[0]
54 if pathToBison:
55 # Make sure this path is in the path so that it can find its auxiliary
56 # binaries (in particular, m4). To avoid other 'm4's being found, insert
57 # at head, rather than tail.
58 os.environ['PATH'] = pathToBison + os.pathsep + os.environ['PATH']
60 inputName = os.path.basename(inputFile)
61 assert inputName == 'XPathGrammar.y'
62 prefix = {'XPathGrammar.y': 'xpathyy'}[inputName]
64 (inputRoot, inputExt) = os.path.splitext(inputName)
66 # The generated .h will be in a different location depending on the bison
67 # version.
68 outputHTries = [
69 os.path.join(outputDir, inputRoot + '.cpp.h'),
70 os.path.join(outputDir, inputRoot + '.hpp'),
73 for outputHTry in outputHTries:
74 try:
75 os.unlink(outputHTry)
76 except OSError, e:
77 if e.errno != errno.ENOENT:
78 raise
80 outputCpp = os.path.join(outputDir, inputRoot + '.cpp')
82 returnCode = subprocess.call([bisonExe, '-d', '-p', prefix, inputFile, '-o', outputCpp])
83 assert returnCode == 0
85 # Find the name that bison used for the generated header file.
86 outputHTmp = None
87 for outputHTry in outputHTries:
88 try:
89 os.stat(outputHTry)
90 outputHTmp = outputHTry
91 break
92 except OSError, e:
93 if e.errno != errno.ENOENT:
94 raise
96 assert outputHTmp != None
98 # Read the header file in under the generated name and remove it.
99 outputHFile = open(outputHTmp)
100 outputHContents = outputHFile.read()
101 outputHFile.close()
102 os.unlink(outputHTmp)
104 # Rewrite the generated header with #include guards.
105 outputH = os.path.join(outputDir, inputRoot + '.h')
107 outputHFile = open(outputH, 'w')
108 print >>outputHFile, '#ifndef %sH' % inputRoot
109 print >>outputHFile, '#define %sH' % inputRoot
110 print >>outputHFile, outputHContents
111 print >>outputHFile, '#endif'
112 outputHFile.close()