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
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
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 # action_cssvaluekeywords.py is a harness script to connect actions sections of
36 # gyp-based builds to makevalues.pl.
38 # usage: action_cssvaluekeywords.py OUTPUTS -- [--defines ENABLE_FLAG1 ENABLE_FLAG2 ...] -- INPUTS
40 # Exactly two outputs must be specified: a path to each of CSSValueKeywords.c
41 # and CSSValueKeywords.h.
43 # Multiple inputs may be specified. One input must have a basename of
44 # makevalues.pl; this is taken as the path to makevalues.pl. All other inputs
45 # are paths to .in files that are used as input to makevalues.pl; at least
46 # one, CSSValueKeywords.in, is required.
57 def SplitArgsIntoSections(args):
61 # If there is no '--' left, everything remaining is an entire section.
64 dashes = args.index('--')
66 sections.append(args[:dashes])
68 # Next time through the loop, look at everything after this '--'.
69 if dashes + 1 == len(args):
70 # If the '--' is at the end of the list, we won't come back through the
71 # loop again. Add an empty section now corresponding to the nothingness
72 # following the final '--'.
76 args = args[dashes + 1:]
81 def SplitDefines(options):
82 # The defines come in as one flat string. Split it up into distinct arguments.
83 if '--defines' in options:
84 definesIndex = options.index('--defines')
85 if definesIndex + 1 < len(options):
86 splitOptions = shlex.split(options[definesIndex + 1])
88 options[definesIndex + 1] = ' '.join(splitOptions)
91 outputs, options, inputs = SplitArgsIntoSections(args[1:])
95 # Make all output pathnames absolute so that they can be accessed after
97 for index in xrange(0, len(outputs)):
98 outputs[index] = os.path.abspath(outputs[index])
100 outputDir = os.path.dirname(outputs[0])
102 # Look at the inputs and figure out which one is makevalues.pl and which are
103 # inputs to that script.
104 makevaluesInput = None
107 # Make input pathnames absolute so they can be accessed after changing
108 # directory. On Windows, convert \ to / for inputs to the perl script to
109 # work around the intermix of activepython + cygwin perl.
110 inputAbs = os.path.abspath(input)
111 inputAbsPosix = inputAbs.replace(os.path.sep, posixpath.sep)
112 inputBasename = os.path.basename(input)
113 if inputBasename == 'makevalues.pl':
114 assert makevaluesInput == None
115 makevaluesInput = inputAbs
116 elif inputBasename.endswith('.in'):
117 inFiles.append(inputAbsPosix)
121 assert makevaluesInput != None
122 assert len(inFiles) >= 1
124 # Change to the output directory because makevalues.pl puts output in its
128 # Merge all inFiles into a single file whose name will be the same as the
129 # first listed inFile, but in the output directory.
130 mergedPath = os.path.basename(inFiles[0])
131 merged = open(mergedPath, 'wb') # 'wb' to get \n only on windows
133 # Concatenate all the input files.
134 for inFilePath in inFiles:
135 inFile = open(inFilePath)
136 shutil.copyfileobj(inFile, merged)
141 # scriptsPath is a Perl include directory, located relative to
143 scriptsPath = os.path.normpath(
144 os.path.join(os.path.dirname(makevaluesInput), os.pardir, 'bindings', 'scripts'))
146 # Build up the command.
147 command = ['perl', '-I', scriptsPath, makevaluesInput]
148 command.extend(options)
150 # Do it. checkCall is new in 2.5, so simulate its behavior with call and
152 returnCode = subprocess.call(command)
153 assert returnCode == 0
155 # Don't leave behind the merged file or the .gperf file created by
157 (root, ext) = os.path.splitext(mergedPath)
158 gperfPath = root + '.gperf'
160 os.unlink(mergedPath)
162 # Go through the outputs. Any output that belongs in a different directory
163 # is moved. Do a copy and delete instead of rename for maximum portability.
164 # Note that all paths used in this section are still absolute.
165 for output in outputs:
166 thisOutputDir = os.path.dirname(output)
167 if thisOutputDir != outputDir:
168 outputBasename = os.path.basename(output)
169 src = os.path.join(outputDir, outputBasename)
170 dst = os.path.join(thisOutputDir, outputBasename)
171 shutil.copyfile(src, dst)
177 if __name__ == '__main__':
178 sys.exit(main(sys.argv))