PageRenderTime 62ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/couchjs/scons/scons-local-2.0.1/SCons/Node/__init__.py

http://github.com/cloudant/bigcouch
Python | 1328 lines | 1310 code | 3 blank | 15 comment | 9 complexity | 0df07245a4b34da728d86f5952198a52 MD5 | raw file
Possible License(s): Apache-2.0
  1. """SCons.Node
  2. The Node package for the SCons software construction utility.
  3. This is, in many ways, the heart of SCons.
  4. A Node is where we encapsulate all of the dependency information about
  5. any thing that SCons can build, or about any thing which SCons can use
  6. to build some other thing. The canonical "thing," of course, is a file,
  7. but a Node can also represent something remote (like a web page) or
  8. something completely abstract (like an Alias).
  9. Each specific type of "thing" is specifically represented by a subclass
  10. of the Node base class: Node.FS.File for files, Node.Alias for aliases,
  11. etc. Dependency information is kept here in the base class, and
  12. information specific to files/aliases/etc. is in the subclass. The
  13. goal, if we've done this correctly, is that any type of "thing" should
  14. be able to depend on any other type of "thing."
  15. """
  16. #
  17. # Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 The SCons Foundation
  18. #
  19. # Permission is hereby granted, free of charge, to any person obtaining
  20. # a copy of this software and associated documentation files (the
  21. # "Software"), to deal in the Software without restriction, including
  22. # without limitation the rights to use, copy, modify, merge, publish,
  23. # distribute, sublicense, and/or sell copies of the Software, and to
  24. # permit persons to whom the Software is furnished to do so, subject to
  25. # the following conditions:
  26. #
  27. # The above copyright notice and this permission notice shall be included
  28. # in all copies or substantial portions of the Software.
  29. #
  30. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
  31. # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  32. # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  33. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  34. # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  35. # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  36. # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  37. __revision__ = "src/engine/SCons/Node/__init__.py 5134 2010/08/16 23:02:40 bdeegan"
  38. import collections
  39. import copy
  40. from itertools import chain
  41. from SCons.Debug import logInstanceCreation
  42. import SCons.Executor
  43. import SCons.Memoize
  44. import SCons.Util
  45. from SCons.Debug import Trace
  46. def classname(obj):
  47. return str(obj.__class__).split('.')[-1]
  48. # Node states
  49. #
  50. # These are in "priority" order, so that the maximum value for any
  51. # child/dependency of a node represents the state of that node if
  52. # it has no builder of its own. The canonical example is a file
  53. # system directory, which is only up to date if all of its children
  54. # were up to date.
  55. no_state = 0
  56. pending = 1
  57. executing = 2
  58. up_to_date = 3
  59. executed = 4
  60. failed = 5
  61. StateString = {
  62. 0 : "no_state",
  63. 1 : "pending",
  64. 2 : "executing",
  65. 3 : "up_to_date",
  66. 4 : "executed",
  67. 5 : "failed",
  68. }
  69. # controls whether implicit dependencies are cached:
  70. implicit_cache = 0
  71. # controls whether implicit dep changes are ignored:
  72. implicit_deps_unchanged = 0
  73. # controls whether the cached implicit deps are ignored:
  74. implicit_deps_changed = 0
  75. # A variable that can be set to an interface-specific function be called
  76. # to annotate a Node with information about its creation.
  77. def do_nothing(node): pass
  78. Annotate = do_nothing
  79. # Classes for signature info for Nodes.
  80. class NodeInfoBase(object):
  81. """
  82. The generic base class for signature information for a Node.
  83. Node subclasses should subclass NodeInfoBase to provide their own
  84. logic for dealing with their own Node-specific signature information.
  85. """
  86. current_version_id = 1
  87. def __init__(self, node=None):
  88. # Create an object attribute from the class attribute so it ends up
  89. # in the pickled data in the .sconsign file.
  90. self._version_id = self.current_version_id
  91. def update(self, node):
  92. try:
  93. field_list = self.field_list
  94. except AttributeError:
  95. return
  96. for f in field_list:
  97. try:
  98. delattr(self, f)
  99. except AttributeError:
  100. pass
  101. try:
  102. func = getattr(node, 'get_' + f)
  103. except AttributeError:
  104. pass
  105. else:
  106. setattr(self, f, func())
  107. def convert(self, node, val):
  108. pass
  109. def merge(self, other):
  110. self.__dict__.update(other.__dict__)
  111. def format(self, field_list=None, names=0):
  112. if field_list is None:
  113. try:
  114. field_list = self.field_list
  115. except AttributeError:
  116. field_list = sorted(self.__dict__.keys())
  117. fields = []
  118. for field in field_list:
  119. try:
  120. f = getattr(self, field)
  121. except AttributeError:
  122. f = None
  123. f = str(f)
  124. if names:
  125. f = field + ': ' + f
  126. fields.append(f)
  127. return fields
  128. class BuildInfoBase(object):
  129. """
  130. The generic base class for build information for a Node.
  131. This is what gets stored in a .sconsign file for each target file.
  132. It contains a NodeInfo instance for this node (signature information
  133. that's specific to the type of Node) and direct attributes for the
  134. generic build stuff we have to track: sources, explicit dependencies,
  135. implicit dependencies, and action information.
  136. """
  137. current_version_id = 1
  138. def __init__(self, node=None):
  139. # Create an object attribute from the class attribute so it ends up
  140. # in the pickled data in the .sconsign file.
  141. self._version_id = self.current_version_id
  142. self.bsourcesigs = []
  143. self.bdependsigs = []
  144. self.bimplicitsigs = []
  145. self.bactsig = None
  146. def merge(self, other):
  147. self.__dict__.update(other.__dict__)
  148. class Node(object):
  149. """The base Node class, for entities that we know how to
  150. build, or use to build other Nodes.
  151. """
  152. if SCons.Memoize.use_memoizer:
  153. __metaclass__ = SCons.Memoize.Memoized_Metaclass
  154. memoizer_counters = []
  155. class Attrs(object):
  156. pass
  157. def __init__(self):
  158. if __debug__: logInstanceCreation(self, 'Node.Node')
  159. # Note that we no longer explicitly initialize a self.builder
  160. # attribute to None here. That's because the self.builder
  161. # attribute may be created on-the-fly later by a subclass (the
  162. # canonical example being a builder to fetch a file from a
  163. # source code system like CVS or Subversion).
  164. # Each list of children that we maintain is accompanied by a
  165. # dictionary used to look up quickly whether a node is already
  166. # present in the list. Empirical tests showed that it was
  167. # fastest to maintain them as side-by-side Node attributes in
  168. # this way, instead of wrapping up each list+dictionary pair in
  169. # a class. (Of course, we could always still do that in the
  170. # future if we had a good reason to...).
  171. self.sources = [] # source files used to build node
  172. self.sources_set = set()
  173. self._specific_sources = False
  174. self.depends = [] # explicit dependencies (from Depends)
  175. self.depends_set = set()
  176. self.ignore = [] # dependencies to ignore
  177. self.ignore_set = set()
  178. self.prerequisites = SCons.Util.UniqueList()
  179. self.implicit = None # implicit (scanned) dependencies (None means not scanned yet)
  180. self.waiting_parents = set()
  181. self.waiting_s_e = set()
  182. self.ref_count = 0
  183. self.wkids = None # Kids yet to walk, when it's an array
  184. self.env = None
  185. self.state = no_state
  186. self.precious = None
  187. self.noclean = 0
  188. self.nocache = 0
  189. self.always_build = None
  190. self.includes = None
  191. self.attributes = self.Attrs() # Generic place to stick information about the Node.
  192. self.side_effect = 0 # true iff this node is a side effect
  193. self.side_effects = [] # the side effects of building this target
  194. self.linked = 0 # is this node linked to the variant directory?
  195. self.clear_memoized_values()
  196. # Let the interface in which the build engine is embedded
  197. # annotate this Node with its own info (like a description of
  198. # what line in what file created the node, for example).
  199. Annotate(self)
  200. def disambiguate(self, must_exist=None):
  201. return self
  202. def get_suffix(self):
  203. return ''
  204. memoizer_counters.append(SCons.Memoize.CountValue('get_build_env'))
  205. def get_build_env(self):
  206. """Fetch the appropriate Environment to build this node.
  207. """
  208. try:
  209. return self._memo['get_build_env']
  210. except KeyError:
  211. pass
  212. result = self.get_executor().get_build_env()
  213. self._memo['get_build_env'] = result
  214. return result
  215. def get_build_scanner_path(self, scanner):
  216. """Fetch the appropriate scanner path for this node."""
  217. return self.get_executor().get_build_scanner_path(scanner)
  218. def set_executor(self, executor):
  219. """Set the action executor for this node."""
  220. self.executor = executor
  221. def get_executor(self, create=1):
  222. """Fetch the action executor for this node. Create one if
  223. there isn't already one, and requested to do so."""
  224. try:
  225. executor = self.executor
  226. except AttributeError:
  227. if not create:
  228. raise
  229. try:
  230. act = self.builder.action
  231. except AttributeError:
  232. executor = SCons.Executor.Null(targets=[self])
  233. else:
  234. executor = SCons.Executor.Executor(act,
  235. self.env or self.builder.env,
  236. [self.builder.overrides],
  237. [self],
  238. self.sources)
  239. self.executor = executor
  240. return executor
  241. def executor_cleanup(self):
  242. """Let the executor clean up any cached information."""
  243. try:
  244. executor = self.get_executor(create=None)
  245. except AttributeError:
  246. pass
  247. else:
  248. executor.cleanup()
  249. def reset_executor(self):
  250. "Remove cached executor; forces recompute when needed."
  251. try:
  252. delattr(self, 'executor')
  253. except AttributeError:
  254. pass
  255. def push_to_cache(self):
  256. """Try to push a node into a cache
  257. """
  258. pass
  259. def retrieve_from_cache(self):
  260. """Try to retrieve the node's content from a cache
  261. This method is called from multiple threads in a parallel build,
  262. so only do thread safe stuff here. Do thread unsafe stuff in
  263. built().
  264. Returns true iff the node was successfully retrieved.
  265. """
  266. return 0
  267. #
  268. # Taskmaster interface subsystem
  269. #
  270. def make_ready(self):
  271. """Get a Node ready for evaluation.
  272. This is called before the Taskmaster decides if the Node is
  273. up-to-date or not. Overriding this method allows for a Node
  274. subclass to be disambiguated if necessary, or for an implicit
  275. source builder to be attached.
  276. """
  277. pass
  278. def prepare(self):
  279. """Prepare for this Node to be built.
  280. This is called after the Taskmaster has decided that the Node
  281. is out-of-date and must be rebuilt, but before actually calling
  282. the method to build the Node.
  283. This default implementation checks that explicit or implicit
  284. dependencies either exist or are derived, and initializes the
  285. BuildInfo structure that will hold the information about how
  286. this node is, uh, built.
  287. (The existence of source files is checked separately by the
  288. Executor, which aggregates checks for all of the targets built
  289. by a specific action.)
  290. Overriding this method allows for for a Node subclass to remove
  291. the underlying file from the file system. Note that subclass
  292. methods should call this base class method to get the child
  293. check and the BuildInfo structure.
  294. """
  295. for d in self.depends:
  296. if d.missing():
  297. msg = "Explicit dependency `%s' not found, needed by target `%s'."
  298. raise SCons.Errors.StopError(msg % (d, self))
  299. if self.implicit is not None:
  300. for i in self.implicit:
  301. if i.missing():
  302. msg = "Implicit dependency `%s' not found, needed by target `%s'."
  303. raise SCons.Errors.StopError(msg % (i, self))
  304. self.binfo = self.get_binfo()
  305. def build(self, **kw):
  306. """Actually build the node.
  307. This is called by the Taskmaster after it's decided that the
  308. Node is out-of-date and must be rebuilt, and after the prepare()
  309. method has gotten everything, uh, prepared.
  310. This method is called from multiple threads in a parallel build,
  311. so only do thread safe stuff here. Do thread unsafe stuff
  312. in built().
  313. """
  314. try:
  315. self.get_executor()(self, **kw)
  316. except SCons.Errors.BuildError, e:
  317. e.node = self
  318. raise
  319. def built(self):
  320. """Called just after this node is successfully built."""
  321. # Clear the implicit dependency caches of any Nodes
  322. # waiting for this Node to be built.
  323. for parent in self.waiting_parents:
  324. parent.implicit = None
  325. self.clear()
  326. self.ninfo.update(self)
  327. def visited(self):
  328. """Called just after this node has been visited (with or
  329. without a build)."""
  330. try:
  331. binfo = self.binfo
  332. except AttributeError:
  333. # Apparently this node doesn't need build info, so
  334. # don't bother calculating or storing it.
  335. pass
  336. else:
  337. self.ninfo.update(self)
  338. self.store_info()
  339. #
  340. #
  341. #
  342. def add_to_waiting_s_e(self, node):
  343. self.waiting_s_e.add(node)
  344. def add_to_waiting_parents(self, node):
  345. """
  346. Returns the number of nodes added to our waiting parents list:
  347. 1 if we add a unique waiting parent, 0 if not. (Note that the
  348. returned values are intended to be used to increment a reference
  349. count, so don't think you can "clean up" this function by using
  350. True and False instead...)
  351. """
  352. wp = self.waiting_parents
  353. if node in wp:
  354. return 0
  355. wp.add(node)
  356. return 1
  357. def postprocess(self):
  358. """Clean up anything we don't need to hang onto after we've
  359. been built."""
  360. self.executor_cleanup()
  361. self.waiting_parents = set()
  362. def clear(self):
  363. """Completely clear a Node of all its cached state (so that it
  364. can be re-evaluated by interfaces that do continuous integration
  365. builds).
  366. """
  367. # The del_binfo() call here isn't necessary for normal execution,
  368. # but is for interactive mode, where we might rebuild the same
  369. # target and need to start from scratch.
  370. self.del_binfo()
  371. self.clear_memoized_values()
  372. self.ninfo = self.new_ninfo()
  373. self.executor_cleanup()
  374. try:
  375. delattr(self, '_calculated_sig')
  376. except AttributeError:
  377. pass
  378. self.includes = None
  379. def clear_memoized_values(self):
  380. self._memo = {}
  381. def builder_set(self, builder):
  382. self.builder = builder
  383. try:
  384. del self.executor
  385. except AttributeError:
  386. pass
  387. def has_builder(self):
  388. """Return whether this Node has a builder or not.
  389. In Boolean tests, this turns out to be a *lot* more efficient
  390. than simply examining the builder attribute directly ("if
  391. node.builder: ..."). When the builder attribute is examined
  392. directly, it ends up calling __getattr__ for both the __len__
  393. and __nonzero__ attributes on instances of our Builder Proxy
  394. class(es), generating a bazillion extra calls and slowing
  395. things down immensely.
  396. """
  397. try:
  398. b = self.builder
  399. except AttributeError:
  400. # There was no explicit builder for this Node, so initialize
  401. # the self.builder attribute to None now.
  402. b = self.builder = None
  403. return b is not None
  404. def set_explicit(self, is_explicit):
  405. self.is_explicit = is_explicit
  406. def has_explicit_builder(self):
  407. """Return whether this Node has an explicit builder
  408. This allows an internal Builder created by SCons to be marked
  409. non-explicit, so that it can be overridden by an explicit
  410. builder that the user supplies (the canonical example being
  411. directories)."""
  412. try:
  413. return self.is_explicit
  414. except AttributeError:
  415. self.is_explicit = None
  416. return self.is_explicit
  417. def get_builder(self, default_builder=None):
  418. """Return the set builder, or a specified default value"""
  419. try:
  420. return self.builder
  421. except AttributeError:
  422. return default_builder
  423. multiple_side_effect_has_builder = has_builder
  424. def is_derived(self):
  425. """
  426. Returns true iff this node is derived (i.e. built).
  427. This should return true only for nodes whose path should be in
  428. the variant directory when duplicate=0 and should contribute their build
  429. signatures when they are used as source files to other derived files. For
  430. example: source with source builders are not derived in this sense,
  431. and hence should not return true.
  432. """
  433. return self.has_builder() or self.side_effect
  434. def alter_targets(self):
  435. """Return a list of alternate targets for this Node.
  436. """
  437. return [], None
  438. def get_found_includes(self, env, scanner, path):
  439. """Return the scanned include lines (implicit dependencies)
  440. found in this node.
  441. The default is no implicit dependencies. We expect this method
  442. to be overridden by any subclass that can be scanned for
  443. implicit dependencies.
  444. """
  445. return []
  446. def get_implicit_deps(self, env, scanner, path):
  447. """Return a list of implicit dependencies for this node.
  448. This method exists to handle recursive invocation of the scanner
  449. on the implicit dependencies returned by the scanner, if the
  450. scanner's recursive flag says that we should.
  451. """
  452. if not scanner:
  453. return []
  454. # Give the scanner a chance to select a more specific scanner
  455. # for this Node.
  456. #scanner = scanner.select(self)
  457. nodes = [self]
  458. seen = {}
  459. seen[self] = 1
  460. deps = []
  461. while nodes:
  462. n = nodes.pop(0)
  463. d = [x for x in n.get_found_includes(env, scanner, path) if x not in seen]
  464. if d:
  465. deps.extend(d)
  466. for n in d:
  467. seen[n] = 1
  468. nodes.extend(scanner.recurse_nodes(d))
  469. return deps
  470. def get_env_scanner(self, env, kw={}):
  471. return env.get_scanner(self.scanner_key())
  472. def get_target_scanner(self):
  473. return self.builder.target_scanner
  474. def get_source_scanner(self, node):
  475. """Fetch the source scanner for the specified node
  476. NOTE: "self" is the target being built, "node" is
  477. the source file for which we want to fetch the scanner.
  478. Implies self.has_builder() is true; again, expect to only be
  479. called from locations where this is already verified.
  480. This function may be called very often; it attempts to cache
  481. the scanner found to improve performance.
  482. """
  483. scanner = None
  484. try:
  485. scanner = self.builder.source_scanner
  486. except AttributeError:
  487. pass
  488. if not scanner:
  489. # The builder didn't have an explicit scanner, so go look up
  490. # a scanner from env['SCANNERS'] based on the node's scanner
  491. # key (usually the file extension).
  492. scanner = self.get_env_scanner(self.get_build_env())
  493. if scanner:
  494. scanner = scanner.select(node)
  495. return scanner
  496. def add_to_implicit(self, deps):
  497. if not hasattr(self, 'implicit') or self.implicit is None:
  498. self.implicit = []
  499. self.implicit_set = set()
  500. self._children_reset()
  501. self._add_child(self.implicit, self.implicit_set, deps)
  502. def scan(self):
  503. """Scan this node's dependents for implicit dependencies."""
  504. # Don't bother scanning non-derived files, because we don't
  505. # care what their dependencies are.
  506. # Don't scan again, if we already have scanned.
  507. if self.implicit is not None:
  508. return
  509. self.implicit = []
  510. self.implicit_set = set()
  511. self._children_reset()
  512. if not self.has_builder():
  513. return
  514. build_env = self.get_build_env()
  515. executor = self.get_executor()
  516. # Here's where we implement --implicit-cache.
  517. if implicit_cache and not implicit_deps_changed:
  518. implicit = self.get_stored_implicit()
  519. if implicit is not None:
  520. # We now add the implicit dependencies returned from the
  521. # stored .sconsign entry to have already been converted
  522. # to Nodes for us. (We used to run them through a
  523. # source_factory function here.)
  524. # Update all of the targets with them. This
  525. # essentially short-circuits an N*M scan of the
  526. # sources for each individual target, which is a hell
  527. # of a lot more efficient.
  528. for tgt in executor.get_all_targets():
  529. tgt.add_to_implicit(implicit)
  530. if implicit_deps_unchanged or self.is_up_to_date():
  531. return
  532. # one of this node's sources has changed,
  533. # so we must recalculate the implicit deps:
  534. self.implicit = []
  535. self.implicit_set = set()
  536. # Have the executor scan the sources.
  537. executor.scan_sources(self.builder.source_scanner)
  538. # If there's a target scanner, have the executor scan the target
  539. # node itself and associated targets that might be built.
  540. scanner = self.get_target_scanner()
  541. if scanner:
  542. executor.scan_targets(scanner)
  543. def scanner_key(self):
  544. return None
  545. def select_scanner(self, scanner):
  546. """Selects a scanner for this Node.
  547. This is a separate method so it can be overridden by Node
  548. subclasses (specifically, Node.FS.Dir) that *must* use their
  549. own Scanner and don't select one the Scanner.Selector that's
  550. configured for the target.
  551. """
  552. return scanner.select(self)
  553. def env_set(self, env, safe=0):
  554. if safe and self.env:
  555. return
  556. self.env = env
  557. #
  558. # SIGNATURE SUBSYSTEM
  559. #
  560. NodeInfo = NodeInfoBase
  561. BuildInfo = BuildInfoBase
  562. def new_ninfo(self):
  563. ninfo = self.NodeInfo(self)
  564. return ninfo
  565. def get_ninfo(self):
  566. try:
  567. return self.ninfo
  568. except AttributeError:
  569. self.ninfo = self.new_ninfo()
  570. return self.ninfo
  571. def new_binfo(self):
  572. binfo = self.BuildInfo(self)
  573. return binfo
  574. def get_binfo(self):
  575. """
  576. Fetch a node's build information.
  577. node - the node whose sources will be collected
  578. cache - alternate node to use for the signature cache
  579. returns - the build signature
  580. This no longer handles the recursive descent of the
  581. node's children's signatures. We expect that they're
  582. already built and updated by someone else, if that's
  583. what's wanted.
  584. """
  585. try:
  586. return self.binfo
  587. except AttributeError:
  588. pass
  589. binfo = self.new_binfo()
  590. self.binfo = binfo
  591. executor = self.get_executor()
  592. ignore_set = self.ignore_set
  593. if self.has_builder():
  594. binfo.bact = str(executor)
  595. binfo.bactsig = SCons.Util.MD5signature(executor.get_contents())
  596. if self._specific_sources:
  597. sources = []
  598. for s in self.sources:
  599. if s not in ignore_set:
  600. sources.append(s)
  601. else:
  602. sources = executor.get_unignored_sources(self, self.ignore)
  603. seen = set()
  604. bsources = []
  605. bsourcesigs = []
  606. for s in sources:
  607. if not s in seen:
  608. seen.add(s)
  609. bsources.append(s)
  610. bsourcesigs.append(s.get_ninfo())
  611. binfo.bsources = bsources
  612. binfo.bsourcesigs = bsourcesigs
  613. depends = self.depends
  614. dependsigs = []
  615. for d in depends:
  616. if d not in ignore_set:
  617. dependsigs.append(d.get_ninfo())
  618. binfo.bdepends = depends
  619. binfo.bdependsigs = dependsigs
  620. implicit = self.implicit or []
  621. implicitsigs = []
  622. for i in implicit:
  623. if i not in ignore_set:
  624. implicitsigs.append(i.get_ninfo())
  625. binfo.bimplicit = implicit
  626. binfo.bimplicitsigs = implicitsigs
  627. return binfo
  628. def del_binfo(self):
  629. """Delete the build info from this node."""
  630. try:
  631. delattr(self, 'binfo')
  632. except AttributeError:
  633. pass
  634. def get_csig(self):
  635. try:
  636. return self.ninfo.csig
  637. except AttributeError:
  638. ninfo = self.get_ninfo()
  639. ninfo.csig = SCons.Util.MD5signature(self.get_contents())
  640. return self.ninfo.csig
  641. def get_cachedir_csig(self):
  642. return self.get_csig()
  643. def store_info(self):
  644. """Make the build signature permanent (that is, store it in the
  645. .sconsign file or equivalent)."""
  646. pass
  647. def do_not_store_info(self):
  648. pass
  649. def get_stored_info(self):
  650. return None
  651. def get_stored_implicit(self):
  652. """Fetch the stored implicit dependencies"""
  653. return None
  654. #
  655. #
  656. #
  657. def set_precious(self, precious = 1):
  658. """Set the Node's precious value."""
  659. self.precious = precious
  660. def set_noclean(self, noclean = 1):
  661. """Set the Node's noclean value."""
  662. # Make sure noclean is an integer so the --debug=stree
  663. # output in Util.py can use it as an index.
  664. self.noclean = noclean and 1 or 0
  665. def set_nocache(self, nocache = 1):
  666. """Set the Node's nocache value."""
  667. # Make sure nocache is an integer so the --debug=stree
  668. # output in Util.py can use it as an index.
  669. self.nocache = nocache and 1 or 0
  670. def set_always_build(self, always_build = 1):
  671. """Set the Node's always_build value."""
  672. self.always_build = always_build
  673. def exists(self):
  674. """Does this node exists?"""
  675. # All node exist by default:
  676. return 1
  677. def rexists(self):
  678. """Does this node exist locally or in a repositiory?"""
  679. # There are no repositories by default:
  680. return self.exists()
  681. def missing(self):
  682. return not self.is_derived() and \
  683. not self.linked and \
  684. not self.rexists()
  685. def remove(self):
  686. """Remove this Node: no-op by default."""
  687. return None
  688. def add_dependency(self, depend):
  689. """Adds dependencies."""
  690. try:
  691. self._add_child(self.depends, self.depends_set, depend)
  692. except TypeError, e:
  693. e = e.args[0]
  694. if SCons.Util.is_List(e):
  695. s = list(map(str, e))
  696. else:
  697. s = str(e)
  698. raise SCons.Errors.UserError("attempted to add a non-Node dependency to %s:\n\t%s is a %s, not a Node" % (str(self), s, type(e)))
  699. def add_prerequisite(self, prerequisite):
  700. """Adds prerequisites"""
  701. self.prerequisites.extend(prerequisite)
  702. self._children_reset()
  703. def add_ignore(self, depend):
  704. """Adds dependencies to ignore."""
  705. try:
  706. self._add_child(self.ignore, self.ignore_set, depend)
  707. except TypeError, e:
  708. e = e.args[0]
  709. if SCons.Util.is_List(e):
  710. s = list(map(str, e))
  711. else:
  712. s = str(e)
  713. raise SCons.Errors.UserError("attempted to ignore a non-Node dependency of %s:\n\t%s is a %s, not a Node" % (str(self), s, type(e)))
  714. def add_source(self, source):
  715. """Adds sources."""
  716. if self._specific_sources:
  717. return
  718. try:
  719. self._add_child(self.sources, self.sources_set, source)
  720. except TypeError, e:
  721. e = e.args[0]
  722. if SCons.Util.is_List(e):
  723. s = list(map(str, e))
  724. else:
  725. s = str(e)
  726. raise SCons.Errors.UserError("attempted to add a non-Node as source of %s:\n\t%s is a %s, not a Node" % (str(self), s, type(e)))
  727. def _add_child(self, collection, set, child):
  728. """Adds 'child' to 'collection', first checking 'set' to see if it's
  729. already present."""
  730. #if type(child) is not type([]):
  731. # child = [child]
  732. #for c in child:
  733. # if not isinstance(c, Node):
  734. # raise TypeError, c
  735. added = None
  736. for c in child:
  737. if c not in set:
  738. set.add(c)
  739. collection.append(c)
  740. added = 1
  741. if added:
  742. self._children_reset()
  743. def set_specific_source(self, source):
  744. self.add_source(source)
  745. self._specific_sources = True
  746. def add_wkid(self, wkid):
  747. """Add a node to the list of kids waiting to be evaluated"""
  748. if self.wkids is not None:
  749. self.wkids.append(wkid)
  750. def _children_reset(self):
  751. self.clear_memoized_values()
  752. # We need to let the Executor clear out any calculated
  753. # build info that it's cached so we can re-calculate it.
  754. self.executor_cleanup()
  755. memoizer_counters.append(SCons.Memoize.CountValue('_children_get'))
  756. def _children_get(self):
  757. try:
  758. return self._memo['children_get']
  759. except KeyError:
  760. pass
  761. # The return list may contain duplicate Nodes, especially in
  762. # source trees where there are a lot of repeated #includes
  763. # of a tangle of .h files. Profiling shows, however, that
  764. # eliminating the duplicates with a brute-force approach that
  765. # preserves the order (that is, something like:
  766. #
  767. # u = []
  768. # for n in list:
  769. # if n not in u:
  770. # u.append(n)"
  771. #
  772. # takes more cycles than just letting the underlying methods
  773. # hand back cached values if a Node's information is requested
  774. # multiple times. (Other methods of removing duplicates, like
  775. # using dictionary keys, lose the order, and the only ordered
  776. # dictionary patterns I found all ended up using "not in"
  777. # internally anyway...)
  778. if self.ignore_set:
  779. if self.implicit is None:
  780. iter = chain(self.sources,self.depends)
  781. else:
  782. iter = chain(self.sources, self.depends, self.implicit)
  783. children = []
  784. for i in iter:
  785. if i not in self.ignore_set:
  786. children.append(i)
  787. else:
  788. if self.implicit is None:
  789. children = self.sources + self.depends
  790. else:
  791. children = self.sources + self.depends + self.implicit
  792. self._memo['children_get'] = children
  793. return children
  794. def all_children(self, scan=1):
  795. """Return a list of all the node's direct children."""
  796. if scan:
  797. self.scan()
  798. # The return list may contain duplicate Nodes, especially in
  799. # source trees where there are a lot of repeated #includes
  800. # of a tangle of .h files. Profiling shows, however, that
  801. # eliminating the duplicates with a brute-force approach that
  802. # preserves the order (that is, something like:
  803. #
  804. # u = []
  805. # for n in list:
  806. # if n not in u:
  807. # u.append(n)"
  808. #
  809. # takes more cycles than just letting the underlying methods
  810. # hand back cached values if a Node's information is requested
  811. # multiple times. (Other methods of removing duplicates, like
  812. # using dictionary keys, lose the order, and the only ordered
  813. # dictionary patterns I found all ended up using "not in"
  814. # internally anyway...)
  815. if self.implicit is None:
  816. return self.sources + self.depends
  817. else:
  818. return self.sources + self.depends + self.implicit
  819. def children(self, scan=1):
  820. """Return a list of the node's direct children, minus those
  821. that are ignored by this node."""
  822. if scan:
  823. self.scan()
  824. return self._children_get()
  825. def set_state(self, state):
  826. self.state = state
  827. def get_state(self):
  828. return self.state
  829. def state_has_changed(self, target, prev_ni):
  830. return (self.state != SCons.Node.up_to_date)
  831. def get_env(self):
  832. env = self.env
  833. if not env:
  834. import SCons.Defaults
  835. env = SCons.Defaults.DefaultEnvironment()
  836. return env
  837. def changed_since_last_build(self, target, prev_ni):
  838. """
  839. Must be overridden in a specific subclass to return True if this
  840. Node (a dependency) has changed since the last time it was used
  841. to build the specified target. prev_ni is this Node's state (for
  842. example, its file timestamp, length, maybe content signature)
  843. as of the last time the target was built.
  844. Note that this method is called through the dependency, not the
  845. target, because a dependency Node must be able to use its own
  846. logic to decide if it changed. For example, File Nodes need to
  847. obey if we're configured to use timestamps, but Python Value Nodes
  848. never use timestamps and always use the content. If this method
  849. were called through the target, then each Node's implementation
  850. of this method would have to have more complicated logic to
  851. handle all the different Node types on which it might depend.
  852. """
  853. raise NotImplementedError
  854. def Decider(self, function):
  855. SCons.Util.AddMethod(self, function, 'changed_since_last_build')
  856. def changed(self, node=None):
  857. """
  858. Returns if the node is up-to-date with respect to the BuildInfo
  859. stored last time it was built. The default behavior is to compare
  860. it against our own previously stored BuildInfo, but the stored
  861. BuildInfo from another Node (typically one in a Repository)
  862. can be used instead.
  863. Note that we now *always* check every dependency. We used to
  864. short-circuit the check by returning as soon as we detected
  865. any difference, but we now rely on checking every dependency
  866. to make sure that any necessary Node information (for example,
  867. the content signature of an #included .h file) is updated.
  868. """
  869. t = 0
  870. if t: Trace('changed(%s [%s], %s)' % (self, classname(self), node))
  871. if node is None:
  872. node = self
  873. result = False
  874. bi = node.get_stored_info().binfo
  875. then = bi.bsourcesigs + bi.bdependsigs + bi.bimplicitsigs
  876. children = self.children()
  877. diff = len(children) - len(then)
  878. if diff:
  879. # The old and new dependency lists are different lengths.
  880. # This always indicates that the Node must be rebuilt.
  881. # We also extend the old dependency list with enough None
  882. # entries to equal the new dependency list, for the benefit
  883. # of the loop below that updates node information.
  884. then.extend([None] * diff)
  885. if t: Trace(': old %s new %s' % (len(then), len(children)))
  886. result = True
  887. for child, prev_ni in zip(children, then):
  888. if child.changed_since_last_build(self, prev_ni):
  889. if t: Trace(': %s changed' % child)
  890. result = True
  891. contents = self.get_executor().get_contents()
  892. if self.has_builder():
  893. import SCons.Util
  894. newsig = SCons.Util.MD5signature(contents)
  895. if bi.bactsig != newsig:
  896. if t: Trace(': bactsig %s != newsig %s' % (bi.bactsig, newsig))
  897. result = True
  898. if not result:
  899. if t: Trace(': up to date')
  900. if t: Trace('\n')
  901. return result
  902. def is_up_to_date(self):
  903. """Default check for whether the Node is current: unknown Node
  904. subtypes are always out of date, so they will always get built."""
  905. return None
  906. def children_are_up_to_date(self):
  907. """Alternate check for whether the Node is current: If all of
  908. our children were up-to-date, then this Node was up-to-date, too.
  909. The SCons.Node.Alias and SCons.Node.Python.Value subclasses
  910. rebind their current() method to this method."""
  911. # Allow the children to calculate their signatures.
  912. self.binfo = self.get_binfo()
  913. if self.always_build:
  914. return None
  915. state = 0
  916. for kid in self.children(None):
  917. s = kid.get_state()
  918. if s and (not state or s > state):
  919. state = s
  920. return (state == 0 or state == SCons.Node.up_to_date)
  921. def is_literal(self):
  922. """Always pass the string representation of a Node to
  923. the command interpreter literally."""
  924. return 1
  925. def render_include_tree(self):
  926. """
  927. Return a text representation, suitable for displaying to the
  928. user, of the include tree for the sources of this node.
  929. """
  930. if self.is_derived() and self.env:
  931. env = self.get_build_env()
  932. for s in self.sources:
  933. scanner = self.get_source_scanner(s)
  934. if scanner:
  935. path = self.get_build_scanner_path(scanner)
  936. else:
  937. path = None
  938. def f(node, env=env, scanner=scanner, path=path):
  939. return node.get_found_includes(env, scanner, path)
  940. return SCons.Util.render_tree(s, f, 1)
  941. else:
  942. return None
  943. def get_abspath(self):
  944. """
  945. Return an absolute path to the Node. This will return simply
  946. str(Node) by default, but for Node types that have a concept of
  947. relative path, this might return something different.
  948. """
  949. return str(self)
  950. def for_signature(self):
  951. """
  952. Return a string representation of the Node that will always
  953. be the same for this particular Node, no matter what. This
  954. is by contrast to the __str__() method, which might, for
  955. instance, return a relative path for a file Node. The purpose
  956. of this method is to generate a value to be used in signature
  957. calculation for the command line used to build a target, and
  958. we use this method instead of str() to avoid unnecessary
  959. rebuilds. This method does not need to return something that
  960. would actually work in a command line; it can return any kind of
  961. nonsense, so long as it does not change.
  962. """
  963. return str(self)
  964. def get_string(self, for_signature):
  965. """This is a convenience function designed primarily to be
  966. used in command generators (i.e., CommandGeneratorActions or
  967. Environment variables that are callable), which are called
  968. with a for_signature argument that is nonzero if the command
  969. generator is being called to generate a signature for the
  970. command line, which determines if we should rebuild or not.
  971. Such command generators should use this method in preference
  972. to str(Node) when converting a Node to a string, passing
  973. in the for_signature parameter, such that we will call
  974. Node.for_signature() or str(Node) properly, depending on whether
  975. we are calculating a signature or actually constructing a
  976. command line."""
  977. if for_signature:
  978. return self.for_signature()
  979. return str(self)
  980. def get_subst_proxy(self):
  981. """
  982. This method is expected to return an object that will function
  983. exactly like this Node, except that it implements any additional
  984. special features that we would like to be in effect for
  985. Environment variable substitution. The principle use is that
  986. some Nodes would like to implement a __getattr__() method,
  987. but putting that in the Node type itself has a tendency to kill
  988. performance. We instead put it in a proxy and return it from
  989. this method. It is legal for this method to return self
  990. if no new functionality is needed for Environment substitution.
  991. """
  992. return self
  993. def explain(self):
  994. if not self.exists():
  995. return "building `%s' because it doesn't exist\n" % self
  996. if self.always_build:
  997. return "rebuilding `%s' because AlwaysBuild() is specified\n" % self
  998. old = self.get_stored_info()
  999. if old is None:
  1000. return None
  1001. old = old.binfo
  1002. old.prepare_dependencies()
  1003. try:
  1004. old_bkids = old.bsources + old.bdepends + old.bimplicit
  1005. old_bkidsigs = old.bsourcesigs + old.bdependsigs + old.bimplicitsigs
  1006. except AttributeError:
  1007. return "Cannot explain why `%s' is being rebuilt: No previous build information found\n" % self
  1008. new = self.get_binfo()
  1009. new_bkids = new.bsources + new.bdepends + new.bimplicit
  1010. new_bkidsigs = new.bsourcesigs + new.bdependsigs + new.bimplicitsigs
  1011. osig = dict(zip(old_bkids, old_bkidsigs))
  1012. nsig = dict(zip(new_bkids, new_bkidsigs))
  1013. # The sources and dependencies we'll want to report are all stored
  1014. # as relative paths to this target's directory, but we want to
  1015. # report them relative to the top-level SConstruct directory,
  1016. # so we only print them after running them through this lambda
  1017. # to turn them into the right relative Node and then return
  1018. # its string.
  1019. def stringify( s, E=self.dir.Entry ) :
  1020. if hasattr( s, 'dir' ) :
  1021. return str(E(s))
  1022. return str(s)
  1023. lines = []
  1024. removed = [x for x in old_bkids if not x in new_bkids]
  1025. if removed:
  1026. removed = list(map(stringify, removed))
  1027. fmt = "`%s' is no longer a dependency\n"
  1028. lines.extend([fmt % s for s in removed])
  1029. for k in new_bkids:
  1030. if not k in old_bkids:
  1031. lines.append("`%s' is a new dependency\n" % stringify(k))
  1032. elif k.changed_since_last_build(self, osig[k]):
  1033. lines.append("`%s' changed\n" % stringify(k))
  1034. if len(lines) == 0 and old_bkids != new_bkids:
  1035. lines.append("the dependency order changed:\n" +
  1036. "%sold: %s\n" % (' '*15, list(map(stringify, old_bkids))) +
  1037. "%snew: %s\n" % (' '*15, list(map(stringify, new_bkids))))
  1038. if len(lines) == 0:
  1039. def fmt_with_title(title, strlines):
  1040. lines = strlines.split('\n')
  1041. sep = '\n' + ' '*(15 + len(title))
  1042. return ' '*15 + title + sep.join(lines) + '\n'
  1043. if old.bactsig != new.bactsig:
  1044. if old.bact == new.bact:
  1045. lines.append("the contents of the build action changed\n" +
  1046. fmt_with_title('action: ', new.bact))
  1047. else:
  1048. lines.append("the build action changed:\n" +
  1049. fmt_with_title('old: ', old.bact) +
  1050. fmt_with_title('new: ', new.bact))
  1051. if len(lines) == 0:
  1052. return "rebuilding `%s' for unknown reasons\n" % self
  1053. preamble = "rebuilding `%s' because" % self
  1054. if len(lines) == 1:
  1055. return "%s %s" % (preamble, lines[0])
  1056. else:
  1057. lines = ["%s:\n" % preamble] + lines
  1058. return ( ' '*11).join(lines)
  1059. class NodeList(collections.UserList):
  1060. def __str__(self):
  1061. return str(list(map(str, self.data)))
  1062. def get_children(node, parent): return node.children()
  1063. def ignore_cycle(node, stack): pass
  1064. def do_nothing(node, parent): pass
  1065. class Walker(object):
  1066. """An iterator for walking a Node tree.
  1067. This is depth-first, children are visited before the parent.
  1068. The Walker object can be initialized with any node, and
  1069. returns the next node on the descent with each get_next() call.
  1070. 'kids_func' is an optional function that will be called to
  1071. get the children of a node instead of calling 'children'.
  1072. 'cycle_func' is an optional function that will be called
  1073. when a cycle is detected.
  1074. This class does not get caught in node cycles caused, for example,
  1075. by C header file include loops.
  1076. """
  1077. def __init__(self, node, kids_func=get_children,
  1078. cycle_func=ignore_cycle,
  1079. eval_func=do_nothing):
  1080. self.kids_func = kids_func
  1081. self.cycle_func = cycle_func
  1082. self.eval_func = eval_func
  1083. node.wkids = copy.copy(kids_func(node, None))
  1084. self.stack = [node]
  1085. self.history = {} # used to efficiently detect and avoid cycles
  1086. self.history[node] = None
  1087. def get_next(self):
  1088. """Return the next node for this walk of the tree.
  1089. This function is intentionally iterative, not recursive,
  1090. to sidestep any issues of stack size limitations.
  1091. """
  1092. while self.stack:
  1093. if self.stack[-1].wkids:
  1094. node = self.stack[-1].wkids.pop(0)
  1095. if not self.stack[-1].wkids:
  1096. self.stack[-1].wkids = None
  1097. if node in self.history:
  1098. self.cycle_func(node, self.stack)
  1099. else:
  1100. node.wkids = copy.copy(self.kids_func(node, self.stack[-1]))
  1101. self.stack.append(node)
  1102. self.history[node] = None
  1103. else:
  1104. node = self.stack.pop()
  1105. del self.history[node]
  1106. if node:
  1107. if self.stack:
  1108. parent = self.stack[-1]
  1109. else:
  1110. parent = None
  1111. self.eval_func(node, parent)
  1112. return node
  1113. return None
  1114. def is_done(self):
  1115. return not self.stack
  1116. arg2nodes_lookups = []
  1117. # Local Variables:
  1118. # tab-width:4
  1119. # indent-tabs-mode:nil
  1120. # End:
  1121. # vim: set expandtab tabstop=4 shiftwidth=4: