Builder object subsystem.
A Builder object is a callable that encapsulates information about how
-to execute actions to create a Node (file) from other Nodes (files), and
-how to create those dependencies for tracking.
+to execute actions to create a target Node (file) from source Nodes
+(files), and how to create those dependencies for tracking.
-The main entry point here is the Builder() factory method. This
-provides a procedural interface that creates the right underlying
-Builder object based on the keyword arguments supplied and the types of
-the arguments.
+The main entry point here is the Builder() factory method. This provides
+a procedural interface that creates the right underlying Builder object
+based on the keyword arguments supplied and the types of the arguments.
The goal is for this external interface to be simple enough that the
vast majority of users can create new Builders as necessary to support
building new types of files in their configurations, without having to
dive any deeper into this subsystem.
+The base class here is BuilderBase. This is a concrete base class which
+does, in fact, represent most Builder objects that we (or users) create.
+
+There is (at present) one subclasses:
+
+ MultiStepBuilder
+
+ This is a Builder that knows how to "chain" Builders so that
+ users can specify a source file that requires multiple steps
+ to turn into a target file. A canonical example is building a
+ program from yacc input file, which requires invoking a builder
+ to turn the .y into a .c, the .c into a .o, and the .o into an
+ executable program.
+
+There is also two proxies that look like Builders:
+
+ CompositeBuilder
+
+ This proxies for a Builder with an action that is actually a
+ dictionary that knows how to map file suffixes to a specific
+ action. This is so that we can invoke different actions
+ (compilers, compile options) for different flavors of source
+ files.
+
+ ListBuilder
+
+ This proxies for a Builder *invocation* where the target
+ is a list of files, not a single file.
+
+Builders and their proxies have the following public interface methods
+used by other modules:
+
+ __call__()
+ THE public interface. Calling a Builder object (with the
+ use of internal helper methods) sets up the target and source
+ dependencies, appropriate mapping to a specific action, and the
+ environment manipulation necessary for overridden construction
+ variable. This also takes care of warning about possible mistakes
+ in keyword arguments.
+
+ targets()
+ Returns the list of targets for a specific builder instance.
+
+ add_emitter()
+ Adds an emitter for a specific file suffix, used by some Tool
+ modules to specify that (for example) a yacc invocation on a .y
+ can create a .h *and* a .c file.
+
+ add_action()
+ Adds an action for a specific file suffix, heavily used by
+ Tool modules to add their specific action(s) for turning
+ a source file into an object file to the global static
+ and shared object file Builders.
+
+There are the following methods for internal use within this module:
+
+ _execute()
+ The internal method that handles the heavily lifting when a
+ Builder is called. This is used so that the __call__() methods
+ can set up warning about possible mistakes in keyword-argument
+ overrides, and *then* execute all of the steps necessary so that
+ the warnings only occur once.
+
+ get_name()
+ Returns the Builder's name within a specific Environment,
+ primarily used to try to return helpful information in error
+ messages.
+
+ adjust_suffix()
+ get_prefix()
+ get_suffix()
+ get_src_suffix()
+ set_src_suffix()
+ Miscellaneous stuff for handling the prefix and suffix
+ manipulation we use in turning source file names into target
+ file names.
+
"""
#
__revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
-import os.path
import UserDict
+import UserList
import SCons.Action
+from SCons.Debug import logInstanceCreation
from SCons.Errors import InternalError, UserError
import SCons.Executor
+import SCons.Node
import SCons.Node.FS
import SCons.Util
import SCons.Warnings
target, source = emitter(target, source, env)
return (target, source)
+class ListEmitter(UserList.UserList):
+ """A callable list of emitters that calls each in sequence,
+ returning the result.
+ """
+ def __call__(self, target, source, env):
+ for e in self.data:
+ target, source = e(target, source, env)
+ return (target, source)
+
+# These are a common errors when calling a Builder;
+# they are similar to the 'target' and 'source' keyword args to builders,
+# so we issue warnings when we see them. The warnings can, of course,
+# be disabled.
+misleading_keywords = {
+ 'targets' : 'target',
+ 'sources' : 'source',
+}
+
+class OverrideWarner(UserDict.UserDict):
+ """A class for warning about keyword arguments that we use as
+ overrides in a Builder call.
+
+ This class exists to handle the fact that a single MultiStepBuilder
+ call can actually invoke multiple builders as a result of a single
+ user-level Builder call. This class only emits the warnings once,
+ no matter how many Builders are invoked.
+ """
+ def __init__(self, dict):
+ UserDict.UserDict.__init__(self, dict)
+ if __debug__: logInstanceCreation(self, 'Builder.OverrideWarner')
+ self.already_warned = None
+ def warn(self):
+ if self.already_warned:
+ return
+ for k in self.keys():
+ try:
+ alt = misleading_keywords[k]
+ except KeyError:
+ pass
+ else:
+ SCons.Warnings.warn(SCons.Warnings.MisleadingKeywordsWarning,
+ "Did you mean to use `%s' instead of `%s'?" % (alt, k))
+ self.already_warned = 1
+
def Builder(**kw):
"""A factory for builder objects."""
composite = None
kw['emitter'] = EmitterProxy(var)
elif SCons.Util.is_Dict(emitter):
kw['emitter'] = DictEmitter(emitter)
+ elif SCons.Util.is_List(emitter):
+ kw['emitter'] = ListEmitter(emitter)
if kw.has_key('src_builder'):
ret = apply(MultiStepBuilder, (), kw)
return ret
-def _init_nodes(builder, env, overrides, tlist, slist):
+def _init_nodes(builder, env, overrides, executor_kw, tlist, slist):
"""Initialize lists of target and source nodes with all of
the proper Builder information.
"""
for t in tlist:
if t.side_effect:
raise UserError, "Multiple ways to build the same target were specified for: %s" % str(t)
- if t.has_builder():
- if t.env != env:
- raise UserError, "Two different environments were specified for the same target: %s"%str(t)
- elif t.overrides != overrides:
- raise UserError, "Two different sets of overrides were specified for the same target: %s"%str(t)
- elif builder.scanner and t.target_scanner and builder.scanner != t.target_scanner:
- raise UserError, "Two different scanners were specified for the same target: %s"%str(t)
+ if t.has_explicit_builder():
+ if not t.env is None and not t.env is env:
+ t_contents = t.builder.action.get_contents(tlist, slist, t.env)
+ contents = t.builder.action.get_contents(tlist, slist, env)
+
+ if t_contents == contents:
+ SCons.Warnings.warn(SCons.Warnings.DuplicateEnvironmentWarning,
+ "Two different environments were specified for target %s,\n\tbut they appear to have the same action: %s"%(str(t), t.builder.action.genstring(tlist, slist, t.env)))
+
+ else:
+ raise UserError, "Two environments with different actions were specified for the same target: %s"%str(t)
if builder.multi:
if t.builder != builder:
raise UserError, "Two different target sets have a target in common: %s"%str(t)
else:
raise UserError, "Two different builders (%s and %s) were specified for the same target: %s"%(t.builder.get_name(env), builder.get_name(env), str(t))
+ elif isinstance(t.builder, ListBuilder) ^ isinstance(builder, ListBuilder):
+ raise UserError, "Cannot build same target `%s' as singular and list"%str(t)
elif t.sources != slist:
- raise UserError, "Multiple ways to build the same target were specified for: %s" % str(t)
+ raise UserError, "Multiple ways to build the same target were specified for: %s (from %s and from %s)" % (str(t), map(str,t.sources), map(str,slist))
+
+ if builder.single_source:
+ if len(slist) > 1:
+ raise UserError, "More than one source given for single-source builder: targets=%s sources=%s" % (map(str,tlist), map(str,slist))
# The targets are fine, so find or make the appropriate Executor to
# build this particular list of targets from this particular list of
else:
executor.add_sources(slist)
if executor is None:
- executor = SCons.Executor.Executor(builder,
- tlist[0].generate_build_env(env),
- overrides,
+ if not builder.action:
+ raise UserError, "Builder %s must have an action to build %s."%(builder.get_name(env or builder.env), map(str,tlist))
+ executor = SCons.Executor.Executor(builder.action,
+ env or builder.env,
+ [], # env already has overrides
tlist,
- slist)
+ slist,
+ executor_kw)
# Now set up the relevant information in the target Nodes themselves.
for t in tlist:
- t.overrides = overrides
t.cwd = SCons.Node.FS.default_fs.getcwd()
t.builder_set(builder)
t.env_set(env)
t.add_source(slist)
t.set_executor(executor)
- if builder.scanner:
- t.target_scanner = builder.scanner
-
- # Last, add scanners from the Environment to the source Nodes.
- for s in slist:
- src_key = s.scanner_key() # the file suffix
- scanner = env.get_scanner(src_key)
- if scanner:
- s.source_scanner = scanner
+ t.set_explicit(builder.is_explicit)
class EmitterProxy:
"""This is a callable class that can act as a
# Recursively substitute the variable.
# We can't use env.subst() because it deals only
# in strings. Maybe we should change that?
- while SCons.Util.is_String(emitter) and \
- env.has_key(emitter):
+ while SCons.Util.is_String(emitter) and env.has_key(emitter):
emitter = env[emitter]
- if not callable(emitter):
- return (target, source)
+ if callable(emitter):
+ target, source = emitter(target, source, env)
+ elif SCons.Util.is_List(emitter):
+ for e in emitter:
+ target, source = e(target, source, env)
+
+ return (target, source)
- return emitter(target, source, env)
def __cmp__(self, other):
return cmp(self.var, other.var)
nodes (files) from input nodes (files).
"""
+ __metaclass__ = SCons.Memoize.Memoized_Metaclass
+
def __init__(self, action = None,
prefix = '',
suffix = '',
src_suffix = '',
- node_factory = SCons.Node.FS.default_fs.File,
- target_factory = None,
- source_factory = None,
- scanner = None,
+ target_factory = SCons.Node.FS.default_fs.File,
+ source_factory = SCons.Node.FS.default_fs.File,
+ target_scanner = None,
+ source_scanner = None,
emitter = None,
multi = 0,
env = None,
- overrides = {}):
+ single_source = 0,
+ name = None,
+ chdir = _null,
+ is_explicit = 1,
+ **overrides):
+ if __debug__: logInstanceCreation(self, 'Builder.BuilderBase')
self.action = SCons.Action.Action(action)
self.multi = multi
if SCons.Util.is_Dict(prefix):
suffix = CallableSelector(suffix)
self.suffix = suffix
self.env = env
+ self.single_source = single_source
+ if overrides.has_key('overrides'):
+ SCons.Warnings.warn(SCons.Warnings.DeprecatedWarning,
+ "The \"overrides\" keyword to Builder() creation has been deprecated;\n" +\
+ "\tspecify the items as keyword arguments to the Builder() call instead.")
+ overrides.update(overrides['overrides'])
+ del overrides['overrides']
+ if overrides.has_key('scanner'):
+ SCons.Warnings.warn(SCons.Warnings.DeprecatedWarning,
+ "The \"scanner\" keyword to Builder() creation has been deprecated;\n"
+ "\tuse: source_scanner or target_scanner as appropriate.")
+ del overrides['scanner']
self.overrides = overrides
self.set_src_suffix(src_suffix)
- self.target_factory = target_factory or node_factory
- self.source_factory = source_factory or node_factory
- self.scanner = scanner
+ self.target_factory = target_factory
+ self.source_factory = source_factory
+ self.target_scanner = target_scanner
+ self.source_scanner = source_scanner
self.emitter = emitter
+ # Optional Builder name should only be used for Builders
+ # that don't get attached to construction environments.
+ if name:
+ self.name = name
+ self.executor_kw = {}
+ if not chdir is _null:
+ self.executor_kw['chdir'] = chdir
+ self.is_explicit = is_explicit
+
def __nonzero__(self):
raise InternalError, "Do not test for the Node.builder attribute directly; use Node.has_builder() instead"
Look at the BUILDERS variable of env, expecting it to be a
dictionary containing this Builder, and return the key of the
- dictionary."""
+ dictionary. If there's no key, then return a directly-configured
+ name (if there is one) or the name of the class (by default)."""
try:
index = env['BUILDERS'].values().index(self)
return env['BUILDERS'].keys()[index]
except (AttributeError, KeyError, ValueError):
- return str(self.__class__)
+ try:
+ return self.name
+ except AttributeError:
+ return str(self.__class__)
def __cmp__(self, other):
return cmp(self.__dict__, other.__dict__)
- def splitext(self, path):
+ def splitext(self, path, env=None):
+ if not env:
+ env = self.env
+ if env:
+ matchsuf = filter(lambda S,path=path: path[-len(S):] == S,
+ self.src_suffixes(env))
+ if matchsuf:
+ suf = max(map(None, map(len, matchsuf), matchsuf))[1]
+ return [path[:-len(suf)], path[-len(suf):]]
return SCons.Util.splitext(path)
- def _create_nodes(self, env, overrides, target = None, source = None):
+ def _create_nodes(self, env, overwarn, target = None, source = None):
"""Create and return lists of target and source nodes.
"""
- def adjustixes(files, pre, suf, self=self):
+ def _adjustixes(files, pre, suf):
if not files:
return []
- ret = []
+ result = []
if not SCons.Util.is_List(files):
files = [files]
for f in files:
if SCons.Util.is_String(f):
- if pre:
- path, fn = os.path.split(os.path.normpath(f))
- if fn[:len(pre)] != pre:
- f = os.path.join(path, pre + fn)
- # Only append a suffix if the file does not have one.
- if suf and not self.splitext(f)[1]:
- if f[-len(suf):] != suf:
- f = f + suf
- ret.append(f)
- return ret
-
- env = env.Override(overrides)
+ f = SCons.Util.adjustixes(f, pre, suf)
+ result.append(f)
+ return result
+
+ overwarn.warn()
src_suf = self.get_src_suffix(env)
- source = adjustixes(source, None, src_suf)
+ source = _adjustixes(source, None, src_suf)
slist = env.arg2nodes(source, self.source_factory)
pre = self.get_prefix(env, slist)
t_from_s = slist[0].target_from_source
except AttributeError:
raise UserError("Do not know how to create a target from source `%s'" % slist[0])
- tlist = [ t_from_s(pre, suf, self.splitext) ]
+ splitext = lambda S,self=self,env=env: self.splitext(S,env)
+ tlist = [ t_from_s(pre, suf, splitext) ]
else:
- target = adjustixes(target, pre, suf)
+ target = _adjustixes(target, pre, suf)
tlist = env.arg2nodes(target, self.target_factory)
if self.emitter:
new_targets = []
for t in tlist:
if not t.is_derived():
- t.builder = self
+ t.builder_set(self)
new_targets.append(t)
-
+
target, source = self.emitter(target=tlist, source=slist, env=env)
# Now delete the temporary builders that we attached to any
if t.builder is self:
# Only delete the temporary builder if the emitter
# didn't change it on us.
- t.builder = None
+ t.builder_set(None)
# Have to call arg2nodes yet again, since it is legal for
# emitters to spit out strings as well as Node instances.
return tlist, slist
- def __call__(self, env, target = None, source = _null, **overrides):
- if source is _null:
- source = target
- target = None
- tlist, slist = self._create_nodes(env, overrides, target, source)
+ def _execute(self, env, target, source, overwarn={}, executor_kw={}):
+ # We now assume that target and source are lists or None.
+ if self.single_source and len(source) > 1 and target is None:
+ result = []
+ if target is None: target = [None]*len(source)
+ for tgt, src in zip(target, source):
+ if not tgt is None: tgt = [tgt]
+ if not src is None: src = [src]
+ result.extend(self._execute(env, tgt, src, overwarn))
+ return result
+
+ tlist, slist = self._create_nodes(env, overwarn, target, source)
if len(tlist) == 1:
- _init_nodes(self, env, overrides, tlist, slist)
- tlist = tlist[0]
+ builder = self
else:
- _init_nodes(ListBuilder(self, env, tlist), env, overrides, tlist, slist)
+ builder = ListBuilder(self, env, tlist)
+ _init_nodes(builder, env, overwarn.data, executor_kw, tlist, slist)
- return tlist
+ return SCons.Node.NodeList(tlist)
+
+ def __call__(self, env, target=None, source=None, chdir=_null, **kw):
+ # We now assume that target and source are lists or None.
+ # The caller (typically Environment.BuilderWrapper) is
+ # responsible for converting any scalar values to lists.
+ if chdir is _null:
+ ekw = self.executor_kw
+ else:
+ ekw = self.executor_kw.copy()
+ ekw['chdir'] = chdir
+ if kw:
+ if self.overrides:
+ env_kw = self.overrides.copy()
+ env_kw.update(kw)
+ else:
+ env_kw = kw
+ else:
+ env_kw = self.overrides
+ env = env.Override(env_kw)
+ return self._execute(env, target, source, OverrideWarner(kw), ekw)
def adjust_suffix(self, suff):
if suff and not suff[0] in [ '.', '_', '$' ]:
"""
self.emitter[suffix] = emitter
+if not SCons.Memoize.has_metaclass:
+ _Base = BuilderBase
+ class BuilderBase(SCons.Memoize.Memoizer, _Base):
+ "Cache-backed version of BuilderBase"
+ def __init__(self, *args, **kw):
+ apply(_Base.__init__, (self,)+args, kw)
+ SCons.Memoize.Memoizer.__init__(self)
+
class ListBuilder(SCons.Util.Proxy):
"""A Proxy to support building an array of targets (for example,
foo.o and foo.h from foo.y) from a single Action execution.
"""
def __init__(self, builder, env, tlist):
+ if __debug__: logInstanceCreation(self, 'Builder.ListBuilder')
SCons.Util.Proxy.__init__(self, builder)
self.builder = builder
- self.scanner = builder.scanner
+ self.target_scanner = builder.target_scanner
+ self.source_scanner = builder.source_scanner
self.env = env
self.tlist = tlist
self.multi = builder.multi
+ self.single_source = builder.single_source
def targets(self, node):
"""Return the list of targets for this builder instance.
"""
return self.tlist
- def __cmp__(self, other):
- return cmp(self.__dict__, other.__dict__)
-
def get_name(self, env):
"""Attempts to get the name of the Builder."""
prefix = '',
suffix = '',
src_suffix = '',
- node_factory = SCons.Node.FS.default_fs.File,
- target_factory = None,
- source_factory = None,
- scanner=None,
- emitter=None):
+ target_factory = SCons.Node.FS.default_fs.File,
+ source_factory = SCons.Node.FS.default_fs.File,
+ target_scanner = None,
+ source_scanner = None,
+ emitter=None,
+ single_source=0):
+ if __debug__: logInstanceCreation(self, 'Builder.MultiStepBuilder')
BuilderBase.__init__(self, action, prefix, suffix, src_suffix,
- node_factory, target_factory, source_factory,
- scanner, emitter)
+ target_factory, source_factory,
+ target_scanner, source_scanner, emitter,
+ single_source = single_source)
if not SCons.Util.is_List(src_builder):
src_builder = [ src_builder ]
self.src_builder = src_builder
- self.sdict = {}
- self.cached_src_suffixes = {} # source suffixes keyed on id(env)
-
- def __call__(self, env, target = None, source = _null, **kw):
- if source is _null:
- source = target
- target = None
+ def _get_sdict(self, env):
+ "__cacheable__"
+ sdict = {}
+ for bld in self.src_builder:
+ if SCons.Util.is_String(bld):
+ try:
+ bld = env['BUILDERS'][bld]
+ except KeyError:
+ continue
+ for suf in bld.src_suffixes(env):
+ sdict[suf] = bld
+ return sdict
+
+ def _execute(self, env, target, source, overwarn={}, executor_kw={}):
+ # We now assume that target and source are lists or None.
slist = env.arg2nodes(source, self.source_factory)
final_sources = []
- try:
- sdict = self.sdict[id(env)]
- except KeyError:
- sdict = {}
- self.sdict[id(env)] = sdict
- for bld in self.src_builder:
- if SCons.Util.is_String(bld):
- try:
- bld = env['BUILDERS'][bld]
- except KeyError:
- continue
- for suf in bld.src_suffixes(env):
- sdict[suf] = bld
+ sdict = self._get_sdict(env)
src_suffixes = self.src_suffixes(env)
+ def match_src_suffix(node, src_suffixes=src_suffixes):
+ # This reaches directly into the Node.name attribute (instead
+ # of using an accessor function) for performance reasons.
+ return filter(lambda s, n=node.name:
+ n[-len(s):] == s,
+ src_suffixes)
+
for snode in slist:
- base, ext = self.splitext(str(snode))
- if sdict.has_key(ext):
- tgt = apply(sdict[ext], (env, None, snode), kw)
- # Only supply the builder with sources it is capable
- # of building.
- if SCons.Util.is_List(tgt):
- tgt = filter(lambda x, self=self, suf=src_suffixes:
- self.splitext(SCons.Util.to_String(x))[1] in suf,
- tgt)
- if not SCons.Util.is_List(tgt):
- final_sources.append(tgt)
+ name = snode.name
+ match = match_src_suffix(snode)
+ if match:
+ try:
+ bld = sdict[match[0]]
+ except KeyError:
+ final_sources.append(snode)
else:
- final_sources.extend(tgt)
+ tlist = bld._execute(env, None, [snode], overwarn)
+ # If the subsidiary Builder returned more than one
+ # target, then filter out any sources that this
+ # Builder isn't capable of building.
+ if len(tlist) > 1:
+ tlist = filter(match_src_suffix, tlist)
+ final_sources.extend(tlist)
else:
final_sources.append(snode)
- return apply(BuilderBase.__call__,
- (self, env, target, final_sources), kw)
+ return BuilderBase._execute(self, env, target, final_sources, overwarn)
def get_src_builders(self, env):
"""Return all the src_builders for this Builder.
def src_suffixes(self, env):
"""Return a list of the src_suffix attributes for all
src_builders of this Builder.
+ __cacheable__
"""
- try:
- return self.cached_src_suffixes[id(env)]
- except KeyError:
- suffixes = BuilderBase.src_suffixes(self, env)
- for builder in self.get_src_builders(env):
- suffixes.extend(builder.src_suffixes(env))
- self.cached_src_suffixes[id(env)] = suffixes
- return suffixes
+ suffixes = BuilderBase.src_suffixes(self, env)
+ for builder in self.get_src_builders(env):
+ suffixes.extend(builder.src_suffixes(env))
+ return suffixes
class CompositeBuilder(SCons.Util.Proxy):
"""A Builder Proxy whose main purpose is to always have
"""
def __init__(self, builder, cmdgen):
+ if __debug__: logInstanceCreation(self, 'Builder.CompositeBuilder')
SCons.Util.Proxy.__init__(self, builder)
# cmdgen should always be an instance of DictCmdGenerator.
def add_action(self, suffix, action):
self.cmdgen.add_action(suffix, action)
self.set_src_suffix(self.cmdgen.src_suffixes())
-
- def __cmp__(self, other):
- return cmp(self.__dict__, other.__dict__)