Debugging: Add code to print backtrace for guest on SIGSEGV
[nativeclient.git] / site_scons / site_tools / component_setup.py
bloba3be218194e52656f4cc5e9208a612278bfca3f0
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 """Main setup for software construction toolkit.
33 This module is a SCons tool which should be include in all environments.
34 It is used as follows:
35 env = Environment(tools = ['component_setup'])
36 and should be the first tool from this toolkit referenced by any environment.
37 """
39 import os
40 import sys
41 import SCons
42 import usage_log
45 #------------------------------------------------------------------------------
48 def InstallUsingLink(target, source, env):
49 """Install function for environment which uses link in preference to copy.
51 Args:
52 target: Destintion filename
53 source: Source filename
54 env: Environment
56 Returns:
57 Return code from SCons Node link function.
58 """
60 # Use link function for Install() and InstallAs(), since it's much much
61 # faster than copying. This is ok for the way we build clients, where we're
62 # installing to a build output directory and not to a permanent location such
63 # as /usr/bin.
64 # Need to force the target and source to be lists of nodes
65 return SCons.Node.FS.LinkFunc([env.Entry(target)], [env.Entry(source)], env)
68 def PreEvaluateVariables(env):
69 """Deferred function to pre-evaluate SCons varables for each build mode.
71 Args:
72 env: Environment for the current build mode.
73 """
74 # Convert directory variables to strings. Must use .abspath not str(), since
75 # otherwise $OBJ_ROOT is converted to a relative path, which evaluates
76 # improperly in SConscripts not in $MAIN_DIR.
77 for var in env.SubstList2('$PRE_EVALUATE_DIRS'):
78 env[var] = env.Dir('$' + var).abspath
81 #------------------------------------------------------------------------------
84 def generate(env):
85 # NOTE: SCons requires the use of this name, which fails gpylint.
86 """SCons entry point for this tool."""
88 # Use MD5 to tell when files differ, if the timestamps differ. This is
89 # better than pure MD5 (since if the timestamps are the same, we don't need
90 # to rescan the file), and also better than pure timestamp (since if a file
91 # is rebuilt to the same contents, we don't need to trigger the build steps
92 # which depend on it).
93 env.Decider('MD5-timestamp')
95 # For duplication order, use hard links then fall back to copying. Don't use
96 # soft links, since those won't do the right thing if the output directory
97 # is tar'd up and moved elsewhere.
98 SCons.Script.SetOption('duplicate', 'hard-copy')
100 # Remove the alias namespace lookup function from the list which SCons uses
101 # when coercing strings into nodes. This prevents SCons from looking up
102 # aliases in input/output lists if they're not explicitly coerced via
103 # Alias(), and removes a conflict where a program has the same shorthand
104 # alias as the program name itself. This conflict manifests itself as a
105 # python exception if you try to build a program in multiple modes on linux,
106 # for example:
107 # hammer --mode=dbg,opt port_test
108 new_lookup_list = []
109 for func in env.lookup_list:
110 if func.im_class != SCons.Node.Alias.AliasNameSpace:
111 new_lookup_list.append(func)
112 env.lookup_list = new_lookup_list
114 # Add command line options
115 SCons.Script.AddOption(
116 '--brief',
117 dest='brief_comstr',
118 default=True,
119 action='store_true',
120 help='brief command line output')
121 SCons.Script.AddOption(
122 '--verbose',
123 dest='brief_comstr',
124 default=True,
125 action='store_false',
126 help='verbose command line output')
128 # Add help for command line options
129 SCons.Script.Help("""\
130 --verbose Print verbose output while building, including
131 the full command lines for all commands.
132 --brief Print brief output while building (the default).
133 This and --verbose are opposites. Use --silent
134 to turn off all output.
135 """)
137 # Cover part of the environment
138 env.Replace(
139 # Add a reference to our python executable, so subprocesses can find and
140 # invoke python.
141 PYTHON = env.File(sys.executable),
143 # Get the absolute path to the directory containing main.scons (or
144 # SConstruct). This should be used in place of the SCons variable '#',
145 # since '#' is not always replaced (for example, when being used to set
146 # an environment variable).
147 MAIN_DIR = env.Dir('#').abspath,
148 # Supply deprecated SCONSTRUCT_DIR for legacy suport
149 # TODO: remove legacy support once everyone has switched over.
150 SCONSTRUCT_DIR = env.Dir('#').abspath,
152 # Use install function above, which uses links in preference to copying.
153 INSTALL = InstallUsingLink,
156 # Specify defaults for variables where we don't need to force replacement
157 env.SetDefault(
158 # Environments are in the 'all' group by default
159 BUILD_GROUPS=['all'],
161 # Directories
162 DESTINATION_ROOT='$MAIN_DIR/scons-out$HOST_PLATFORM_SUFFIX',
163 TARGET_ROOT='$DESTINATION_ROOT/$BUILD_TYPE',
164 OBJ_ROOT='$TARGET_ROOT/obj',
165 ARTIFACTS_DIR='$TARGET_ROOT/artifacts',
168 # Add default list of variables we should pre-evaluate for each build mode
169 env.Append(PRE_EVALUATE_DIRS = [
170 'ARTIFACTS_DIR',
171 'DESTINATION_ROOT',
172 'OBJ_ROOT',
173 'SOURCE_ROOT',
174 'TARGET_ROOT',
175 'TOOL_ROOT',
178 # If a host platform was specified on the command line, need to put the SCons
179 # output in its own destination directory.
180 force_host_platform = SCons.Script.GetOption('host_platform')
181 if force_host_platform:
182 env['HOST_PLATFORM_SUFFIX'] = '-' + force_host_platform
184 # Put the .sconsign.dblite file in our destination root directory, so that we
185 # don't pollute the source tree. Use the '_' + sys.platform suffix to prevent
186 # the .sconsign.dblite from being shared between host platforms, even in the
187 # case where the --host_platform option is not used (for instance when the
188 # project has platform suffixes on all the build types).
190 # This will prevent host platforms from mistakenly using each other's
191 # .sconsign databases and will allow two host platform builds to occur in the
192 # same # shared tree simulataneously.
194 # Note that we use sys.platform here rather than HOST_PLATFORM, since we need
195 # different sconsign databases for cygwin vs. win32.
196 sconsign_dir = env.Dir('$DESTINATION_ROOT').abspath
197 sconsign_filename = '$DESTINATION_ROOT/.sconsign_%s' % sys.platform
198 sconsign_file = env.File(sconsign_filename).abspath
199 # SConsignFile() doesn't seem to like it if the destination directory
200 # doesn't already exist, so make sure it exists.
201 # TODO: Remove once SCons has fixed this bug.
202 if not os.path.isdir(sconsign_dir):
203 os.makedirs(sconsign_dir)
204 SCons.Script.SConsignFile(sconsign_file)
206 # Build all by default
207 # TODO: This would be more nicely done by creating an 'all' alias and mapping
208 # that to $DESTINATION_ROOT (or the accumulation of all $TARGET_ROOT's for
209 # the environments which apply to the current host platform). Ideally, that
210 # would be done in site_init.py and not here. But since we can't do that,
211 # just set the default to be DESTINATION_ROOT here. Note that this currently
212 # forces projects which want to override the default to do so after including
213 # the component_setup tool (reasonable, since component_setup should pretty
214 # much be the first thing in a SConstruct.
215 env.Default('$DESTINATION_ROOT')
217 # Use brief command line strings if necessary
218 if env.GetOption('brief_comstr'):
219 env.SetDefault(
220 ARCOMSTR='________Creating library $TARGET',
221 ASCOMSTR='________Assembling $TARGET',
222 CCCOMSTR='________Compiling $TARGET',
223 CONCAT_SOURCE_COMSTR='________ConcatSource $TARGET',
224 CXXCOMSTR='________Compiling $TARGET',
225 LDMODULECOMSTR='________Building loadable module $TARGET',
226 LINKCOMSTR='________Linking $TARGET',
227 MANIFEST_COMSTR='________Updating manifest for $TARGET',
228 MIDLCOMSTR='________Compiling IDL $TARGET',
229 PCHCOMSTR='________Precompiling $TARGET',
230 RANLIBCOMSTR='________Indexing $TARGET',
231 RCCOMSTR='________Compiling resource $TARGET',
232 SHCCCOMSTR='________Compiling $TARGET',
233 SHCXXCOMSTR='________Compiling $TARGET',
234 SHLINKCOMSTR='________Linking $TARGET',
235 SHMANIFEST_COMSTR='________Updating manifest for $TARGET',
238 # Add other default tools from our toolkit
239 # TODO: Currently this needs to be before SOURCE_ROOT in case a tool needs to
240 # redefine it. Need a better way to handle order-dependency in tool setup.
241 for t in component_setup_tools:
242 env.Tool(t)
244 # The following environment replacements use env.Dir() to force immediate
245 # evaluation/substitution of SCons variables. They can't be part of the
246 # preceding env.Replace() since they they may rely indirectly on variables
247 # defined there, and the env.Dir() calls would be evaluated before the
248 # env.Replace().
250 # Set default SOURCE_ROOT if there is none, assuming we're in a local
251 # site_scons directory for the project.
252 source_root_relative = os.path.normpath(
253 os.path.join(os.path.dirname(__file__), '../..'))
254 source_root = env.get('SOURCE_ROOT', source_root_relative)
255 env['SOURCE_ROOT'] = env.Dir(source_root).abspath
257 usage_log.log.SetParam('component_setup.project_path',
258 env.RelativePath('$SOURCE_ROOT', '$MAIN_DIR'))
260 # Make tool root separate from source root so it can be overridden when we
261 # have a common location for tools outside of the current clientspec. Need
262 # to check if it's defined already, so it can be set prior to this tool
263 # being included.
264 tool_root = env.get('TOOL_ROOT', '$SOURCE_ROOT')
265 env['TOOL_ROOT'] = env.Dir(tool_root).abspath
267 # Defer pre-evaluating some environment variables, but do before building
268 # SConscripts.
269 env.Defer(PreEvaluateVariables)
270 env.Defer('BuildEnvironmentSConscripts', after=PreEvaluateVariables)