Debugging: Add code to print backtrace for guest on SIGSEGV
[nativeclient.git] / site_scons / site_tools / concat_source.py
blob5b448c184777ff102d9ee1448dfcecf908c58ec9
1 #!/usr/bin/python2.4
2 # Copyright 2009, Google Inc.
3 # 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 """Source concatenation builder for SCons."""
34 import SCons.Script
37 def ConcatSourceBuilder(target, source, env):
38 """ConcatSource builder.
40 Args:
41 target: List of target nodes
42 source: List of source nodes
43 env: Environment in which to build
45 Returns:
46 None if successful; 1 if error.
47 """
48 if len(target) != 1:
49 print 'ERROR: multiple ConcatSource targets when 1 expected'
50 return 1
52 output_lines = [
53 '// This file is auto-generated by the ConcatSource builder.']
55 for source_path in map(str, source):
56 if env.get('CC') == 'cl':
57 # Add message pragma for nicer progress indication when building with
58 # MSVC.
59 output_lines.append('#pragma message("--%s")' % (
60 source_path.replace("\\", "/")))
62 output_lines.append('#include "%s"' % source_path)
64 output_file = open(str(target[0]), 'w')
65 # Need an EOL at the end of the file for more finicky build tools
66 output_file.write('\n'.join(output_lines) + '\n')
67 output_file.close()
70 def ConcatSourcePseudoBuilder(self, target, source):
71 """ConcatSource pseudo-builder; calls builder or passes through source nodes.
73 Args:
74 self: Environment in which to build
75 target: List of target nodes
76 source: List of source nodes
78 Returns:
79 If self['CONCAT_SOURCE_ENABLE'], calls self.ConcatSource and returns
80 the list of target nodes. Otherwise, returns the list of source nodes.
81 Source nodes which are not CPP files are passed through unchanged to the
82 list of output nodes.
83 """
84 if self.get('CONCAT_SOURCE_ENABLE', True):
85 # Scan down source list and separate CPP sources (which we concatenate)
86 # from other files (which we pass through).
87 cppsource = []
88 outputs = []
89 suffixes = self.Flatten(self.subst_list('$CONCAT_SOURCE_SUFFIXES'))
90 for source_file in self.arg2nodes(source):
91 if source_file.suffix in suffixes:
92 cppsource.append(source_file)
93 else:
94 outputs.append(source_file)
96 if len(cppsource) > 1:
97 # More than one file, so concatenate them together
98 outputs += self.ConcatSourceBuilder(target, cppsource)
99 else:
100 # <2 files, so pass them through; no need for a ConcatSource target
101 outputs += cppsource
102 return outputs
103 else:
104 # ConcatSource is disabled, so pass through the list of source nodes.
105 return source
108 def generate(env):
109 # NOTE: SCons requires the use of this name, which fails gpylint.
110 """SCons entry point for this tool."""
112 # Add the builder
113 action = SCons.Script.Action(ConcatSourceBuilder, '$CONCAT_SOURCE_COMSTR',
114 varlist = ['CONCAT_SOURCE_SUFFIXES'])
115 builder = SCons.Script.Builder(action = action, suffix = '$CXXFILESUFFIX')
116 env.Append(BUILDERS={'ConcatSourceBuilder': builder})
118 env.SetDefault(
119 CONCAT_SOURCE_COMSTR = 'Creating ConcatSource $TARGET from $SOURCES',
120 # Suffixes of sources we can concatenate. Files not in this list will be
121 # passed through untouched. (Note that on Mac, Objective C/C++ files
122 # cannot be concatenated with regular C/C++ files.)
123 # TODO: Probably shouldn't mix C, C++ either...
124 CONCAT_SOURCE_SUFFIXES = ['.c', '.C', '.cxx', '.cpp', '.c++', '.cc',
125 '.h', '.H', '.hxx', '.hpp', '.hh'],
128 # Add a psuedo-builder method which can look at the environment to determine
129 # whether to call the ConcatSource builder or not
130 env.AddMethod(ConcatSourcePseudoBuilder, 'ConcatSource')