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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
|
"""SCons.Script.SConscript
This module defines the Python API provided to SConscript and SConstruct
files.
"""
#
# __COPYRIGHT__
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
__revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
import SCons
import SCons.Action
import SCons.Builder
import SCons.Defaults
import SCons.Environment
import SCons.Errors
import SCons.Node
import SCons.Node.Alias
import SCons.Node.FS
import SCons.Options
import SCons.Platform
import SCons.SConf
import SCons.Script
import SCons.Tool
import SCons.Util
import os
import os.path
import re
import string
import sys
import traceback
import types
import UserList
launch_dir = os.path.abspath(os.curdir)
help_text = None
def HelpFunction(text):
global help_text
if help_text is None:
help_text = text
else:
help_text = help_text + text
Arguments = {}
ArgList = []
CommandLineTargets = []
DefaultCalled = None
DefaultTargets = []
GlobalDict = {}
class TargetList(UserList.UserList):
def _do_nothing(self, *args, **kw):
pass
def _add_Default(self, list):
self.extend(list)
def _clear(self):
del self[:]
BuildTargets = TargetList()
# global exports set by Export():
global_exports = {}
# chdir flag
sconscript_chdir = 1
# will be set to 1, if we are reading a SConscript
sconscript_reading = 0
def _scons_add_args(alist):
for arg in alist:
a, b = string.split(arg, '=', 1)
Arguments[a] = b
ArgList.append((a, b))
def _scons_add_targets(tlist):
if tlist:
CommandLineTargets.extend(tlist)
BuildTargets.extend(tlist)
BuildTargets._add_Default = BuildTargets._do_nothing
BuildTargets._clear = BuildTargets._do_nothing
def get_calling_namespaces():
"""Return the locals and globals for the function that called
into this module in the current callstack."""
try: 1/0
except ZeroDivisionError: frame = sys.exc_info()[2].tb_frame
while frame.f_globals.get("__name__") == __name__: frame = frame.f_back
return frame.f_locals, frame.f_globals
def compute_exports(exports):
"""Compute a dictionary of exports given one of the parameters
to the Export() function or the exports argument to SConscript()."""
loc, glob = get_calling_namespaces()
retval = {}
try:
for export in exports:
if SCons.Util.is_Dict(export):
retval.update(export)
else:
try:
retval[export] = loc[export]
except KeyError:
retval[export] = glob[export]
except KeyError, x:
raise SCons.Errors.UserError, "Export of non-existent variable '%s'"%x
return retval
class Frame:
"""A frame on the SConstruct/SConscript call stack"""
def __init__(self, exports, sconscript):
self.globals = BuildDefaultGlobals()
self.retval = None
self.prev_dir = SCons.Node.FS.default_fs.getcwd()
self.exports = compute_exports(exports) # exports from the calling SConscript
# make sure the sconscript attr is a Node.
if isinstance(sconscript, SCons.Node.Node):
self.sconscript = sconscript
else:
self.sconscript = SCons.Node.FS.default_fs.File(str(sconscript))
# the SConstruct/SConscript call stack:
stack = []
# For documentation on the methods in this file, see the scons man-page
def Return(*vars):
retval = []
try:
for var in vars:
for v in string.split(var):
retval.append(stack[-1].globals[v])
except KeyError, x:
raise SCons.Errors.UserError, "Return of non-existent variable '%s'"%x
if len(retval) == 1:
stack[-1].retval = retval[0]
else:
stack[-1].retval = tuple(retval)
stack_bottom = '% Stack boTTom %' # hard to define a variable w/this name :)
def _SConscript(fs, *files, **kw):
top = fs.Top
sd = fs.SConstruct_dir.rdir()
exports = kw.get('exports', [])
# evaluate each SConscript file
results = []
for fn in files:
stack.append(Frame(exports,fn))
old_sys_path = sys.path
try:
global sconscript_reading
sconscript_reading = 1
if fn == "-":
exec sys.stdin in stack[-1].globals
else:
if isinstance(fn, SCons.Node.Node):
f = fn
else:
f = fs.File(str(fn))
_file_ = None
# Change directory to the top of the source
# tree to make sure the os's cwd and the cwd of
# fs match so we can open the SConscript.
fs.chdir(top, change_os_dir=1)
if f.rexists():
_file_ = open(f.rstr(), "r")
elif f.has_src_builder():
# The SConscript file apparently exists in a source
# code management system. Build it, but then clear
# the builder so that it doesn't get built *again*
# during the actual build phase.
f.build()
f.builder_set(None)
s = str(f)
if os.path.exists(s):
_file_ = open(s, "r")
if _file_:
# Chdir to the SConscript directory. Use a path
# name relative to the SConstruct file so that if
# we're using the -f option, we're essentially
# creating a parallel SConscript directory structure
# in our local directory tree.
#
# XXX This is broken for multiple-repository cases
# where the SConstruct and SConscript files might be
# in different Repositories. For now, cross that
# bridge when someone comes to it.
ldir = fs.Dir(f.dir.get_path(sd))
try:
fs.chdir(ldir, change_os_dir=sconscript_chdir)
except OSError:
# There was no local directory, so we should be
# able to chdir to the Repository directory.
# Note that we do this directly, not through
# fs.chdir(), because we still need to
# interpret the stuff within the SConscript file
# relative to where we are logically.
fs.chdir(ldir, change_os_dir=0)
os.chdir(f.rfile().dir.get_abspath())
# Append the SConscript directory to the beginning
# of sys.path so Python modules in the SConscript
# directory can be easily imported.
sys.path = [ f.dir.get_abspath() ] + sys.path
# This is the magic line that actually reads up
# and executes the stuff in the SConscript file.
# The locals for this frame contain the special
# bottom-of-the-stack marker so that any
# exceptions that occur when processing this
# SConscript can base the printed frames at this
# level and not show SCons internals as well.
stack[-1].globals.update({stack_bottom:1})
exec _file_ in stack[-1].globals
else:
SCons.Warnings.warn(SCons.Warnings.MissingSConscriptWarning,
"Ignoring missing SConscript '%s'" % f.path)
finally:
sconscript_reading = 0
sys.path = old_sys_path
frame = stack.pop()
try:
fs.chdir(frame.prev_dir, change_os_dir=sconscript_chdir)
except OSError:
# There was no local directory, so chdir to the
# Repository directory. Like above, we do this
# directly.
fs.chdir(frame.prev_dir, change_os_dir=0)
os.chdir(frame.prev_dir.rdir().get_abspath())
results.append(frame.retval)
# if we only have one script, don't return a tuple
if len(results) == 1:
return results[0]
else:
return tuple(results)
def SConscript_exception(file=sys.stderr):
"""Print an exception stack trace just for the SConscript file(s).
This will show users who have Python errors where the problem is,
without cluttering the output with all of the internal calls leading
up to where we exec the SConscript."""
exc_type, exc_value, exc_tb = sys.exc_info()
tb = exc_tb
while tb and not tb.tb_frame.f_locals.has_key(stack_bottom):
tb = tb.tb_next
if not tb:
# We did not find our exec statement, so this was actually a bug
# in SCons itself. Show the whole stack.
tb = exc_tb
stack = traceback.extract_tb(tb)
type = str(exc_type)
if type[:11] == "exceptions.":
type = type[11:]
file.write('%s: %s:\n' % (type, exc_value))
for fname, line, func, text in stack:
file.write(' File "%s", line %d:\n' % (fname, line))
file.write(' %s\n' % text)
def annotate(node):
"""Annotate a node with the stack frame describing the
SConscript file and line number that created it."""
tb = exc_tb = sys.exc_info()[2]
while tb and not tb.tb_frame.f_locals.has_key(stack_bottom):
tb = tb.tb_next
if not tb:
# We did not find any exec of an SConscript file: what?!
raise InternalError, "could not find SConscript stack frame"
node.creator = traceback.extract_stack(tb)[0]
# The following line would cause each Node to be annotated using the
# above function. Unfortunately, this is a *huge* performance hit, so
# leave this disabled until we find a more efficient mechanism.
#SCons.Node.Annotate = annotate
class SConsEnvironment(SCons.Environment.Base):
"""An Environment subclass that contains all of the methods that
are particular to the wrapper SCons interface and which aren't
(or shouldn't be) part of the build engine itself.
Note that not all of the methods of this class have corresponding
global functions, there are some private methods.
"""
#
# Private methods of an SConsEnvironment.
#
def _exceeds_version(self, major, minor, v_major, v_minor):
"""Return 1 if 'major' and 'minor' are greater than the version
in 'v_major' and 'v_minor', and 0 otherwise."""
return (major > v_major or (major == v_major and minor > v_minor))
def _get_major_minor(self, version_string):
"""Split a version string into major and minor parts. This
is complicated by the fact that a version string can be something
like 3.2b1."""
version = string.split(string.split(version_string, ' ')[0], '.')
v_major = int(version[0])
v_minor = int(re.match('\d+', version[1]).group())
return v_major, v_minor
def _get_SConscript_filenames(self, ls, kw):
"""
Convert the parameters passed to # SConscript() calls into a list
of files and export variables. If the parameters are invalid,
throws SCons.Errors.UserError. Returns a tuple (l, e) where l
is a list of SConscript filenames and e is a list of exports.
"""
exports = []
if len(ls) == 0:
try:
dirs = kw["dirs"]
except KeyError:
raise SCons.Errors.UserError, \
"Invalid SConscript usage - no parameters"
if not SCons.Util.is_List(dirs):
dirs = [ dirs ]
dirs = map(str, dirs)
name = kw.get('name', 'SConscript')
files = map(lambda n, name = name: os.path.join(n, name), dirs)
elif len(ls) == 1:
files = ls[0]
elif len(ls) == 2:
files = ls[0]
exports = self.Split(ls[1])
else:
raise SCons.Errors.UserError, \
"Invalid SConscript() usage - too many arguments"
if not SCons.Util.is_List(files):
files = [ files ]
if kw.get('exports'):
exports.extend(self.Split(kw['exports']))
build_dir = kw.get('build_dir')
if build_dir:
if len(files) != 1:
raise SCons.Errors.UserError, \
"Invalid SConscript() usage - can only specify one SConscript with a build_dir"
duplicate = kw.get('duplicate', 1)
src_dir = kw.get('src_dir')
if not src_dir:
src_dir, fname = os.path.split(str(files[0]))
else:
if not isinstance(src_dir, SCons.Node.Node):
src_dir = self.fs.Dir(src_dir)
fn = files[0]
if not isinstance(fn, SCons.Node.Node):
fn = self.fs.File(fn)
if fn.is_under(src_dir):
# Get path relative to the source directory.
fname = fn.get_path(src_dir)
else:
# Fast way to only get the terminal path component of a Node.
fname = fn.get_path(fn.dir)
self.fs.BuildDir(build_dir, src_dir, duplicate)
files = [os.path.join(str(build_dir), fname)]
return (files, exports)
#
# Public methods of an SConsEnvironment. These get
# entry points in the global name space so they can be called
# as global functions.
#
def Default(self, *targets):
global DefaultCalled
global DefaultTargets
DefaultCalled = 1
for t in targets:
if t is None:
# Delete the elements from the list in-place, don't
# reassign an empty list to DefaultTargets, so that the
# DEFAULT_TARGETS variable will still point to the
# same object we point to.
del DefaultTargets[:]
BuildTargets._clear()
elif isinstance(t, SCons.Node.Node):
DefaultTargets.append(t)
BuildTargets._add_Default([t])
else:
nodes = self.arg2nodes(t, self.fs.Entry)
DefaultTargets.extend(nodes)
BuildTargets._add_Default(nodes)
def EnsureSConsVersion(self, major, minor):
"""Exit abnormally if the SCons version is not late enough."""
v_major, v_minor = self._get_major_minor(SCons.__version__)
if self._exceeds_version(major, minor, v_major, v_minor):
print "SCons %d.%d or greater required, but you have SCons %s" %(major,minor,SCons.__version__)
sys.exit(2)
def EnsurePythonVersion(self, major, minor):
"""Exit abnormally if the Python version is not late enough."""
try:
v_major, v_minor, v_micro, release, serial = sys.version_info
except AttributeError:
v_major, v_minor = self._get_major_minor(sys.version)
if self._exceeds_version(major, minor, v_major, v_minor):
v = string.split(sys.version, " ", 1)[0]
print "Python %d.%d or greater required, but you have Python %s" %(major,minor,v)
sys.exit(2)
def Exit(self, value=0):
sys.exit(value)
def Export(self, *vars):
for var in vars:
global_exports.update(compute_exports(self.Split(var)))
def GetLaunchDir(self):
global launch_dir
return launch_dir
def GetOption(self, name):
name = self.subst(name)
return SCons.Script.ssoptions.get(name)
def Help(self, text):
text = self.subst(text, raw=1)
HelpFunction(text)
def Import(self, *vars):
try:
for var in vars:
var = self.Split(var)
for v in var:
if v == '*':
stack[-1].globals.update(global_exports)
stack[-1].globals.update(stack[-1].exports)
else:
if stack[-1].exports.has_key(v):
stack[-1].globals[v] = stack[-1].exports[v]
else:
stack[-1].globals[v] = global_exports[v]
except KeyError,x:
raise SCons.Errors.UserError, "Import of non-existent variable '%s'"%x
def SConscript(self, *ls, **kw):
def subst_element(x, subst=self.subst):
if SCons.Util.is_List(x):
x = map(subst, x)
else:
x = subst(x)
return x
ls = map(subst_element, ls)
subst_kw = {}
for key, val in kw.items():
if SCons.Util.is_String(val):
val = self.subst(val)
elif SCons.Util.is_List(val):
result = []
for v in val:
if SCons.Util.is_String(v):
v = self.subst(v)
result.append(v)
val = result
subst_kw[key] = val
files, exports = self._get_SConscript_filenames(ls, subst_kw)
return apply(_SConscript, [self.fs,] + files, {'exports' : exports})
def SConscriptChdir(self, flag):
global sconscript_chdir
sconscript_chdir = flag
def SetOption(self, name, value):
name = self.subst(name)
SCons.Script.ssoptions.set(name, value)
#
#
#
SCons.Environment.Environment = SConsEnvironment
def Options(files=None, args=Arguments):
return SCons.Options.Options(files, args)
#
_DefaultEnvironmentProxy = None
def get_DefaultEnvironmentProxy():
global _DefaultEnvironmentProxy
if not _DefaultEnvironmentProxy:
default_env = SCons.Defaults.DefaultEnvironment()
_DefaultEnvironmentProxy = SCons.Environment.NoSubstitutionProxy(default_env)
return _DefaultEnvironmentProxy
class DefaultEnvironmentCall:
"""A class that implements "global function" calls of
Environment methods by fetching the specified method from the
DefaultEnvironment's class. Note that this uses an intermediate
proxy class instead of calling the DefaultEnvironment method
directly so that the proxy can override the subst() method and
thereby prevent expansion of construction variables (since from
the user's point of view this was called as a global function,
with no associated construction environment)."""
def __init__(self, method_name):
self.method_name = method_name
def __call__(self, *args, **kw):
proxy = get_DefaultEnvironmentProxy()
method = getattr(proxy, self.method_name)
return apply(method, args, kw)
# The list of global functions to add to the SConscript name space
# that end up calling corresponding methods or Builders in the
# DefaultEnvironment().
GlobalDefaultEnvironmentFunctions = [
# Methods from the SConsEnvironment class, above.
'Default',
'EnsurePythonVersion',
'EnsureSConsVersion',
'Exit',
'Export',
'GetLaunchDir',
'GetOption',
'Help',
'Import',
'SConscript',
'SConscriptChdir',
'SetOption',
# Methods from the Environment.Base class.
'AddPostAction',
'AddPreAction',
'Alias',
'AlwaysBuild',
'BuildDir',
'CacheDir',
'Clean',
'Command',
'Depends',
'Dir',
'Execute',
'File',
'FindFile',
'Flatten',
'GetBuildPath',
'Ignore',
'Install',
'InstallAs',
'Literal',
'Local',
'ParseDepends',
'Precious',
'Repository',
'SConsignFile',
'SideEffect',
'SourceCode',
'SourceSignatures',
'Split',
'TargetSignatures',
'Value',
]
GlobalDefaultBuilders = [
# Supported builders.
'CFile',
'CXXFile',
'DVI',
'Jar',
'Java',
'JavaH',
'Library',
'M4',
'MSVSProject',
'Object',
'PCH',
'PDF',
'PostScript',
'Program',
'RES',
'RMIC',
'SharedLibrary',
'SharedObject',
'StaticLibrary',
'StaticObject',
'Tar',
'TypeLibrary',
'Zip',
]
for name in GlobalDefaultEnvironmentFunctions + GlobalDefaultBuilders:
GlobalDict[name] = DefaultEnvironmentCall(name)
def BuildDefaultGlobals():
"""
Create a dictionary containing all the default globals for
SConstruct and SConscript files.
"""
globals = {
# Global functions that don't get executed through the
# default Environment.
'Action' : SCons.Action.Action,
'BoolOption' : SCons.Options.BoolOption,
'Builder' : SCons.Builder.Builder,
'Configure' : SCons.SConf.SConf,
'EnumOption' : SCons.Options.EnumOption,
'Environment' : SCons.Environment.Environment,
'ListOption' : SCons.Options.ListOption,
'Options' : Options,
'PackageOption' : SCons.Options.PackageOption,
'PathOption' : SCons.Options.PathOption,
'Platform' : SCons.Platform.Platform,
'Return' : Return,
'Scanner' : SCons.Scanner.Base,
'Tool' : SCons.Tool.Tool,
'WhereIs' : SCons.Util.WhereIs,
# Action factories.
'Chmod' : SCons.Defaults.Chmod,
'Copy' : SCons.Defaults.Copy,
'Delete' : SCons.Defaults.Delete,
'Mkdir' : SCons.Defaults.Mkdir,
'Move' : SCons.Defaults.Move,
'Touch' : SCons.Defaults.Touch,
# Other variables we provide.
'ARGUMENTS' : Arguments,
'ARGLIST' : ArgList,
'BUILD_TARGETS' : BuildTargets,
'COMMAND_LINE_TARGETS' : CommandLineTargets,
'DEFAULT_TARGETS' : DefaultTargets,
}
# Functions we might still convert to Environment methods.
globals['CScan'] = SCons.Defaults.CScan
globals['DefaultEnvironment'] = SCons.Defaults.DefaultEnvironment
globals.update(GlobalDict)
return globals
|