PageRenderTime 33ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/couchjs/scons/scons-local-2.0.1/SCons/Environment.py

http://github.com/cloudant/bigcouch
Python | 2318 lines | 2060 code | 65 blank | 193 comment | 169 complexity | 2ee5e5fb396ce76ef1d2d08daddad50d MD5 | raw file
Possible License(s): Apache-2.0
  1. """SCons.Environment
  2. Base class for construction Environments. These are
  3. the primary objects used to communicate dependency and
  4. construction information to the build engine.
  5. Keyword arguments supplied when the construction Environment
  6. is created are construction variables used to initialize the
  7. Environment
  8. """
  9. #
  10. # Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 The SCons Foundation
  11. #
  12. # Permission is hereby granted, free of charge, to any person obtaining
  13. # a copy of this software and associated documentation files (the
  14. # "Software"), to deal in the Software without restriction, including
  15. # without limitation the rights to use, copy, modify, merge, publish,
  16. # distribute, sublicense, and/or sell copies of the Software, and to
  17. # permit persons to whom the Software is furnished to do so, subject to
  18. # the following conditions:
  19. #
  20. # The above copyright notice and this permission notice shall be included
  21. # in all copies or substantial portions of the Software.
  22. #
  23. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
  24. # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  25. # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. __revision__ = "src/engine/SCons/Environment.py 5134 2010/08/16 23:02:40 bdeegan"
  31. import copy
  32. import os
  33. import sys
  34. import re
  35. import shlex
  36. from collections import UserDict
  37. import SCons.Action
  38. import SCons.Builder
  39. from SCons.Debug import logInstanceCreation
  40. import SCons.Defaults
  41. import SCons.Errors
  42. import SCons.Memoize
  43. import SCons.Node
  44. import SCons.Node.Alias
  45. import SCons.Node.FS
  46. import SCons.Node.Python
  47. import SCons.Platform
  48. import SCons.SConf
  49. import SCons.SConsign
  50. import SCons.Subst
  51. import SCons.Tool
  52. import SCons.Util
  53. import SCons.Warnings
  54. class _Null(object):
  55. pass
  56. _null = _Null
  57. _warn_copy_deprecated = True
  58. _warn_source_signatures_deprecated = True
  59. _warn_target_signatures_deprecated = True
  60. CleanTargets = {}
  61. CalculatorArgs = {}
  62. semi_deepcopy = SCons.Util.semi_deepcopy
  63. # Pull UserError into the global name space for the benefit of
  64. # Environment().SourceSignatures(), which has some import statements
  65. # which seem to mess up its ability to reference SCons directly.
  66. UserError = SCons.Errors.UserError
  67. def alias_builder(env, target, source):
  68. pass
  69. AliasBuilder = SCons.Builder.Builder(action = alias_builder,
  70. target_factory = SCons.Node.Alias.default_ans.Alias,
  71. source_factory = SCons.Node.FS.Entry,
  72. multi = 1,
  73. is_explicit = None,
  74. name='AliasBuilder')
  75. def apply_tools(env, tools, toolpath):
  76. # Store the toolpath in the Environment.
  77. if toolpath is not None:
  78. env['toolpath'] = toolpath
  79. if not tools:
  80. return
  81. # Filter out null tools from the list.
  82. for tool in [_f for _f in tools if _f]:
  83. if SCons.Util.is_List(tool) or isinstance(tool, tuple):
  84. toolname = tool[0]
  85. toolargs = tool[1] # should be a dict of kw args
  86. tool = env.Tool(toolname, **toolargs)
  87. else:
  88. env.Tool(tool)
  89. # These names are (or will be) controlled by SCons; users should never
  90. # set or override them. This warning can optionally be turned off,
  91. # but scons will still ignore the illegal variable names even if it's off.
  92. reserved_construction_var_names = [
  93. 'CHANGED_SOURCES',
  94. 'CHANGED_TARGETS',
  95. 'SOURCE',
  96. 'SOURCES',
  97. 'TARGET',
  98. 'TARGETS',
  99. 'UNCHANGED_SOURCES',
  100. 'UNCHANGED_TARGETS',
  101. ]
  102. future_reserved_construction_var_names = [
  103. #'HOST_OS',
  104. #'HOST_ARCH',
  105. #'HOST_CPU',
  106. ]
  107. def copy_non_reserved_keywords(dict):
  108. result = semi_deepcopy(dict)
  109. for k in result.keys():
  110. if k in reserved_construction_var_names:
  111. msg = "Ignoring attempt to set reserved variable `$%s'"
  112. SCons.Warnings.warn(SCons.Warnings.ReservedVariableWarning, msg % k)
  113. del result[k]
  114. return result
  115. def _set_reserved(env, key, value):
  116. msg = "Ignoring attempt to set reserved variable `$%s'"
  117. SCons.Warnings.warn(SCons.Warnings.ReservedVariableWarning, msg % key)
  118. def _set_future_reserved(env, key, value):
  119. env._dict[key] = value
  120. msg = "`$%s' will be reserved in a future release and setting it will become ignored"
  121. SCons.Warnings.warn(SCons.Warnings.FutureReservedVariableWarning, msg % key)
  122. def _set_BUILDERS(env, key, value):
  123. try:
  124. bd = env._dict[key]
  125. for k in bd.keys():
  126. del bd[k]
  127. except KeyError:
  128. bd = BuilderDict(kwbd, env)
  129. env._dict[key] = bd
  130. for k, v in value.items():
  131. if not SCons.Builder.is_a_Builder(v):
  132. raise SCons.Errors.UserError('%s is not a Builder.' % repr(v))
  133. bd.update(value)
  134. def _del_SCANNERS(env, key):
  135. del env._dict[key]
  136. env.scanner_map_delete()
  137. def _set_SCANNERS(env, key, value):
  138. env._dict[key] = value
  139. env.scanner_map_delete()
  140. def _delete_duplicates(l, keep_last):
  141. """Delete duplicates from a sequence, keeping the first or last."""
  142. seen={}
  143. result=[]
  144. if keep_last: # reverse in & out, then keep first
  145. l.reverse()
  146. for i in l:
  147. try:
  148. if i not in seen:
  149. result.append(i)
  150. seen[i]=1
  151. except TypeError:
  152. # probably unhashable. Just keep it.
  153. result.append(i)
  154. if keep_last:
  155. result.reverse()
  156. return result
  157. # The following is partly based on code in a comment added by Peter
  158. # Shannon at the following page (there called the "transplant" class):
  159. #
  160. # ASPN : Python Cookbook : Dynamically added methods to a class
  161. # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/81732
  162. #
  163. # We had independently been using the idiom as BuilderWrapper, but
  164. # factoring out the common parts into this base class, and making
  165. # BuilderWrapper a subclass that overrides __call__() to enforce specific
  166. # Builder calling conventions, simplified some of our higher-layer code.
  167. class MethodWrapper(object):
  168. """
  169. A generic Wrapper class that associates a method (which can
  170. actually be any callable) with an object. As part of creating this
  171. MethodWrapper object an attribute with the specified (by default,
  172. the name of the supplied method) is added to the underlying object.
  173. When that new "method" is called, our __call__() method adds the
  174. object as the first argument, simulating the Python behavior of
  175. supplying "self" on method calls.
  176. We hang on to the name by which the method was added to the underlying
  177. base class so that we can provide a method to "clone" ourselves onto
  178. a new underlying object being copied (without which we wouldn't need
  179. to save that info).
  180. """
  181. def __init__(self, object, method, name=None):
  182. if name is None:
  183. name = method.__name__
  184. self.object = object
  185. self.method = method
  186. self.name = name
  187. setattr(self.object, name, self)
  188. def __call__(self, *args, **kwargs):
  189. nargs = (self.object,) + args
  190. return self.method(*nargs, **kwargs)
  191. def clone(self, new_object):
  192. """
  193. Returns an object that re-binds the underlying "method" to
  194. the specified new object.
  195. """
  196. return self.__class__(new_object, self.method, self.name)
  197. class BuilderWrapper(MethodWrapper):
  198. """
  199. A MethodWrapper subclass that that associates an environment with
  200. a Builder.
  201. This mainly exists to wrap the __call__() function so that all calls
  202. to Builders can have their argument lists massaged in the same way
  203. (treat a lone argument as the source, treat two arguments as target
  204. then source, make sure both target and source are lists) without
  205. having to have cut-and-paste code to do it.
  206. As a bit of obsessive backwards compatibility, we also intercept
  207. attempts to get or set the "env" or "builder" attributes, which were
  208. the names we used before we put the common functionality into the
  209. MethodWrapper base class. We'll keep this around for a while in case
  210. people shipped Tool modules that reached into the wrapper (like the
  211. Tool/qt.py module does, or did). There shouldn't be a lot attribute
  212. fetching or setting on these, so a little extra work shouldn't hurt.
  213. """
  214. def __call__(self, target=None, source=_null, *args, **kw):
  215. if source is _null:
  216. source = target
  217. target = None
  218. if target is not None and not SCons.Util.is_List(target):
  219. target = [target]
  220. if source is not None and not SCons.Util.is_List(source):
  221. source = [source]
  222. return MethodWrapper.__call__(self, target, source, *args, **kw)
  223. def __repr__(self):
  224. return '<BuilderWrapper %s>' % repr(self.name)
  225. def __str__(self):
  226. return self.__repr__()
  227. def __getattr__(self, name):
  228. if name == 'env':
  229. return self.object
  230. elif name == 'builder':
  231. return self.method
  232. else:
  233. raise AttributeError(name)
  234. def __setattr__(self, name, value):
  235. if name == 'env':
  236. self.object = value
  237. elif name == 'builder':
  238. self.method = value
  239. else:
  240. self.__dict__[name] = value
  241. # This allows a Builder to be executed directly
  242. # through the Environment to which it's attached.
  243. # In practice, we shouldn't need this, because
  244. # builders actually get executed through a Node.
  245. # But we do have a unit test for this, and can't
  246. # yet rule out that it would be useful in the
  247. # future, so leave it for now.
  248. #def execute(self, **kw):
  249. # kw['env'] = self.env
  250. # self.builder.execute(**kw)
  251. class BuilderDict(UserDict):
  252. """This is a dictionary-like class used by an Environment to hold
  253. the Builders. We need to do this because every time someone changes
  254. the Builders in the Environment's BUILDERS dictionary, we must
  255. update the Environment's attributes."""
  256. def __init__(self, dict, env):
  257. # Set self.env before calling the superclass initialization,
  258. # because it will end up calling our other methods, which will
  259. # need to point the values in this dictionary to self.env.
  260. self.env = env
  261. UserDict.__init__(self, dict)
  262. def __semi_deepcopy__(self):
  263. return self.__class__(self.data, self.env)
  264. def __setitem__(self, item, val):
  265. try:
  266. method = getattr(self.env, item).method
  267. except AttributeError:
  268. pass
  269. else:
  270. self.env.RemoveMethod(method)
  271. UserDict.__setitem__(self, item, val)
  272. BuilderWrapper(self.env, val, item)
  273. def __delitem__(self, item):
  274. UserDict.__delitem__(self, item)
  275. delattr(self.env, item)
  276. def update(self, dict):
  277. for i, v in dict.items():
  278. self.__setitem__(i, v)
  279. _is_valid_var = re.compile(r'[_a-zA-Z]\w*$')
  280. def is_valid_construction_var(varstr):
  281. """Return if the specified string is a legitimate construction
  282. variable.
  283. """
  284. return _is_valid_var.match(varstr)
  285. class SubstitutionEnvironment(object):
  286. """Base class for different flavors of construction environments.
  287. This class contains a minimal set of methods that handle contruction
  288. variable expansion and conversion of strings to Nodes, which may or
  289. may not be actually useful as a stand-alone class. Which methods
  290. ended up in this class is pretty arbitrary right now. They're
  291. basically the ones which we've empirically determined are common to
  292. the different construction environment subclasses, and most of the
  293. others that use or touch the underlying dictionary of construction
  294. variables.
  295. Eventually, this class should contain all the methods that we
  296. determine are necessary for a "minimal" interface to the build engine.
  297. A full "native Python" SCons environment has gotten pretty heavyweight
  298. with all of the methods and Tools and construction variables we've
  299. jammed in there, so it would be nice to have a lighter weight
  300. alternative for interfaces that don't need all of the bells and
  301. whistles. (At some point, we'll also probably rename this class
  302. "Base," since that more reflects what we want this class to become,
  303. but because we've released comments that tell people to subclass
  304. Environment.Base to create their own flavors of construction
  305. environment, we'll save that for a future refactoring when this
  306. class actually becomes useful.)
  307. """
  308. if SCons.Memoize.use_memoizer:
  309. __metaclass__ = SCons.Memoize.Memoized_Metaclass
  310. def __init__(self, **kw):
  311. """Initialization of an underlying SubstitutionEnvironment class.
  312. """
  313. if __debug__: logInstanceCreation(self, 'Environment.SubstitutionEnvironment')
  314. self.fs = SCons.Node.FS.get_default_fs()
  315. self.ans = SCons.Node.Alias.default_ans
  316. self.lookup_list = SCons.Node.arg2nodes_lookups
  317. self._dict = kw.copy()
  318. self._init_special()
  319. self.added_methods = []
  320. #self._memo = {}
  321. def _init_special(self):
  322. """Initial the dispatch tables for special handling of
  323. special construction variables."""
  324. self._special_del = {}
  325. self._special_del['SCANNERS'] = _del_SCANNERS
  326. self._special_set = {}
  327. for key in reserved_construction_var_names:
  328. self._special_set[key] = _set_reserved
  329. for key in future_reserved_construction_var_names:
  330. self._special_set[key] = _set_future_reserved
  331. self._special_set['BUILDERS'] = _set_BUILDERS
  332. self._special_set['SCANNERS'] = _set_SCANNERS
  333. # Freeze the keys of self._special_set in a list for use by
  334. # methods that need to check. (Empirically, list scanning has
  335. # gotten better than dict.has_key() in Python 2.5.)
  336. self._special_set_keys = list(self._special_set.keys())
  337. def __cmp__(self, other):
  338. return cmp(self._dict, other._dict)
  339. def __delitem__(self, key):
  340. special = self._special_del.get(key)
  341. if special:
  342. special(self, key)
  343. else:
  344. del self._dict[key]
  345. def __getitem__(self, key):
  346. return self._dict[key]
  347. def __setitem__(self, key, value):
  348. # This is heavily used. This implementation is the best we have
  349. # according to the timings in bench/env.__setitem__.py.
  350. #
  351. # The "key in self._special_set_keys" test here seems to perform
  352. # pretty well for the number of keys we have. A hard-coded
  353. # list works a little better in Python 2.5, but that has the
  354. # disadvantage of maybe getting out of sync if we ever add more
  355. # variable names. Using self._special_set.has_key() works a
  356. # little better in Python 2.4, but is worse than this test.
  357. # So right now it seems like a good trade-off, but feel free to
  358. # revisit this with bench/env.__setitem__.py as needed (and
  359. # as newer versions of Python come out).
  360. if key in self._special_set_keys:
  361. self._special_set[key](self, key, value)
  362. else:
  363. # If we already have the entry, then it's obviously a valid
  364. # key and we don't need to check. If we do check, using a
  365. # global, pre-compiled regular expression directly is more
  366. # efficient than calling another function or a method.
  367. if key not in self._dict \
  368. and not _is_valid_var.match(key):
  369. raise SCons.Errors.UserError("Illegal construction variable `%s'" % key)
  370. self._dict[key] = value
  371. def get(self, key, default=None):
  372. """Emulates the get() method of dictionaries."""
  373. return self._dict.get(key, default)
  374. def has_key(self, key):
  375. return key in self._dict
  376. def __contains__(self, key):
  377. return self._dict.__contains__(key)
  378. def items(self):
  379. return list(self._dict.items())
  380. def arg2nodes(self, args, node_factory=_null, lookup_list=_null, **kw):
  381. if node_factory is _null:
  382. node_factory = self.fs.File
  383. if lookup_list is _null:
  384. lookup_list = self.lookup_list
  385. if not args:
  386. return []
  387. args = SCons.Util.flatten(args)
  388. nodes = []
  389. for v in args:
  390. if SCons.Util.is_String(v):
  391. n = None
  392. for l in lookup_list:
  393. n = l(v)
  394. if n is not None:
  395. break
  396. if n is not None:
  397. if SCons.Util.is_String(n):
  398. # n = self.subst(n, raw=1, **kw)
  399. kw['raw'] = 1
  400. n = self.subst(n, **kw)
  401. if node_factory:
  402. n = node_factory(n)
  403. if SCons.Util.is_List(n):
  404. nodes.extend(n)
  405. else:
  406. nodes.append(n)
  407. elif node_factory:
  408. # v = node_factory(self.subst(v, raw=1, **kw))
  409. kw['raw'] = 1
  410. v = node_factory(self.subst(v, **kw))
  411. if SCons.Util.is_List(v):
  412. nodes.extend(v)
  413. else:
  414. nodes.append(v)
  415. else:
  416. nodes.append(v)
  417. return nodes
  418. def gvars(self):
  419. return self._dict
  420. def lvars(self):
  421. return {}
  422. def subst(self, string, raw=0, target=None, source=None, conv=None, executor=None):
  423. """Recursively interpolates construction variables from the
  424. Environment into the specified string, returning the expanded
  425. result. Construction variables are specified by a $ prefix
  426. in the string and begin with an initial underscore or
  427. alphabetic character followed by any number of underscores
  428. or alphanumeric characters. The construction variable names
  429. may be surrounded by curly braces to separate the name from
  430. trailing characters.
  431. """
  432. gvars = self.gvars()
  433. lvars = self.lvars()
  434. lvars['__env__'] = self
  435. if executor:
  436. lvars.update(executor.get_lvars())
  437. return SCons.Subst.scons_subst(string, self, raw, target, source, gvars, lvars, conv)
  438. def subst_kw(self, kw, raw=0, target=None, source=None):
  439. nkw = {}
  440. for k, v in kw.items():
  441. k = self.subst(k, raw, target, source)
  442. if SCons.Util.is_String(v):
  443. v = self.subst(v, raw, target, source)
  444. nkw[k] = v
  445. return nkw
  446. def subst_list(self, string, raw=0, target=None, source=None, conv=None, executor=None):
  447. """Calls through to SCons.Subst.scons_subst_list(). See
  448. the documentation for that function."""
  449. gvars = self.gvars()
  450. lvars = self.lvars()
  451. lvars['__env__'] = self
  452. if executor:
  453. lvars.update(executor.get_lvars())
  454. return SCons.Subst.scons_subst_list(string, self, raw, target, source, gvars, lvars, conv)
  455. def subst_path(self, path, target=None, source=None):
  456. """Substitute a path list, turning EntryProxies into Nodes
  457. and leaving Nodes (and other objects) as-is."""
  458. if not SCons.Util.is_List(path):
  459. path = [path]
  460. def s(obj):
  461. """This is the "string conversion" routine that we have our
  462. substitutions use to return Nodes, not strings. This relies
  463. on the fact that an EntryProxy object has a get() method that
  464. returns the underlying Node that it wraps, which is a bit of
  465. architectural dependence that we might need to break or modify
  466. in the future in response to additional requirements."""
  467. try:
  468. get = obj.get
  469. except AttributeError:
  470. obj = SCons.Util.to_String_for_subst(obj)
  471. else:
  472. obj = get()
  473. return obj
  474. r = []
  475. for p in path:
  476. if SCons.Util.is_String(p):
  477. p = self.subst(p, target=target, source=source, conv=s)
  478. if SCons.Util.is_List(p):
  479. if len(p) == 1:
  480. p = p[0]
  481. else:
  482. # We have an object plus a string, or multiple
  483. # objects that we need to smush together. No choice
  484. # but to make them into a string.
  485. p = ''.join(map(SCons.Util.to_String_for_subst, p))
  486. else:
  487. p = s(p)
  488. r.append(p)
  489. return r
  490. subst_target_source = subst
  491. def backtick(self, command):
  492. import subprocess
  493. # common arguments
  494. kw = { 'stdin' : 'devnull',
  495. 'stdout' : subprocess.PIPE,
  496. 'stderr' : subprocess.PIPE,
  497. 'universal_newlines' : True,
  498. }
  499. # if the command is a list, assume it's been quoted
  500. # othewise force a shell
  501. if not SCons.Util.is_List(command): kw['shell'] = True
  502. # run constructed command
  503. p = SCons.Action._subproc(self, command, **kw)
  504. out,err = p.communicate()
  505. status = p.wait()
  506. if err:
  507. sys.stderr.write(unicode(err))
  508. if status:
  509. raise OSError("'%s' exited %d" % (command, status))
  510. return out
  511. def AddMethod(self, function, name=None):
  512. """
  513. Adds the specified function as a method of this construction
  514. environment with the specified name. If the name is omitted,
  515. the default name is the name of the function itself.
  516. """
  517. method = MethodWrapper(self, function, name)
  518. self.added_methods.append(method)
  519. def RemoveMethod(self, function):
  520. """
  521. Removes the specified function's MethodWrapper from the
  522. added_methods list, so we don't re-bind it when making a clone.
  523. """
  524. self.added_methods = [dm for dm in self.added_methods if not dm.method is function]
  525. def Override(self, overrides):
  526. """
  527. Produce a modified environment whose variables are overriden by
  528. the overrides dictionaries. "overrides" is a dictionary that
  529. will override the variables of this environment.
  530. This function is much more efficient than Clone() or creating
  531. a new Environment because it doesn't copy the construction
  532. environment dictionary, it just wraps the underlying construction
  533. environment, and doesn't even create a wrapper object if there
  534. are no overrides.
  535. """
  536. if not overrides: return self
  537. o = copy_non_reserved_keywords(overrides)
  538. if not o: return self
  539. overrides = {}
  540. merges = None
  541. for key, value in o.items():
  542. if key == 'parse_flags':
  543. merges = value
  544. else:
  545. overrides[key] = SCons.Subst.scons_subst_once(value, self, key)
  546. env = OverrideEnvironment(self, overrides)
  547. if merges: env.MergeFlags(merges)
  548. return env
  549. def ParseFlags(self, *flags):
  550. """
  551. Parse the set of flags and return a dict with the flags placed
  552. in the appropriate entry. The flags are treated as a typical
  553. set of command-line flags for a GNU-like toolchain and used to
  554. populate the entries in the dict immediately below. If one of
  555. the flag strings begins with a bang (exclamation mark), it is
  556. assumed to be a command and the rest of the string is executed;
  557. the result of that evaluation is then added to the dict.
  558. """
  559. dict = {
  560. 'ASFLAGS' : SCons.Util.CLVar(''),
  561. 'CFLAGS' : SCons.Util.CLVar(''),
  562. 'CCFLAGS' : SCons.Util.CLVar(''),
  563. 'CPPDEFINES' : [],
  564. 'CPPFLAGS' : SCons.Util.CLVar(''),
  565. 'CPPPATH' : [],
  566. 'FRAMEWORKPATH' : SCons.Util.CLVar(''),
  567. 'FRAMEWORKS' : SCons.Util.CLVar(''),
  568. 'LIBPATH' : [],
  569. 'LIBS' : [],
  570. 'LINKFLAGS' : SCons.Util.CLVar(''),
  571. 'RPATH' : [],
  572. }
  573. def do_parse(arg):
  574. # if arg is a sequence, recurse with each element
  575. if not arg:
  576. return
  577. if not SCons.Util.is_String(arg):
  578. for t in arg: do_parse(t)
  579. return
  580. # if arg is a command, execute it
  581. if arg[0] == '!':
  582. arg = self.backtick(arg[1:])
  583. # utility function to deal with -D option
  584. def append_define(name, dict = dict):
  585. t = name.split('=')
  586. if len(t) == 1:
  587. dict['CPPDEFINES'].append(name)
  588. else:
  589. dict['CPPDEFINES'].append([t[0], '='.join(t[1:])])
  590. # Loop through the flags and add them to the appropriate option.
  591. # This tries to strike a balance between checking for all possible
  592. # flags and keeping the logic to a finite size, so it doesn't
  593. # check for some that don't occur often. It particular, if the
  594. # flag is not known to occur in a config script and there's a way
  595. # of passing the flag to the right place (by wrapping it in a -W
  596. # flag, for example) we don't check for it. Note that most
  597. # preprocessor options are not handled, since unhandled options
  598. # are placed in CCFLAGS, so unless the preprocessor is invoked
  599. # separately, these flags will still get to the preprocessor.
  600. # Other options not currently handled:
  601. # -iqoutedir (preprocessor search path)
  602. # -u symbol (linker undefined symbol)
  603. # -s (linker strip files)
  604. # -static* (linker static binding)
  605. # -shared* (linker dynamic binding)
  606. # -symbolic (linker global binding)
  607. # -R dir (deprecated linker rpath)
  608. # IBM compilers may also accept -qframeworkdir=foo
  609. params = shlex.split(arg)
  610. append_next_arg_to = None # for multi-word args
  611. for arg in params:
  612. if append_next_arg_to:
  613. if append_next_arg_to == 'CPPDEFINES':
  614. append_define(arg)
  615. elif append_next_arg_to == '-include':
  616. t = ('-include', self.fs.File(arg))
  617. dict['CCFLAGS'].append(t)
  618. elif append_next_arg_to == '-isysroot':
  619. t = ('-isysroot', arg)
  620. dict['CCFLAGS'].append(t)
  621. dict['LINKFLAGS'].append(t)
  622. elif append_next_arg_to == '-arch':
  623. t = ('-arch', arg)
  624. dict['CCFLAGS'].append(t)
  625. dict['LINKFLAGS'].append(t)
  626. else:
  627. dict[append_next_arg_to].append(arg)
  628. append_next_arg_to = None
  629. elif not arg[0] in ['-', '+']:
  630. dict['LIBS'].append(self.fs.File(arg))
  631. elif arg[:2] == '-L':
  632. if arg[2:]:
  633. dict['LIBPATH'].append(arg[2:])
  634. else:
  635. append_next_arg_to = 'LIBPATH'
  636. elif arg[:2] == '-l':
  637. if arg[2:]:
  638. dict['LIBS'].append(arg[2:])
  639. else:
  640. append_next_arg_to = 'LIBS'
  641. elif arg[:2] == '-I':
  642. if arg[2:]:
  643. dict['CPPPATH'].append(arg[2:])
  644. else:
  645. append_next_arg_to = 'CPPPATH'
  646. elif arg[:4] == '-Wa,':
  647. dict['ASFLAGS'].append(arg[4:])
  648. dict['CCFLAGS'].append(arg)
  649. elif arg[:4] == '-Wl,':
  650. if arg[:11] == '-Wl,-rpath=':
  651. dict['RPATH'].append(arg[11:])
  652. elif arg[:7] == '-Wl,-R,':
  653. dict['RPATH'].append(arg[7:])
  654. elif arg[:6] == '-Wl,-R':
  655. dict['RPATH'].append(arg[6:])
  656. else:
  657. dict['LINKFLAGS'].append(arg)
  658. elif arg[:4] == '-Wp,':
  659. dict['CPPFLAGS'].append(arg)
  660. elif arg[:2] == '-D':
  661. if arg[2:]:
  662. append_define(arg[2:])
  663. else:
  664. append_next_arg_to = 'CPPDEFINES'
  665. elif arg == '-framework':
  666. append_next_arg_to = 'FRAMEWORKS'
  667. elif arg[:14] == '-frameworkdir=':
  668. dict['FRAMEWORKPATH'].append(arg[14:])
  669. elif arg[:2] == '-F':
  670. if arg[2:]:
  671. dict['FRAMEWORKPATH'].append(arg[2:])
  672. else:
  673. append_next_arg_to = 'FRAMEWORKPATH'
  674. elif arg == '-mno-cygwin':
  675. dict['CCFLAGS'].append(arg)
  676. dict['LINKFLAGS'].append(arg)
  677. elif arg == '-mwindows':
  678. dict['LINKFLAGS'].append(arg)
  679. elif arg == '-pthread':
  680. dict['CCFLAGS'].append(arg)
  681. dict['LINKFLAGS'].append(arg)
  682. elif arg[:5] == '-std=':
  683. dict['CFLAGS'].append(arg) # C only
  684. elif arg[0] == '+':
  685. dict['CCFLAGS'].append(arg)
  686. dict['LINKFLAGS'].append(arg)
  687. elif arg in ['-include', '-isysroot', '-arch']:
  688. append_next_arg_to = arg
  689. else:
  690. dict['CCFLAGS'].append(arg)
  691. for arg in flags:
  692. do_parse(arg)
  693. return dict
  694. def MergeFlags(self, args, unique=1, dict=None):
  695. """
  696. Merge the dict in args into the construction variables of this
  697. env, or the passed-in dict. If args is not a dict, it is
  698. converted into a dict using ParseFlags. If unique is not set,
  699. the flags are appended rather than merged.
  700. """
  701. if dict is None:
  702. dict = self
  703. if not SCons.Util.is_Dict(args):
  704. args = self.ParseFlags(args)
  705. if not unique:
  706. self.Append(**args)
  707. return self
  708. for key, value in args.items():
  709. if not value:
  710. continue
  711. try:
  712. orig = self[key]
  713. except KeyError:
  714. orig = value
  715. else:
  716. if not orig:
  717. orig = value
  718. elif value:
  719. # Add orig and value. The logic here was lifted from
  720. # part of env.Append() (see there for a lot of comments
  721. # about the order in which things are tried) and is
  722. # used mainly to handle coercion of strings to CLVar to
  723. # "do the right thing" given (e.g.) an original CCFLAGS
  724. # string variable like '-pipe -Wall'.
  725. try:
  726. orig = orig + value
  727. except (KeyError, TypeError):
  728. try:
  729. add_to_orig = orig.append
  730. except AttributeError:
  731. value.insert(0, orig)
  732. orig = value
  733. else:
  734. add_to_orig(value)
  735. t = []
  736. if key[-4:] == 'PATH':
  737. ### keep left-most occurence
  738. for v in orig:
  739. if v not in t:
  740. t.append(v)
  741. else:
  742. ### keep right-most occurence
  743. orig.reverse()
  744. for v in orig:
  745. if v not in t:
  746. t.insert(0, v)
  747. self[key] = t
  748. return self
  749. # def MergeShellPaths(self, args, prepend=1):
  750. # """
  751. # Merge the dict in args into the shell environment in env['ENV'].
  752. # Shell path elements are appended or prepended according to prepend.
  753. # Uses Pre/AppendENVPath, so it always appends or prepends uniquely.
  754. # Example: env.MergeShellPaths({'LIBPATH': '/usr/local/lib'})
  755. # prepends /usr/local/lib to env['ENV']['LIBPATH'].
  756. # """
  757. # for pathname, pathval in args.items():
  758. # if not pathval:
  759. # continue
  760. # if prepend:
  761. # self.PrependENVPath(pathname, pathval)
  762. # else:
  763. # self.AppendENVPath(pathname, pathval)
  764. def default_decide_source(dependency, target, prev_ni):
  765. f = SCons.Defaults.DefaultEnvironment().decide_source
  766. return f(dependency, target, prev_ni)
  767. def default_decide_target(dependency, target, prev_ni):
  768. f = SCons.Defaults.DefaultEnvironment().decide_target
  769. return f(dependency, target, prev_ni)
  770. def default_copy_from_cache(src, dst):
  771. f = SCons.Defaults.DefaultEnvironment().copy_from_cache
  772. return f(src, dst)
  773. class Base(SubstitutionEnvironment):
  774. """Base class for "real" construction Environments. These are the
  775. primary objects used to communicate dependency and construction
  776. information to the build engine.
  777. Keyword arguments supplied when the construction Environment
  778. is created are construction variables used to initialize the
  779. Environment.
  780. """
  781. memoizer_counters = []
  782. #######################################################################
  783. # This is THE class for interacting with the SCons build engine,
  784. # and it contains a lot of stuff, so we're going to try to keep this
  785. # a little organized by grouping the methods.
  786. #######################################################################
  787. #######################################################################
  788. # Methods that make an Environment act like a dictionary. These have
  789. # the expected standard names for Python mapping objects. Note that
  790. # we don't actually make an Environment a subclass of UserDict for
  791. # performance reasons. Note also that we only supply methods for
  792. # dictionary functionality that we actually need and use.
  793. #######################################################################
  794. def __init__(self,
  795. platform=None,
  796. tools=None,
  797. toolpath=None,
  798. variables=None,
  799. parse_flags = None,
  800. **kw):
  801. """
  802. Initialization of a basic SCons construction environment,
  803. including setting up special construction variables like BUILDER,
  804. PLATFORM, etc., and searching for and applying available Tools.
  805. Note that we do *not* call the underlying base class
  806. (SubsitutionEnvironment) initialization, because we need to
  807. initialize things in a very specific order that doesn't work
  808. with the much simpler base class initialization.
  809. """
  810. if __debug__: logInstanceCreation(self, 'Environment.Base')
  811. self._memo = {}
  812. self.fs = SCons.Node.FS.get_default_fs()
  813. self.ans = SCons.Node.Alias.default_ans
  814. self.lookup_list = SCons.Node.arg2nodes_lookups
  815. self._dict = semi_deepcopy(SCons.Defaults.ConstructionEnvironment)
  816. self._init_special()
  817. self.added_methods = []
  818. # We don't use AddMethod, or define these as methods in this
  819. # class, because we *don't* want these functions to be bound
  820. # methods. They need to operate independently so that the
  821. # settings will work properly regardless of whether a given
  822. # target ends up being built with a Base environment or an
  823. # OverrideEnvironment or what have you.
  824. self.decide_target = default_decide_target
  825. self.decide_source = default_decide_source
  826. self.copy_from_cache = default_copy_from_cache
  827. self._dict['BUILDERS'] = BuilderDict(self._dict['BUILDERS'], self)
  828. if platform is None:
  829. platform = self._dict.get('PLATFORM', None)
  830. if platform is None:
  831. platform = SCons.Platform.Platform()
  832. if SCons.Util.is_String(platform):
  833. platform = SCons.Platform.Platform(platform)
  834. self._dict['PLATFORM'] = str(platform)
  835. platform(self)
  836. self._dict['HOST_OS'] = self._dict.get('HOST_OS',None)
  837. self._dict['HOST_ARCH'] = self._dict.get('HOST_ARCH',None)
  838. # Now set defaults for TARGET_{OS|ARCH}
  839. self._dict['TARGET_OS'] = self._dict.get('HOST_OS',None)
  840. self._dict['TARGET_ARCH'] = self._dict.get('HOST_ARCH',None)
  841. # Apply the passed-in and customizable variables to the
  842. # environment before calling the tools, because they may use
  843. # some of them during initialization.
  844. if 'options' in kw:
  845. # Backwards compatibility: they may stll be using the
  846. # old "options" keyword.
  847. variables = kw['options']
  848. del kw['options']
  849. self.Replace(**kw)
  850. keys = list(kw.keys())
  851. if variables:
  852. keys = keys + list(variables.keys())
  853. variables.Update(self)
  854. save = {}
  855. for k in keys:
  856. try:
  857. save[k] = self._dict[k]
  858. except KeyError:
  859. # No value may have been set if they tried to pass in a
  860. # reserved variable name like TARGETS.
  861. pass
  862. SCons.Tool.Initializers(self)
  863. if tools is None:
  864. tools = self._dict.get('TOOLS', None)
  865. if tools is None:
  866. tools = ['default']
  867. apply_tools(self, tools, toolpath)
  868. # Now restore the passed-in and customized variables
  869. # to the environment, since the values the user set explicitly
  870. # should override any values set by the tools.
  871. for key, val in save.items():
  872. self._dict[key] = val
  873. # Finally, apply any flags to be merged in
  874. if parse_flags: self.MergeFlags(parse_flags)
  875. #######################################################################
  876. # Utility methods that are primarily for internal use by SCons.
  877. # These begin with lower-case letters.
  878. #######################################################################
  879. def get_builder(self, name):
  880. """Fetch the builder with the specified name from the environment.
  881. """
  882. try:
  883. return self._dict['BUILDERS'][name]
  884. except KeyError:
  885. return None
  886. def get_CacheDir(self):
  887. try:
  888. path = self._CacheDir_path
  889. except AttributeError:
  890. path = SCons.Defaults.DefaultEnvironment()._CacheDir_path
  891. try:
  892. if path == self._last_CacheDir_path:
  893. return self._last_CacheDir
  894. except AttributeError:
  895. pass
  896. cd = SCons.CacheDir.CacheDir(path)
  897. self._last_CacheDir_path = path
  898. self._last_CacheDir = cd
  899. return cd
  900. def get_factory(self, factory, default='File'):
  901. """Return a factory function for creating Nodes for this
  902. construction environment.
  903. """
  904. name = default
  905. try:
  906. is_node = issubclass(factory, SCons.Node.FS.Base)
  907. except TypeError:
  908. # The specified factory isn't a Node itself--it's
  909. # most likely None, or possibly a callable.
  910. pass
  911. else:
  912. if is_node:
  913. # The specified factory is a Node (sub)class. Try to
  914. # return the FS method that corresponds to the Node's
  915. # name--that is, we return self.fs.Dir if they want a Dir,
  916. # self.fs.File for a File, etc.
  917. try: name = factory.__name__
  918. except AttributeError: pass
  919. else: factory = None
  920. if not factory:
  921. # They passed us None, or we picked up a name from a specified
  922. # class, so return the FS method. (Note that we *don't*
  923. # use our own self.{Dir,File} methods because that would
  924. # cause env.subst() to be called twice on the file name,
  925. # interfering with files that have $$ in them.)
  926. factory = getattr(self.fs, name)
  927. return factory
  928. memoizer_counters.append(SCons.Memoize.CountValue('_gsm'))
  929. def _gsm(self):
  930. try:
  931. return self._memo['_gsm']
  932. except KeyError:
  933. pass
  934. result = {}
  935. try:
  936. scanners = self._dict['SCANNERS']
  937. except KeyError:
  938. pass
  939. else:
  940. # Reverse the scanner list so that, if multiple scanners
  941. # claim they can scan the same suffix, earlier scanners
  942. # in the list will overwrite later scanners, so that
  943. # the result looks like a "first match" to the user.
  944. if not SCons.Util.is_List(scanners):
  945. scanners = [scanners]
  946. else:
  947. scanners = scanners[:] # copy so reverse() doesn't mod original
  948. scanners.reverse()
  949. for scanner in scanners:
  950. for k in scanner.get_skeys(self):
  951. if k and self['PLATFORM'] == 'win32':
  952. k = k.lower()
  953. result[k] = scanner
  954. self._memo['_gsm'] = result
  955. return result
  956. def get_scanner(self, skey):
  957. """Find the appropriate scanner given a key (usually a file suffix).
  958. """
  959. if skey and self['PLATFORM'] == 'win32':
  960. skey = skey.lower()
  961. return self._gsm().get(skey)
  962. def scanner_map_delete(self, kw=None):
  963. """Delete the cached scanner map (if we need to).
  964. """
  965. try:
  966. del self._memo['_gsm']
  967. except KeyError:
  968. pass
  969. def _update(self, dict):
  970. """Update an environment's values directly, bypassing the normal
  971. checks that occur when users try to set items.
  972. """
  973. self._dict.update(dict)
  974. def get_src_sig_type(self):
  975. try:
  976. return self.src_sig_type
  977. except AttributeError:
  978. t = SCons.Defaults.DefaultEnvironment().src_sig_type
  979. self.src_sig_type = t
  980. return t
  981. def get_tgt_sig_type(self):
  982. try:
  983. return self.tgt_sig_type
  984. except AttributeError:
  985. t = SCons.Defaults.DefaultEnvironment().tgt_sig_type
  986. self.tgt_sig_type = t
  987. return t
  988. #######################################################################
  989. # Public methods for manipulating an Environment. These begin with
  990. # upper-case letters. The essential characteristic of methods in
  991. # this section is that they do *not* have corresponding same-named
  992. # global functions. For example, a stand-alone Append() function
  993. # makes no sense, because Append() is all about appending values to
  994. # an Environment's construction variables.
  995. #######################################################################
  996. def Append(self, **kw):
  997. """Append values to existing construction variables
  998. in an Environment.
  999. """
  1000. kw = copy_non_reserved_keywords(kw)
  1001. for key, val in kw.items():
  1002. # It would be easier on the eyes to write this using
  1003. # "continue" statements whenever we finish processing an item,
  1004. # but Python 1.5.2 apparently doesn't let you use "continue"
  1005. # within try:-except: blocks, so we have to nest our code.
  1006. try:
  1007. orig = self._dict[key]
  1008. except KeyError:
  1009. # No existing variable in the environment, so just set
  1010. # it to the new value.
  1011. self._dict[key] = val
  1012. else:
  1013. try:
  1014. # Check if the original looks like a dictionary.
  1015. # If it is, we can't just try adding the value because
  1016. # dictionaries don't have __add__() methods, and
  1017. # things like UserList will incorrectly coerce the
  1018. # original dict to a list (which we don't want).
  1019. update_dict = orig.update
  1020. except AttributeError:
  1021. try:
  1022. # Most straightforward: just try to add them
  1023. # together. This will work in most cases, when the
  1024. # original and new values are of compatible types.
  1025. self._dict[key] = orig + val
  1026. except (KeyError, TypeError):
  1027. try:
  1028. # Check if the original is a list.
  1029. add_to_orig = orig.append
  1030. except AttributeError:
  1031. # The original isn't a list, but the new
  1032. # value is (by process of elimination),
  1033. # so insert the original in the new value
  1034. # (if there's one to insert) and replace
  1035. # the variable with it.
  1036. if orig:
  1037. val.insert(0, orig)
  1038. self._dict[key] = val
  1039. else:
  1040. # The original is a list, so append the new
  1041. # value to it (if there's a value to append).
  1042. if val:
  1043. add_to_orig(val)
  1044. else:
  1045. # The original looks like a dictionary, so update it
  1046. # based on what we think the value looks like.
  1047. if SCons.Util.is_List(val):
  1048. for v in val:
  1049. orig[v] = None
  1050. else:
  1051. try:
  1052. update_dict(val)
  1053. except (AttributeError, TypeError, ValueError):
  1054. if SCons.Util.is_Dict(val):
  1055. for k, v in val.items():
  1056. orig[k] = v
  1057. else:
  1058. orig[val] = None
  1059. self.scanner_map_delete(kw)
  1060. # allow Dirs and strings beginning with # for top-relative
  1061. # Note this uses the current env's fs (in self).
  1062. def _canonicalize(self, path):
  1063. if not SCons.Util.is_String(path): # typically a Dir
  1064. path = str(path)
  1065. if path and path[0] == '#':
  1066. path = str(self.fs.Dir(path))
  1067. return path
  1068. def AppendENVPath(self, name, newpath, envname = 'ENV',
  1069. sep = os.pathsep, delete_existing=1):
  1070. """Append path elements to the path 'name' in the 'ENV'
  1071. dictionary for this environment. Will only add any particular
  1072. path once, and will normpath and normcase all paths to help
  1073. assure this. This can also handle the case where the env
  1074. variable is a list instead of a string.
  1075. If delete_existing is 0, a newpath which is already in the path
  1076. will not be moved to the end (it will be left where it is).
  1077. """
  1078. orig = ''
  1079. if envname in self._dict and name in self._dict[envname]:
  1080. orig = self._dict[envname][name]
  1081. nv = SCons.Util.AppendPath(orig, newpath, sep, delete_existing,
  1082. canonicalize=self._canonicalize)
  1083. if envname not in self._dict:
  1084. self._dict[envname] = {}
  1085. self._dict[envname][name] = nv
  1086. def AppendUnique(self, delete_existing=0, **kw):
  1087. """Append values to existing construction variables
  1088. in an Environment, if they're not already there.
  1089. If delete_existing is 1, removes existing values first, so
  1090. values move to end.
  1091. """
  1092. kw = copy_non_reserved_keywords(kw)
  1093. for key, val in kw.items():
  1094. if SCons.Util.is_List(val):
  1095. val = _delete_duplicates(val, delete_existing)
  1096. if key not in self._dict or self._dict[key] in ('', None):
  1097. self._dict[key] = val
  1098. elif SCons.Util.is_Dict(self._dict[key]) and \
  1099. SCons.Util.is_Dict(val):
  1100. self._dict[key].update(val)
  1101. elif SCons.Util.is_List(val):
  1102. dk = self._dict[key]
  1103. if not SCons.Util.is_List(dk):
  1104. dk = [dk]
  1105. if delete_existing:
  1106. dk = [x for x in dk if x not in val]
  1107. else:
  1108. val = [x for x in val if x not in dk]
  1109. self._dict[key] = dk + val
  1110. else:
  1111. dk = self._dict[key]
  1112. if SCons.Util.is_List(dk):
  1113. # By elimination, val is not a list. Since dk is a
  1114. # list, wrap val in a list first.
  1115. if delete_existing:
  1116. dk = [x for x in dk if x not in val]
  1117. self._dict[key] = dk + [val]
  1118. else:
  1119. if not val in dk:
  1120. self._dict[key] = dk + [val]
  1121. else:
  1122. if delete_existing:
  1123. dk = [x for x in dk if x not in val]
  1124. self._dict[key] = dk + val
  1125. self.scanner_map_delete(kw)
  1126. def Clone(self, tools=[], toolpath=None, parse_flags = None, **kw):
  1127. """Return a copy of a construction Environment. The
  1128. copy is like a Python "deep copy"--that is, independent
  1129. copies are made recursively of each objects--except that
  1130. a reference is copied when an object is not deep-copyable
  1131. (like a function). There are no references to any mutable
  1132. objects in the original Environment.
  1133. """
  1134. clone = copy.copy(self)
  1135. clone._dict = semi_deepcopy(self._dict)
  1136. try:
  1137. cbd = clone._dict['BUILDERS']
  1138. except KeyError:
  1139. pass
  1140. else:
  1141. clone._dict['BUILDERS'] = BuilderDict(cbd, clone)
  1142. # Check the methods added via AddMethod() and re-bind them to
  1143. # the cloned environment. Only do this if the attribute hasn't
  1144. # been overwritten by the user explicitly and still points to
  1145. # the added method.
  1146. clone.added_methods = []
  1147. for mw in self.added_methods:
  1148. if mw == getattr(self, mw.name):
  1149. clone.added_methods.append(mw.clone(clone))
  1150. clone._memo = {}
  1151. # Apply passed-in variables before the tools
  1152. # so the tools can use the new variables
  1153. kw = copy_non_reserved_keywords(kw)
  1154. new = {}
  1155. for key, value in kw.items():
  1156. new[key] = SCons.Subst.scons_subst_once(value, self, key)
  1157. clone.Replace(**new)
  1158. apply_tools(clone, tools, toolpath)
  1159. # apply them again in case the tools overwrote them
  1160. clone.Replace(**new)
  1161. # Finally, apply any flags to be merged in
  1162. if parse_flags: clone.MergeFlags(parse_flags)
  1163. if __debug__: logInstanceCreation(self, 'Environment.EnvironmentClone')
  1164. return clone
  1165. def Copy(self, *args, **kw):
  1166. global _warn_copy_deprecated
  1167. if _warn_copy_deprecated:
  1168. msg = "The env.Copy() method is deprecated; use the env.Clone() method instead."
  1169. SCons.Warnings.warn(SCons.Warnings.DeprecatedCopyWarning, msg)
  1170. _warn_copy_deprecated = False
  1171. return self.Clone(*args, **kw)
  1172. def _changed_build(self, dependency, target, prev_ni):
  1173. if dependency.changed_state(target, prev_ni):
  1174. return 1
  1175. return self.decide_source(dependency, target, prev_ni)
  1176. def _changed_content(self, dependency, target, prev_ni):
  1177. return dependency.changed_content(target, prev_ni)
  1178. def _changed_source(self, dependency, target, prev_ni):
  1179. target_env = dependency.get_build_env()
  1180. type = target_env.get_tgt_sig_type()
  1181. if type == 'source':
  1182. return target_env.decide_source(dependency, target, prev_ni)
  1183. else:
  1184. return target_env.decide_target(dependency, target, prev_ni)
  1185. def _changed_timestamp_then_content(self, dependency, target, prev_ni):
  1186. return dependency.changed_timestamp_then_content(target, prev_ni)
  1187. def _changed_timestamp_newer(self, dependency, target, prev_ni):
  1188. return dependency.changed_timestamp_newer(target, prev_ni)
  1189. def _changed_timestamp_match(self, dependency, target, prev_ni):
  1190. return dependency.changed_timestamp_match(target, prev_ni)
  1191. def _copy_from_cache(self, src, dst):
  1192. return self.fs.copy(src, dst)
  1193. def _copy2_from_cache(self, src, dst):
  1194. return self.fs.copy2(src, dst)
  1195. def Decider(self, function):
  1196. copy_function = self._copy2_from_cache
  1197. if function in ('MD5', 'content'):
  1198. if not SCons.Util.md5:
  1199. raise UserError("MD5 signatures are not available in this version of Python.")
  1200. function = self._changed_content
  1201. elif function == 'MD5-timestamp':
  1202. function = self._changed_timestamp_then_content
  1203. elif function in ('timestamp-newer', 'make'):
  1204. function = self._changed_timestamp_newer
  1205. copy_function = self._copy_from_cache
  1206. elif function == 'timestamp-match':
  1207. function = self._changed_timestamp_match
  1208. elif not callable(function):
  1209. raise UserError("Unknown Decider value %s" % repr(function))
  1210. # We don't use AddMethod because we don't want to turn the
  1211. # function, which only expects three arguments, into a bound
  1212. # method, which would add self as an initial, fourth argument.
  1213. self.decide_target = function
  1214. self.decide_source = function
  1215. self.copy_from_cache = copy_function
  1216. def Detect(self, progs):
  1217. """Return the first available program in progs.
  1218. """
  1219. if not SCons.Util.is_List(progs):
  1220. progs = [ progs ]
  1221. for prog in progs:
  1222. path = self.WhereIs(prog)
  1223. if path: return prog
  1224. return None
  1225. def Dictionary(self, *args):
  1226. if not args:
  1227. return self._dict
  1228. dlist = [self._dict[x] for x in args]
  1229. if len(dlist) == 1:
  1230. dlist = dlist[0]
  1231. return dlist
  1232. def Dump(self, key = None):
  1233. """
  1234. Using the standard Python pretty printer, dump the contents of the
  1235. scons build environment to stdout.
  1236. If the key passed in is anything other than None, then that will
  1237. be used as an index into the build environment dictionary and
  1238. whatever is found there will be fed into the pretty printer. Note
  1239. that this key is case sensitive.
  1240. """
  1241. import pprint
  1242. pp = pprint.PrettyPrinter(indent=2)
  1243. if key:
  1244. dict = self.Dictionary(key)
  1245. else:
  1246. dict = self.Dictionary()
  1247. return pp.pformat(dict)
  1248. def FindIxes(self, paths, prefix, suffix):
  1249. """
  1250. Search a list of paths for something that matches the prefix and suffix.
  1251. paths - the list of paths or nodes.
  1252. prefix - construction variable for the prefix.
  1253. suffix - construction variable for the suffix.
  1254. """
  1255. suffix = self.subst('$'+suffix)
  1256. prefix = self.subst('$'+prefix)
  1257. for path in paths:
  1258. dir,name = os.path.split(str(path))
  1259. if name[:len(prefix)] == prefix and name[-len(suffix):] == suffix:
  1260. return path
  1261. def ParseConfig(self, command, function=None, unique=1):
  1262. """
  1263. Use the specified function to parse the output of the command
  1264. in order to modify the current environment. The 'command' can
  1265. be a string or a list of strings representing a command and
  1266. its arguments. 'Function' is an optional argument that takes
  1267. the environment, the output of the command, and the unique flag.
  1268. If no function is specified, MergeFlags, which treats the output
  1269. as the result of a typical 'X-config' command (i.e. gtk-config),
  1270. will merge the output into the appropriate variables.
  1271. """
  1272. if function is None:
  1273. def parse_conf(env, cmd, unique=unique):
  1274. return env.MergeFlags(cmd, unique)
  1275. function = parse_conf
  1276. if SCons.Util.is_List(command):
  1277. command = ' '.join(command)
  1278. command = self.subst(command)
  1279. return function(self, self.backtick(command))
  1280. def ParseDepends(self, filename, must_exist=None, only_one=0):
  1281. """
  1282. Parse a mkdep-style file for explicit dependencies. This is
  1283. completely abusable, and should be unnecessary in the "normal"
  1284. case of proper SCons configuration, but it may help make
  1285. the transition from a Make hierarchy easier for some people
  1286. to swallow. It can also be genuinely useful when using a tool
  1287. that can write a .d file, but for which writing a scanner would
  1288. be too complicated.
  1289. """
  1290. filename = self.subst(filename)
  1291. try:
  1292. fp = open(filename, 'r')
  1293. except IOError:
  1294. if must_exist:
  1295. raise
  1296. return
  1297. lines = SCons.Util.LogicalLines(fp).readlines()
  1298. lines = [l for l in lines if l[0] != '#']
  1299. tdlist = []
  1300. for line in lines:
  1301. try:
  1302. target, depends = line.split(':', 1)
  1303. except (AttributeError, ValueError):
  1304. # Throws AttributeError if line isn't a string. Can throw
  1305. # ValueError if line doesn't split into two or more elements.
  1306. pass
  1307. else:
  1308. tdlist.append((target.split(), depends.split()))
  1309. if only_one:
  1310. targets = []
  1311. for td in tdlist:
  1312. targets.extend(td[0])
  1313. if len(targets) > 1:
  1314. raise SCons.Errors.UserError(
  1315. "More than one dependency target found in `%s': %s"
  1316. % (filename, targets))
  1317. for target, depends in tdlist:
  1318. self.Depends(target, depends)
  1319. def Platform(self, platform):
  1320. platform = self.subst(platform)
  1321. return SCons.Platform.Platform(platform)(self)
  1322. def Prepend(self, **kw):
  1323. """Prepend values to existing construction variables
  1324. in an Environment.
  1325. """
  1326. kw = copy_non_reserved_keywords(kw)
  1327. for key, val in kw.items():
  1328. # It would be easier on the eyes to write this using
  1329. # "continue" statements whenever we finish processing an item,
  1330. # but Python 1.5.2 apparently doesn't let you use "continue"
  1331. # within try:-except: blocks, so we have to nest our code.
  1332. try:
  1333. orig = self._dict[key]
  1334. except KeyError:
  1335. # No existing variable in the environment, so just set
  1336. # it to the new value.
  1337. self._dict[key] = val
  1338. else:
  1339. try:
  1340. # Check if the original looks like a dictionary.
  1341. # If it is, we can't just try adding the value because
  1342. # dictionaries don't have __add__() methods, and
  1343. # things like UserList will incorrectly coerce the
  1344. # original dict to a list (which we don't want).
  1345. update_dict = orig.update
  1346. except AttributeError:
  1347. try:
  1348. # Most straightforward: just try to add them
  1349. # together. This will work in most cases, when the
  1350. # original and new values are of compatible types.
  1351. self._dict[key] = val + orig
  1352. except (KeyError, TypeError):
  1353. try:
  1354. # Check if the added value is a list.
  1355. add_to_val = val.append
  1356. except AttributeError:
  1357. # The added value isn't a list, but the
  1358. # original is (by process of elimination),
  1359. # so insert the the new value in the original
  1360. # (if there's one to insert).
  1361. if val:
  1362. orig.insert(0, val)
  1363. else:
  1364. # The added value is a list, so append
  1365. # the original to it (if there's a value
  1366. # to append).
  1367. if orig:
  1368. add_to_val(orig)
  1369. self._dict[key] = val
  1370. else:
  1371. # The original looks like a dictionary, so update it
  1372. # based on what we think the value looks like.
  1373. if SCons.Util.is_List(val):
  1374. for v in val:
  1375. orig[v] = None
  1376. else:
  1377. try:
  1378. update_dict(val)
  1379. except (AttributeError, TypeError, ValueError):
  1380. if SCons.Util.is_Dict(val):
  1381. for k, v in val.items():
  1382. orig[k] = v
  1383. else:
  1384. orig[val] = None
  1385. self.scanner_map_delete(kw)
  1386. def PrependENVPath(self, name, newpath, envname = 'ENV', sep = os.pathsep,
  1387. delete_existing=1):
  1388. """Prepend path elements to the path 'name' in the 'ENV'
  1389. dictionary for this environment. Will only add any particular
  1390. path once, and will normpath and normcase all paths to help
  1391. assure this. This can also handle the case where the env
  1392. variable is a list instead of a string.
  1393. If delete_existing is 0, a newpath which is already in the path
  1394. will not be moved to the front (it will be left where it is).
  1395. """
  1396. orig = ''
  1397. if envname in self._dict and name in self._dict[envname]:
  1398. orig = self._dict[envname][name]
  1399. nv = SCons.Util.PrependPath(orig, newpath, sep, delete_existing,
  1400. canonicalize=self._canonicalize)
  1401. if envname not in self._dict:
  1402. self._dict[envname] = {}
  1403. self._dict[envname][name] = nv
  1404. def PrependUnique(self, delete_existing=0, **kw):
  1405. """Prepend values to existing construction variables
  1406. in an Environment, if they're not already there.
  1407. If delete_existing is 1, removes existing values first, so
  1408. values move to front.
  1409. """
  1410. kw = copy_non_reserved_keywords(kw)
  1411. for key, val in kw.items():
  1412. if SCons.Util.is_List(val):
  1413. val = _delete_duplicates(val, not delete_existing)
  1414. if key not in self._dict or self._dict[key] in ('', None):
  1415. self._dict[key] = val
  1416. elif SCons.Util.is_Dict(self._dict[key]) and \
  1417. SCons.Util.is_Dict(val):
  1418. self._dict[key].update(val)
  1419. elif SCons.Util.is_List(val):
  1420. dk = self._dict[key]
  1421. if not SCons.Util.is_List(dk):
  1422. dk = [dk]
  1423. if delete_existing:
  1424. dk = [x for x in dk if x not in val]
  1425. else:
  1426. val = [x for x in val if x not in dk]
  1427. self._dict[key] = val + dk
  1428. else:
  1429. dk = self._dict[key]
  1430. if SCons.Util.is_List(dk):
  1431. # By elimination, val is not a list. Since dk is a
  1432. # list, wrap val in a list first.
  1433. if delete_existing:
  1434. dk = [x for x in dk if x not in val]
  1435. self._dict[key] = [val] + dk
  1436. else:
  1437. if not val in dk:
  1438. self._dict[key] = [val] + dk
  1439. else:
  1440. if delete_existing:
  1441. dk = [x for x in dk if x not in val]
  1442. self._dict[key] = val + dk
  1443. self.scanner_map_delete(kw)
  1444. def Replace(self, **kw):
  1445. """Replace existing construction variables in an Environment
  1446. with new construction variables and/or values.
  1447. """
  1448. try:
  1449. kwbd = kw['BUILDERS']
  1450. except KeyError:
  1451. pass
  1452. else:
  1453. kwbd = semi_deepcopy(kwbd)
  1454. del kw['BUILDERS']
  1455. self.__setitem__('BUILDERS', kwbd)
  1456. kw = copy_non_reserved_keywords(kw)
  1457. self._update(semi_deepcopy(kw))
  1458. self.scanner_map_delete(kw)
  1459. def ReplaceIxes(self, path, old_prefix, old_suffix, new_prefix, new_suffix):
  1460. """
  1461. Replace old_prefix with new_prefix and old_suffix with new_suffix.
  1462. env - Environment used to interpolate variables.
  1463. path - the path that will be modified.
  1464. old_prefix - construction variable for the old prefix.
  1465. old_suffix - construction variable for the old suffix.
  1466. new_prefix - construction variable for the new prefix.
  1467. new_suffix - construction variable for the new suffix.
  1468. """
  1469. old_prefix = self.subst('$'+old_prefix)
  1470. old_suffix = self.subst('$'+old_suffix)
  1471. new_prefix = self.subst('$'+new_prefix)
  1472. new_suffix = self.subst('$'+new_suffix)
  1473. dir,name = os.path.split(str(path))
  1474. if name[:len(old_prefix)] == old_prefix:
  1475. name = name[len(old_prefix):]
  1476. if name[-len(old_suffix):] == old_suffix:
  1477. name = name[:-len(old_suffix)]
  1478. return os.path.join(dir, new_prefix+name+new_suffix)
  1479. def SetDefault(self, **kw):
  1480. for k in kw.keys():
  1481. if k in self._dict:
  1482. del kw[k]
  1483. self.Replace(**kw)
  1484. def _find_toolpath_dir(self, tp):
  1485. return self.fs.Dir(self.subst(tp)).srcnode().abspath
  1486. def Tool(self, tool, toolpath=None, **kw):
  1487. if SCons.Util.is_String(tool):
  1488. tool = self.subst(tool)
  1489. if toolpath is None:
  1490. toolpath = self.get('toolpath', [])
  1491. toolpath = list(map(self._find_toolpath_dir, toolpath))
  1492. tool = SCons.Tool.Tool(tool, toolpath, **kw)
  1493. tool(self)
  1494. def WhereIs(self, prog, path=None, pathext=None, reject=[]):
  1495. """Find prog in the path.
  1496. """
  1497. if path is None:
  1498. try:
  1499. path = self['ENV']['PATH']
  1500. except KeyError:
  1501. pass
  1502. elif SCons.Util.is_String(path):
  1503. path = self.subst(path)
  1504. if pathext is None:
  1505. try:
  1506. pathext = self['ENV']['PATHEXT']
  1507. except KeyError:
  1508. pass
  1509. elif SCons.Util.is_String(pathext):
  1510. pathext = self.subst(pathext)
  1511. prog = self.subst(prog)
  1512. path = SCons.Util.WhereIs(prog, path, pathext, reject)
  1513. if path: return path
  1514. return None
  1515. #######################################################################
  1516. # Public methods for doing real "SCons stuff" (manipulating
  1517. # dependencies, setting attributes on targets, etc.). These begin
  1518. # with upper-case letters. The essential characteristic of methods
  1519. # in this section is that they all *should* have corresponding
  1520. # same-named global functions.
  1521. #######################################################################
  1522. def Action(self, *args, **kw):
  1523. def subst_string(a, self=self):
  1524. if SCons.Util.is_String(a):
  1525. a = self.subst(a)
  1526. return a
  1527. nargs = list(map(subst_string, args))
  1528. nkw = self.subst_kw(kw)
  1529. return SCons.Action.Action(*nargs, **nkw)
  1530. def AddPreAction(self, files, action):
  1531. nodes = self.arg2nodes(files, self.fs.Entry)
  1532. action = SCons.Action.Action(action)
  1533. uniq = {}
  1534. for executor in [n.get_executor() for n in nodes]:
  1535. uniq[executor] = 1
  1536. for executor in uniq.keys():
  1537. executor.add_pre_action(action)
  1538. return nodes
  1539. def AddPostAction(self, files, action):
  1540. nodes = self.arg2nodes(files, self.fs.Entry)
  1541. action = SCons.Action.Action(action)
  1542. uniq = {}
  1543. for executor in [n.get_executor() for n in nodes]:
  1544. uniq[executor] = 1
  1545. for executor in uniq.keys():
  1546. executor.add_post_action(action)
  1547. return nodes
  1548. def Alias(self, target, source=[], action=None, **kw):
  1549. tlist = self.arg2nodes(target, self.ans.Alias)
  1550. if not SCons.Util.is_List(source):
  1551. source = [source]
  1552. source = [_f for _f in source if _f]
  1553. if not action:
  1554. if not source:
  1555. # There are no source files and no action, so just
  1556. # return a target list of classic Alias Nodes, without
  1557. # any builder. The externally visible effect is that
  1558. # this will make the wrapping Script.BuildTask class
  1559. # say that there's "Nothing to be done" for this Alias,
  1560. # instead of that it's "up to date."
  1561. return tlist
  1562. # No action, but there are sources. Re-call all the target
  1563. # builders to add the sources to each target.
  1564. result = []
  1565. for t in tlist:
  1566. bld = t.get_builder(AliasBuilder)
  1567. result.extend(bld(self, t, source))
  1568. return result
  1569. nkw = self.subst_kw(kw)
  1570. nkw.update({
  1571. 'action' : SCons.Action.Action(action),
  1572. 'source_factory' : self.fs.Entry,
  1573. 'multi' : 1,
  1574. 'is_explicit' : None,
  1575. })
  1576. bld = SCons.Builder.Builder(**nkw)
  1577. # Apply the Builder separately to each target so that the Aliases
  1578. # stay separate. If we did one "normal" Builder call with the
  1579. # whole target list, then all of the target Aliases would be
  1580. # associated under a single Executor.
  1581. result = []
  1582. for t in tlist:
  1583. # Calling the convert() method will cause a new Executor to be
  1584. # created from scratch, so we have to explicitly initialize
  1585. # it with the target's existing sources, plus our new ones,
  1586. # so nothing gets lost.
  1587. b = t.get_builder()
  1588. if b is None or b is AliasBuilder:
  1589. b = bld
  1590. else:
  1591. nkw['action'] = b.action + action
  1592. b = SCons.Builder.Builder(**nkw)
  1593. t.convert()
  1594. result.extend(b(self, t, t.sources + source))
  1595. return result
  1596. def AlwaysBuild(self, *targets):
  1597. tlist = []
  1598. for t in targets:
  1599. tlist.extend(self.arg2nodes(t, self.fs.Entry))
  1600. for t in tlist:
  1601. t.set_always_build()
  1602. return tlist
  1603. def BuildDir(self, *args, **kw):
  1604. msg = """BuildDir() and the build_dir keyword have been deprecated;\n\tuse VariantDir() and the variant_dir keyword instead."""
  1605. SCons.Warnings.warn(SCons.Warnings.DeprecatedBuildDirWarning, msg)
  1606. if 'build_dir' in kw:
  1607. kw['variant_dir'] = kw['build_dir']
  1608. del kw['build_dir']
  1609. return self.VariantDir(*args, **kw)
  1610. def Builder(self, **kw):
  1611. nkw = self.subst_kw(kw)
  1612. return SCons.Builder.Builder(**nkw)
  1613. def CacheDir(self, path):
  1614. import SCons.CacheDir
  1615. if path is not None:
  1616. path = self.subst(path)
  1617. self._CacheDir_path = path
  1618. def Clean(self, targets, files):
  1619. global CleanTargets
  1620. tlist = self.arg2nodes(targets, self.fs.Entry)
  1621. flist = self.arg2nodes(files, self.fs.Entry)
  1622. for t in tlist:
  1623. try:
  1624. CleanTargets[t].extend(flist)
  1625. except KeyError:
  1626. CleanTargets[t] = flist
  1627. def Configure(self, *args, **kw):
  1628. nargs = [self]
  1629. if args:
  1630. nargs = nargs + self.subst_list(args)[0]
  1631. nkw = self.subst_kw(kw)
  1632. nkw['_depth'] = kw.get('_depth', 0) + 1
  1633. try:
  1634. nkw['custom_tests'] = self.subst_kw(nkw['custom_tests'])
  1635. except KeyError:
  1636. pass
  1637. return SCons.SConf.SConf(*nargs, **nkw)
  1638. def Command(self, target, source, action, **kw):
  1639. """Builds the supplied target files from the supplied
  1640. source files using the supplied action. Action may
  1641. be any type that the Builder constructor will accept
  1642. for an action."""
  1643. bkw = {
  1644. 'action' : action,
  1645. 'target_factory' : self.fs.Entry,
  1646. 'source_factory' : self.fs.Entry,
  1647. }
  1648. try: bkw['source_scanner'] = kw['source_scanner']
  1649. except KeyError: pass
  1650. else: del kw['source_scanner']
  1651. bld = SCons.Builder.Builder(**bkw)
  1652. return bld(self, target, source, **kw)
  1653. def Depends(self, target, dependency):
  1654. """Explicity specify that 'target's depend on 'dependency'."""
  1655. tlist = self.arg2nodes(target, self.fs.Entry)
  1656. dlist = self.arg2nodes(dependency, self.fs.Entry)
  1657. for t in tlist:
  1658. t.add_dependency(dlist)
  1659. return tlist
  1660. def Dir(self, name, *args, **kw):
  1661. """
  1662. """
  1663. s = self.subst(name)
  1664. if SCons.Util.is_Sequence(s):
  1665. result=[]
  1666. for e in s:
  1667. result.append(self.fs.Dir(e, *args, **kw))
  1668. return result
  1669. return self.fs.Dir(s, *args, **kw)
  1670. def NoClean(self, *targets):
  1671. """Tags a target so that it will not be cleaned by -c"""
  1672. tlist = []
  1673. for t in targets:
  1674. tlist.extend(self.arg2nodes(t, self.fs.Entry))
  1675. for t in tlist:
  1676. t.set_noclean()
  1677. return tlist
  1678. def NoCache(self, *targets):
  1679. """Tags a target so that it will not be cached"""
  1680. tlist = []
  1681. for t in targets:
  1682. tlist.extend(self.arg2nodes(t, self.fs.Entry))
  1683. for t in tlist:
  1684. t.set_nocache()
  1685. return tlist
  1686. def Entry(self, name, *args, **kw):
  1687. """
  1688. """
  1689. s = self.subst(name)
  1690. if SCons.Util.is_Sequence(s):
  1691. result=[]
  1692. for e in s:
  1693. result.append(self.fs.Entry(e, *args, **kw))
  1694. return result
  1695. return self.fs.Entry(s, *args, **kw)
  1696. def Environment(self, **kw):
  1697. return SCons.Environment.Environment(**self.subst_kw(kw))
  1698. def Execute(self, action, *args, **kw):
  1699. """Directly execute an action through an Environment
  1700. """
  1701. action = self.Action(action, *args, **kw)
  1702. result = action([], [], self)
  1703. if isinstance(result, SCons.Errors.BuildError):
  1704. errstr = result.errstr
  1705. if result.filename:
  1706. errstr = result.filename + ': ' + errstr
  1707. sys.stderr.write("scons: *** %s\n" % errstr)
  1708. return result.status
  1709. else:
  1710. return result
  1711. def File(self, name, *args, **kw):
  1712. """
  1713. """
  1714. s = self.subst(name)
  1715. if SCons.Util.is_Sequence(s):
  1716. result=[]
  1717. for e in s:
  1718. result.append(self.fs.File(e, *args, **kw))
  1719. return result
  1720. return self.fs.File(s, *args, **kw)
  1721. def FindFile(self, file, dirs):
  1722. file = self.subst(file)
  1723. nodes = self.arg2nodes(dirs, self.fs.Dir)
  1724. return SCons.Node.FS.find_file(file, tuple(nodes))
  1725. def Flatten(self, sequence):
  1726. return SCons.Util.flatten(sequence)
  1727. def GetBuildPath(self, files):
  1728. result = list(map(str, self.arg2nodes(files, self.fs.Entry)))
  1729. if SCons.Util.is_List(files):
  1730. return result
  1731. else:
  1732. return result[0]
  1733. def Glob(self, pattern, ondisk=True, source=False, strings=False):
  1734. return self.fs.Glob(self.subst(pattern), ondisk, source, strings)
  1735. def Ignore(self, target, dependency):
  1736. """Ignore a dependency."""
  1737. tlist = self.arg2nodes(target, self.fs.Entry)
  1738. dlist = self.arg2nodes(dependency, self.fs.Entry)
  1739. for t in tlist:
  1740. t.add_ignore(dlist)
  1741. return tlist
  1742. def Literal(self, string):
  1743. return SCons.Subst.Literal(string)
  1744. def Local(self, *targets):
  1745. ret = []
  1746. for targ in targets:
  1747. if isinstance(targ, SCons.Node.Node):
  1748. targ.set_local()
  1749. ret.append(targ)
  1750. else:
  1751. for t in self.arg2nodes(targ, self.fs.Entry):
  1752. t.set_local()
  1753. ret.append(t)
  1754. return ret
  1755. def Precious(self, *targets):
  1756. tlist = []
  1757. for t in targets:
  1758. tlist.extend(self.arg2nodes(t, self.fs.Entry))
  1759. for t in tlist:
  1760. t.set_precious()
  1761. return tlist
  1762. def Repository(self, *dirs, **kw):
  1763. dirs = self.arg2nodes(list(dirs), self.fs.Dir)
  1764. self.fs.Repository(*dirs, **kw)
  1765. def Requires(self, target, prerequisite):
  1766. """Specify that 'prerequisite' must be built before 'target',
  1767. (but 'target' does not actually depend on 'prerequisite'
  1768. and need not be rebuilt if it changes)."""
  1769. tlist = self.arg2nodes(target, self.fs.Entry)
  1770. plist = self.arg2nodes(prerequisite, self.fs.Entry)
  1771. for t in tlist:
  1772. t.add_prerequisite(plist)
  1773. return tlist
  1774. def Scanner(self, *args, **kw):
  1775. nargs = []
  1776. for arg in args:
  1777. if SCons.Util.is_String(arg):
  1778. arg = self.subst(arg)
  1779. nargs.append(arg)
  1780. nkw = self.subst_kw(kw)
  1781. return SCons.Scanner.Base(*nargs, **nkw)
  1782. def SConsignFile(self, name=".sconsign", dbm_module=None):
  1783. if name is not None:
  1784. name = self.subst(name)
  1785. if not os.path.isabs(name):
  1786. name = os.path.join(str(self.fs.SConstruct_dir), name)
  1787. if name:
  1788. name = os.path.normpath(name)
  1789. sconsign_dir = os.path.dirname(name)
  1790. if sconsign_dir and not os.path.exists(sconsign_dir):
  1791. self.Execute(SCons.Defaults.Mkdir(sconsign_dir))
  1792. SCons.SConsign.File(name, dbm_module)
  1793. def SideEffect(self, side_effect, target):
  1794. """Tell scons that side_effects are built as side
  1795. effects of building targets."""
  1796. side_effects = self.arg2nodes(side_effect, self.fs.Entry)
  1797. targets = self.arg2nodes(target, self.fs.Entry)
  1798. for side_effect in side_effects:
  1799. if side_effect.multiple_side_effect_has_builder():
  1800. raise SCons.Errors.UserError("Multiple ways to build the same target were specified for: %s" % str(side_effect))
  1801. side_effect.add_source(targets)
  1802. side_effect.side_effect = 1
  1803. self.Precious(side_effect)
  1804. for target in targets:
  1805. target.side_effects.append(side_effect)
  1806. return side_effects
  1807. def SourceCode(self, entry, builder):
  1808. """Arrange for a source code builder for (part of) a tree."""
  1809. msg = """SourceCode() has been deprecated and there is no replacement.
  1810. \tIf you need this function, please contact dev@scons.tigris.org."""
  1811. SCons.Warnings.warn(SCons.Warnings.DeprecatedSourceCodeWarning, msg)
  1812. entries = self.arg2nodes(entry, self.fs.Entry)
  1813. for entry in entries:
  1814. entry.set_src_builder(builder)
  1815. return entries
  1816. def SourceSignatures(self, type):
  1817. global _warn_source_signatures_deprecated
  1818. if _warn_source_signatures_deprecated:
  1819. msg = "The env.SourceSignatures() method is deprecated;\n" + \
  1820. "\tconvert your build to use the env.Decider() method instead."
  1821. SCons.Warnings.warn(SCons.Warnings.DeprecatedSourceSignaturesWarning, msg)
  1822. _warn_source_signatures_deprecated = False
  1823. type = self.subst(type)
  1824. self.src_sig_type = type
  1825. if type == 'MD5':
  1826. if not SCons.Util.md5:
  1827. raise UserError("MD5 signatures are not available in this version of Python.")
  1828. self.decide_source = self._changed_content
  1829. elif type == 'timestamp':
  1830. self.decide_source = self._changed_timestamp_match
  1831. else:
  1832. raise UserError("Unknown source signature type '%s'" % type)
  1833. def Split(self, arg):
  1834. """This function converts a string or list into a list of strings
  1835. or Nodes. This makes things easier for users by allowing files to
  1836. be specified as a white-space separated list to be split.
  1837. The input rules are:
  1838. - A single string containing names separated by spaces. These will be
  1839. split apart at the spaces.
  1840. - A single Node instance
  1841. - A list containing either strings or Node instances. Any strings
  1842. in the list are not split at spaces.
  1843. In all cases, the function returns a list of Nodes and strings."""
  1844. if SCons.Util.is_List(arg):
  1845. return list(map(self.subst, arg))
  1846. elif SCons.Util.is_String(arg):
  1847. return self.subst(arg).split()
  1848. else:
  1849. return [self.subst(arg)]
  1850. def TargetSignatures(self, type):
  1851. global _warn_target_signatures_deprecated
  1852. if _warn_target_signatures_deprecated:
  1853. msg = "The env.TargetSignatures() method is deprecated;\n" + \
  1854. "\tconvert your build to use the env.Decider() method instead."
  1855. SCons.Warnings.warn(SCons.Warnings.DeprecatedTargetSignaturesWarning, msg)
  1856. _warn_target_signatures_deprecated = False
  1857. type = self.subst(type)
  1858. self.tgt_sig_type = type
  1859. if type in ('MD5', 'content'):
  1860. if not SCons.Util.md5:
  1861. raise UserError("MD5 signatures are not available in this version of Python.")
  1862. self.decide_target = self._changed_content
  1863. elif type == 'timestamp':
  1864. self.decide_target = self._changed_timestamp_match
  1865. elif type == 'build':
  1866. self.decide_target = self._changed_build
  1867. elif type == 'source':
  1868. self.decide_target = self._changed_source
  1869. else:
  1870. raise UserError("Unknown target signature type '%s'"%type)
  1871. def Value(self, value, built_value=None):
  1872. """
  1873. """
  1874. return SCons.Node.Python.Value(value, built_value)
  1875. def VariantDir(self, variant_dir, src_dir, duplicate=1):
  1876. variant_dir = self.arg2nodes(variant_dir, self.fs.Dir)[0]
  1877. src_dir = self.arg2nodes(src_dir, self.fs.Dir)[0]
  1878. self.fs.VariantDir(variant_dir, src_dir, duplicate)
  1879. def FindSourceFiles(self, node='.'):
  1880. """ returns a list of all source files.
  1881. """
  1882. node = self.arg2nodes(node, self.fs.Entry)[0]
  1883. sources = []
  1884. def build_source(ss):
  1885. for s in ss:
  1886. if isinstance(s, SCons.Node.FS.Dir):
  1887. build_source(s.all_children())
  1888. elif s.has_builder():
  1889. build_source(s.sources)
  1890. elif isinstance(s.disambiguate(), SCons.Node.FS.File):
  1891. sources.append(s)
  1892. build_source(node.all_children())
  1893. # THIS CODE APPEARS TO HAVE NO EFFECT
  1894. # # get the final srcnode for all nodes, this means stripping any
  1895. # # attached build node by calling the srcnode function
  1896. # for file in sources:
  1897. # srcnode = file.srcnode()
  1898. # while srcnode != file.srcnode():
  1899. # srcnode = file.srcnode()
  1900. # remove duplicates
  1901. return list(set(sources))
  1902. def FindInstalledFiles(self):
  1903. """ returns the list of all targets of the Install and InstallAs Builder.
  1904. """
  1905. from SCons.Tool import install
  1906. if install._UNIQUE_INSTALLED_FILES is None:
  1907. install._UNIQUE_INSTALLED_FILES = SCons.Util.uniquer_hashables(install._INSTALLED_FILES)
  1908. return install._UNIQUE_INSTALLED_FILES
  1909. class OverrideEnvironment(Base):
  1910. """A proxy that overrides variables in a wrapped construction
  1911. environment by returning values from an overrides dictionary in
  1912. preference to values from the underlying subject environment.
  1913. This is a lightweight (I hope) proxy that passes through most use of
  1914. attributes to the underlying Environment.Base class, but has just
  1915. enough additional methods defined to act like a real construction
  1916. environment with overridden values. It can wrap either a Base
  1917. construction environment, or another OverrideEnvironment, which
  1918. can in turn nest arbitrary OverrideEnvironments...
  1919. Note that we do *not* call the underlying base class
  1920. (SubsitutionEnvironment) initialization, because we get most of those
  1921. from proxying the attributes of the subject construction environment.
  1922. But because we subclass SubstitutionEnvironment, this class also
  1923. has inherited arg2nodes() and subst*() methods; those methods can't
  1924. be proxied because they need *this* object's methods to fetch the
  1925. values from the overrides dictionary.
  1926. """
  1927. def __init__(self, subject, overrides={}):
  1928. if __debug__: logInstanceCreation(self, 'Environment.OverrideEnvironment')
  1929. self.__dict__['__subject'] = subject
  1930. self.__dict__['overrides'] = overrides
  1931. # Methods that make this class act like a proxy.
  1932. def __getattr__(self, name):
  1933. return getattr(self.__dict__['__subject'], name)
  1934. def __setattr__(self, name, value):
  1935. setattr(self.__dict__['__subject'], name, value)
  1936. # Methods that make this class act like a dictionary.
  1937. def __getitem__(self, key):
  1938. try:
  1939. return self.__dict__['overrides'][key]
  1940. except KeyError:
  1941. return self.__dict__['__subject'].__getitem__(key)
  1942. def __setitem__(self, key, value):
  1943. if not is_valid_construction_var(key):
  1944. raise SCons.Errors.UserError("Illegal construction variable `%s'" % key)
  1945. self.__dict__['overrides'][key] = value
  1946. def __delitem__(self, key):
  1947. try:
  1948. del self.__dict__['overrides'][key]
  1949. except KeyError:
  1950. deleted = 0
  1951. else:
  1952. deleted = 1
  1953. try:
  1954. result = self.__dict__['__subject'].__delitem__(key)
  1955. except KeyError:
  1956. if not deleted:
  1957. raise
  1958. result = None
  1959. return result
  1960. def get(self, key, default=None):
  1961. """Emulates the get() method of dictionaries."""
  1962. try:
  1963. return self.__dict__['overrides'][key]
  1964. except KeyError:
  1965. return self.__dict__['__subject'].get(key, default)
  1966. def has_key(self, key):
  1967. try:
  1968. self.__dict__['overrides'][key]
  1969. return 1
  1970. except KeyError:
  1971. return key in self.__dict__['__subject']
  1972. def __contains__(self, key):
  1973. if self.__dict__['overrides'].__contains__(key):
  1974. return 1
  1975. return self.__dict__['__subject'].__contains__(key)
  1976. def Dictionary(self):
  1977. """Emulates the items() method of dictionaries."""
  1978. d = self.__dict__['__subject'].Dictionary().copy()
  1979. d.update(self.__dict__['overrides'])
  1980. return d
  1981. def items(self):
  1982. """Emulates the items() method of dictionaries."""
  1983. return list(self.Dictionary().items())
  1984. # Overridden private construction environment methods.
  1985. def _update(self, dict):
  1986. """Update an environment's values directly, bypassing the normal
  1987. checks that occur when users try to set items.
  1988. """
  1989. self.__dict__['overrides'].update(dict)
  1990. def gvars(self):
  1991. return self.__dict__['__subject'].gvars()
  1992. def lvars(self):
  1993. lvars = self.__dict__['__subject'].lvars()
  1994. lvars.update(self.__dict__['overrides'])
  1995. return lvars
  1996. # Overridden public construction environment methods.
  1997. def Replace(self, **kw):
  1998. kw = copy_non_reserved_keywords(kw)
  1999. self.__dict__['overrides'].update(semi_deepcopy(kw))
  2000. # The entry point that will be used by the external world
  2001. # to refer to a construction environment. This allows the wrapper
  2002. # interface to extend a construction environment for its own purposes
  2003. # by subclassing SCons.Environment.Base and then assigning the
  2004. # class to SCons.Environment.Environment.
  2005. Environment = Base
  2006. # An entry point for returning a proxy subclass instance that overrides
  2007. # the subst*() methods so they don't actually perform construction
  2008. # variable substitution. This is specifically intended to be the shim
  2009. # layer in between global function calls (which don't want construction
  2010. # variable substitution) and the DefaultEnvironment() (which would
  2011. # substitute variables if left to its own devices)."""
  2012. #
  2013. # We have to wrap this in a function that allows us to delay definition of
  2014. # the class until it's necessary, so that when it subclasses Environment
  2015. # it will pick up whatever Environment subclass the wrapper interface
  2016. # might have assigned to SCons.Environment.Environment.
  2017. def NoSubstitutionProxy(subject):
  2018. class _NoSubstitutionProxy(Environment):
  2019. def __init__(self, subject):
  2020. self.__dict__['__subject'] = subject
  2021. def __getattr__(self, name):
  2022. return getattr(self.__dict__['__subject'], name)
  2023. def __setattr__(self, name, value):
  2024. return setattr(self.__dict__['__subject'], name, value)
  2025. def raw_to_mode(self, dict):
  2026. try:
  2027. raw = dict['raw']
  2028. except KeyError:
  2029. pass
  2030. else:
  2031. del dict['raw']
  2032. dict['mode'] = raw
  2033. def subst(self, string, *args, **kwargs):
  2034. return string
  2035. def subst_kw(self, kw, *args, **kwargs):
  2036. return kw
  2037. def subst_list(self, string, *args, **kwargs):
  2038. nargs = (string, self,) + args
  2039. nkw = kwargs.copy()
  2040. nkw['gvars'] = {}
  2041. self.raw_to_mode(nkw)
  2042. return SCons.Subst.scons_subst_list(*nargs, **nkw)
  2043. def subst_target_source(self, string, *args, **kwargs):
  2044. nargs = (string, self,) + args
  2045. nkw = kwargs.copy()
  2046. nkw['gvars'] = {}
  2047. self.raw_to_mode(nkw)
  2048. return SCons.Subst.scons_subst(*nargs, **nkw)
  2049. return _NoSubstitutionProxy(subject)
  2050. # Local Variables:
  2051. # tab-width:4
  2052. # indent-tabs-mode:nil
  2053. # End:
  2054. # vim: set expandtab tabstop=4 shiftwidth=4: