You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2443 lines
95 KiB

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