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