| OLD | NEW |
| 1 #!/usr/bin/python2.4 | 1 #!/usr/bin/python2.4 |
| 2 # Copyright 2008, Google Inc. | 2 # Copyright 2008, Google Inc. |
| 3 # All rights reserved. | 3 # All rights reserved. |
| 4 # | 4 # |
| 5 # Redistribution and use in source and binary forms, with or without | 5 # Redistribution and use in source and binary forms, with or without |
| 6 # modification, are permitted provided that the following conditions are | 6 # modification, are permitted provided that the following conditions are |
| 7 # met: | 7 # met: |
| 8 # | 8 # |
| 9 # * Redistributions of source code must retain the above copyright | 9 # * Redistributions of source code must retain the above copyright |
| 10 # notice, this list of conditions and the following disclaimer. | 10 # notice, this list of conditions and the following disclaimer. |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 29 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | 30 |
| 31 """Set up tools for environments for for software construction toolkit. | 31 """Set up tools for environments for for software construction toolkit. |
| 32 | 32 |
| 33 This module is a SCons tool which should be include in all environments. It | 33 This module is a SCons tool which should be include in all environments. It |
| 34 will automatically be included by the component_setup tool. | 34 will automatically be included by the component_setup tool. |
| 35 """ | 35 """ |
| 36 | 36 |
| 37 | 37 |
| 38 import os |
| 38 import SCons | 39 import SCons |
| 39 | 40 |
| 40 | 41 |
| 41 #------------------------------------------------------------------------------ | 42 #------------------------------------------------------------------------------ |
| 42 | 43 |
| 43 | 44 |
| 44 def FilterOut(self, **kw): | 45 def FilterOut(self, **kw): |
| 45 """Removes values from existing construction variables in an Environment. | 46 """Removes values from existing construction variables in an Environment. |
| 46 | 47 |
| 47 The values to remove should be a list. For example: | 48 The values to remove should be a list. For example: |
| 48 | 49 |
| 49 self.FilterOut(CPPDEFINES=['REMOVE_ME', 'ME_TOO']) | 50 self.FilterOut(CPPDEFINES=['REMOVE_ME', 'ME_TOO']) |
| 50 | 51 |
| 51 Args: | 52 Args: |
| 52 self: Environment to alter. | 53 self: Environment to alter. |
| 53 kw: (Any other named arguments are values to remove). | 54 kw: (Any other named arguments are values to remove). |
| 54 """ | 55 """ |
| 55 | 56 |
| 56 kw = SCons.Environment.copy_non_reserved_keywords(kw) | 57 kw = SCons.Environment.copy_non_reserved_keywords(kw) |
| 57 for key, val in kw.items(): | 58 for key, val in kw.items(): |
| 58 envval = self.get(key, None) | 59 envval = self.get(key, None) |
| 59 if envval is None: | 60 if envval is None: |
| 60 # No existing variable in the environment, so nothing to delete. | 61 # No existing variable in the environment, so nothing to delete. |
| 61 continue | 62 continue |
| 62 | 63 |
| 63 for vremove in val: | 64 for vremove in val: |
| 64 if vremove in envval: | 65 # Use while not if, so we can handle duplicates. |
| 66 while vremove in envval: |
| 65 envval.remove(vremove) | 67 envval.remove(vremove) |
| 66 | 68 |
| 67 self[key] = envval | 69 self[key] = envval |
| 68 | 70 |
| 69 # TODO(sgk): SCons.Environment.Append() has much more logic to deal | 71 # TODO(sgk): SCons.Environment.Append() has much more logic to deal |
| 70 # with various types of values. We should handle all those cases in here | 72 # with various types of values. We should handle all those cases in here |
| 71 # too. | 73 # too. (If variable is a dict, etc.) |
| 72 | 74 |
| 73 #------------------------------------------------------------------------------ | 75 #------------------------------------------------------------------------------ |
| 74 | 76 |
| 75 | 77 |
| 76 def Overlap(self, values1, values2): | 78 def Overlap(self, values1, values2): |
| 77 """Checks for overlap between the values. | 79 """Checks for overlap between the values. |
| 78 | 80 |
| 79 Args: | 81 Args: |
| 80 self: Environment to use for variable substitution. | 82 self: Environment to use for variable substitution. |
| 81 values1: First value(s) to compare. May be a string or list of strings. | 83 values1: First value(s) to compare. May be a string or list of strings. |
| 82 values2: Second value(s) to compare. May be a string or list of strings. | 84 values2: Second value(s) to compare. May be a string or list of strings. |
| 83 | 85 |
| 84 Returns: | 86 Returns: |
| 85 The list of values in common after substitution, or an empty list if | 87 The list of values in common after substitution, or an empty list if |
| 86 the values do no overlap. | 88 the values do not overlap. |
| 87 | 89 |
| 88 Converts the values to a set of plain strings via self.subst() before | 90 Converts the values to a set of plain strings via self.SubstList2() before |
| 89 comparison, so SCons $ variables are evaluated. | 91 comparison, so SCons $ variables are evaluated. |
| 90 """ | 92 """ |
| 91 | 93 set1 = set(self.SubstList2(values1)) |
| 92 set1 = set() | 94 set2 = set(self.SubstList2(values2)) |
| 93 for v in self.Flatten(values1): | |
| 94 set1.add(self.subst(v)) | |
| 95 | |
| 96 set2 = set() | |
| 97 for v in self.Flatten(values2): | |
| 98 set2.add(self.subst(v)) | |
| 99 | |
| 100 return list(set1.intersection(set2)) | 95 return list(set1.intersection(set2)) |
| 101 | 96 |
| 102 | |
| 103 #------------------------------------------------------------------------------ | 97 #------------------------------------------------------------------------------ |
| 104 | 98 |
| 105 | 99 |
| 106 def ApplySConscript(self, sconscript_file): | 100 def ApplySConscript(self, sconscript_file): |
| 107 """Applies a SConscript to the current environment. | 101 """Applies a SConscript to the current environment. |
| 108 | 102 |
| 109 Args: | 103 Args: |
| 110 self: Environment to modify. | 104 self: Environment to modify. |
| 111 sconscript_file: Name of SConscript file to apply. | 105 sconscript_file: Name of SConscript file to apply. |
| 112 | 106 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 130 calling environment: | 124 calling environment: |
| 131 Import('env') | 125 Import('env') |
| 132 | 126 |
| 133 Changes made to env in the called SConscript will be applied to the | 127 Changes made to env in the called SConscript will be applied to the |
| 134 environment calling ApplySConscript() - that is, env in the called SConscript | 128 environment calling ApplySConscript() - that is, env in the called SConscript |
| 135 is a reference to the calling environment. | 129 is a reference to the calling environment. |
| 136 | 130 |
| 137 If you need to export multiple variables to the called SConscript, or return | 131 If you need to export multiple variables to the called SConscript, or return |
| 138 variables from it, use the existing SConscript() function. | 132 variables from it, use the existing SConscript() function. |
| 139 """ | 133 """ |
| 140 return SCons.Script.SConscript(sconscript_file, exports={'env':self}) | 134 return SCons.Script.SConscript(sconscript_file, exports={'env': self}) |
| 141 | 135 |
| 142 #------------------------------------------------------------------------------ | 136 #------------------------------------------------------------------------------ |
| 143 | 137 |
| 144 | 138 |
| 145 def BuildSConscript(self, sconscript_file): | 139 def BuildSConscript(self, sconscript_file): |
| 146 """Builds a SConscript based on the current environment. | 140 """Builds a SConscript based on the current environment. |
| 147 | 141 |
| 148 Args: | 142 Args: |
| 149 self: Environment to clone and pass to the called SConscript. | 143 self: Environment to clone and pass to the called SConscript. |
| 150 sconscript_file: Name of SConscript file to build. If this is a directory, | 144 sconscript_file: Name of SConscript file to build. If this is a directory, |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 187 # or a file). This isn't a problem in BuildComponents(), since the variant | 181 # or a file). This isn't a problem in BuildComponents(), since the variant |
| 188 # dir is only set inside its call to SConscript(). | 182 # dir is only set inside its call to SConscript(). |
| 189 if self.Entry(sconscript_file).srcnode().isdir(): | 183 if self.Entry(sconscript_file).srcnode().isdir(): |
| 190 # Building a subdirectory, so look for build.scons or SConscript | 184 # Building a subdirectory, so look for build.scons or SConscript |
| 191 script_file = sconscript_file + '/build.scons' | 185 script_file = sconscript_file + '/build.scons' |
| 192 if not self.File(script_file).srcnode().exists(): | 186 if not self.File(script_file).srcnode().exists(): |
| 193 script_file = sconscript_file + '/SConscript' | 187 script_file = sconscript_file + '/SConscript' |
| 194 else: | 188 else: |
| 195 script_file = sconscript_file | 189 script_file = sconscript_file |
| 196 | 190 |
| 197 self.SConscript(script_file, exports={'env':self.Clone()}) | 191 self.SConscript(script_file, exports={'env': self.Clone()}) |
| 192 |
| 193 #------------------------------------------------------------------------------ |
| 194 |
| 195 |
| 196 def SubstList2(self, *args): |
| 197 """Replacement subst_list designed for flags/parameters, not command lines. |
| 198 |
| 199 Args: |
| 200 self: Environment context. |
| 201 args: One or more strings or lists of strings. |
| 202 |
| 203 Returns: |
| 204 A flattened, substituted list of strings. |
| 205 |
| 206 SCons's built-in subst_list evaluates (substitutes) variables in its |
| 207 arguments, and returns a list of lists (one per positional argument). Since |
| 208 it is designed for use in command line expansion, the list items are |
| 209 SCons.Subst.CmdStringHolder instances. These instances can't be passed into |
| 210 env.File() (or subsequent calls to env.subst(), either). The returned |
| 211 nested lists also need to be flattened via env.Flatten() before the caller |
| 212 can iterate over the contents. |
| 213 |
| 214 SubstList2() does a subst_list, flattens the result, then maps the flattened |
| 215 list to strings. |
| 216 |
| 217 It is better to do: |
| 218 for x in env.SubstList2('$MYPARAMS'): |
| 219 than to do: |
| 220 for x in env.get('MYPARAMS', []): |
| 221 and definitely better than: |
| 222 for x in env['MYPARAMS']: |
| 223 which will throw an exception if MYPARAMS isn't defined. |
| 224 """ |
| 225 return map(str, self.Flatten(self.subst_list(args))) |
| 226 |
| 227 |
| 228 #------------------------------------------------------------------------------ |
| 229 |
| 230 |
| 231 def RelativePath(self, source, target, sep=os.sep, source_is_file=False): |
| 232 """Calculates the relative path from source to target. |
| 233 |
| 234 Args: |
| 235 self: Environment context. |
| 236 source: Source path or node. |
| 237 target: Target path or node. |
| 238 sep: Path separator to use in returned relative path. |
| 239 source_is_file: If true, calculates the relative path from the directory |
| 240 containing the source, rather than the source itself. Note that if |
| 241 source is a node, you can pass in source.dir instead, which is shorter. |
| 242 |
| 243 Returns: |
| 244 The relative path from source to target. |
| 245 """ |
| 246 # Split source and target into list of directories |
| 247 source = self.Entry(str(source)) |
| 248 if source_is_file: |
| 249 source = source.dir |
| 250 source = source.abspath.split(os.sep) |
| 251 target = self.Entry(str(target)).abspath.split(os.sep) |
| 252 |
| 253 # Handle source and target identical |
| 254 if source == target: |
| 255 if source_is_file: |
| 256 return source[-1] # Bare filename |
| 257 else: |
| 258 return '.' # Directory pointing to itself |
| 259 |
| 260 # TODO(rspangler): Handle UNC paths and drive letters (fine if they're the |
| 261 # same, but if they're different, there IS no relative path) |
| 262 |
| 263 # Remove common elements |
| 264 while source and target and source[0] == target[0]: |
| 265 source.pop(0) |
| 266 target.pop(0) |
| 267 # Join the remaining elements |
| 268 return sep.join(['..'] * len(source) + target) |
| 269 |
| 198 | 270 |
| 199 #------------------------------------------------------------------------------ | 271 #------------------------------------------------------------------------------ |
| 200 | 272 |
| 201 | 273 |
| 202 def generate(env): | 274 def generate(env): |
| 203 # NOTE: SCons requires the use of this name, which fails gpylint. | 275 # NOTE: SCons requires the use of this name, which fails gpylint. |
| 204 """SCons entry point for this tool.""" | 276 """SCons entry point for this tool.""" |
| 205 | 277 |
| 206 # Add methods to environment | 278 # Add methods to environment |
| 207 env.AddMethod(ApplySConscript) | 279 env.AddMethod(ApplySConscript) |
| 208 env.AddMethod(BuildSConscript) | 280 env.AddMethod(BuildSConscript) |
| 209 env.AddMethod(FilterOut) | 281 env.AddMethod(FilterOut) |
| 210 env.AddMethod(Overlap) | 282 env.AddMethod(Overlap) |
| 283 env.AddMethod(RelativePath) |
| 284 env.AddMethod(SubstList2) |
| OLD | NEW |