Generate an error message if a BUILDERS entry is set to something that isn't
[scons.git] / src / engine / SCons / Environment.py
index e2883f47f9d46be3cda1a5fbc3e916f061adecc8..f840dfc358bee4dae0860a64da99a01d343148d4 100644 (file)
@@ -6,7 +6,7 @@ construction information to the build engine.
 
 Keyword arguments supplied when the construction Environment
 is created are construction variables used to initialize the
-Environment 
+Environment
 """
 
 #
@@ -37,8 +37,9 @@ __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
 
 import copy
 import os
-import os.path
-import popen2
+import sys
+import re
+import shlex
 import string
 from UserDict import UserDict
 
@@ -47,14 +48,14 @@ import SCons.Builder
 from SCons.Debug import logInstanceCreation
 import SCons.Defaults
 import SCons.Errors
+import SCons.Memoize
 import SCons.Node
 import SCons.Node.Alias
 import SCons.Node.FS
 import SCons.Node.Python
 import SCons.Platform
+import SCons.SConf
 import SCons.SConsign
-import SCons.Sig
-import SCons.Sig.TimeStamp
 import SCons.Subst
 import SCons.Tool
 import SCons.Util
@@ -65,35 +66,20 @@ class _Null:
 
 _null = _Null
 
+_warn_copy_deprecated = True
+_warn_source_signatures_deprecated = True
+_warn_target_signatures_deprecated = True
+
 CleanTargets = {}
 CalculatorArgs = {}
 
+semi_deepcopy = SCons.Util.semi_deepcopy
+
 # Pull UserError into the global name space for the benefit of
 # Environment().SourceSignatures(), which has some import statements
 # which seem to mess up its ability to reference SCons directly.
 UserError = SCons.Errors.UserError
 
-def installFunc(target, source, env):
-    """Install a source file into a target using the function specified
-    as the INSTALL construction variable."""
-    try:
-        install = env['INSTALL']
-    except KeyError:
-        raise SCons.Errors.UserError('Missing INSTALL construction variable.')
-    return install(target[0].path, source[0].path, env)
-
-def installString(target, source, env):
-    s = env.get('INSTALLSTR', '')
-    if callable(s):
-        return s(target[0].path, source[0].path, env)
-    else:
-        return env.subst_target_source(s, 0, target, source)
-
-installAction = SCons.Action.Action(installFunc, installString)
-
-InstallBuilder = SCons.Builder.Builder(action=installAction,
-                                       name='InstallBuilder')
-
 def alias_builder(env, target, source):
     pass
 
@@ -104,23 +90,6 @@ AliasBuilder = SCons.Builder.Builder(action = alias_builder,
                                      is_explicit = None,
                                      name='AliasBuilder')
 
-def our_deepcopy(x):
-   """deepcopy lists and dictionaries, and just copy the reference
-   for everything else."""
-   if SCons.Util.is_Dict(x):
-       copy = {}
-       for key in x.keys():
-           copy[key] = our_deepcopy(x[key])
-   elif SCons.Util.is_List(x):
-       copy = map(our_deepcopy, x)
-       try:
-           copy = x.__class__(copy)
-       except AttributeError:
-           pass
-   else:
-       copy = x
-   return copy
-
 def apply_tools(env, tools, toolpath):
     # Store the toolpath in the Environment.
     if toolpath is not None:
@@ -137,24 +106,43 @@ def apply_tools(env, tools, toolpath):
         else:
             env.Tool(tool)
 
-# These names are controlled by SCons; users should never set or override
-# them.  This warning can optionally be turned off, but scons will still
-# ignore the illegal variable names even if it's off.
-reserved_construction_var_names = \
-    ['TARGET', 'TARGETS', 'SOURCE', 'SOURCES']
+# These names are (or will be) controlled by SCons; users should never
+# set or override them.  This warning can optionally be turned off,
+# but scons will still ignore the illegal variable names even if it's off.
+reserved_construction_var_names = [
+    'CHANGED_SOURCES',
+    'CHANGED_TARGETS',
+    'SOURCE',
+    'SOURCES',
+    'TARGET',
+    'TARGETS',
+    'UNCHANGED_SOURCES',
+    'UNCHANGED_TARGETS',
+]
+
+future_reserved_construction_var_names = [
+    #'HOST_OS',
+    #'HOST_ARCH',
+    #'HOST_CPU',
+    ]
 
 def copy_non_reserved_keywords(dict):
-    result = our_deepcopy(dict)
+    result = semi_deepcopy(dict)
     for k in result.keys():
         if k in reserved_construction_var_names:
-            SCons.Warnings.warn(SCons.Warnings.ReservedVariableWarning,
-                                "Ignoring attempt to set reserved variable `%s'" % k)
+            msg = "Ignoring attempt to set reserved variable `$%s'"
+            SCons.Warnings.warn(SCons.Warnings.ReservedVariableWarning, msg % k)
             del result[k]
     return result
 
 def _set_reserved(env, key, value):
-    msg = "Ignoring attempt to set reserved variable `%s'" % key
-    SCons.Warnings.warn(SCons.Warnings.ReservedVariableWarning, msg)
+    msg = "Ignoring attempt to set reserved variable `$%s'"
+    SCons.Warnings.warn(SCons.Warnings.ReservedVariableWarning, msg % key)
+
+def _set_future_reserved(env, key, value):
+    env._dict[key] = value
+    msg = "`$%s' will be reserved in a future release and setting it will become ignored"
+    SCons.Warnings.warn(SCons.Warnings.FutureReservedVariableWarning, msg % key)
 
 def _set_BUILDERS(env, key, value):
     try:
@@ -162,8 +150,12 @@ def _set_BUILDERS(env, key, value):
         for k in bd.keys():
             del bd[k]
     except KeyError:
-        env._dict[key] = BuilderDict(kwbd, env)
-    env._dict[key].update(value)
+        bd = BuilderDict(kwbd, env)
+        env._dict[key] = bd
+    for k, v in value.items():
+        if not SCons.Builder.is_a_Builder(v):
+            raise SCons.Errors.UserError('%s is not a Builder.' % repr(v))
+    bd.update(value)
 
 def _del_SCANNERS(env, key):
     del env._dict[key]
@@ -173,22 +165,121 @@ def _set_SCANNERS(env, key, value):
     env._dict[key] = value
     env.scanner_map_delete()
 
-class BuilderWrapper:
-    """Wrapper class that associates an environment with a Builder at
-    instantiation."""
-    def __init__(self, env, builder):
-        self.env = env
-        self.builder = builder
+def _delete_duplicates(l, keep_last):
+    """Delete duplicates from a sequence, keeping the first or last."""
+    seen={}
+    result=[]
+    if keep_last:           # reverse in & out, then keep first
+        l.reverse()
+    for i in l:
+        try:
+            if not seen.has_key(i):
+                result.append(i)
+                seen[i]=1
+        except TypeError:
+            # probably unhashable.  Just keep it.
+            result.append(i)
+    if keep_last:
+        result.reverse()
+    return result
+
+
 
+# The following is partly based on code in a comment added by Peter
+# Shannon at the following page (there called the "transplant" class):
+#
+# ASPN : Python Cookbook : Dynamically added methods to a class
+# http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/81732
+#
+# We had independently been using the idiom as BuilderWrapper, but
+# factoring out the common parts into this base class, and making
+# BuilderWrapper a subclass that overrides __call__() to enforce specific
+# Builder calling conventions, simplified some of our higher-layer code.
+
+class MethodWrapper:
+    """
+    A generic Wrapper class that associates a method (which can
+    actually be any callable) with an object.  As part of creating this
+    MethodWrapper object an attribute with the specified (by default,
+    the name of the supplied method) is added to the underlying object.
+    When that new "method" is called, our __call__() method adds the
+    object as the first argument, simulating the Python behavior of
+    supplying "self" on method calls.
+
+    We hang on to the name by which the method was added to the underlying
+    base class so that we can provide a method to "clone" ourselves onto
+    a new underlying object being copied (without which we wouldn't need
+    to save that info).
+    """
+    def __init__(self, object, method, name=None):
+        if name is None:
+            name = method.__name__
+        self.object = object
+        self.method = method
+        self.name = name
+        setattr(self.object, name, self)
+
+    def __call__(self, *args, **kwargs):
+        nargs = (self.object,) + args
+        return apply(self.method, nargs, kwargs)
+
+    def clone(self, new_object):
+        """
+        Returns an object that re-binds the underlying "method" to
+        the specified new object.
+        """
+        return self.__class__(new_object, self.method, self.name)
+
+class BuilderWrapper(MethodWrapper):
+    """
+    A MethodWrapper subclass that that associates an environment with
+    a Builder.
+
+    This mainly exists to wrap the __call__() function so that all calls
+    to Builders can have their argument lists massaged in the same way
+    (treat a lone argument as the source, treat two arguments as target
+    then source, make sure both target and source are lists) without
+    having to have cut-and-paste code to do it.
+
+    As a bit of obsessive backwards compatibility, we also intercept
+    attempts to get or set the "env" or "builder" attributes, which were
+    the names we used before we put the common functionality into the
+    MethodWrapper base class.  We'll keep this around for a while in case
+    people shipped Tool modules that reached into the wrapper (like the
+    Tool/qt.py module does, or did).  There shouldn't be a lot attribute
+    fetching or setting on these, so a little extra work shouldn't hurt.
+    """
     def __call__(self, target=None, source=_null, *args, **kw):
         if source is _null:
             source = target
             target = None
-        if not target is None and not SCons.Util.is_List(target):
+        if target is not None and not SCons.Util.is_List(target):
             target = [target]
-        if not source is None and not SCons.Util.is_List(source):
+        if source is not None and not SCons.Util.is_List(source):
             source = [source]
-        return apply(self.builder, (self.env, target, source) + args, kw)
+        return apply(MethodWrapper.__call__, (self, target, source) + args, kw)
+
+    def __repr__(self):
+        return '<BuilderWrapper %s>' % repr(self.name)
+
+    def __str__(self):
+        return self.__repr__()
+
+    def __getattr__(self, name):
+        if name == 'env':
+            return self.object
+        elif name == 'builder':
+            return self.method
+        else:
+            raise AttributeError, name
+
+    def __setattr__(self, name, value):
+        if name == 'env':
+            self.object = value
+        elif name == 'builder':
+            self.method = value
+        else:
+            self.__dict__[name] = value
 
     # This allows a Builder to be executed directly
     # through the Environment to which it's attached.
@@ -197,9 +288,9 @@ class BuilderWrapper:
     # But we do have a unit test for this, and can't
     # yet rule out that it would be useful in the
     # future, so leave it for now.
-    def execute(self, **kw):
-        kw['env'] = self.env
-        apply(self.builder.execute, (), kw)
+    #def execute(self, **kw):
+    #    kw['env'] = self.env
+    #    apply(self.builder.execute, (), kw)
 
 class BuilderDict(UserDict):
     """This is a dictionary-like class used by an Environment to hold
@@ -213,28 +304,18 @@ class BuilderDict(UserDict):
         self.env = env
         UserDict.__init__(self, dict)
 
+    def __semi_deepcopy__(self):
+        return self.__class__(self.data, self.env)
+
     def __setitem__(self, item, val):
-        UserDict.__setitem__(self, item, val)
         try:
-            self.setenvattr(item, val)
+            method = getattr(self.env, item).method
         except AttributeError:
-            # Have to catch this because sometimes __setitem__ gets
-            # called out of __init__, when we don't have an env
-            # attribute yet, nor do we want one!
             pass
-
-    def setenvattr(self, item, val):
-        """Set the corresponding environment attribute for this Builder.
-
-        If the value is already a BuilderWrapper, we pull the builder
-        out of it and make another one, so that making a copy of an
-        existing BuilderDict is guaranteed separate wrappers for each
-        Builder + Environment pair."""
-        try:
-            builder = val.builder
-        except AttributeError:
-            builder = val
-        setattr(self.env, item, BuilderWrapper(self.env, builder))
+        else:
+            self.env.RemoveMethod(method)
+        UserDict.__setitem__(self, item, val)
+        BuilderWrapper(self.env, val, item)
 
     def __delitem__(self, item):
         UserDict.__delitem__(self, item)
@@ -244,6 +325,18 @@ class BuilderDict(UserDict):
         for i, v in dict.items():
             self.__setitem__(i, v)
 
+
+
+_is_valid_var = re.compile(r'[_a-zA-Z]\w*$')
+
+def is_valid_construction_var(varstr):
+    """Return if the specified string is a legitimate construction
+    variable.
+    """
+    return _is_valid_var.match(varstr)
+
+
+
 class SubstitutionEnvironment:
     """Base class for different flavors of construction environments.
 
@@ -277,11 +370,12 @@ class SubstitutionEnvironment:
         """Initialization of an underlying SubstitutionEnvironment class.
         """
         if __debug__: logInstanceCreation(self, 'Environment.SubstitutionEnvironment')
-        self.fs = SCons.Node.FS.default_fs or SCons.Node.FS.FS()
+        self.fs = SCons.Node.FS.get_default_fs()
         self.ans = SCons.Node.Alias.default_ans
         self.lookup_list = SCons.Node.arg2nodes_lookups
         self._dict = kw.copy()
         self._init_special()
+        self.added_methods = []
         #self._memo = {}
 
     def _init_special(self):
@@ -293,9 +387,16 @@ class SubstitutionEnvironment:
         self._special_set = {}
         for key in reserved_construction_var_names:
             self._special_set[key] = _set_reserved
+        for key in future_reserved_construction_var_names:
+            self._special_set[key] = _set_future_reserved
         self._special_set['BUILDERS'] = _set_BUILDERS
         self._special_set['SCANNERS'] = _set_SCANNERS
 
+        # Freeze the keys of self._special_set in a list for use by
+        # methods that need to check.  (Empirically, list scanning has
+        # gotten better than dict.has_key() in Python 2.5.)
+        self._special_set_keys = self._special_set.keys()
+
     def __cmp__(self, other):
         return cmp(self._dict, other._dict)
 
@@ -310,25 +411,44 @@ class SubstitutionEnvironment:
         return self._dict[key]
 
     def __setitem__(self, key, value):
-        special = self._special_set.get(key)
-        if special:
-            special(self, key, value)
+        # This is heavily used.  This implementation is the best we have
+        # according to the timings in bench/env.__setitem__.py.
+        #
+        # The "key in self._special_set_keys" test here seems to perform
+        # pretty well for the number of keys we have.  A hard-coded
+        # list works a little better in Python 2.5, but that has the
+        # disadvantage of maybe getting out of sync if we ever add more
+        # variable names.  Using self._special_set.has_key() works a
+        # little better in Python 2.4, but is worse then this test.
+        # So right now it seems like a good trade-off, but feel free to
+        # revisit this with bench/env.__setitem__.py as needed (and
+        # as newer versions of Python come out).
+        if key in self._special_set_keys:
+            self._special_set[key](self, key, value)
         else:
-            if not SCons.Util.is_valid_construction_var(key):
-                raise SCons.Errors.UserError, "Illegal construction variable `%s'" % key
+            # If we already have the entry, then it's obviously a valid
+            # key and we don't need to check.  If we do check, using a
+            # global, pre-compiled regular expression directly is more
+            # efficient than calling another function or a method.
+            if not self._dict.has_key(key) \
+               and not _is_valid_var.match(key):
+                    raise SCons.Errors.UserError, "Illegal construction variable `%s'" % key
             self._dict[key] = value
 
     def get(self, key, default=None):
-        "Emulates the get() method of dictionaries."""
+        """Emulates the get() method of dictionaries."""
         return self._dict.get(key, default)
 
     def has_key(self, key):
         return self._dict.has_key(key)
 
+    def __contains__(self, key):
+        return self._dict.__contains__(key)
+
     def items(self):
         return self._dict.items()
 
-    def arg2nodes(self, args, node_factory=_null, lookup_list=_null):
+    def arg2nodes(self, args, node_factory=_null, lookup_list=_null, **kw):
         if node_factory is _null:
             node_factory = self.fs.File
         if lookup_list is _null:
@@ -337,10 +457,7 @@ class SubstitutionEnvironment:
         if not args:
             return []
 
-        if SCons.Util.is_List(args):
-            args = SCons.Util.flatten(args)
-        else:
-            args = [args]
+        args = SCons.Util.flatten(args)
 
         nodes = []
         for v in args:
@@ -348,11 +465,13 @@ class SubstitutionEnvironment:
                 n = None
                 for l in lookup_list:
                     n = l(v)
-                    if not n is None:
+                    if n is not None:
                         break
-                if not n is None:
+                if n is not None:
                     if SCons.Util.is_String(n):
-                        n = self.subst(n, raw=1)
+                        # n = self.subst(n, raw=1, **kw)
+                        kw['raw'] = 1
+                        n = apply(self.subst, (n,), kw)
                         if node_factory:
                             n = node_factory(n)
                     if SCons.Util.is_List(n):
@@ -360,14 +479,16 @@ class SubstitutionEnvironment:
                     else:
                         nodes.append(n)
                 elif node_factory:
-                    v = node_factory(self.subst(v, raw=1))
+                    # v = node_factory(self.subst(v, raw=1, **kw))
+                    kw['raw'] = 1
+                    v = node_factory(apply(self.subst, (v,), kw))
                     if SCons.Util.is_List(v):
                         nodes.extend(v)
                     else:
                         nodes.append(v)
             else:
                 nodes.append(v)
-    
+
         return nodes
 
     def gvars(self):
@@ -376,7 +497,7 @@ class SubstitutionEnvironment:
     def lvars(self):
         return {}
 
-    def subst(self, string, raw=0, target=None, source=None, conv=None):
+    def subst(self, string, raw=0, target=None, source=None, conv=None, executor=None):
         """Recursively interpolates construction variables from the
         Environment into the specified string, returning the expanded
         result.  Construction variables are specified by a $ prefix
@@ -389,6 +510,8 @@ class SubstitutionEnvironment:
         gvars = self.gvars()
         lvars = self.lvars()
         lvars['__env__'] = self
+        if executor:
+            lvars.update(executor.get_lvars())
         return SCons.Subst.scons_subst(string, self, raw, target, source, gvars, lvars, conv)
 
     def subst_kw(self, kw, raw=0, target=None, source=None):
@@ -400,12 +523,14 @@ class SubstitutionEnvironment:
             nkw[k] = v
         return nkw
 
-    def subst_list(self, string, raw=0, target=None, source=None, conv=None):
+    def subst_list(self, string, raw=0, target=None, source=None, conv=None, executor=None):
         """Calls through to SCons.Subst.scons_subst_list().  See
         the documentation for that function."""
         gvars = self.gvars()
         lvars = self.lvars()
         lvars['__env__'] = self
+        if executor:
+            lvars.update(executor.get_lvars())
         return SCons.Subst.scons_subst_list(string, self, raw, target, source, gvars, lvars, conv)
 
     def subst_path(self, path, target=None, source=None):
@@ -425,7 +550,7 @@ class SubstitutionEnvironment:
             try:
                 get = obj.get
             except AttributeError:
-                pass
+                obj = SCons.Util.to_String_for_subst(obj)
             else:
                 obj = get()
             return obj
@@ -441,7 +566,7 @@ class SubstitutionEnvironment:
                         # We have an object plus a string, or multiple
                         # objects that we need to smush together.  No choice
                         # but to make them into a string.
-                        p = string.join(map(SCons.Util.to_String, p), '')
+                        p = string.join(map(SCons.Util.to_String_for_subst, p), '')
             else:
                 p = s(p)
             r.append(p)
@@ -450,33 +575,44 @@ class SubstitutionEnvironment:
     subst_target_source = subst
 
     def backtick(self, command):
-        try:
-            popen2.Popen3
-        except AttributeError:
-            (tochild, fromchild, childerr) = os.popen3(self.subst(command))
-            tochild.close()
-            err = childerr.read()
-            out = fromchild.read()
-            fromchild.close()
-            status = childerr.close()
-        else:
-            p = popen2.Popen3(command, 1)
-            p.tochild.close()
-            out = p.fromchild.read()
-            err = p.childerr.read()
-            status = p.wait()
+        import subprocess
+        # common arguments
+        kw = { 'stdin' : 'devnull',
+               'stdout' : subprocess.PIPE,
+               'stderr' : subprocess.PIPE,
+               'universal_newlines' : True,
+             }
+        # if the command is a list, assume it's been quoted
+        # othewise force a shell
+        if not SCons.Util.is_List(command): kw['shell'] = True
+        # run constructed command
+        #TODO(1.5) p = SCons.Action._subproc(self, command, **kw)
+        p = apply(SCons.Action._subproc, (self, command), kw)
+        out,err = p.communicate()
+        status = p.wait()
         if err:
-            import sys
             sys.stderr.write(err)
         if status:
-            try:
-                if os.WIFEXITED(status):
-                    status = os.WEXITSTATUS(status)
-            except AttributeError:
-                pass
-            raise OSError("'%s' exited %s" % (command, status))
+            raise OSError("'%s' exited %d" % (command, status))
         return out
 
+    def AddMethod(self, function, name=None):
+        """
+        Adds the specified function as a method of this construction
+        environment with the specified name.  If the name is omitted,
+        the default name is the name of the function itself.
+        """
+        method = MethodWrapper(self, function, name)
+        self.added_methods.append(method)
+
+    def RemoveMethod(self, function):
+        """
+        Removes the specified function's MethodWrapper from the
+        added_methods list, so we don't re-bind it when making a clone.
+        """
+        is_not_func = lambda dm, f=function: not dm.method is f
+        self.added_methods = filter(is_not_func, self.added_methods)
+
     def Override(self, overrides):
         """
         Produce a modified environment whose variables are overriden by
@@ -489,16 +625,19 @@ class SubstitutionEnvironment:
         environment, and doesn't even create a wrapper object if there
         are no overrides.
         """
-        if overrides:
-            o = copy_non_reserved_keywords(overrides)
-            overrides = {}
-            for key, value in o.items():
+        if not overrides: return self
+        o = copy_non_reserved_keywords(overrides)
+        if not o: return self
+        overrides = {}
+        merges = None
+        for key, value in o.items():
+            if key == 'parse_flags':
+                merges = value
+            else:
                 overrides[key] = SCons.Subst.scons_subst_once(value, self, key)
-        if overrides:
-            env = OverrideEnvironment(self, overrides)
-            return env
-        else:
-            return self
+        env = OverrideEnvironment(self, overrides)
+        if merges: env.MergeFlags(merges)
+        return env
 
     def ParseFlags(self, *flags):
         """
@@ -511,17 +650,17 @@ class SubstitutionEnvironment:
         the result of that evaluation is then added to the dict.
         """
         dict = {
-            'ASFLAGS'       : [],
-            'CFLAGS'        : [],
-            'CCFLAGS'       : [],
+            'ASFLAGS'       : SCons.Util.CLVar(''),
+            'CFLAGS'        : SCons.Util.CLVar(''),
+            'CCFLAGS'       : SCons.Util.CLVar(''),
             'CPPDEFINES'    : [],
-            'CPPFLAGS'      : [],
+            'CPPFLAGS'      : SCons.Util.CLVar(''),
             'CPPPATH'       : [],
-            'FRAMEWORKPATH' : [],
-            'FRAMEWORKS'    : [],
+            'FRAMEWORKPATH' : SCons.Util.CLVar(''),
+            'FRAMEWORKS'    : SCons.Util.CLVar(''),
             'LIBPATH'       : [],
             'LIBS'          : [],
-            'LINKFLAGS'     : [],
+            'LINKFLAGS'     : SCons.Util.CLVar(''),
             'RPATH'         : [],
         }
 
@@ -568,7 +707,7 @@ class SubstitutionEnvironment:
             #  -R dir          (deprecated linker rpath)
             # IBM compilers may also accept -qframeworkdir=foo
     
-            params = string.split(arg)
+            params = shlex.split(arg)
             append_next_arg_to = None   # for multi-word args
             for arg in params:
                 if append_next_arg_to:
@@ -623,7 +762,7 @@ class SubstitutionEnvironment:
                     if arg[2:]:
                         append_define(arg[2:])
                     else:
-                        appencd_next_arg_to = 'CPPDEFINES'
+                        append_next_arg_to = 'CPPDEFINES'
                 elif arg == '-framework':
                     append_next_arg_to = 'FRAMEWORKS'
                 elif arg[:14] == '-frameworkdir=':
@@ -655,20 +794,23 @@ class SubstitutionEnvironment:
             do_parse(arg, do_parse)
         return dict
 
-    def MergeFlags(self, args, unique=1):
+    def MergeFlags(self, args, unique=1, dict=None):
         """
-        Merge the dict in args into the construction variables.  If args
-        is not a dict, it is converted into a dict using ParseFlags.
-        If unique is not set, the flags are appended rather than merged.
+        Merge the dict in args into the construction variables of this
+        env, or the passed-in dict.  If args is not a dict, it is
+        converted into a dict using ParseFlags.  If unique is not set,
+        the flags are appended rather than merged.
         """
 
+        if dict is None:
+            dict = self
         if not SCons.Util.is_Dict(args):
             args = self.ParseFlags(args)
         if not unique:
             apply(self.Append, (), args)
             return self
         for key, value in args.items():
-            if value == '':
+            if not value:
                 continue
             try:
                 orig = self[key]
@@ -676,10 +818,24 @@ class SubstitutionEnvironment:
                 orig = value
             else:
                 if not orig:
-                    orig = []
-                elif not SCons.Util.is_List(orig): 
-                    orig = [orig]
-                orig = orig + value
+                    orig = value
+                elif value:
+                    # Add orig and value.  The logic here was lifted from
+                    # part of env.Append() (see there for a lot of comments
+                    # about the order in which things are tried) and is
+                    # used mainly to handle coercion of strings to CLVar to
+                    # "do the right thing" given (e.g.) an original CCFLAGS
+                    # string variable like '-pipe -Wall'.
+                    try:
+                        orig = orig + value
+                    except (KeyError, TypeError):
+                        try:
+                            add_to_orig = orig.append
+                        except AttributeError:
+                            value.insert(0, orig)
+                            orig = value
+                        else:
+                            add_to_orig(value)
             t = []
             if key[-4:] == 'PATH':
                 ### keep left-most occurence
@@ -695,6 +851,49 @@ class SubstitutionEnvironment:
             self[key] = t
         return self
 
+#     def MergeShellPaths(self, args, prepend=1):
+#         """
+#         Merge the dict in args into the shell environment in env['ENV'].  
+#         Shell path elements are appended or prepended according to prepend.
+
+#         Uses Pre/AppendENVPath, so it always appends or prepends uniquely.
+
+#         Example: env.MergeShellPaths({'LIBPATH': '/usr/local/lib'})
+#         prepends /usr/local/lib to env['ENV']['LIBPATH'].
+#         """
+
+#         for pathname, pathval in args.items():
+#             if not pathval:
+#                 continue
+#             if prepend:
+#                 apply(self.PrependENVPath, (pathname, pathval))
+#             else:
+#                 apply(self.AppendENVPath, (pathname, pathval))
+
+
+# Used by the FindSourceFiles() method, below.
+# Stuck here for support of pre-2.2 Python versions.
+def build_source(ss, result):
+    for s in ss:
+        if isinstance(s, SCons.Node.FS.Dir):
+            build_source(s.all_children(), result)
+        elif s.has_builder():
+            build_source(s.sources, result)
+        elif isinstance(s.disambiguate(), SCons.Node.FS.File):
+            result.append(s)
+
+def default_decide_source(dependency, target, prev_ni):
+    f = SCons.Defaults.DefaultEnvironment().decide_source
+    return f(dependency, target, prev_ni)
+
+def default_decide_target(dependency, target, prev_ni):
+    f = SCons.Defaults.DefaultEnvironment().decide_target
+    return f(dependency, target, prev_ni)
+
+def default_copy_from_cache(src, dst):
+    f = SCons.Defaults.DefaultEnvironment().copy_from_cache
+    return f(src, dst)
+
 class Base(SubstitutionEnvironment):
     """Base class for "real" construction Environments.  These are the
     primary objects used to communicate dependency and construction
@@ -705,9 +904,6 @@ class Base(SubstitutionEnvironment):
     Environment.
     """
 
-    if SCons.Memoize.use_memoizer:
-        __metaclass__ = SCons.Memoize.Memoized_Metaclass
-
     memoizer_counters = []
 
     #######################################################################
@@ -728,7 +924,8 @@ class Base(SubstitutionEnvironment):
                  platform=None,
                  tools=None,
                  toolpath=None,
-                 options=None,
+                 variables=None,
+                 parse_flags = None,
                  **kw):
         """
         Initialization of a basic SCons construction environment,
@@ -742,11 +939,23 @@ class Base(SubstitutionEnvironment):
         """
         if __debug__: logInstanceCreation(self, 'Environment.Base')
         self._memo = {}
-        self.fs = SCons.Node.FS.default_fs or SCons.Node.FS.FS()
+        self.fs = SCons.Node.FS.get_default_fs()
         self.ans = SCons.Node.Alias.default_ans
         self.lookup_list = SCons.Node.arg2nodes_lookups
-        self._dict = our_deepcopy(SCons.Defaults.ConstructionEnvironment)
+        self._dict = semi_deepcopy(SCons.Defaults.ConstructionEnvironment)
         self._init_special()
+        self.added_methods = []
+
+        # We don't use AddMethod, or define these as methods in this
+        # class, because we *don't* want these functions to be bound
+        # methods.  They need to operate independently so that the
+        # settings will work properly regardless of whether a given
+        # target ends up being built with a Base environment or an
+        # OverrideEnvironment or what have you.
+        self.decide_target = default_decide_target
+        self.decide_source = default_decide_source
+
+        self.copy_from_cache = default_copy_from_cache
 
         self._dict['BUILDERS'] = BuilderDict(self._dict['BUILDERS'], self)
 
@@ -758,15 +967,28 @@ class Base(SubstitutionEnvironment):
             platform = SCons.Platform.Platform(platform)
         self._dict['PLATFORM'] = str(platform)
         platform(self)
+        
+        self._dict['HOST_OS']      = self._dict.get('HOST_OS',None)
+        self._dict['HOST_ARCH']    = self._dict.get('HOST_ARCH',None)
+        
+        # Now set defaults for TARGET_{OS|ARCH}
+        self._dict['TARGET_OS']      = self._dict.get('HOST_OS',None)
+        self._dict['TARGET_ARCH']    = self._dict.get('HOST_ARCH',None)
+        
 
-        # Apply the passed-in variables and customizable options to the
+        # Apply the passed-in and customizable variables to the
         # environment before calling the tools, because they may use
         # some of them during initialization.
+        if kw.has_key('options'):
+            # Backwards compatibility:  they may stll be using the
+            # old "options" keyword.
+            variables = kw['options']
+            del kw['options']
         apply(self.Replace, (), kw)
         keys = kw.keys()
-        if options:
-            keys = keys + options.keys()
-            options.Update(self)
+        if variables:
+            keys = keys + variables.keys()
+            variables.Update(self)
 
         save = {}
         for k in keys:
@@ -777,18 +999,23 @@ class Base(SubstitutionEnvironment):
                 # reserved variable name like TARGETS.
                 pass
 
+        SCons.Tool.Initializers(self)
+
         if tools is None:
             tools = self._dict.get('TOOLS', None)
             if tools is None:
                 tools = ['default']
         apply_tools(self, tools, toolpath)
 
-        # Now restore the passed-in variables and customized options
+        # Now restore the passed-in and customized variables
         # to the environment, since the values the user set explicitly
         # should override any values set by the tools.
         for key, val in save.items():
             self._dict[key] = val
 
+        # Finally, apply any flags to be merged in
+        if parse_flags: self.MergeFlags(parse_flags)
+
     #######################################################################
     # Utility methods that are primarily for internal use by SCons.
     # These begin with lower-case letters.
@@ -802,16 +1029,20 @@ class Base(SubstitutionEnvironment):
         except KeyError:
             return None
 
-    def get_calculator(self):
+    def get_CacheDir(self):
+        try:
+            path = self._CacheDir_path
+        except AttributeError:
+            path = SCons.Defaults.DefaultEnvironment()._CacheDir_path
         try:
-            module = self._calc_module
-            c = apply(SCons.Sig.Calculator, (module,), CalculatorArgs)
+            if path == self._last_CacheDir_path:
+                return self._last_CacheDir
         except AttributeError:
-            # Note that we're calling get_calculator() here, so the
-            # DefaultEnvironment() must have a _calc_module attribute
-            # to avoid infinite recursion.
-            c = SCons.Defaults.DefaultEnvironment().get_calculator()
-        return c
+            pass
+        cd = SCons.CacheDir.CacheDir(path)
+        self._last_CacheDir_path = path
+        self._last_CacheDir = cd
+        return cd
 
     def get_factory(self, factory, default='File'):
         """Return a factory function for creating Nodes for this
@@ -819,7 +1050,7 @@ class Base(SubstitutionEnvironment):
         """
         name = default
         try:
-            is_node = issubclass(factory, SCons.Node.Node)
+            is_node = issubclass(factory, SCons.Node.FS.Base)
         except TypeError:
             # The specified factory isn't a Node itself--it's
             # most likely None, or possibly a callable.
@@ -868,15 +1099,19 @@ class Base(SubstitutionEnvironment):
             scanners.reverse()
             for scanner in scanners:
                 for k in scanner.get_skeys(self):
+                    if k and self['PLATFORM'] == 'win32':
+                        k = string.lower(k)
                     result[k] = scanner
 
         self._memo['_gsm'] = result
 
         return result
-        
+
     def get_scanner(self, skey):
         """Find the appropriate scanner given a key (usually a file suffix).
         """
+        if skey and self['PLATFORM'] == 'win32':
+            skey = string.lower(skey)
         return self._gsm().get(skey)
 
     def scanner_map_delete(self, kw=None):
@@ -893,13 +1128,21 @@ class Base(SubstitutionEnvironment):
         """
         self._dict.update(dict)
 
-    def use_build_signature(self):
+    def get_src_sig_type(self):
+        try:
+            return self.src_sig_type
+        except AttributeError:
+            t = SCons.Defaults.DefaultEnvironment().src_sig_type
+            self.src_sig_type = t
+            return t
+
+    def get_tgt_sig_type(self):
         try:
-            return self._build_signature
+            return self.tgt_sig_type
         except AttributeError:
-            b = SCons.Defaults.DefaultEnvironment()._build_signature
-            self._build_signature = b
-            return b
+            t = SCons.Defaults.DefaultEnvironment().tgt_sig_type
+            self.tgt_sig_type = t
+            return t
 
     #######################################################################
     # Public methods for manipulating an Environment.  These begin with
@@ -968,34 +1211,56 @@ class Base(SubstitutionEnvironment):
                         try:
                             update_dict(val)
                         except (AttributeError, TypeError, ValueError):
-                            orig[val] = None
+                            if SCons.Util.is_Dict(val):
+                                for k, v in val.items():
+                                    orig[k] = v
+                            else:
+                                orig[val] = None
         self.scanner_map_delete(kw)
 
-    def AppendENVPath(self, name, newpath, envname = 'ENV', sep = os.pathsep):
+    # allow Dirs and strings beginning with # for top-relative
+    # Note this uses the current env's fs (in self).
+    def _canonicalize(self, path):
+        if not SCons.Util.is_String(path): # typically a Dir
+            path = str(path)
+        if path and path[0] == '#':
+            path = str(self.fs.Dir(path))
+        return path
+
+    def AppendENVPath(self, name, newpath, envname = 'ENV', 
+                      sep = os.pathsep, delete_existing=1):
         """Append path elements to the path 'name' in the 'ENV'
         dictionary for this environment.  Will only add any particular
         path once, and will normpath and normcase all paths to help
         assure this.  This can also handle the case where the env
         variable is a list instead of a string.
+
+        If delete_existing is 0, a newpath which is already in the path
+        will not be moved to the end (it will be left where it is).
         """
 
         orig = ''
         if self._dict.has_key(envname) and self._dict[envname].has_key(name):
             orig = self._dict[envname][name]
 
-        nv = SCons.Util.AppendPath(orig, newpath, sep)
-            
+        nv = SCons.Util.AppendPath(orig, newpath, sep, delete_existing,
+                                   canonicalize=self._canonicalize)
+
         if not self._dict.has_key(envname):
             self._dict[envname] = {}
 
         self._dict[envname][name] = nv
 
-    def AppendUnique(self, **kw):
+    def AppendUnique(self, delete_existing=0, **kw):
         """Append values to existing construction variables
         in an Environment, if they're not already there.
+        If delete_existing is 1, removes existing values first, so
+        values move to end.
         """
         kw = copy_non_reserved_keywords(kw)
         for key, val in kw.items():
+            if SCons.Util.is_List(val):
+                val = _delete_duplicates(val, delete_existing)
             if not self._dict.has_key(key) or self._dict[key] in ('', None):
                 self._dict[key] = val
             elif SCons.Util.is_Dict(self._dict[key]) and \
@@ -1005,20 +1270,29 @@ class Base(SubstitutionEnvironment):
                 dk = self._dict[key]
                 if not SCons.Util.is_List(dk):
                     dk = [dk]
-                val = filter(lambda x, dk=dk: x not in dk, val)
+                if delete_existing:
+                    dk = filter(lambda x, val=val: x not in val, dk)
+                else:
+                    val = filter(lambda x, dk=dk: x not in dk, val)
                 self._dict[key] = dk + val
             else:
                 dk = self._dict[key]
                 if SCons.Util.is_List(dk):
                     # By elimination, val is not a list.  Since dk is a
                     # list, wrap val in a list first.
-                    if not val in dk:
+                    if delete_existing:
+                        dk = filter(lambda x, val=val: x not in val, dk)
                         self._dict[key] = dk + [val]
+                    else:
+                        if not val in dk:
+                            self._dict[key] = dk + [val]
                 else:
-                    self._dict[key] = self._dict[key] + val
+                    if delete_existing:
+                        dk = filter(lambda x, val=val: x not in val, dk)
+                    self._dict[key] = dk + val
         self.scanner_map_delete(kw)
 
-    def Clone(self, tools=[], toolpath=None, **kw):
+    def Clone(self, tools=[], toolpath=None, parse_flags = None, **kw):
         """Return a copy of a construction Environment.  The
         copy is like a Python "deep copy"--that is, independent
         copies are made recursively of each objects--except that
@@ -1027,29 +1301,108 @@ class Base(SubstitutionEnvironment):
         objects in the original Environment.
         """
         clone = copy.copy(self)
-        clone._dict = our_deepcopy(self._dict)
+        clone._dict = semi_deepcopy(self._dict)
+
         try:
             cbd = clone._dict['BUILDERS']
-            clone._dict['BUILDERS'] = BuilderDict(cbd, clone)
         except KeyError:
             pass
+        else:
+            clone._dict['BUILDERS'] = BuilderDict(cbd, clone)
 
-        clone._memo = {}
+        # Check the methods added via AddMethod() and re-bind them to
+        # the cloned environment.  Only do this if the attribute hasn't
+        # been overwritten by the user explicitly and still points to
+        # the added method.
+        clone.added_methods = []
+        for mw in self.added_methods:
+            if mw == getattr(self, mw.name):
+                clone.added_methods.append(mw.clone(clone))
 
-        apply_tools(clone, tools, toolpath)
+        clone._memo = {}
 
-        # Apply passed-in variables after the new tools.
+        # Apply passed-in variables before the tools
+        # so the tools can use the new variables
         kw = copy_non_reserved_keywords(kw)
         new = {}
         for key, value in kw.items():
             new[key] = SCons.Subst.scons_subst_once(value, self, key)
         apply(clone.Replace, (), new)
+
+        apply_tools(clone, tools, toolpath)
+
+        # apply them again in case the tools overwrote them
+        apply(clone.Replace, (), new)        
+
+        # Finally, apply any flags to be merged in
+        if parse_flags: clone.MergeFlags(parse_flags)
+
         if __debug__: logInstanceCreation(self, 'Environment.EnvironmentClone')
         return clone
 
     def Copy(self, *args, **kw):
+        global _warn_copy_deprecated
+        if _warn_copy_deprecated:
+            msg = "The env.Copy() method is deprecated; use the env.Clone() method instead."
+            SCons.Warnings.warn(SCons.Warnings.DeprecatedCopyWarning, msg)
+            _warn_copy_deprecated = False
         return apply(self.Clone, args, kw)
 
+    def _changed_build(self, dependency, target, prev_ni):
+        if dependency.changed_state(target, prev_ni):
+            return 1
+        return self.decide_source(dependency, target, prev_ni)
+
+    def _changed_content(self, dependency, target, prev_ni):
+        return dependency.changed_content(target, prev_ni)
+
+    def _changed_source(self, dependency, target, prev_ni):
+        target_env = dependency.get_build_env()
+        type = target_env.get_tgt_sig_type()
+        if type == 'source':
+            return target_env.decide_source(dependency, target, prev_ni)
+        else:
+            return target_env.decide_target(dependency, target, prev_ni)
+
+    def _changed_timestamp_then_content(self, dependency, target, prev_ni):
+        return dependency.changed_timestamp_then_content(target, prev_ni)
+
+    def _changed_timestamp_newer(self, dependency, target, prev_ni):
+        return dependency.changed_timestamp_newer(target, prev_ni)
+
+    def _changed_timestamp_match(self, dependency, target, prev_ni):
+        return dependency.changed_timestamp_match(target, prev_ni)
+
+    def _copy_from_cache(self, src, dst):
+        return self.fs.copy(src, dst)
+
+    def _copy2_from_cache(self, src, dst):
+        return self.fs.copy2(src, dst)
+
+    def Decider(self, function):
+        copy_function = self._copy2_from_cache
+        if function in ('MD5', 'content'):
+            if not SCons.Util.md5:
+                raise UserError, "MD5 signatures are not available in this version of Python."
+            function = self._changed_content
+        elif function == 'MD5-timestamp':
+            function = self._changed_timestamp_then_content
+        elif function in ('timestamp-newer', 'make'):
+            function = self._changed_timestamp_newer
+            copy_function = self._copy_from_cache
+        elif function == 'timestamp-match':
+            function = self._changed_timestamp_match
+        elif not callable(function):
+            raise UserError, "Unknown Decider value %s" % repr(function)
+
+        # We don't use AddMethod because we don't want to turn the
+        # function, which only expects three arguments, into a bound
+        # method, which would add self as an initial, fourth argument.
+        self.decide_target = function
+        self.decide_source = function
+
+        self.copy_from_cache = copy_function
+
     def Detect(self, progs):
         """Return the first available program in progs.
         """
@@ -1100,7 +1453,7 @@ class Base(SubstitutionEnvironment):
 
         for path in paths:
             dir,name = os.path.split(str(path))
-            if name[:len(prefix)] == prefix and name[-len(suffix):] == suffix: 
+            if name[:len(prefix)] == prefix and name[-len(suffix):] == suffix:
                 return path
 
     def ParseConfig(self, command, function=None, unique=1):
@@ -1223,34 +1576,47 @@ class Base(SubstitutionEnvironment):
                         try:
                             update_dict(val)
                         except (AttributeError, TypeError, ValueError):
-                            orig[val] = None
+                            if SCons.Util.is_Dict(val):
+                                for k, v in val.items():
+                                    orig[k] = v
+                            else:
+                                orig[val] = None
         self.scanner_map_delete(kw)
 
-    def PrependENVPath(self, name, newpath, envname = 'ENV', sep = os.pathsep):
+    def PrependENVPath(self, name, newpath, envname = 'ENV', sep = os.pathsep,
+                       delete_existing=1):
         """Prepend path elements to the path 'name' in the 'ENV'
         dictionary for this environment.  Will only add any particular
         path once, and will normpath and normcase all paths to help
         assure this.  This can also handle the case where the env
         variable is a list instead of a string.
+
+        If delete_existing is 0, a newpath which is already in the path
+        will not be moved to the front (it will be left where it is).
         """
 
         orig = ''
         if self._dict.has_key(envname) and self._dict[envname].has_key(name):
             orig = self._dict[envname][name]
 
-        nv = SCons.Util.PrependPath(orig, newpath, sep)
-            
+        nv = SCons.Util.PrependPath(orig, newpath, sep, delete_existing,
+                                    canonicalize=self._canonicalize)
+
         if not self._dict.has_key(envname):
             self._dict[envname] = {}
 
         self._dict[envname][name] = nv
 
-    def PrependUnique(self, **kw):
-        """Append values to existing construction variables
+    def PrependUnique(self, delete_existing=0, **kw):
+        """Prepend values to existing construction variables
         in an Environment, if they're not already there.
+        If delete_existing is 1, removes existing values first, so
+        values move to front.
         """
         kw = copy_non_reserved_keywords(kw)
         for key, val in kw.items():
+            if SCons.Util.is_List(val):
+                val = _delete_duplicates(val, not delete_existing)
             if not self._dict.has_key(key) or self._dict[key] in ('', None):
                 self._dict[key] = val
             elif SCons.Util.is_Dict(self._dict[key]) and \
@@ -1260,16 +1626,25 @@ class Base(SubstitutionEnvironment):
                 dk = self._dict[key]
                 if not SCons.Util.is_List(dk):
                     dk = [dk]
-                val = filter(lambda x, dk=dk: x not in dk, val)
+                if delete_existing:
+                    dk = filter(lambda x, val=val: x not in val, dk)
+                else:
+                    val = filter(lambda x, dk=dk: x not in dk, val)
                 self._dict[key] = val + dk
             else:
                 dk = self._dict[key]
                 if SCons.Util.is_List(dk):
                     # By elimination, val is not a list.  Since dk is a
                     # list, wrap val in a list first.
-                    if not val in dk:
+                    if delete_existing:
+                        dk = filter(lambda x, val=val: x not in val, dk)
                         self._dict[key] = [val] + dk
+                    else:
+                        if not val in dk:
+                            self._dict[key] = [val] + dk
                 else:
+                    if delete_existing:
+                        dk = filter(lambda x, val=val: x not in val, dk)
                     self._dict[key] = val + dk
         self.scanner_map_delete(kw)
 
@@ -1278,13 +1653,15 @@ class Base(SubstitutionEnvironment):
         with new construction variables and/or values.
         """
         try:
-            kwbd = our_deepcopy(kw['BUILDERS'])
-            del kw['BUILDERS']
-            self.__setitem__('BUILDERS', kwbd)
+            kwbd = kw['BUILDERS']
         except KeyError:
             pass
+        else:
+            kwbd = semi_deepcopy(kwbd)
+            del kw['BUILDERS']
+            self.__setitem__('BUILDERS', kwbd)
         kw = copy_non_reserved_keywords(kw)
-        self._update(our_deepcopy(kw))
+        self._update(semi_deepcopy(kw))
         self.scanner_map_delete(kw)
 
     def ReplaceIxes(self, path, old_prefix, old_suffix, new_prefix, new_suffix):
@@ -1317,12 +1694,15 @@ class Base(SubstitutionEnvironment):
                 del kw[k]
         apply(self.Replace, (), kw)
 
+    def _find_toolpath_dir(self, tp):
+        return self.fs.Dir(self.subst(tp)).srcnode().abspath
+
     def Tool(self, tool, toolpath=None, **kw):
         if SCons.Util.is_String(tool):
             tool = self.subst(tool)
             if toolpath is None:
                 toolpath = self.get('toolpath', [])
-            toolpath = map(self.subst, toolpath)
+            toolpath = map(self._find_toolpath_dir, toolpath)
             tool = apply(SCons.Tool.Tool, (tool, toolpath), kw)
         tool(self)
 
@@ -1343,6 +1723,7 @@ class Base(SubstitutionEnvironment):
                 pass
         elif SCons.Util.is_String(pathext):
             pathext = self.subst(pathext)
+        prog = self.subst(prog)
         path = SCons.Util.WhereIs(prog, path, pathext, reject)
         if path: return path
         return None
@@ -1440,22 +1821,26 @@ class Base(SubstitutionEnvironment):
     def AlwaysBuild(self, *targets):
         tlist = []
         for t in targets:
-            tlist.extend(self.arg2nodes(t, self.fs.File))
+            tlist.extend(self.arg2nodes(t, self.fs.Entry))
         for t in tlist:
             t.set_always_build()
         return tlist
 
-    def BuildDir(self, build_dir, src_dir, duplicate=1):
-        build_dir = self.arg2nodes(build_dir, self.fs.Dir)[0]
-        src_dir = self.arg2nodes(src_dir, self.fs.Dir)[0]
-        self.fs.BuildDir(build_dir, src_dir, duplicate)
+    def BuildDir(self, *args, **kw):
+        if kw.has_key('build_dir'):
+            kw['variant_dir'] = kw['build_dir']
+            del kw['build_dir']
+        return apply(self.VariantDir, args, kw)
 
     def Builder(self, **kw):
         nkw = self.subst_kw(kw)
         return apply(SCons.Builder.Builder, [], nkw)
 
     def CacheDir(self, path):
-        self.fs.CacheDir(self.subst(path))
+        import SCons.CacheDir
+        if path is not None:
+            path = self.subst(path)
+        self._CacheDir_path = path
 
     def Clean(self, targets, files):
         global CleanTargets
@@ -1506,7 +1891,13 @@ class Base(SubstitutionEnvironment):
     def Dir(self, name, *args, **kw):
         """
         """
-        return apply(self.fs.Dir, (self.subst(name),) + args, kw)
+        s = self.subst(name)
+        if SCons.Util.is_Sequence(s):
+            result=[]
+            for e in s:
+                result.append(apply(self.fs.Dir, (e,) + args, kw))
+            return result
+        return apply(self.fs.Dir, (s,) + args, kw)
 
     def NoClean(self, *targets):
         """Tags a target so that it will not be cleaned by -c"""
@@ -1517,10 +1908,25 @@ class Base(SubstitutionEnvironment):
             t.set_noclean()
         return tlist
 
+    def NoCache(self, *targets):
+        """Tags a target so that it will not be cached"""
+        tlist = []
+        for t in targets:
+            tlist.extend(self.arg2nodes(t, self.fs.Entry))
+        for t in tlist:
+            t.set_nocache()
+        return tlist
+
     def Entry(self, name, *args, **kw):
         """
         """
-        return apply(self.fs.Entry, (self.subst(name),) + args, kw)
+        s = self.subst(name)
+        if SCons.Util.is_Sequence(s):
+            result=[]
+            for e in s:
+                result.append(apply(self.fs.Entry, (e,) + args, kw))
+            return result
+        return apply(self.fs.Entry, (s,) + args, kw)
 
     def Environment(self, **kw):
         return apply(SCons.Environment.Environment, [], self.subst_kw(kw))
@@ -1529,12 +1935,26 @@ class Base(SubstitutionEnvironment):
         """Directly execute an action through an Environment
         """
         action = apply(self.Action, (action,) + args, kw)
-        return action([], [], self)
+        result = action([], [], self)
+        if isinstance(result, SCons.Errors.BuildError):
+            errstr = result.errstr
+            if result.filename:
+                errstr = result.filename + ': ' + errstr
+            sys.stderr.write("scons: *** %s\n" % errstr)
+            return result.status
+        else:
+            return result
 
     def File(self, name, *args, **kw):
         """
         """
-        return apply(self.fs.File, (self.subst(name),) + args, kw)
+        s = self.subst(name)
+        if SCons.Util.is_Sequence(s):
+            result=[]
+            for e in s:
+                result.append(apply(self.fs.File, (e,) + args, kw))
+            return result
+        return apply(self.fs.File, (s,) + args, kw)
 
     def FindFile(self, file, dirs):
         file = self.subst(file)
@@ -1551,6 +1971,9 @@ class Base(SubstitutionEnvironment):
         else:
             return result[0]
 
+    def Glob(self, pattern, ondisk=True, source=False, strings=False):
+        return self.fs.Glob(self.subst(pattern), ondisk, source, strings)
+
     def Ignore(self, target, dependency):
         """Ignore a dependency."""
         tlist = self.arg2nodes(target, self.fs.Entry)
@@ -1559,47 +1982,6 @@ class Base(SubstitutionEnvironment):
             t.add_ignore(dlist)
         return tlist
 
-    def Install(self, dir, source):
-        """Install specified files in the given directory."""
-        try:
-            dnodes = self.arg2nodes(dir, self.fs.Dir)
-        except TypeError:
-            fmt = "Target `%s' of Install() is a file, but should be a directory.  Perhaps you have the Install() arguments backwards?"
-            raise SCons.Errors.UserError, fmt % str(dir)
-        try:
-            sources = self.arg2nodes(source, self.fs.Entry)
-        except TypeError:
-            if SCons.Util.is_List(source):
-                s = repr(map(str, source))
-            else:
-                s = str(source)
-            fmt = "Source `%s' of Install() is neither a file nor a directory.  Install() source must be one or more files or directories"
-            raise SCons.Errors.UserError, fmt % s
-        tgt = []
-        for dnode in dnodes:
-            for src in sources:
-                target = self.fs.Entry(src.name, dnode)
-                tgt.extend(InstallBuilder(self, target, src))
-        return tgt
-
-    def InstallAs(self, target, source):
-        """Install sources as targets."""
-        sources = self.arg2nodes(source, self.fs.Entry)
-        targets = self.arg2nodes(target, self.fs.Entry)
-        if len(sources) != len(targets):
-            if not SCons.Util.is_List(target):
-                target = [target]
-            if not SCons.Util.is_List(source):
-                source = [source]
-            t = repr(map(str, target))
-            s = repr(map(str, source))
-            fmt = "Target (%s) and source (%s) lists of InstallAs() must be the same length."
-            raise SCons.Errors.UserError, fmt % (t, s)
-        result = []
-        for src, tgt in map(lambda x, y: (x, y), sources, targets):
-            result.extend(InstallBuilder(self, tgt, src))
-        return result
-
     def Literal(self, string):
         return SCons.Subst.Literal(string)
 
@@ -1627,6 +2009,16 @@ class Base(SubstitutionEnvironment):
         dirs = self.arg2nodes(list(dirs), self.fs.Dir)
         apply(self.fs.Repository, dirs, kw)
 
+    def Requires(self, target, prerequisite):
+        """Specify that 'prerequisite' must be built before 'target',
+        (but 'target' does not actually depend on 'prerequisite'
+        and need not be rebuilt if it changes)."""
+        tlist = self.arg2nodes(target, self.fs.Entry)
+        plist = self.arg2nodes(prerequisite, self.fs.Entry)
+        for t in tlist:
+            t.add_prerequisite(plist)
+        return tlist
+
     def Scanner(self, *args, **kw):
         nargs = []
         for arg in args:
@@ -1634,17 +2026,22 @@ class Base(SubstitutionEnvironment):
                 arg = self.subst(arg)
             nargs.append(arg)
         nkw = self.subst_kw(kw)
-        return apply(SCons.Scanner.Scanner, nargs, nkw)
+        return apply(SCons.Scanner.Base, nargs, nkw)
 
     def SConsignFile(self, name=".sconsign", dbm_module=None):
-        if not name is None:
+        if name is not None:
             name = self.subst(name)
             if not os.path.isabs(name):
                 name = os.path.join(str(self.fs.SConstruct_dir), name)
+        if name:
+            name = os.path.normpath(name)
+            sconsign_dir = os.path.dirname(name)
+            if sconsign_dir and not os.path.exists(sconsign_dir):
+                self.Execute(SCons.Defaults.Mkdir(sconsign_dir))
         SCons.SConsign.File(name, dbm_module)
 
     def SideEffect(self, side_effect, target):
-        """Tell scons that side_effects are built as side 
+        """Tell scons that side_effects are built as side
         effects of building targets."""
         side_effects = self.arg2nodes(side_effect, self.fs.Entry)
         targets = self.arg2nodes(target, self.fs.Entry)
@@ -1667,22 +2064,22 @@ class Base(SubstitutionEnvironment):
         return entries
 
     def SourceSignatures(self, type):
+        global _warn_source_signatures_deprecated
+        if _warn_source_signatures_deprecated:
+            msg = "The env.SourceSignatures() method is deprecated;\n" + \
+                  "\tconvert your build to use the env.Decider() method instead."
+            SCons.Warnings.warn(SCons.Warnings.DeprecatedSourceSignaturesWarning, msg)
+            _warn_source_signatures_deprecated = False
         type = self.subst(type)
+        self.src_sig_type = type
         if type == 'MD5':
-            try:
-                import SCons.Sig.MD5
-            except ImportError:
-                msg = "No MD5 module available, using time stamps"
-                SCons.Warnings.warn(SCons.Warnings.NoMD5ModuleWarning, msg)
-                import SCons.Sig.TimeStamp
-                self._calc_module = SCons.Sig.TimeStamp
-            else:
-                self._calc_module = SCons.Sig.MD5
+            if not SCons.Util.md5:
+                raise UserError, "MD5 signatures are not available in this version of Python."
+            self.decide_source = self._changed_content
         elif type == 'timestamp':
-            import SCons.Sig.TimeStamp
-            self._calc_module = SCons.Sig.TimeStamp
+            self.decide_source = self._changed_timestamp_match
         else:
-            raise UserError, "Unknown source signature type '%s'"%type
+            raise UserError, "Unknown source signature type '%s'" % type
 
     def Split(self, arg):
         """This function converts a string or list into a list of strings
@@ -1703,19 +2100,74 @@ class Base(SubstitutionEnvironment):
             return [self.subst(arg)]
 
     def TargetSignatures(self, type):
+        global _warn_target_signatures_deprecated
+        if _warn_target_signatures_deprecated:
+            msg = "The env.TargetSignatures() method is deprecated;\n" + \
+                  "\tconvert your build to use the env.Decider() method instead."
+            SCons.Warnings.warn(SCons.Warnings.DeprecatedTargetSignaturesWarning, msg)
+            _warn_target_signatures_deprecated = False
         type = self.subst(type)
-        if type == 'build':
-            self._build_signature = 1
-        elif type == 'content':
-            self._build_signature = 0
+        self.tgt_sig_type = type
+        if type in ('MD5', 'content'):
+            if not SCons.Util.md5:
+                raise UserError, "MD5 signatures are not available in this version of Python."
+            self.decide_target = self._changed_content
+        elif type == 'timestamp':
+            self.decide_target = self._changed_timestamp_match
+        elif type == 'build':
+            self.decide_target = self._changed_build
+        elif type == 'source':
+            self.decide_target = self._changed_source
         else:
-            raise SCons.Errors.UserError, "Unknown target signature type '%s'"%type
+            raise UserError, "Unknown target signature type '%s'"%type
 
     def Value(self, value, built_value=None):
         """
         """
         return SCons.Node.Python.Value(value, built_value)
 
+    def VariantDir(self, variant_dir, src_dir, duplicate=1):
+        variant_dir = self.arg2nodes(variant_dir, self.fs.Dir)[0]
+        src_dir = self.arg2nodes(src_dir, self.fs.Dir)[0]
+        self.fs.VariantDir(variant_dir, src_dir, duplicate)
+
+    def FindSourceFiles(self, node='.'):
+        """ returns a list of all source files.
+        """
+        node = self.arg2nodes(node, self.fs.Entry)[0]
+
+        sources = []
+        # Uncomment this and get rid of the global definition when we
+        # drop support for pre-2.2 Python versions.
+        #def build_source(ss, result):
+        #    for s in ss:
+        #        if isinstance(s, SCons.Node.FS.Dir):
+        #            build_source(s.all_children(), result)
+        #        elif s.has_builder():
+        #            build_source(s.sources, result)
+        #        elif isinstance(s.disambiguate(), SCons.Node.FS.File):
+        #            result.append(s)
+        build_source(node.all_children(), sources)
+
+    # THIS CODE APPEARS TO HAVE NO EFFECT
+    #    # get the final srcnode for all nodes, this means stripping any
+    #    # attached build node by calling the srcnode function
+    #    for file in sources:
+    #        srcnode = file.srcnode()
+    #        while srcnode != file.srcnode():
+    #            srcnode = file.srcnode()
+
+        # remove duplicates
+        return list(set(sources))
+
+    def FindInstalledFiles(self):
+        """ returns the list of all targets of the Install and InstallAs Builder.
+        """
+        from SCons.Tool import install
+        if install._UNIQUE_INSTALLED_FILES is None:
+            install._UNIQUE_INSTALLED_FILES = SCons.Util.uniquer_hashables(install._INSTALLED_FILES)
+        return install._UNIQUE_INSTALLED_FILES
+
 class OverrideEnvironment(Base):
     """A proxy that overrides variables in a wrapped construction
     environment by returning values from an overrides dictionary in
@@ -1737,9 +2189,6 @@ class OverrideEnvironment(Base):
     values from the overrides dictionary.
     """
 
-    if SCons.Memoize.use_memoizer:
-        __metaclass__ = SCons.Memoize.Memoized_Metaclass
-
     def __init__(self, subject, overrides={}):
         if __debug__: logInstanceCreation(self, 'Environment.OverrideEnvironment')
         self.__dict__['__subject'] = subject
@@ -1749,7 +2198,7 @@ class OverrideEnvironment(Base):
     def __getattr__(self, name):
         return getattr(self.__dict__['__subject'], name)
     def __setattr__(self, name, value):
-        return setattr(self.__dict__['__subject'], name, value)
+        setattr(self.__dict__['__subject'], name, value)
 
     # Methods that make this class act like a dictionary.
     def __getitem__(self, key):
@@ -1758,7 +2207,7 @@ class OverrideEnvironment(Base):
         except KeyError:
             return self.__dict__['__subject'].__getitem__(key)
     def __setitem__(self, key, value):
-        if not SCons.Util.is_valid_construction_var(key):
+        if not is_valid_construction_var(key):
             raise SCons.Errors.UserError, "Illegal construction variable `%s'" % key
         self.__dict__['overrides'][key] = value
     def __delitem__(self, key):
@@ -1787,6 +2236,10 @@ class OverrideEnvironment(Base):
             return 1
         except KeyError:
             return self.__dict__['__subject'].has_key(key)
+    def __contains__(self, key):
+        if self.__dict__['overrides'].__contains__(key):
+            return 1
+        return self.__dict__['__subject'].__contains__(key)
     def Dictionary(self):
         """Emulates the items() method of dictionaries."""
         d = self.__dict__['__subject'].Dictionary().copy()
@@ -1814,7 +2267,7 @@ class OverrideEnvironment(Base):
     # Overridden public construction environment methods.
     def Replace(self, **kw):
         kw = copy_non_reserved_keywords(kw)
-        self.__dict__['overrides'].update(our_deepcopy(kw))
+        self.__dict__['overrides'].update(semi_deepcopy(kw))
 
 # The entry point that will be used by the external world
 # to refer to a construction environment.  This allows the wrapper
@@ -1869,3 +2322,9 @@ def NoSubstitutionProxy(subject):
             self.raw_to_mode(nkw)
             return apply(SCons.Subst.scons_subst, nargs, nkw)
     return _NoSubstitutionProxy(subject)
+
+# Local Variables:
+# tab-width:4
+# indent-tabs-mode:nil
+# End:
+# vim: set expandtab tabstop=4 shiftwidth=4: