PageRenderTime 32ms CodeModel.GetById 21ms 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

Large files files are truncated, but you can click here to view the full file

  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

Large files files are truncated, but you can click here to view the full file