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