1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
|
#!/usr/bin/python2.4
# Copyright 2008, Google Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following disclaimer
# in the documentation and/or other materials provided with the
# distribution.
# * Neither the name of Google Inc. nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""Set up tools for environments for for software construction toolkit.
This module is a SCons tool which should be include in all environments. It
will automatically be included by the component_setup tool.
"""
import os
import SCons
#------------------------------------------------------------------------------
def FilterOut(self, **kw):
"""Removes values from existing construction variables in an Environment.
The values to remove should be a list. For example:
self.FilterOut(CPPDEFINES=['REMOVE_ME', 'ME_TOO'])
Args:
self: Environment to alter.
kw: (Any other named arguments are values to remove).
"""
kw = SCons.Environment.copy_non_reserved_keywords(kw)
for key, val in kw.items():
envval = self.get(key, None)
if envval is None:
# No existing variable in the environment, so nothing to delete.
continue
for vremove in val:
# Use while not if, so we can handle duplicates.
while vremove in envval:
envval.remove(vremove)
self[key] = envval
# TODO(sgk): SCons.Environment.Append() has much more logic to deal
# with various types of values. We should handle all those cases in here
# too. (If variable is a dict, etc.)
#------------------------------------------------------------------------------
def Overlap(self, values1, values2):
"""Checks for overlap between the values.
Args:
self: Environment to use for variable substitution.
values1: First value(s) to compare. May be a string or list of strings.
values2: Second value(s) to compare. May be a string or list of strings.
Returns:
The list of values in common after substitution, or an empty list if
the values do not overlap.
Converts the values to a set of plain strings via self.SubstList2() before
comparison, so SCons $ variables are evaluated.
"""
set1 = set(self.SubstList2(values1))
set2 = set(self.SubstList2(values2))
return list(set1.intersection(set2))
#------------------------------------------------------------------------------
def ApplySConscript(self, sconscript_file):
"""Applies a SConscript to the current environment.
Args:
self: Environment to modify.
sconscript_file: Name of SConscript file to apply.
Returns:
The return value from the call to SConscript().
ApplySConscript() should be used when an existing SConscript which sets up an
environment gets too large, or when there is common setup between multiple
environments which can't be reduced into a parent environment which the
multiple child environments Clone() from. The latter case is necessary
because env.Clone() only enables single inheritance for environments.
ApplySConscript() is NOT intended to replace the Tool() method. If you need
to add methods or builders to one or more environments, do that as a tool
(and write unit tests for them).
ApplySConscript() is equivalent to the following SCons call:
SConscript(sconscript_file, exports={'env':self})
The called SConscript should import the 'env' variable to get access to the
calling environment:
Import('env')
Changes made to env in the called SConscript will be applied to the
environment calling ApplySConscript() - that is, env in the called SConscript
is a reference to the calling environment.
If you need to export multiple variables to the called SConscript, or return
variables from it, use the existing SConscript() function.
"""
return SCons.Script.SConscript(sconscript_file, exports={'env': self})
#------------------------------------------------------------------------------
def BuildSConscript(self, sconscript_file):
"""Builds a SConscript based on the current environment.
Args:
self: Environment to clone and pass to the called SConscript.
sconscript_file: Name of SConscript file to build. If this is a directory,
this method will look for sconscript_file+'/build.scons', and if that
is not found, sconscript_file+'/SConscript'.
Returns:
The return value from the call to SConscript().
BuildSConscript() should be used when an existing SConscript which builds a
project gets too large, or when a group of SConscripts are logically related
but should not directly affect each others' environments (for example, a
library might want to build a number of unit tests which exist in
subdirectories, but not allow those tests' SConscripts to affect/pollute the
library's environment.
BuildSConscript() is NOT intended to replace the Tool() method. If you need
to add methods or builders to one or more environments, do that as a tool
(and write unit tests for them).
BuildSConscript() is equivalent to the following SCons call:
SConscript(sconscript_file, exports={'env':self.Clone()})
or if sconscript_file is a directory:
SConscript(sconscript_file+'/build.scons', exports={'env':self.Clone()})
The called SConscript should import the 'env' variable to get access to the
calling environment:
Import('env')
Changes made to env in the called SConscript will NOT be applied to the
environment calling BuildSConscript() - that is, env in the called SConscript
is a clone/copy of the calling environment, not a reference to that
environment.
If you need to export multiple variables to the called SConscript, or return
variables from it, use the existing SConscript() function.
"""
# Need to look for the source node, since by default SCons will look for the
# entry in the variant_dir, which won't exist (and thus won't be a directory
# or a file). This isn't a problem in BuildComponents(), since the variant
# dir is only set inside its call to SConscript().
if self.Entry(sconscript_file).srcnode().isdir():
# Building a subdirectory, so look for build.scons or SConscript
script_file = sconscript_file + '/build.scons'
if not self.File(script_file).srcnode().exists():
script_file = sconscript_file + '/SConscript'
else:
script_file = sconscript_file
self.SConscript(script_file, exports={'env': self.Clone()})
#------------------------------------------------------------------------------
def SubstList2(self, *args):
"""Replacement subst_list designed for flags/parameters, not command lines.
Args:
self: Environment context.
args: One or more strings or lists of strings.
Returns:
A flattened, substituted list of strings.
SCons's built-in subst_list evaluates (substitutes) variables in its
arguments, and returns a list of lists (one per positional argument). Since
it is designed for use in command line expansion, the list items are
SCons.Subst.CmdStringHolder instances. These instances can't be passed into
env.File() (or subsequent calls to env.subst(), either). The returned
nested lists also need to be flattened via env.Flatten() before the caller
can iterate over the contents.
SubstList2() does a subst_list, flattens the result, then maps the flattened
list to strings.
It is better to do:
for x in env.SubstList2('$MYPARAMS'):
than to do:
for x in env.get('MYPARAMS', []):
and definitely better than:
for x in env['MYPARAMS']:
which will throw an exception if MYPARAMS isn't defined.
"""
return map(str, self.Flatten(self.subst_list(args)))
#------------------------------------------------------------------------------
def RelativePath(self, source, target, sep=os.sep, source_is_file=False):
"""Calculates the relative path from source to target.
Args:
self: Environment context.
source: Source path or node.
target: Target path or node.
sep: Path separator to use in returned relative path.
source_is_file: If true, calculates the relative path from the directory
containing the source, rather than the source itself. Note that if
source is a node, you can pass in source.dir instead, which is shorter.
Returns:
The relative path from source to target.
"""
# Split source and target into list of directories
source = self.Entry(str(source))
if source_is_file:
source = source.dir
source = source.abspath.split(os.sep)
target = self.Entry(str(target)).abspath.split(os.sep)
# Handle source and target identical
if source == target:
if source_is_file:
return source[-1] # Bare filename
else:
return '.' # Directory pointing to itself
# TODO(rspangler): Handle UNC paths and drive letters (fine if they're the
# same, but if they're different, there IS no relative path)
# Remove common elements
while source and target and source[0] == target[0]:
source.pop(0)
target.pop(0)
# Join the remaining elements
return sep.join(['..'] * len(source) + target)
#------------------------------------------------------------------------------
def generate(env):
# NOTE: SCons requires the use of this name, which fails gpylint.
"""SCons entry point for this tool."""
# Add methods to environment
env.AddMethod(ApplySConscript)
env.AddMethod(BuildSConscript)
env.AddMethod(FilterOut)
env.AddMethod(Overlap)
env.AddMethod(RelativePath)
env.AddMethod(SubstList2)
|