PageRenderTime 51ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://github.com/cloudant/bigcouch
Python | 877 lines | 740 code | 32 blank | 105 comment | 70 complexity | 2d610278f8a8566b6276dcf236048965 MD5 | raw file
Possible License(s): Apache-2.0
  1. """SCons.Builder
  2. Builder object subsystem.
  3. A Builder object is a callable that encapsulates information about how
  4. to execute actions to create a target Node (file) from source Nodes
  5. (files), and how to create those dependencies for tracking.
  6. The main entry point here is the Builder() factory method. This provides
  7. a procedural interface that creates the right underlying Builder object
  8. based on the keyword arguments supplied and the types of the arguments.
  9. The goal is for this external interface to be simple enough that the
  10. vast majority of users can create new Builders as necessary to support
  11. building new types of files in their configurations, without having to
  12. dive any deeper into this subsystem.
  13. The base class here is BuilderBase. This is a concrete base class which
  14. does, in fact, represent the Builder objects that we (or users) create.
  15. There is also a proxy that looks like a Builder:
  16. CompositeBuilder
  17. This proxies for a Builder with an action that is actually a
  18. dictionary that knows how to map file suffixes to a specific
  19. action. This is so that we can invoke different actions
  20. (compilers, compile options) for different flavors of source
  21. files.
  22. Builders and their proxies have the following public interface methods
  23. used by other modules:
  24. __call__()
  25. THE public interface. Calling a Builder object (with the
  26. use of internal helper methods) sets up the target and source
  27. dependencies, appropriate mapping to a specific action, and the
  28. environment manipulation necessary for overridden construction
  29. variable. This also takes care of warning about possible mistakes
  30. in keyword arguments.
  31. add_emitter()
  32. Adds an emitter for a specific file suffix, used by some Tool
  33. modules to specify that (for example) a yacc invocation on a .y
  34. can create a .h *and* a .c file.
  35. add_action()
  36. Adds an action for a specific file suffix, heavily used by
  37. Tool modules to add their specific action(s) for turning
  38. a source file into an object file to the global static
  39. and shared object file Builders.
  40. There are the following methods for internal use within this module:
  41. _execute()
  42. The internal method that handles the heavily lifting when a
  43. Builder is called. This is used so that the __call__() methods
  44. can set up warning about possible mistakes in keyword-argument
  45. overrides, and *then* execute all of the steps necessary so that
  46. the warnings only occur once.
  47. get_name()
  48. Returns the Builder's name within a specific Environment,
  49. primarily used to try to return helpful information in error
  50. messages.
  51. adjust_suffix()
  52. get_prefix()
  53. get_suffix()
  54. get_src_suffix()
  55. set_src_suffix()
  56. Miscellaneous stuff for handling the prefix and suffix
  57. manipulation we use in turning source file names into target
  58. file names.
  59. """
  60. #
  61. # Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 The SCons Foundation
  62. #
  63. # Permission is hereby granted, free of charge, to any person obtaining
  64. # a copy of this software and associated documentation files (the
  65. # "Software"), to deal in the Software without restriction, including
  66. # without limitation the rights to use, copy, modify, merge, publish,
  67. # distribute, sublicense, and/or sell copies of the Software, and to
  68. # permit persons to whom the Software is furnished to do so, subject to
  69. # the following conditions:
  70. #
  71. # The above copyright notice and this permission notice shall be included
  72. # in all copies or substantial portions of the Software.
  73. #
  74. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
  75. # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  76. # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  77. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  78. # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  79. # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  80. # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  81. __revision__ = "src/engine/SCons/Builder.py 5134 2010/08/16 23:02:40 bdeegan"
  82. import collections
  83. import SCons.Action
  84. from SCons.Debug import logInstanceCreation
  85. from SCons.Errors import InternalError, UserError
  86. import SCons.Executor
  87. import SCons.Memoize
  88. import SCons.Node
  89. import SCons.Node.FS
  90. import SCons.Util
  91. import SCons.Warnings
  92. class _Null(object):
  93. pass
  94. _null = _Null
  95. def match_splitext(path, suffixes = []):
  96. if suffixes:
  97. matchsuf = [S for S in suffixes if path[-len(S):] == S]
  98. if matchsuf:
  99. suf = max([(len(_f),_f) for _f in matchsuf])[1]
  100. return [path[:-len(suf)], path[-len(suf):]]
  101. return SCons.Util.splitext(path)
  102. class DictCmdGenerator(SCons.Util.Selector):
  103. """This is a callable class that can be used as a
  104. command generator function. It holds on to a dictionary
  105. mapping file suffixes to Actions. It uses that dictionary
  106. to return the proper action based on the file suffix of
  107. the source file."""
  108. def __init__(self, dict=None, source_ext_match=1):
  109. SCons.Util.Selector.__init__(self, dict)
  110. self.source_ext_match = source_ext_match
  111. def src_suffixes(self):
  112. return list(self.keys())
  113. def add_action(self, suffix, action):
  114. """Add a suffix-action pair to the mapping.
  115. """
  116. self[suffix] = action
  117. def __call__(self, target, source, env, for_signature):
  118. if not source:
  119. return []
  120. if self.source_ext_match:
  121. suffixes = self.src_suffixes()
  122. ext = None
  123. for src in map(str, source):
  124. my_ext = match_splitext(src, suffixes)[1]
  125. if ext and my_ext != ext:
  126. raise UserError("While building `%s' from `%s': Cannot build multiple sources with different extensions: %s, %s"
  127. % (repr(list(map(str, target))), src, ext, my_ext))
  128. ext = my_ext
  129. else:
  130. ext = match_splitext(str(source[0]), self.src_suffixes())[1]
  131. if not ext:
  132. #return ext
  133. raise UserError("While building `%s': "
  134. "Cannot deduce file extension from source files: %s"
  135. % (repr(list(map(str, target))), repr(list(map(str, source)))))
  136. try:
  137. ret = SCons.Util.Selector.__call__(self, env, source, ext)
  138. except KeyError, e:
  139. raise UserError("Ambiguous suffixes after environment substitution: %s == %s == %s" % (e.args[0], e.args[1], e.args[2]))
  140. if ret is None:
  141. raise UserError("While building `%s' from `%s': Don't know how to build from a source file with suffix `%s'. Expected a suffix in this list: %s." % \
  142. (repr(list(map(str, target))), repr(list(map(str, source))), ext, repr(list(self.keys()))))
  143. return ret
  144. class CallableSelector(SCons.Util.Selector):
  145. """A callable dictionary that will, in turn, call the value it
  146. finds if it can."""
  147. def __call__(self, env, source):
  148. value = SCons.Util.Selector.__call__(self, env, source)
  149. if callable(value):
  150. value = value(env, source)
  151. return value
  152. class DictEmitter(SCons.Util.Selector):
  153. """A callable dictionary that maps file suffixes to emitters.
  154. When called, it finds the right emitter in its dictionary for the
  155. suffix of the first source file, and calls that emitter to get the
  156. right lists of targets and sources to return. If there's no emitter
  157. for the suffix in its dictionary, the original target and source are
  158. returned.
  159. """
  160. def __call__(self, target, source, env):
  161. emitter = SCons.Util.Selector.__call__(self, env, source)
  162. if emitter:
  163. target, source = emitter(target, source, env)
  164. return (target, source)
  165. class ListEmitter(collections.UserList):
  166. """A callable list of emitters that calls each in sequence,
  167. returning the result.
  168. """
  169. def __call__(self, target, source, env):
  170. for e in self.data:
  171. target, source = e(target, source, env)
  172. return (target, source)
  173. # These are a common errors when calling a Builder;
  174. # they are similar to the 'target' and 'source' keyword args to builders,
  175. # so we issue warnings when we see them. The warnings can, of course,
  176. # be disabled.
  177. misleading_keywords = {
  178. 'targets' : 'target',
  179. 'sources' : 'source',
  180. }
  181. class OverrideWarner(collections.UserDict):
  182. """A class for warning about keyword arguments that we use as
  183. overrides in a Builder call.
  184. This class exists to handle the fact that a single Builder call
  185. can actually invoke multiple builders. This class only emits the
  186. warnings once, no matter how many Builders are invoked.
  187. """
  188. def __init__(self, dict):
  189. collections.UserDict.__init__(self, dict)
  190. if __debug__: logInstanceCreation(self, 'Builder.OverrideWarner')
  191. self.already_warned = None
  192. def warn(self):
  193. if self.already_warned:
  194. return
  195. for k in self.keys():
  196. if k in misleading_keywords:
  197. alt = misleading_keywords[k]
  198. msg = "Did you mean to use `%s' instead of `%s'?" % (alt, k)
  199. SCons.Warnings.warn(SCons.Warnings.MisleadingKeywordsWarning, msg)
  200. self.already_warned = 1
  201. def Builder(**kw):
  202. """A factory for builder objects."""
  203. composite = None
  204. if 'generator' in kw:
  205. if 'action' in kw:
  206. raise UserError("You must not specify both an action and a generator.")
  207. kw['action'] = SCons.Action.CommandGeneratorAction(kw['generator'], {})
  208. del kw['generator']
  209. elif 'action' in kw:
  210. source_ext_match = kw.get('source_ext_match', 1)
  211. if 'source_ext_match' in kw:
  212. del kw['source_ext_match']
  213. if SCons.Util.is_Dict(kw['action']):
  214. composite = DictCmdGenerator(kw['action'], source_ext_match)
  215. kw['action'] = SCons.Action.CommandGeneratorAction(composite, {})
  216. kw['src_suffix'] = composite.src_suffixes()
  217. else:
  218. kw['action'] = SCons.Action.Action(kw['action'])
  219. if 'emitter' in kw:
  220. emitter = kw['emitter']
  221. if SCons.Util.is_String(emitter):
  222. # This allows users to pass in an Environment
  223. # variable reference (like "$FOO") as an emitter.
  224. # We will look in that Environment variable for
  225. # a callable to use as the actual emitter.
  226. var = SCons.Util.get_environment_var(emitter)
  227. if not var:
  228. raise UserError("Supplied emitter '%s' does not appear to refer to an Environment variable" % emitter)
  229. kw['emitter'] = EmitterProxy(var)
  230. elif SCons.Util.is_Dict(emitter):
  231. kw['emitter'] = DictEmitter(emitter)
  232. elif SCons.Util.is_List(emitter):
  233. kw['emitter'] = ListEmitter(emitter)
  234. result = BuilderBase(**kw)
  235. if not composite is None:
  236. result = CompositeBuilder(result, composite)
  237. return result
  238. def _node_errors(builder, env, tlist, slist):
  239. """Validate that the lists of target and source nodes are
  240. legal for this builder and environment. Raise errors or
  241. issue warnings as appropriate.
  242. """
  243. # First, figure out if there are any errors in the way the targets
  244. # were specified.
  245. for t in tlist:
  246. if t.side_effect:
  247. raise UserError("Multiple ways to build the same target were specified for: %s" % t)
  248. if t.has_explicit_builder():
  249. if not t.env is None and not t.env is env:
  250. action = t.builder.action
  251. t_contents = action.get_contents(tlist, slist, t.env)
  252. contents = action.get_contents(tlist, slist, env)
  253. if t_contents == contents:
  254. msg = "Two different environments were specified for target %s,\n\tbut they appear to have the same action: %s" % (t, action.genstring(tlist, slist, t.env))
  255. SCons.Warnings.warn(SCons.Warnings.DuplicateEnvironmentWarning, msg)
  256. else:
  257. msg = "Two environments with different actions were specified for the same target: %s" % t
  258. raise UserError(msg)
  259. if builder.multi:
  260. if t.builder != builder:
  261. msg = "Two different builders (%s and %s) were specified for the same target: %s" % (t.builder.get_name(env), builder.get_name(env), t)
  262. raise UserError(msg)
  263. # TODO(batch): list constructed each time!
  264. if t.get_executor().get_all_targets() != tlist:
  265. msg = "Two different target lists have a target in common: %s (from %s and from %s)" % (t, list(map(str, t.get_executor().get_all_targets())), list(map(str, tlist)))
  266. raise UserError(msg)
  267. elif t.sources != slist:
  268. msg = "Multiple ways to build the same target were specified for: %s (from %s and from %s)" % (t, list(map(str, t.sources)), list(map(str, slist)))
  269. raise UserError(msg)
  270. if builder.single_source:
  271. if len(slist) > 1:
  272. raise UserError("More than one source given for single-source builder: targets=%s sources=%s" % (list(map(str,tlist)), list(map(str,slist))))
  273. class EmitterProxy(object):
  274. """This is a callable class that can act as a
  275. Builder emitter. It holds on to a string that
  276. is a key into an Environment dictionary, and will
  277. look there at actual build time to see if it holds
  278. a callable. If so, we will call that as the actual
  279. emitter."""
  280. def __init__(self, var):
  281. self.var = SCons.Util.to_String(var)
  282. def __call__(self, target, source, env):
  283. emitter = self.var
  284. # Recursively substitute the variable.
  285. # We can't use env.subst() because it deals only
  286. # in strings. Maybe we should change that?
  287. while SCons.Util.is_String(emitter) and emitter in env:
  288. emitter = env[emitter]
  289. if callable(emitter):
  290. target, source = emitter(target, source, env)
  291. elif SCons.Util.is_List(emitter):
  292. for e in emitter:
  293. target, source = e(target, source, env)
  294. return (target, source)
  295. def __cmp__(self, other):
  296. return cmp(self.var, other.var)
  297. class BuilderBase(object):
  298. """Base class for Builders, objects that create output
  299. nodes (files) from input nodes (files).
  300. """
  301. if SCons.Memoize.use_memoizer:
  302. __metaclass__ = SCons.Memoize.Memoized_Metaclass
  303. memoizer_counters = []
  304. def __init__(self, action = None,
  305. prefix = '',
  306. suffix = '',
  307. src_suffix = '',
  308. target_factory = None,
  309. source_factory = None,
  310. target_scanner = None,
  311. source_scanner = None,
  312. emitter = None,
  313. multi = 0,
  314. env = None,
  315. single_source = 0,
  316. name = None,
  317. chdir = _null,
  318. is_explicit = 1,
  319. src_builder = None,
  320. ensure_suffix = False,
  321. **overrides):
  322. if __debug__: logInstanceCreation(self, 'Builder.BuilderBase')
  323. self._memo = {}
  324. self.action = action
  325. self.multi = multi
  326. if SCons.Util.is_Dict(prefix):
  327. prefix = CallableSelector(prefix)
  328. self.prefix = prefix
  329. if SCons.Util.is_Dict(suffix):
  330. suffix = CallableSelector(suffix)
  331. self.env = env
  332. self.single_source = single_source
  333. if 'overrides' in overrides:
  334. SCons.Warnings.warn(SCons.Warnings.DeprecatedBuilderKeywordsWarning,
  335. "The \"overrides\" keyword to Builder() creation has been deprecated;\n" +\
  336. "\tspecify the items as keyword arguments to the Builder() call instead.")
  337. overrides.update(overrides['overrides'])
  338. del overrides['overrides']
  339. if 'scanner' in overrides:
  340. SCons.Warnings.warn(SCons.Warnings.DeprecatedBuilderKeywordsWarning,
  341. "The \"scanner\" keyword to Builder() creation has been deprecated;\n"
  342. "\tuse: source_scanner or target_scanner as appropriate.")
  343. del overrides['scanner']
  344. self.overrides = overrides
  345. self.set_suffix(suffix)
  346. self.set_src_suffix(src_suffix)
  347. self.ensure_suffix = ensure_suffix
  348. self.target_factory = target_factory
  349. self.source_factory = source_factory
  350. self.target_scanner = target_scanner
  351. self.source_scanner = source_scanner
  352. self.emitter = emitter
  353. # Optional Builder name should only be used for Builders
  354. # that don't get attached to construction environments.
  355. if name:
  356. self.name = name
  357. self.executor_kw = {}
  358. if not chdir is _null:
  359. self.executor_kw['chdir'] = chdir
  360. self.is_explicit = is_explicit
  361. if src_builder is None:
  362. src_builder = []
  363. elif not SCons.Util.is_List(src_builder):
  364. src_builder = [ src_builder ]
  365. self.src_builder = src_builder
  366. def __nonzero__(self):
  367. raise InternalError("Do not test for the Node.builder attribute directly; use Node.has_builder() instead")
  368. def get_name(self, env):
  369. """Attempts to get the name of the Builder.
  370. Look at the BUILDERS variable of env, expecting it to be a
  371. dictionary containing this Builder, and return the key of the
  372. dictionary. If there's no key, then return a directly-configured
  373. name (if there is one) or the name of the class (by default)."""
  374. try:
  375. index = list(env['BUILDERS'].values()).index(self)
  376. return list(env['BUILDERS'].keys())[index]
  377. except (AttributeError, KeyError, TypeError, ValueError):
  378. try:
  379. return self.name
  380. except AttributeError:
  381. return str(self.__class__)
  382. def __cmp__(self, other):
  383. return cmp(self.__dict__, other.__dict__)
  384. def splitext(self, path, env=None):
  385. if not env:
  386. env = self.env
  387. if env:
  388. suffixes = self.src_suffixes(env)
  389. else:
  390. suffixes = []
  391. return match_splitext(path, suffixes)
  392. def _adjustixes(self, files, pre, suf, ensure_suffix=False):
  393. if not files:
  394. return []
  395. result = []
  396. if not SCons.Util.is_List(files):
  397. files = [files]
  398. for f in files:
  399. if SCons.Util.is_String(f):
  400. f = SCons.Util.adjustixes(f, pre, suf, ensure_suffix)
  401. result.append(f)
  402. return result
  403. def _create_nodes(self, env, target = None, source = None):
  404. """Create and return lists of target and source nodes.
  405. """
  406. src_suf = self.get_src_suffix(env)
  407. target_factory = env.get_factory(self.target_factory)
  408. source_factory = env.get_factory(self.source_factory)
  409. source = self._adjustixes(source, None, src_suf)
  410. slist = env.arg2nodes(source, source_factory)
  411. pre = self.get_prefix(env, slist)
  412. suf = self.get_suffix(env, slist)
  413. if target is None:
  414. try:
  415. t_from_s = slist[0].target_from_source
  416. except AttributeError:
  417. raise UserError("Do not know how to create a target from source `%s'" % slist[0])
  418. except IndexError:
  419. tlist = []
  420. else:
  421. splitext = lambda S: self.splitext(S,env)
  422. tlist = [ t_from_s(pre, suf, splitext) ]
  423. else:
  424. target = self._adjustixes(target, pre, suf, self.ensure_suffix)
  425. tlist = env.arg2nodes(target, target_factory, target=target, source=source)
  426. if self.emitter:
  427. # The emitter is going to do str(node), but because we're
  428. # being called *from* a builder invocation, the new targets
  429. # don't yet have a builder set on them and will look like
  430. # source files. Fool the emitter's str() calls by setting
  431. # up a temporary builder on the new targets.
  432. new_targets = []
  433. for t in tlist:
  434. if not t.is_derived():
  435. t.builder_set(self)
  436. new_targets.append(t)
  437. orig_tlist = tlist[:]
  438. orig_slist = slist[:]
  439. target, source = self.emitter(target=tlist, source=slist, env=env)
  440. # Now delete the temporary builders that we attached to any
  441. # new targets, so that _node_errors() doesn't do weird stuff
  442. # to them because it thinks they already have builders.
  443. for t in new_targets:
  444. if t.builder is self:
  445. # Only delete the temporary builder if the emitter
  446. # didn't change it on us.
  447. t.builder_set(None)
  448. # Have to call arg2nodes yet again, since it is legal for
  449. # emitters to spit out strings as well as Node instances.
  450. tlist = env.arg2nodes(target, target_factory,
  451. target=orig_tlist, source=orig_slist)
  452. slist = env.arg2nodes(source, source_factory,
  453. target=orig_tlist, source=orig_slist)
  454. return tlist, slist
  455. def _execute(self, env, target, source, overwarn={}, executor_kw={}):
  456. # We now assume that target and source are lists or None.
  457. if self.src_builder:
  458. source = self.src_builder_sources(env, source, overwarn)
  459. if self.single_source and len(source) > 1 and target is None:
  460. result = []
  461. if target is None: target = [None]*len(source)
  462. for tgt, src in zip(target, source):
  463. if not tgt is None: tgt = [tgt]
  464. if not src is None: src = [src]
  465. result.extend(self._execute(env, tgt, src, overwarn))
  466. return SCons.Node.NodeList(result)
  467. overwarn.warn()
  468. tlist, slist = self._create_nodes(env, target, source)
  469. # Check for errors with the specified target/source lists.
  470. _node_errors(self, env, tlist, slist)
  471. # The targets are fine, so find or make the appropriate Executor to
  472. # build this particular list of targets from this particular list of
  473. # sources.
  474. executor = None
  475. key = None
  476. if self.multi:
  477. try:
  478. executor = tlist[0].get_executor(create = 0)
  479. except (AttributeError, IndexError):
  480. pass
  481. else:
  482. executor.add_sources(slist)
  483. if executor is None:
  484. if not self.action:
  485. fmt = "Builder %s must have an action to build %s."
  486. raise UserError(fmt % (self.get_name(env or self.env),
  487. list(map(str,tlist))))
  488. key = self.action.batch_key(env or self.env, tlist, slist)
  489. if key:
  490. try:
  491. executor = SCons.Executor.GetBatchExecutor(key)
  492. except KeyError:
  493. pass
  494. else:
  495. executor.add_batch(tlist, slist)
  496. if executor is None:
  497. executor = SCons.Executor.Executor(self.action, env, [],
  498. tlist, slist, executor_kw)
  499. if key:
  500. SCons.Executor.AddBatchExecutor(key, executor)
  501. # Now set up the relevant information in the target Nodes themselves.
  502. for t in tlist:
  503. t.cwd = env.fs.getcwd()
  504. t.builder_set(self)
  505. t.env_set(env)
  506. t.add_source(slist)
  507. t.set_executor(executor)
  508. t.set_explicit(self.is_explicit)
  509. return SCons.Node.NodeList(tlist)
  510. def __call__(self, env, target=None, source=None, chdir=_null, **kw):
  511. # We now assume that target and source are lists or None.
  512. # The caller (typically Environment.BuilderWrapper) is
  513. # responsible for converting any scalar values to lists.
  514. if chdir is _null:
  515. ekw = self.executor_kw
  516. else:
  517. ekw = self.executor_kw.copy()
  518. ekw['chdir'] = chdir
  519. if kw:
  520. if 'srcdir' in kw:
  521. def prependDirIfRelative(f, srcdir=kw['srcdir']):
  522. import os.path
  523. if SCons.Util.is_String(f) and not os.path.isabs(f):
  524. f = os.path.join(srcdir, f)
  525. return f
  526. if not SCons.Util.is_List(source):
  527. source = [source]
  528. source = list(map(prependDirIfRelative, source))
  529. del kw['srcdir']
  530. if self.overrides:
  531. env_kw = self.overrides.copy()
  532. env_kw.update(kw)
  533. else:
  534. env_kw = kw
  535. else:
  536. env_kw = self.overrides
  537. env = env.Override(env_kw)
  538. return self._execute(env, target, source, OverrideWarner(kw), ekw)
  539. def adjust_suffix(self, suff):
  540. if suff and not suff[0] in [ '.', '_', '$' ]:
  541. return '.' + suff
  542. return suff
  543. def get_prefix(self, env, sources=[]):
  544. prefix = self.prefix
  545. if callable(prefix):
  546. prefix = prefix(env, sources)
  547. return env.subst(prefix)
  548. def set_suffix(self, suffix):
  549. if not callable(suffix):
  550. suffix = self.adjust_suffix(suffix)
  551. self.suffix = suffix
  552. def get_suffix(self, env, sources=[]):
  553. suffix = self.suffix
  554. if callable(suffix):
  555. suffix = suffix(env, sources)
  556. return env.subst(suffix)
  557. def set_src_suffix(self, src_suffix):
  558. if not src_suffix:
  559. src_suffix = []
  560. elif not SCons.Util.is_List(src_suffix):
  561. src_suffix = [ src_suffix ]
  562. self.src_suffix = [callable(suf) and suf or self.adjust_suffix(suf) for suf in src_suffix]
  563. def get_src_suffix(self, env):
  564. """Get the first src_suffix in the list of src_suffixes."""
  565. ret = self.src_suffixes(env)
  566. if not ret:
  567. return ''
  568. return ret[0]
  569. def add_emitter(self, suffix, emitter):
  570. """Add a suffix-emitter mapping to this Builder.
  571. This assumes that emitter has been initialized with an
  572. appropriate dictionary type, and will throw a TypeError if
  573. not, so the caller is responsible for knowing that this is an
  574. appropriate method to call for the Builder in question.
  575. """
  576. self.emitter[suffix] = emitter
  577. def add_src_builder(self, builder):
  578. """
  579. Add a new Builder to the list of src_builders.
  580. This requires wiping out cached values so that the computed
  581. lists of source suffixes get re-calculated.
  582. """
  583. self._memo = {}
  584. self.src_builder.append(builder)
  585. def _get_sdict(self, env):
  586. """
  587. Returns a dictionary mapping all of the source suffixes of all
  588. src_builders of this Builder to the underlying Builder that
  589. should be called first.
  590. This dictionary is used for each target specified, so we save a
  591. lot of extra computation by memoizing it for each construction
  592. environment.
  593. Note that this is re-computed each time, not cached, because there
  594. might be changes to one of our source Builders (or one of their
  595. source Builders, and so on, and so on...) that we can't "see."
  596. The underlying methods we call cache their computed values,
  597. though, so we hope repeatedly aggregating them into a dictionary
  598. like this won't be too big a hit. We may need to look for a
  599. better way to do this if performance data show this has turned
  600. into a significant bottleneck.
  601. """
  602. sdict = {}
  603. for bld in self.get_src_builders(env):
  604. for suf in bld.src_suffixes(env):
  605. sdict[suf] = bld
  606. return sdict
  607. def src_builder_sources(self, env, source, overwarn={}):
  608. sdict = self._get_sdict(env)
  609. src_suffixes = self.src_suffixes(env)
  610. lengths = list(set(map(len, src_suffixes)))
  611. def match_src_suffix(name, src_suffixes=src_suffixes, lengths=lengths):
  612. node_suffixes = [name[-l:] for l in lengths]
  613. for suf in src_suffixes:
  614. if suf in node_suffixes:
  615. return suf
  616. return None
  617. result = []
  618. for s in SCons.Util.flatten(source):
  619. if SCons.Util.is_String(s):
  620. match_suffix = match_src_suffix(env.subst(s))
  621. if not match_suffix and not '.' in s:
  622. src_suf = self.get_src_suffix(env)
  623. s = self._adjustixes(s, None, src_suf)[0]
  624. else:
  625. match_suffix = match_src_suffix(s.name)
  626. if match_suffix:
  627. try:
  628. bld = sdict[match_suffix]
  629. except KeyError:
  630. result.append(s)
  631. else:
  632. tlist = bld._execute(env, None, [s], overwarn)
  633. # If the subsidiary Builder returned more than one
  634. # target, then filter out any sources that this
  635. # Builder isn't capable of building.
  636. if len(tlist) > 1:
  637. tlist = [t for t in tlist if match_src_suffix(t.name)]
  638. result.extend(tlist)
  639. else:
  640. result.append(s)
  641. source_factory = env.get_factory(self.source_factory)
  642. return env.arg2nodes(result, source_factory)
  643. def _get_src_builders_key(self, env):
  644. return id(env)
  645. memoizer_counters.append(SCons.Memoize.CountDict('get_src_builders', _get_src_builders_key))
  646. def get_src_builders(self, env):
  647. """
  648. Returns the list of source Builders for this Builder.
  649. This exists mainly to look up Builders referenced as
  650. strings in the 'BUILDER' variable of the construction
  651. environment and cache the result.
  652. """
  653. memo_key = id(env)
  654. try:
  655. memo_dict = self._memo['get_src_builders']
  656. except KeyError:
  657. memo_dict = {}
  658. self._memo['get_src_builders'] = memo_dict
  659. else:
  660. try:
  661. return memo_dict[memo_key]
  662. except KeyError:
  663. pass
  664. builders = []
  665. for bld in self.src_builder:
  666. if SCons.Util.is_String(bld):
  667. try:
  668. bld = env['BUILDERS'][bld]
  669. except KeyError:
  670. continue
  671. builders.append(bld)
  672. memo_dict[memo_key] = builders
  673. return builders
  674. def _subst_src_suffixes_key(self, env):
  675. return id(env)
  676. memoizer_counters.append(SCons.Memoize.CountDict('subst_src_suffixes', _subst_src_suffixes_key))
  677. def subst_src_suffixes(self, env):
  678. """
  679. The suffix list may contain construction variable expansions,
  680. so we have to evaluate the individual strings. To avoid doing
  681. this over and over, we memoize the results for each construction
  682. environment.
  683. """
  684. memo_key = id(env)
  685. try:
  686. memo_dict = self._memo['subst_src_suffixes']
  687. except KeyError:
  688. memo_dict = {}
  689. self._memo['subst_src_suffixes'] = memo_dict
  690. else:
  691. try:
  692. return memo_dict[memo_key]
  693. except KeyError:
  694. pass
  695. suffixes = [env.subst(x) for x in self.src_suffix]
  696. memo_dict[memo_key] = suffixes
  697. return suffixes
  698. def src_suffixes(self, env):
  699. """
  700. Returns the list of source suffixes for all src_builders of this
  701. Builder.
  702. This is essentially a recursive descent of the src_builder "tree."
  703. (This value isn't cached because there may be changes in a
  704. src_builder many levels deep that we can't see.)
  705. """
  706. sdict = {}
  707. suffixes = self.subst_src_suffixes(env)
  708. for s in suffixes:
  709. sdict[s] = 1
  710. for builder in self.get_src_builders(env):
  711. for s in builder.src_suffixes(env):
  712. if s not in sdict:
  713. sdict[s] = 1
  714. suffixes.append(s)
  715. return suffixes
  716. class CompositeBuilder(SCons.Util.Proxy):
  717. """A Builder Proxy whose main purpose is to always have
  718. a DictCmdGenerator as its action, and to provide access
  719. to the DictCmdGenerator's add_action() method.
  720. """
  721. def __init__(self, builder, cmdgen):
  722. if __debug__: logInstanceCreation(self, 'Builder.CompositeBuilder')
  723. SCons.Util.Proxy.__init__(self, builder)
  724. # cmdgen should always be an instance of DictCmdGenerator.
  725. self.cmdgen = cmdgen
  726. self.builder = builder
  727. __call__ = SCons.Util.Delegate('__call__')
  728. def add_action(self, suffix, action):
  729. self.cmdgen.add_action(suffix, action)
  730. self.set_src_suffix(self.cmdgen.src_suffixes())
  731. def is_a_Builder(obj):
  732. """"Returns True iff the specified obj is one of our Builder classes.
  733. The test is complicated a bit by the fact that CompositeBuilder
  734. is a proxy, not a subclass of BuilderBase.
  735. """
  736. return (isinstance(obj, BuilderBase)
  737. or isinstance(obj, CompositeBuilder)
  738. or callable(obj))
  739. # Local Variables:
  740. # tab-width:4
  741. # indent-tabs-mode:nil
  742. # End:
  743. # vim: set expandtab tabstop=4 shiftwidth=4: