http://scons.tigris.org/issues/show_bug.cgi?id=2345
[scons.git] / src / engine / SCons / Node / FS.py
index 7d4d8dfee5bcc0a723d53bda626ce2322698d444..6dd5b0b78db163b4b6025e9e21ea442ca429ba27 100644 (file)
@@ -32,27 +32,103 @@ that can be used by scripts or modules looking for the canonical default.
 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 #
+from __future__ import generators  ### KEEP FOR COMPATIBILITY FIXERS
 
 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
 
+from itertools import izip
+import fnmatch
 import os
 import os.path
+import re
 import shutil
 import stat
-import string
 import sys
 import time
-import cStringIO
+
+try:
+    import codecs
+except ImportError:
+    pass
+else:
+    # TODO(2.2):  Remove when 2.3 becomes the minimal supported version.
+    try:
+        codecs.BOM_UTF8
+    except AttributeError:
+        codecs.BOM_UTF8 = '\xef\xbb\xbf'
+    try:
+        codecs.BOM_UTF16_LE
+        codecs.BOM_UTF16_BE
+    except AttributeError:
+        codecs.BOM_UTF16_LE = '\xff\xfe'
+        codecs.BOM_UTF16_BE = '\xfe\xff'
+
+    # Provide a wrapper function to handle decoding differences in
+    # different versions of Python.  Normally, we'd try to do this in the
+    # compat layer (and maybe it still makes sense to move there?) but
+    # that doesn't provide a way to supply the string class used in
+    # pre-2.3 Python versions with a .decode() method that all strings
+    # naturally have.  Plus, the 2.[01] encodings behave differently
+    # enough that we have to settle for a lowest-common-denominator
+    # wrapper approach.
+    #
+    # Note that the 2.[012] implementations below may be inefficient
+    # because they perform an explicit look up of the encoding for every
+    # decode, but they're old enough (and we want to stop supporting
+    # them soon enough) that it's not worth complicating the interface.
+    # Think of it as additional incentive for people to upgrade...
+    try:
+        ''.decode
+    except AttributeError:
+        # 2.0 through 2.2:  strings have no .decode() method
+        try:
+            codecs.lookup('ascii').decode
+        except AttributeError:
+            # 2.0 and 2.1:  encodings are a tuple of functions, and the
+            # decode() function returns a (result, length) tuple.
+            def my_decode(contents, encoding):
+                return codecs.lookup(encoding)[1](contents)[0]
+        else:
+            # 2.2:  encodings are an object with methods, and the
+            # .decode() method returns just the decoded bytes.
+            def my_decode(contents, encoding):
+                return codecs.lookup(encoding).decode(contents)
+    else:
+        # 2.3 or later:  use the .decode() string method
+        def my_decode(contents, encoding):
+            return contents.decode(encoding)
 
 import SCons.Action
 from SCons.Debug import logInstanceCreation
 import SCons.Errors
+import SCons.Memoize
 import SCons.Node
-import SCons.Sig.MD5
+import SCons.Node.Alias
 import SCons.Subst
 import SCons.Util
 import SCons.Warnings
 
+from SCons.Debug import Trace
+
+do_store_info = True
+
+
+class EntryProxyAttributeError(AttributeError):
+    """
+    An AttributeError subclass for recording and displaying the name
+    of the underlying Entry involved in an AttributeError exception.
+    """
+    def __init__(self, entry_proxy, attribute):
+        AttributeError.__init__(self)
+        self.entry_proxy = entry_proxy
+        self.attribute = attribute
+    def __str__(self):
+        entry = self.entry_proxy.get()
+        fmt = "%s instance %s has no attribute %s"
+        return fmt % (entry.__class__.__name__,
+                      repr(entry.name),
+                      repr(self.attribute))
+
 # The max_drift value:  by default, use a cached signature value for
 # any file that's been untouched for more than two days.
 default_max_drift = 2*24*60*60
@@ -68,10 +144,10 @@ default_max_drift = 2*24*60*60
 #
 # A number of the above factors, however, can be set after we've already
 # been asked to return a string for a Node, because a Repository() or
-# BuildDir() call or the like may not occur until later in SConscript
+# VariantDir() call or the like may not occur until later in SConscript
 # files.  So this variable controls whether we bother trying to save
 # string values for Nodes.  The wrapper interface can set this whenever
-# they're done mucking with Repository and BuildDir and the other stuff,
+# they're done mucking with Repository and VariantDir and the other stuff,
 # to let this module know it can start returning saved string values
 # for Nodes.
 #
@@ -81,6 +157,42 @@ def save_strings(val):
     global Save_Strings
     Save_Strings = val
 
+#
+# Avoid unnecessary function calls by recording a Boolean value that
+# tells us whether or not os.path.splitdrive() actually does anything
+# on this system, and therefore whether we need to bother calling it
+# when looking up path names in various methods below.
+# 
+
+do_splitdrive = None
+
+def initialize_do_splitdrive():
+    global do_splitdrive
+    drive, path = os.path.splitdrive('X:/foo')
+    do_splitdrive = not not drive
+
+initialize_do_splitdrive()
+
+#
+
+needs_normpath_check = None
+
+def initialize_normpath_check():
+    """
+    Initialize the normpath_check regular expression.
+
+    This function is used by the unit tests to re-initialize the pattern
+    when testing for behavior with different values of os.sep.
+    """
+    global needs_normpath_check
+    if os.sep == '/':
+        pattern = r'.*/|\.$|\.\.$'
+    else:
+        pattern = r'.*[/%s]|\.$|\.\.$' % re.escape(os.sep)
+    needs_normpath_check = re.compile(pattern)
+
+initialize_normpath_check()
+
 #
 # SCons.Action objects for interacting with the outside world.
 #
@@ -95,10 +207,33 @@ def save_strings(val):
 # there should be *no* changes to the external file system(s)...
 #
 
-def _copy_func(src, dest):
+if hasattr(os, 'link'):
+    def _hardlink_func(fs, src, dst):
+        # If the source is a symlink, we can't just hard-link to it
+        # because a relative symlink may point somewhere completely
+        # different.  We must disambiguate the symlink and then
+        # hard-link the final destination file.
+        while fs.islink(src):
+            link = fs.readlink(src)
+            if not os.path.isabs(link):
+                src = link
+            else:
+                src = os.path.join(os.path.dirname(src), link)
+        fs.link(src, dst)
+else:
+    _hardlink_func = None
+
+if hasattr(os, 'symlink'):
+    def _softlink_func(fs, src, dst):
+        fs.symlink(src, dst)
+else:
+    _softlink_func = None
+
+def _copy_func(fs, src, dest):
     shutil.copy2(src, dest)
-    st=os.stat(src)
-    os.chmod(dest, stat.S_IMODE(st[stat.ST_MODE]) | stat.S_IWRITE)
+    st = fs.stat(src)
+    fs.chmod(dest, stat.S_IMODE(st[stat.ST_MODE]) | stat.S_IWRITE)
+
 
 Valid_Duplicates = ['hard-soft-copy', 'soft-hard-copy',
                     'hard-copy', 'soft-copy', 'copy']
@@ -113,16 +248,6 @@ def set_duplicate(duplicate):
     # underlying implementations.  We do this inside this function,
     # not in the top-level module code, so that we can remap os.link
     # and os.symlink for testing purposes.
-    try:
-        _hardlink_func = os.link
-    except AttributeError:
-        _hardlink_func = None
-
-    try:
-        _softlink_func = os.symlink
-    except AttributeError:
-        _softlink_func = None
-
     link_dict = {
         'hard' : _hardlink_func,
         'soft' : _softlink_func,
@@ -130,11 +255,11 @@ def set_duplicate(duplicate):
     }
 
     if not duplicate in Valid_Duplicates:
-        raise SCons.Errors.InternalError("The argument of set_duplicate "
+        raise SCons.Errors.InternalError("The argument of set_duplicate "
                                            "should be in Valid_Duplicates")
     global Link_Funcs
     Link_Funcs = []
-    for func in string.split(duplicate,'-'):
+    for func in duplicate.split('-'):
         if link_dict[func]:
             Link_Funcs.append(link_dict[func])
 
@@ -152,10 +277,11 @@ def LinkFunc(target, source, env):
     if not Link_Funcs:
         # Set a default order of link functions.
         set_duplicate('hard-soft-copy')
+    fs = source[0].fs
     # Now link the files with the previously specified order.
     for func in Link_Funcs:
         try:
-            func(src,dest)
+            func(fs, src, dest)
             break
         except (IOError, OSError):
             # An OSError indicates something happened like a permissions
@@ -167,8 +293,6 @@ def LinkFunc(target, source, env):
             if func == Link_Funcs[-1]:
                 # exception of the last link method (copy) are fatal
                 raise
-            else:
-                pass
     return 0
 
 Link = SCons.Action.Action(LinkFunc, None)
@@ -198,68 +322,17 @@ def get_MkdirBuilder():
     global MkdirBuilder
     if MkdirBuilder is None:
         import SCons.Builder
+        import SCons.Defaults
         # "env" will get filled in by Executor.get_build_env()
         # calling SCons.Defaults.DefaultEnvironment() when necessary.
         MkdirBuilder = SCons.Builder.Builder(action = Mkdir,
                                              env = None,
                                              explain = None,
                                              is_explicit = None,
+                                             target_scanner = SCons.Defaults.DirEntryScanner,
                                              name = "MkdirBuilder")
     return MkdirBuilder
 
-def CacheRetrieveFunc(target, source, env):
-    t = target[0]
-    fs = t.fs
-    cachedir, cachefile = t.cachepath()
-    if fs.exists(cachefile):
-        if SCons.Action.execute_actions:
-            fs.copy2(cachefile, t.path)
-            st = fs.stat(cachefile)
-            fs.chmod(t.path, stat.S_IMODE(st[stat.ST_MODE]) | stat.S_IWRITE)
-        return 0
-    return 1
-
-def CacheRetrieveString(target, source, env):
-    t = target[0]
-    cachedir, cachefile = t.cachepath()
-    if t.fs.exists(cachefile):
-        return "Retrieved `%s' from cache" % t.path
-    return None
-
-CacheRetrieve = SCons.Action.Action(CacheRetrieveFunc, CacheRetrieveString)
-
-CacheRetrieveSilent = SCons.Action.Action(CacheRetrieveFunc, None)
-
-def CachePushFunc(target, source, env):
-    t = target[0]
-    fs = t.fs
-    cachedir, cachefile = t.cachepath()
-    if fs.exists(cachefile):
-        # Don't bother copying it if it's already there.
-        return
-
-    if not fs.isdir(cachedir):
-        fs.makedirs(cachedir)
-
-    tempfile = cachefile+'.tmp'
-    try:
-        fs.copy2(t.path, tempfile)
-        fs.rename(tempfile, cachefile)
-        st = fs.stat(t.path)
-        fs.chmod(cachefile, stat.S_IMODE(st[stat.ST_MODE]) | stat.S_IWRITE)
-    except (IOError, OSError):
-        # It's possible someone else tried writing the file at the
-        # same time we did, or else that there was some problem like
-        # the CacheDir being on a separate file system that's full.
-        # In any case, inability to push a file to cache doesn't affect
-        # the correctness of the build, so just print a warning.
-        SCons.Warnings.warn(SCons.Warnings.CacheWriteErrorWarning,
-                            "Unable to copy %s to cache. Cache file is %s"
-                                % (str(target), cachefile))
-        return
-
-CachePush = SCons.Action.Action(CachePushFunc, None)
-
 class _Null:
     pass
 
@@ -299,7 +372,7 @@ if os.path.normcase("TeSt") == os.path.normpath("TeSt") and not _is_cygwin:
         return x
 else:
     def _my_normcase(x):
-        return string.upper(x)
+        return x.upper()
 
 
 
@@ -320,9 +393,20 @@ class DiskChecker:
             self.set_ignore()
 
 def do_diskcheck_match(node, predicate, errorfmt):
-    path = node.abspath
-    if predicate(path):
-        raise TypeError, errorfmt % path
+    result = predicate()
+    try:
+        # If calling the predicate() cached a None value from stat(),
+        # remove it so it doesn't interfere with later attempts to
+        # build this Node as we walk the DAG.  (This isn't a great way
+        # to do this, we're reaching into an interface that doesn't
+        # really belong to us, but it's all about performance, so
+        # for now we'll just document the dependency...)
+        if node._memo['stat'] is None:
+            del node._memo['stat']
+    except (AttributeError, KeyError):
+        pass
+    if result:
+        raise TypeError(errorfmt % node.abspath)
 
 def ignore_diskcheck_match(node, predicate, errorfmt):
     pass
@@ -331,8 +415,14 @@ def do_diskcheck_rcs(node, name):
     try:
         rcs_dir = node.rcs_dir
     except AttributeError:
-        rcs_dir = node.rcs_dir = node.Dir('RCS')
-    return rcs_dir.entry_exists_on_disk(name+',v')
+        if node.entry_exists_on_disk('RCS'):
+            rcs_dir = node.Dir('RCS')
+        else:
+            rcs_dir = None
+        node.rcs_dir = rcs_dir
+    if rcs_dir:
+        return rcs_dir.entry_exists_on_disk(name+',v')
+    return None
 
 def ignore_diskcheck_rcs(node, name):
     return None
@@ -341,8 +431,14 @@ def do_diskcheck_sccs(node, name):
     try:
         sccs_dir = node.sccs_dir
     except AttributeError:
-        sccs_dir = node.sccs_dir = node.Dir('SCCS')
-    return sccs_dir.entry_exists_on_disk('s.'+name)
+        if node.entry_exists_on_disk('SCCS'):
+            sccs_dir = node.Dir('SCCS')
+        else:
+            sccs_dir = None
+        node.sccs_dir = sccs_dir
+    if sccs_dir:
+        return sccs_dir.entry_exists_on_disk('s.'+name)
+    return None
 
 def ignore_diskcheck_sccs(node, name):
     return None
@@ -362,7 +458,7 @@ def set_diskcheck(list):
         dc.set(list)
 
 def diskcheck_types():
-    return map(lambda dc: dc.type, diskcheckers)
+    return [dc.type for dc in diskcheckers]
 
 
 
@@ -400,25 +496,25 @@ class EntryProxy(SCons.Util.Proxy):
             return self
         else:
             entry = self.get()
-            r = string.replace(entry.get_path(), os.sep, '/')
+            r = entry.get_path().replace(os.sep, '/')
             return SCons.Subst.SpecialAttrWrapper(r, entry.name + "_posix")
 
-    def __get_win32_path(self):
+    def __get_windows_path(self):
         """Return the path with \ as the path separator,
         regardless of platform."""
         if os.sep == '\\':
             return self
         else:
             entry = self.get()
-            r = string.replace(entry.get_path(), os.sep, '\\')
-            return SCons.Subst.SpecialAttrWrapper(r, entry.name + "_win32")
+            r = entry.get_path().replace(os.sep, '\\')
+            return SCons.Subst.SpecialAttrWrapper(r, entry.name + "_windows")
 
     def __get_srcnode(self):
         return EntryProxy(self.get().srcnode())
 
     def __get_srcdir(self):
         """Returns the directory containing the source node linked to this
-        node via BuildDir(), or the directory of this node if not linked."""
+        node via VariantDir(), or the directory of this node if not linked."""
         return EntryProxy(self.get().srcnode().dir)
 
     def __get_rsrcnode(self):
@@ -426,15 +522,16 @@ class EntryProxy(SCons.Util.Proxy):
 
     def __get_rsrcdir(self):
         """Returns the directory containing the source node linked to this
-        node via BuildDir(), or the directory of this node if not linked."""
+        node via VariantDir(), or the directory of this node if not linked."""
         return EntryProxy(self.get().srcnode().rfile().dir)
 
     def __get_dir(self):
         return EntryProxy(self.get().dir)
-    
+
     dictSpecialAttrs = { "base"     : __get_base_path,
                          "posix"    : __get_posix_path,
-                         "win32"    : __get_win32_path,
+                         "windows"  : __get_windows_path,
+                         "win32"    : __get_windows_path,
                          "srcpath"  : __get_srcnode,
                          "srcdir"   : __get_srcdir,
                          "dir"      : __get_dir,
@@ -450,21 +547,18 @@ class EntryProxy(SCons.Util.Proxy):
         # This is how we implement the "special" attributes
         # such as base, posix, srcdir, etc.
         try:
-            return self.dictSpecialAttrs[name](self)
+            attr_function = self.dictSpecialAttrs[name]
         except KeyError:
             try:
                 attr = SCons.Util.Proxy.__getattr__(self, name)
-            except AttributeError:
-                entry = self.get()
-                classname = string.split(str(entry.__class__), '.')[-1]
-                if classname[-2:] == "'>":
-                    # new-style classes report their name as:
-                    #   "<class 'something'>"
-                    # instead of the classic classes:
-                    #   "something"
-                    classname = classname[:-2]
-                raise AttributeError, "%s instance '%s' has no attribute '%s'" % (classname, entry.name, name)
+            except AttributeError, e:
+                # Raise our own AttributeError subclass with an
+                # overridden __str__() method that identifies the
+                # name of the entry that caused the exception.
+                raise EntryProxyAttributeError(self, name)
             return attr
+        else:
+            return attr_function(self)
 
 class Base(SCons.Node.Node):
     """A generic class for file system entries.  This class is for
@@ -479,9 +573,11 @@ class Base(SCons.Node.Node):
     object identity comparisons.
     """
 
+    memoizer_counters = []
+
     def __init__(self, name, directory, fs):
         """Initialize a generic Node.FS.Base object.
-        
+
         Call the superclass initialization, take care of setting up
         our relative and absolute paths, identify our parent
         directory, and indicate that this node should use
@@ -489,40 +585,48 @@ class Base(SCons.Node.Node):
         if __debug__: logInstanceCreation(self, 'Node.FS.Base')
         SCons.Node.Node.__init__(self)
 
-        self.name = name
+        # Filenames and paths are probably reused and are intern'ed to
+        # save some memory.
+        self.name = SCons.Util.silent_intern(name)
+        self.suffix = SCons.Util.silent_intern(SCons.Util.splitext(name)[1])
         self.fs = fs
 
         assert directory, "A directory must be provided"
 
-        self.abspath = directory.entry_abspath(name)
+        self.abspath = SCons.Util.silent_intern(directory.entry_abspath(name))
+        self.labspath = SCons.Util.silent_intern(directory.entry_labspath(name))
         if directory.path == '.':
-            self.path = name
+            self.path = SCons.Util.silent_intern(name)
         else:
-            self.path = directory.entry_path(name)
+            self.path = SCons.Util.silent_intern(directory.entry_path(name))
         if directory.tpath == '.':
-            self.tpath = name
+            self.tpath = SCons.Util.silent_intern(name)
         else:
-            self.tpath = directory.entry_tpath(name)
+            self.tpath = SCons.Util.silent_intern(directory.entry_tpath(name))
         self.path_elements = directory.path_elements + [self]
 
         self.dir = directory
         self.cwd = None # will hold the SConscript directory for target nodes
         self.duplicate = directory.duplicate
 
-    def clear(self):
-        """Completely clear a Node.FS.Base object of all its cached
-        state (so that it can be re-evaluated by interfaces that do
-        continuous integration builds).
-        __cache_reset__
+    def str_for_display(self):
+        return '"' + self.__str__() + '"'
+
+    def must_be_same(self, klass):
+        """
+        This node, which already existed, is being looked up as the
+        specified klass.  Raise an exception if it isn't.
         """
-        SCons.Node.Node.clear(self)
+        if isinstance(self, klass) or klass is Entry:
+            return
+        raise TypeError("Tried to lookup %s '%s' as a %s." %\
+              (self.__class__.__name__, self.path, klass.__name__))
 
     def get_dir(self):
         return self.dir
 
     def get_suffix(self):
-        "__cacheable__"
-        return SCons.Util.splitext(self.name)[1]
+        return self.suffix
 
     def rfile(self):
         return self
@@ -535,28 +639,58 @@ class Base(SCons.Node.Node):
             return self._save_str()
         return self._get_str()
 
+    memoizer_counters.append(SCons.Memoize.CountValue('_save_str'))
+
     def _save_str(self):
-        "__cacheable__"
-        return self._get_str()
+        try:
+            return self._memo['_save_str']
+        except KeyError:
+            pass
+        result = sys.intern(self._get_str())
+        self._memo['_save_str'] = result
+        return result
 
     def _get_str(self):
+        global Save_Strings
         if self.duplicate or self.is_derived():
             return self.get_path()
-        return self.srcnode().get_path()
+        srcnode = self.srcnode()
+        if srcnode.stat() is None and self.stat() is not None:
+            result = self.get_path()
+        else:
+            result = srcnode.get_path()
+        if not Save_Strings:
+            # We're not at the point where we're saving the string string
+            # representations of FS Nodes (because we haven't finished
+            # reading the SConscript files and need to have str() return
+            # things relative to them).  That also means we can't yet
+            # cache values returned (or not returned) by stat(), since
+            # Python code in the SConscript files might still create
+            # or otherwise affect the on-disk file.  So get rid of the
+            # values that the underlying stat() method saved.
+            try: del self._memo['stat']
+            except KeyError: pass
+            if self is not srcnode:
+                try: del srcnode._memo['stat']
+                except KeyError: pass
+        return result
 
     rstr = __str__
 
+    memoizer_counters.append(SCons.Memoize.CountValue('stat'))
+
     def stat(self):
-        "__cacheable__"
-        try: return self.fs.stat(self.abspath)
-        except os.error: return None
+        try: return self._memo['stat']
+        except KeyError: pass
+        try: result = self.fs.stat(self.abspath)
+        except os.error: result = None
+        self._memo['stat'] = result
+        return result
 
     def exists(self):
-        "__cacheable__"
-        return not self.stat() is None
+        return self.stat() is not None
 
     def rexists(self):
-        "__cacheable__"
         return self.rfile().exists()
 
     def getmtime(self):
@@ -571,11 +705,11 @@ class Base(SCons.Node.Node):
 
     def isdir(self):
         st = self.stat()
-        return not st is None and stat.S_ISDIR(st[stat.ST_MODE])
+        return st is not None and stat.S_ISDIR(st[stat.ST_MODE])
 
     def isfile(self):
         st = self.stat()
-        return not st is None and stat.S_ISREG(st[stat.ST_MODE])
+        return st is not None and stat.S_ISREG(st[stat.ST_MODE])
 
     if hasattr(os, 'symlink'):
         def islink(self):
@@ -599,16 +733,12 @@ class Base(SCons.Node.Node):
         """If this node is in a build path, return the node
         corresponding to its source file.  Otherwise, return
         ourself.
-        __cacheable__"""
-        dir=self.dir
-        name=self.name
-        while dir:
-            if dir.srcdir:
-                srcnode = self.fs.Entry(name, dir.srcdir,
-                                        klass=self.__class__)
-                return srcnode
-            name = dir.name + os.sep + name
-            dir = dir.up()
+        """
+        srcdir_list = self.dir.srcdir_list()
+        if srcdir_list:
+            srcnode = srcdir_list[0].Entry(self.name)
+            srcnode.must_be_same(self.__class__)
+            return srcnode
         return self
 
     def get_path(self, dir=None):
@@ -622,8 +752,8 @@ class Base(SCons.Node.Node):
         try: i = path_elems.index(dir)
         except ValueError: pass
         else: path_elems = path_elems[i+1:]
-        path_elems = map(lambda n: n.name, path_elems)
-        return string.join(path_elems, os.sep)
+        path_elems = [n.name for n in path_elems]
+        return os.sep.join(path_elems)
 
     def set_src_builder(self, builder):
         """Set the source code builder for this node."""
@@ -663,6 +793,84 @@ class Base(SCons.Node.Node):
             self._proxy = ret
             return ret
 
+    def target_from_source(self, prefix, suffix, splitext=SCons.Util.splitext):
+        """
+
+        Generates a target entry that corresponds to this entry (usually
+        a source file) with the specified prefix and suffix.
+
+        Note that this method can be overridden dynamically for generated
+        files that need different behavior.  See Tool/swig.py for
+        an example.
+        """
+        return self.dir.Entry(prefix + splitext(self.name)[0] + suffix)
+
+    def _Rfindalldirs_key(self, pathlist):
+        return pathlist
+
+    memoizer_counters.append(SCons.Memoize.CountDict('Rfindalldirs', _Rfindalldirs_key))
+
+    def Rfindalldirs(self, pathlist):
+        """
+        Return all of the directories for a given path list, including
+        corresponding "backing" directories in any repositories.
+
+        The Node lookups are relative to this Node (typically a
+        directory), so memoizing result saves cycles from looking
+        up the same path for each target in a given directory.
+        """
+        try:
+            memo_dict = self._memo['Rfindalldirs']
+        except KeyError:
+            memo_dict = {}
+            self._memo['Rfindalldirs'] = memo_dict
+        else:
+            try:
+                return memo_dict[pathlist]
+            except KeyError:
+                pass
+
+        create_dir_relative_to_self = self.Dir
+        result = []
+        for path in pathlist:
+            if isinstance(path, SCons.Node.Node):
+                result.append(path)
+            else:
+                dir = create_dir_relative_to_self(path)
+                result.extend(dir.get_all_rdirs())
+
+        memo_dict[pathlist] = result
+
+        return result
+
+    def RDirs(self, pathlist):
+        """Search for a list of directories in the Repository list."""
+        cwd = self.cwd or self.fs._cwd
+        return cwd.Rfindalldirs(pathlist)
+
+    memoizer_counters.append(SCons.Memoize.CountValue('rentry'))
+
+    def rentry(self):
+        try:
+            return self._memo['rentry']
+        except KeyError:
+            pass
+        result = self
+        if not self.exists():
+            norm_name = _my_normcase(self.name)
+            for dir in self.dir.get_all_rdirs():
+                try:
+                    node = dir.entries[norm_name]
+                except KeyError:
+                    if dir.entry_exists_on_disk(self.name):
+                        result = dir.Entry(self.name)
+                        break
+        self._memo['rentry'] = result
+        return result
+
+    def _glob1(self, pattern, ondisk=True, source=False, strings=False):
+        return []
+
 class Entry(Base):
     """This is the class for generic Node.FS entries--that is, things
     that could be a File or a Dir, but we're just not sure yet.
@@ -674,14 +882,39 @@ class Entry(Base):
     def diskcheck_match(self):
         pass
 
-    def disambiguate(self):
+    def disambiguate(self, must_exist=None):
+        """
+        """
         if self.isdir():
             self.__class__ = Dir
             self._morph()
-        else:
+        elif self.isfile():
             self.__class__ = File
             self._morph()
             self.clear()
+        else:
+            # There was nothing on-disk at this location, so look in
+            # the src directory.
+            #
+            # We can't just use self.srcnode() straight away because
+            # that would create an actual Node for this file in the src
+            # directory, and there might not be one.  Instead, use the
+            # dir_on_disk() method to see if there's something on-disk
+            # with that name, in which case we can go ahead and call
+            # self.srcnode() to create the right type of entry.
+            srcdir = self.dir.srcnode()
+            if srcdir != self.dir and \
+               srcdir.entry_exists_on_disk(self.name) and \
+               self.srcnode().isdir():
+                self.__class__ = Dir
+                self._morph()
+            elif must_exist:
+                msg = "No such file or directory: '%s'" % self.abspath
+                raise SCons.Errors.UserError(msg)
+            else:
+                self.__class__ = File
+                self._morph()
+                self.clear()
         return self
 
     def rfile(self):
@@ -692,34 +925,60 @@ class Entry(Base):
         self.clear()
         return File.rfile(self)
 
-    def get_found_includes(self, env, scanner, path):
-        """If we're looking for included files, it's because this Entry
-        is really supposed to be a File itself."""
-        return self.disambiguate().get_found_includes(env, scanner, path)
-
     def scanner_key(self):
         return self.get_suffix()
 
     def get_contents(self):
-        """Fetch the contents of the entry.
-        
-        Since this should return the real contents from the file
+        """Fetch the contents of the entry.  Returns the exact binary
+        contents of the file."""
+        try:
+            self = self.disambiguate(must_exist=1)
+        except SCons.Errors.UserError:
+            # There was nothing on disk with which to disambiguate
+            # this entry.  Leave it as an Entry, but return a null
+            # string so calls to get_contents() in emitters and the
+            # like (e.g. in qt.py) don't have to disambiguate by hand
+            # or catch the exception.
+            return ''
+        else:
+            return self.get_contents()
+
+    def get_text_contents(self):
+        """Fetch the decoded text contents of a Unicode encoded Entry.
+
+        Since this should return the text contents from the file
         system, we check to see into what sort of subclass we should
         morph this Entry."""
-        if self.isfile():
-            self.__class__ = File
-            self._morph()
-            return self.get_contents()
-        if self.isdir():
-            self.__class__ = Dir
+        try:
+            self = self.disambiguate(must_exist=1)
+        except SCons.Errors.UserError:
+            # There was nothing on disk with which to disambiguate
+            # this entry.  Leave it as an Entry, but return a null
+            # string so calls to get_text_contents() in emitters and
+            # the like (e.g. in qt.py) don't have to disambiguate by
+            # hand or catch the exception.
+            return ''
+        else:
+            return self.get_text_contents()
+
+    def must_be_same(self, klass):
+        """Called to make sure a Node is a Dir.  Since we're an
+        Entry, we can morph into one."""
+        if self.__class__ is not klass:
+            self.__class__ = klass
             self._morph()
-            return self.get_contents()
-        if self.islink():
-            return ''             # avoid errors for dangling symlinks
-        raise AttributeError
+            self.clear()
 
-    def rel_path(self, other):
-        return self.disambiguate().rel_path(other)
+    # The following methods can get called before the Taskmaster has
+    # had a chance to call disambiguate() directly to see if this Entry
+    # should really be a Dir or a File.  We therefore use these to call
+    # disambiguate() transparently (from our caller's point of view).
+    #
+    # Right now, this minimal set of methods has been derived by just
+    # looking at some of the methods that will obviously be called early
+    # in any of the various Taskmasters' calling sequences, and then
+    # empirically figuring out which additional methods are necessary
+    # to make various tests pass.
 
     def exists(self):
         """Return if the Entry exists.  Check the file system to see
@@ -727,18 +986,23 @@ class Entry(Base):
         directory."""
         return self.disambiguate().exists()
 
-    def calc_signature(self, calc=None):
-        """Return the Entry's calculated signature.  Check the file
-        system to see what we should turn into first.  Assume a file if
-        there's no directory."""
-        return self.disambiguate().calc_signature(calc)
+    def rel_path(self, other):
+        d = self.disambiguate()
+        if d.__class__ is Entry:
+            raise Exception("rel_path() could not disambiguate File/Dir")
+        return d.rel_path(other)
 
-    def must_be_a_Dir(self):
-        """Called to make sure a Node is a Dir.  Since we're an
-        Entry, we can morph into one."""
-        self.__class__ = Dir
-        self._morph()
-        return self
+    def new_ninfo(self):
+        return self.disambiguate().new_ninfo()
+
+    def changed_since_last_build(self, target, prev_ni):
+        return self.disambiguate().changed_since_last_build(target, prev_ni)
+
+    def _glob1(self, pattern, ondisk=True, source=False, strings=False):
+        return self.disambiguate()._glob1(pattern, ondisk, source, strings)
+
+    def get_subst_proxy(self):
+        return self.disambiguate().get_subst_proxy()
 
 # This is for later so we can differentiate between Entry the class and Entry
 # the method of the FS class.
@@ -749,7 +1013,7 @@ class LocalFS:
 
     if SCons.Memoize.use_memoizer:
         __metaclass__ = SCons.Memoize.Memoized_Metaclass
-    
+
     # This class implements an abstraction layer for operations involving
     # a local file system.  Essentially, this wraps any function in
     # the os, os.path or shutil modules that we use to actually go do
@@ -768,6 +1032,8 @@ class LocalFS:
     #    return os.chdir(path)
     def chmod(self, path, mode):
         return os.chmod(path, mode)
+    def copy(self, src, dst):
+        return shutil.copy(src, dst)
     def copy2(self, src, dst):
         return shutil.copy2(src, dst)
     def exists(self, path):
@@ -808,12 +1074,12 @@ class LocalFS:
         def islink(self, path):
             return 0                    # no symlinks
 
-if SCons.Memoize.use_old_memoization():
-    _FSBase = LocalFS
-    class LocalFS(SCons.Memoize.Memoizer, _FSBase):
-        def __init__(self, *args, **kw):
-            apply(_FSBase.__init__, (self,)+args, kw)
-            SCons.Memoize.Memoizer.__init__(self)
+    if hasattr(os, 'readlink'):
+        def readlink(self, file):
+            return os.readlink(file)
+    else:
+        def readlink(self, file):
+            return ''
 
 
 #class RemoteFS:
@@ -827,6 +1093,8 @@ if SCons.Memoize.use_old_memoization():
 
 class FS(LocalFS):
 
+    memoizer_counters = []
+
     def __init__(self, path = None):
         """Initialize the Node.FS subsystem.
 
@@ -838,11 +1106,10 @@ class FS(LocalFS):
         """
         if __debug__: logInstanceCreation(self, 'Node.FS')
 
+        self._memo = {}
+
         self.Root = {}
         self.SConstruct_dir = None
-        self.CachePath = None
-        self.cache_force = None
-        self.cache_show = None
         self.max_drift = default_max_drift
 
         self.Top = None
@@ -852,14 +1119,13 @@ class FS(LocalFS):
             self.pathTop = path
         self.defaultDrive = _my_normcase(os.path.splitdrive(self.pathTop)[0])
 
-        self.Top = self._doLookup(Dir, os.path.normpath(self.pathTop))
+        self.Top = self.Dir(self.pathTop)
         self.Top.path = '.'
         self.Top.tpath = '.'
         self._cwd = self.Top
 
-    def clear_cache(self):
-        "__cache_reset__"
-        pass
+        DirNodeInfo.fs = self
+        FileNodeInfo.fs = self
     
     def set_SConstruct_dir(self, dir):
         self.SConstruct_dir = dir
@@ -873,138 +1139,6 @@ class FS(LocalFS):
     def getcwd(self):
         return self._cwd
 
-    def __checkClass(self, node, klass):
-        if isinstance(node, klass) or klass == Entry:
-            return node
-        if node.__class__ == Entry:
-            node.__class__ = klass
-            node._morph()
-            return node
-        raise TypeError, "Tried to lookup %s '%s' as a %s." % \
-              (node.__class__.__name__, node.path, klass.__name__)
-        
-    def _doLookup(self, fsclass, name, directory = None, create = 1):
-        """This method differs from the File and Dir factory methods in
-        one important way: the meaning of the directory parameter.
-        In this method, if directory is None or not supplied, the supplied
-        name is expected to be an absolute path.  If you try to look up a
-        relative path with directory=None, then an AssertionError will be
-        raised.
-        __cacheable__"""
-
-        if not name:
-            # This is a stupid hack to compensate for the fact that
-            # the POSIX and Win32 versions of os.path.normpath() behave
-            # differently in older versions of Python.  In particular,
-            # in POSIX:
-            #   os.path.normpath('./') == '.'
-            # in Win32
-            #   os.path.normpath('./') == ''
-            #   os.path.normpath('.\\') == ''
-            #
-            # This is a definite bug in the Python library, but we have
-            # to live with it.
-            name = '.'
-        path_orig = string.split(name, os.sep)
-        path_norm = string.split(_my_normcase(name), os.sep)
-
-        first_orig = path_orig.pop(0)   # strip first element
-        first_norm = path_norm.pop(0)   # strip first element
-
-        drive, path_first = os.path.splitdrive(first_orig)
-        if path_first:
-            path_orig = [ path_first, ] + path_orig
-            path_norm = [ _my_normcase(path_first), ] + path_norm
-        else:
-            # Absolute path
-            drive = _my_normcase(drive)
-            try:
-                directory = self.Root[drive]
-            except KeyError:
-                if not create:
-                    raise SCons.Errors.UserError
-                directory = RootDir(drive, self)
-                self.Root[drive] = directory
-                if not drive:
-                    self.Root[self.defaultDrive] = directory
-                elif drive == self.defaultDrive:
-                    self.Root[''] = directory
-
-        if not path_orig:
-            return directory
-
-        last_orig = path_orig.pop()     # strip last element
-        last_norm = path_norm.pop()     # strip last element
-            
-        # Lookup the directory
-        for orig, norm in map(None, path_orig, path_norm):
-            try:
-                entries = directory.entries
-            except AttributeError:
-                # We tried to look up the entry in either an Entry or
-                # a File.  Give whatever it is a chance to do what's
-                # appropriate: morph into a Dir or raise an exception.
-                directory.must_be_a_Dir()
-                entries = directory.entries
-            try:
-                directory = entries[norm]
-            except KeyError:
-                if not create:
-                    raise SCons.Errors.UserError
-
-                d = Dir(orig, directory, self)
-
-                # Check the file system (or not, as configured) to make
-                # sure there isn't already a file there.
-                d.diskcheck_match()
-
-                directory.entries[norm] = d
-                directory.add_wkid(d)
-                directory = d
-
-        directory.must_be_a_Dir()
-
-        try:
-            e = directory.entries[last_norm]
-        except KeyError:
-            if not create:
-                raise SCons.Errors.UserError
-
-            result = fsclass(last_orig, directory, self)
-
-            # Check the file system (or not, as configured) to make
-            # sure there isn't already a directory at the path on
-            # disk where we just created a File node, and vice versa.
-            result.diskcheck_match()
-
-            directory.entries[last_norm] = result 
-            directory.add_wkid(result)
-        else:
-            result = self.__checkClass(e, fsclass)
-        return result 
-
-    def _transformPath(self, name, directory):
-        """Take care of setting up the correct top-level directory,
-        usually in preparation for a call to doLookup().
-
-        If the path name is prepended with a '#', then it is unconditionally
-        interpreted as relative to the top-level directory of this FS.
-
-        If directory is None, and name is a relative path,
-        then the same applies.
-        """
-        if name and name[0] == '#':
-            directory = self.Top
-            name = name[1:]
-            if name and (name[0] == os.sep or name[0] == '/'):
-                # Correct such that '#/foo' is equivalent
-                # to '#foo'.
-                name = name[1:]
-            name = os.path.join('.', os.path.normpath(name))
-        elif not directory:
-            directory = self._cwd
-        return (os.path.normpath(name), directory)
-
     def chdir(self, dir, change_os_dir=0):
         """Change the current working directory for lookups.
         If change_os_dir is true, we will also change the "real" cwd
@@ -1012,7 +1146,7 @@ class FS(LocalFS):
         """
         curr=self._cwd
         try:
-            if not dir is None:
+            if dir is not None:
                 self._cwd = dir
                 if change_os_dir:
                     os.chdir(dir.abspath)
@@ -1020,27 +1154,114 @@ class FS(LocalFS):
             self._cwd = curr
             raise
 
-    def Entry(self, name, directory = None, create = 1, klass=None):
-        """Lookup or create a generic Entry node with the specified name.
+    def get_root(self, drive):
+        """
+        Returns the root directory for the specified drive, creating
+        it if necessary.
+        """
+        drive = _my_normcase(drive)
+        try:
+            return self.Root[drive]
+        except KeyError:
+            root = RootDir(drive, self)
+            self.Root[drive] = root
+            if not drive:
+                self.Root[self.defaultDrive] = root
+            elif drive == self.defaultDrive:
+                self.Root[''] = root
+            return root
+
+    def _lookup(self, p, directory, fsclass, create=1):
+        """
+        The generic entry point for Node lookup with user-supplied data.
+
+        This translates arbitrary input into a canonical Node.FS object
+        of the specified fsclass.  The general approach for strings is
+        to turn it into a fully normalized absolute path and then call
+        the root directory's lookup_abs() method for the heavy lifting.
+
+        If the path name begins with '#', it is unconditionally
+        interpreted relative to the top-level directory of this FS.  '#'
+        is treated as a synonym for the top-level SConstruct directory,
+        much like '~' is treated as a synonym for the user's home
+        directory in a UNIX shell.  So both '#foo' and '#/foo' refer
+        to the 'foo' subdirectory underneath the top-level SConstruct
+        directory.
+
+        If the path name is relative, then the path is looked up relative
+        to the specified directory, or the current directory (self._cwd,
+        typically the SConscript directory) if the specified directory
+        is None.
+        """
+        if isinstance(p, Base):
+            # It's already a Node.FS object.  Make sure it's the right
+            # class and return.
+            p.must_be_same(fsclass)
+            return p
+        # str(p) in case it's something like a proxy object
+        p = str(p)
+
+        initial_hash = (p[0:1] == '#')
+        if initial_hash:
+            # There was an initial '#', so we strip it and override
+            # whatever directory they may have specified with the
+            # top-level SConstruct directory.
+            p = p[1:]
+            directory = self.Top
+
+        if directory and not isinstance(directory, Dir):
+            directory = self.Dir(directory)
+
+        if do_splitdrive:
+            drive, p = os.path.splitdrive(p)
+        else:
+            drive = ''
+        if drive and not p:
+            # This causes a naked drive letter to be treated as a synonym
+            # for the root directory on that drive.
+            p = os.sep
+        absolute = os.path.isabs(p)
+
+        needs_normpath = needs_normpath_check.match(p)
+
+        if initial_hash or not absolute:
+            # This is a relative lookup, either to the top-level
+            # SConstruct directory (because of the initial '#') or to
+            # the current directory (the path name is not absolute).
+            # Add the string to the appropriate directory lookup path,
+            # after which the whole thing gets normalized.
+            if not directory:
+                directory = self._cwd
+            if p:
+                p = directory.labspath + '/' + p
+            else:
+                p = directory.labspath
+
+        if needs_normpath:
+            p = os.path.normpath(p)
+
+        if drive or absolute:
+            root = self.get_root(drive)
+        else:
+            if not directory:
+                directory = self._cwd
+            root = directory.root
+
+        if os.sep != '/':
+            p = p.replace(os.sep, '/')
+        return root._lookup_abs(p, fsclass, create)
+
+    def Entry(self, name, directory = None, create = 1):
+        """Look up or create a generic Entry node with the specified name.
         If the name is a relative path (begins with ./, ../, or a file
         name), then it is looked up relative to the supplied directory
         node, or to the top level directory of the FS (supplied at
         construction time) if no directory is supplied.
         """
+        return self._lookup(name, directory, Entry, create)
 
-        if not klass:
-            klass = Entry
-
-        if isinstance(name, Base):
-            return self.__checkClass(name, klass)
-        else:
-            if directory and not isinstance(directory, Dir):
-                directory = self.Dir(directory)
-            name, directory = self._transformPath(name, directory)
-            return self._doLookup(klass, name, directory, create)
-    
     def File(self, name, directory = None, create = 1):
-        """Lookup or create a File node with the specified name.  If
+        """Look up or create a File node with the specified name.  If
         the name is a relative path (begins with ./, ../, or a file name),
         then it is looked up relative to the supplied directory node,
         or to the top level directory of the FS (supplied at construction
@@ -1049,11 +1270,10 @@ class FS(LocalFS):
         This method will raise TypeError if a directory is found at the
         specified path.
         """
+        return self._lookup(name, directory, File, create)
 
-        return self.Entry(name, directory, create, File)
-    
-    def Dir(self, name, directory = None, create = 1):
-        """Lookup or create a Dir node with the specified name.  If
+    def Dir(self, name, directory = None, create = True):
+        """Look up or create a Dir node with the specified name.  If
         the name is a relative path (begins with ./, ../, or a file name),
         then it is looked up relative to the supplied directory node,
         or to the top level directory of the FS (supplied at construction
@@ -1062,24 +1282,23 @@ class FS(LocalFS):
         This method will raise TypeError if a normal file is found at the
         specified path.
         """
+        return self._lookup(name, directory, Dir, create)
 
-        return self.Entry(name, directory, create, Dir)
-    
-    def BuildDir(self, build_dir, src_dir, duplicate=1):
-        """Link the supplied build directory to the source directory
+    def VariantDir(self, variant_dir, src_dir, duplicate=1):
+        """Link the supplied variant directory to the source directory
         for purposes of building files."""
-        
+
         if not isinstance(src_dir, SCons.Node.Node):
             src_dir = self.Dir(src_dir)
-        if not isinstance(build_dir, SCons.Node.Node):
-            build_dir = self.Dir(build_dir)
-        if src_dir.is_under(build_dir):
-            raise SCons.Errors.UserError, "Source directory cannot be under build directory."
-        if build_dir.srcdir:
-            if build_dir.srcdir == src_dir:
+        if not isinstance(variant_dir, SCons.Node.Node):
+            variant_dir = self.Dir(variant_dir)
+        if src_dir.is_under(variant_dir):
+            raise SCons.Errors.UserError("Source directory cannot be under variant directory.")
+        if variant_dir.srcdir:
+            if variant_dir.srcdir == src_dir:
                 return # We already did this.
-            raise SCons.Errors.UserError, "'%s' already has a source directory: '%s'."%(build_dir, build_dir.srcdir)
-        build_dir.link(src_dir, duplicate)
+            raise SCons.Errors.UserError("'%s' already has a source directory: '%s'."%(variant_dir, variant_dir.srcdir))
+        variant_dir.link(src_dir, duplicate)
 
     def Repository(self, *dirs):
         """Specify Repository directories to search."""
@@ -1088,53 +1307,77 @@ class FS(LocalFS):
                 d = self.Dir(d)
             self.Top.addRepository(d)
 
-    def Rfindalldirs(self, pathlist, cwd):
-        """__cacheable__"""
-        if SCons.Util.is_String(pathlist):
-            pathlist = string.split(pathlist, os.pathsep)
-        if not SCons.Util.is_List(pathlist):
-            pathlist = [pathlist]
-        result = []
-        for path in filter(None, pathlist):
-            if isinstance(path, SCons.Node.Node):
-                result.append(path)
-                continue
-            path, dir = self._transformPath(path, cwd)
-            dir = dir.Dir(path)
-            result.extend(dir.get_all_rdirs())
-        return result
-
-    def CacheDir(self, path):
-        self.CachePath = path
-
-    def build_dir_target_climb(self, orig, dir, tail):
-        """Create targets in corresponding build directories
+    def variant_dir_target_climb(self, orig, dir, tail):
+        """Create targets in corresponding variant directories
 
         Climb the directory tree, and look up path names
-        relative to any linked build directories we find.
-        __cacheable__
+        relative to any linked variant directories we find.
+
+        Even though this loops and walks up the tree, we don't memoize
+        the return value because this is really only used to process
+        the command-line targets.
         """
         targets = []
         message = None
-        fmt = "building associated BuildDir targets: %s"
+        fmt = "building associated VariantDir targets: %s"
         start_dir = dir
         while dir:
-            for bd in dir.build_dirs:
+            for bd in dir.variant_dirs:
                 if start_dir.is_under(bd):
                     # If already in the build-dir location, don't reflect
                     return [orig], fmt % str(orig)
-                p = apply(os.path.join, [bd.path] + tail)
+                p = os.path.join(bd.path, *tail)
                 targets.append(self.Entry(p))
             tail = [dir.name] + tail
             dir = dir.up()
         if targets:
-            message = fmt % string.join(map(str, targets))
+            message = fmt % ' '.join(map(str, targets))
         return targets, message
 
+    def Glob(self, pathname, ondisk=True, source=True, strings=False, cwd=None):
+        """
+        Globs
+
+        This is mainly a shim layer 
+        """
+        if cwd is None:
+            cwd = self.getcwd()
+        return cwd.glob(pathname, ondisk, source, strings)
+
+class DirNodeInfo(SCons.Node.NodeInfoBase):
+    # This should get reset by the FS initialization.
+    current_version_id = 1
+
+    fs = None
+
+    def str_to_node(self, s):
+        top = self.fs.Top
+        root = top.root
+        if do_splitdrive:
+            drive, s = os.path.splitdrive(s)
+            if drive:
+                root = self.fs.get_root(drive)
+        if not os.path.isabs(s):
+            s = top.labspath + '/' + s
+        return root._lookup_abs(s, Entry)
+
+class DirBuildInfo(SCons.Node.BuildInfoBase):
+    current_version_id = 1
+
+glob_magic_check = re.compile('[*?[]')
+
+def has_glob_magic(s):
+    return glob_magic_check.search(s) is not None
+
 class Dir(Base):
     """A class for directories in a file system.
     """
 
+    memoizer_counters = []
+
+    NodeInfo = DirNodeInfo
+    BuildInfo = DirBuildInfo
+
     def __init__(self, name, directory, fs):
         if __debug__: logInstanceCreation(self, 'Node.FS.Dir')
         Base.__init__(self, name, directory, fs)
@@ -1147,7 +1390,7 @@ class Dir(Base):
         Set up this directory's entries and hook it into the file
         system tree.  Specify that directories (this Node) don't use
         signatures for calculating whether they're current.
-        __cache_reset__"""
+        """
 
         self.repositories = []
         self.srcdir = None
@@ -1158,7 +1401,8 @@ class Dir(Base):
         self.cwd = self
         self.searched = 0
         self._sconsign = None
-        self.build_dirs = []
+        self.variant_dirs = []
+        self.root = self.dir.root
 
         # Don't just reset the executor, replace its action list,
         # because it might have some pre-or post-actions that need to
@@ -1167,11 +1411,8 @@ class Dir(Base):
         self.get_executor().set_action_list(self.builder.action)
 
     def diskcheck_match(self):
-        diskcheck_match(self, self.fs.isfile,
-                           "File %s found where directory expected.")
-
-    def disambiguate(self):
-        return self
+        diskcheck_match(self, self.isfile,
+                        "File %s found where directory expected.")
 
     def __clearRepositoryCache(self, duplicate=None):
         """Called when we change the repository(ies) for a directory.
@@ -1188,50 +1429,88 @@ class Dir(Base):
                         del node._srcreps
                     except AttributeError:
                         pass
-                    if duplicate != None:
+                    if duplicate is not None:
                         node.duplicate=duplicate
-    
+
     def __resetDuplicate(self, node):
         if node != self:
             node.duplicate = node.get_dir().duplicate
 
     def Entry(self, name):
-        """Create an entry node named 'name' relative to this directory."""
+        """
+        Looks up or creates an entry node named 'name' relative to
+        this directory.
+        """
         return self.fs.Entry(name, self)
 
-    def Dir(self, name):
-        """Create a directory node named 'name' relative to this directory."""
-        return self.fs.Dir(name, self)
+    def Dir(self, name, create=True):
+        """
+        Looks up or creates a directory node named 'name' relative to
+        this directory.
+        """
+        return self.fs.Dir(name, self, create)
 
     def File(self, name):
-        """Create a file node named 'name' relative to this directory."""
+        """
+        Looks up or creates a file node named 'name' relative to
+        this directory.
+        """
         return self.fs.File(name, self)
 
+    def _lookup_rel(self, name, klass, create=1):
+        """
+        Looks up a *normalized* relative path name, relative to this
+        directory.
+
+        This method is intended for use by internal lookups with
+        already-normalized path data.  For general-purpose lookups,
+        use the Entry(), Dir() and File() methods above.
+
+        This method does *no* input checking and will die or give
+        incorrect results if it's passed a non-normalized path name (e.g.,
+        a path containing '..'), an absolute path name, a top-relative
+        ('#foo') path name, or any kind of object.
+        """
+        name = self.entry_labspath(name)
+        return self.root._lookup_abs(name, klass, create)
+
     def link(self, srcdir, duplicate):
-        """Set this directory as the build directory for the
+        """Set this directory as the variant directory for the
         supplied source directory."""
         self.srcdir = srcdir
         self.duplicate = duplicate
         self.__clearRepositoryCache(duplicate)
-        srcdir.build_dirs.append(self)
+        srcdir.variant_dirs.append(self)
 
     def getRepositories(self):
         """Returns a list of repositories for this directory.
-        __cacheable__"""
+        """
         if self.srcdir and not self.duplicate:
             return self.srcdir.get_all_rdirs() + self.repositories
         return self.repositories
 
+    memoizer_counters.append(SCons.Memoize.CountValue('get_all_rdirs'))
+
     def get_all_rdirs(self):
-        """__cacheable__"""
+        try:
+            return list(self._memo['get_all_rdirs'])
+        except KeyError:
+            pass
+
         result = [self]
         fname = '.'
         dir = self
         while dir:
             for rep in dir.getRepositories():
                 result.append(rep.Dir(fname))
-            fname = dir.name + os.sep + fname
+            if fname == '.':
+                fname = dir.name
+            else:
+                fname = dir.name + os.sep + fname
             dir = dir.up()
+
+        self._memo['get_all_rdirs'] = list(result)
+
         return result
 
     def addRepository(self, dir):
@@ -1243,54 +1522,84 @@ class Dir(Base):
     def up(self):
         return self.entries['..']
 
+    def _rel_path_key(self, other):
+        return str(other)
+
+    memoizer_counters.append(SCons.Memoize.CountDict('rel_path', _rel_path_key))
+
     def rel_path(self, other):
         """Return a path to "other" relative to this directory.
-        __cacheable__"""
-        if isinstance(other, Dir):
-            name = []
+        """
+
+        # This complicated and expensive method, which constructs relative
+        # paths between arbitrary Node.FS objects, is no longer used
+        # by SCons itself.  It was introduced to store dependency paths
+        # in .sconsign files relative to the target, but that ended up
+        # being significantly inefficient.
+        #
+        # We're continuing to support the method because some SConstruct
+        # files out there started using it when it was available, and
+        # we're all about backwards compatibility..
+
+        try:
+            memo_dict = self._memo['rel_path']
+        except KeyError:
+            memo_dict = {}
+            self._memo['rel_path'] = memo_dict
         else:
             try:
-                name = [other.name]
-                other = other.dir
-            except AttributeError:
-                return str(other)
+                return memo_dict[other]
+            except KeyError:
+                pass
+
         if self is other:
-            return name and name[0] or '.'
-        i = 0
-        for x, y in map(None, self.path_elements, other.path_elements):
-            if not x is y:
-                break
-            i = i + 1
-        path_elems = ['..']*(len(self.path_elements)-i) \
-                   + map(lambda n: n.name, other.path_elements[i:]) \
-                   + name
+            result = '.'
+
+        elif not other in self.path_elements:
+            try:
+                other_dir = other.get_dir()
+            except AttributeError:
+                result = str(other)
+            else:
+                if other_dir is None:
+                    result = other.name
+                else:
+                    dir_rel_path = self.rel_path(other_dir)
+                    if dir_rel_path == '.':
+                        result = other.name
+                    else:
+                        result = dir_rel_path + os.sep + other.name
+        else:
+            i = self.path_elements.index(other) + 1
+
+            path_elems = ['..'] * (len(self.path_elements) - i) \
+                         + [n.name for n in other.path_elements[i:]]
              
-        return string.join(path_elems, os.sep)
+            result = os.sep.join(path_elems)
 
-    def scan(self):
-        if not self.implicit is None:
-            return
-        self.implicit = []
-        self.implicit_dict = {}
-        self._children_reset()
+        memo_dict[other] = result
+
+        return result
 
-        dont_scan = lambda k: k not in ['.', '..', '.sconsign']
-        deps = filter(dont_scan, self.entries.keys())
-        # keys() is going to give back the entries in an internal,
-        # unsorted order.  Sort 'em so the order is deterministic.
-        deps.sort()
-        entries = map(lambda n, e=self.entries: e[n], deps)
+    def get_env_scanner(self, env, kw={}):
+        import SCons.Defaults
+        return SCons.Defaults.DirEntryScanner
 
-        self._add_child(self.implicit, self.implicit_dict, entries)
+    def get_target_scanner(self):
+        import SCons.Defaults
+        return SCons.Defaults.DirEntryScanner
 
     def get_found_includes(self, env, scanner, path):
-        """Return the included implicit dependencies in this file.
-        Cache results so we only scan the file once per path
-        regardless of how many times this information is requested.
-        __cacheable__"""
+        """Return this directory's implicit dependencies.
+
+        We don't bother caching the results because the scan typically
+        shouldn't be requested more than once (as opposed to scanning
+        .h file contents, which can be requested as many times as the
+        files is #included by other files).
+        """
         if not scanner:
             return []
-        # Clear cached info for this Node.  If we already visited this
+        # Clear cached info for this Dir.  If we already visited this
         # directory on our walk down the tree (because we didn't know at
         # that point it was being used as the source for another Node)
         # then we may have calculated build signature before realizing
@@ -1301,11 +1610,22 @@ class Dir(Base):
         self.clear()
         return scanner(self, env, path)
 
+    #
+    # Taskmaster interface subsystem
+    #
+
+    def prepare(self):
+        pass
+
     def build(self, **kw):
         """A null "builder" for directories."""
         global MkdirBuilder
-        if not self.builder is MkdirBuilder:
-            apply(SCons.Node.Node.build, [self,], kw)
+        if self.builder is not MkdirBuilder:
+            SCons.Node.Node.build(self, **kw)
+
+    #
+    #
+    #
 
     def _create(self):
         """Create this directory, silently and without worrying about
@@ -1318,7 +1638,9 @@ class Dir(Base):
             listDirs.append(parent)
             p = parent.up()
             if p is None:
-                raise SCons.Errors.StopError, parent.path
+                # Don't use while: - else: for this condition because
+                # if so, then parent is None and has no .path attribute.
+                raise SCons.Errors.StopError(parent.path)
             parent = p
         listDirs.reverse()
         for dirnode in listDirs:
@@ -1339,48 +1661,56 @@ class Dir(Base):
 
     def multiple_side_effect_has_builder(self):
         global MkdirBuilder
-        return not self.builder is MkdirBuilder and self.has_builder()
+        return self.builder is not MkdirBuilder and self.has_builder()
 
     def alter_targets(self):
-        """Return any corresponding targets in a build directory.
+        """Return any corresponding targets in a variant directory.
         """
-        return self.fs.build_dir_target_climb(self, self, [])
+        return self.fs.variant_dir_target_climb(self, self, [])
 
     def scanner_key(self):
         """A directory does not get scanned."""
         return None
 
-    def get_contents(self):
-        """Return aggregate contents of all our children."""
-        contents = cStringIO.StringIO()
-        for kid in self.children():
-            contents.write(kid.get_contents())
-        return contents.getvalue()
+    def get_text_contents(self):
+        """We already emit things in text, so just return the binary
+        version."""
+        return self.get_contents()
 
-    def prepare(self):
-        pass
+    def get_contents(self):
+        """Return content signatures and names of all our children
+        separated by new-lines. Ensure that the nodes are sorted."""
+        contents = []
+        for node in sorted(self.children(), key=lambda t: t.name):
+            contents.append('%s %s\n' % (node.get_csig(), node.name))
+        return ''.join(contents)
+
+    def get_csig(self):
+        """Compute the content signature for Directory nodes. In
+        general, this is not needed and the content signature is not
+        stored in the DirNodeInfo. However, if get_contents on a Dir
+        node is called which has a child directory, the child
+        directory should return the hash of its contents."""
+        contents = self.get_contents()
+        return SCons.Util.MD5signature(contents)
 
     def do_duplicate(self, src):
         pass
 
-    def current(self, calc=None):
-        """If all of our children were up-to-date, then this
-        directory was up-to-date, too."""
-        if not self.builder is MkdirBuilder and not self.exists():
+    changed_since_last_build = SCons.Node.Node.state_has_changed
+
+    def is_up_to_date(self):
+        """If any child is not up-to-date, then this directory isn't,
+        either."""
+        if self.builder is not MkdirBuilder and not self.exists():
             return 0
-        state = 0
+        up_to_date = SCons.Node.up_to_date
         for kid in self.children():
-            s = kid.get_state()
-            if s and (not state or s > state):
-                state = s
-        import SCons.Node
-        if state == 0 or state == SCons.Node.up_to_date:
-            return 1
-        else:
-            return 0
+            if kid.get_state() > up_to_date:
+                return 0
+        return 1
 
     def rdir(self):
-        "__cacheable__"
         if not self.exists():
             norm_name = _my_normcase(self.name)
             for dir in self.dir.get_all_rdirs():
@@ -1417,19 +1747,16 @@ class Dir(Base):
     def entry_abspath(self, name):
         return self.abspath + os.sep + name
 
+    def entry_labspath(self, name):
+        return self.labspath + '/' + name
+
     def entry_path(self, name):
         return self.path + os.sep + name
 
     def entry_tpath(self, name):
         return self.tpath + os.sep + name
 
-    def must_be_a_Dir(self):
-        """Called to make sure a Node is a Dir.  Since we're already
-        one, this is a no-op for us."""
-        return self
-
     def entry_exists_on_disk(self, name):
-        """__cacheable__"""
         try:
             d = self.on_disk_entries
         except AttributeError:
@@ -1440,47 +1767,79 @@ class Dir(Base):
                 pass
             else:
                 for entry in map(_my_normcase, entries):
-                    d[entry] = 1
+                    d[entry] = True
             self.on_disk_entries = d
-        return d.has_key(_my_normcase(name))
+        if sys.platform == 'win32':
+            name = _my_normcase(name)
+            result = d.get(name)
+            if result is None:
+                # Belt-and-suspenders for Windows:  check directly for
+                # 8.3 file names that don't show up in os.listdir().
+                result = os.path.exists(self.abspath + os.sep + name)
+                d[name] = result
+            return result
+        else:
+            return name in d
+
+    memoizer_counters.append(SCons.Memoize.CountValue('srcdir_list'))
 
     def srcdir_list(self):
-        """__cacheable__"""
+        try:
+            return self._memo['srcdir_list']
+        except KeyError:
+            pass
+
         result = []
 
         dirname = '.'
         dir = self
         while dir:
             if dir.srcdir:
-                d = dir.srcdir.Dir(dirname)
-                if d.is_under(dir):
-                    # Shouldn't source from something in the build path:
-                    # build_dir is probably under src_dir, in which case
-                    # we are reflecting.
-                    break
-                result.append(d)
+                result.append(dir.srcdir.Dir(dirname))
             dirname = dir.name + os.sep + dirname
             dir = dir.up()
 
+        self._memo['srcdir_list'] = result
+
         return result
 
     def srcdir_duplicate(self, name):
         for dir in self.srcdir_list():
+            if self.is_under(dir):
+                # We shouldn't source from something in the build path;
+                # variant_dir is probably under src_dir, in which case
+                # we are reflecting.
+                break
             if dir.entry_exists_on_disk(name):
-                srcnode = dir.File(name)
+                srcnode = dir.Entry(name).disambiguate()
                 if self.duplicate:
-                    node = self.File(name)
+                    node = self.Entry(name).disambiguate()
                     node.do_duplicate(srcnode)
                     return node
                 else:
                     return srcnode
         return None
 
+    def _srcdir_find_file_key(self, filename):
+        return filename
+
+    memoizer_counters.append(SCons.Memoize.CountDict('srcdir_find_file', _srcdir_find_file_key))
+
     def srcdir_find_file(self, filename):
-        """__cacheable__"""
+        try:
+            memo_dict = self._memo['srcdir_find_file']
+        except KeyError:
+            memo_dict = {}
+            self._memo['srcdir_find_file'] = memo_dict
+        else:
+            try:
+                return memo_dict[filename]
+            except KeyError:
+                pass
+
         def func(node):
             if (isinstance(node, File) or isinstance(node, Entry)) and \
-               (node.is_derived() or node.is_pseudo_derived() or node.exists()):
+               (node.is_derived() or node.exists()):
                     return node
             return None
 
@@ -1491,7 +1850,9 @@ class Dir(Base):
             except KeyError: node = rdir.file_on_disk(filename)
             else: node = func(node)
             if node:
-                return node, self
+                result = (node, self)
+                memo_dict[filename] = result
+                return result
 
         for srcdir in self.srcdir_list():
             for rdir in srcdir.get_all_rdirs():
@@ -1499,15 +1860,22 @@ class Dir(Base):
                 except KeyError: node = rdir.file_on_disk(filename)
                 else: node = func(node)
                 if node:
-                    return File(filename, self, self.fs), srcdir
+                    result = (File(filename, self, self.fs), srcdir)
+                    memo_dict[filename] = result
+                    return result
 
-        return None, None
+        result = (None, None)
+        memo_dict[filename] = result
+        return result
 
     def dir_on_disk(self, name):
         if self.entry_exists_on_disk(name):
             try: return self.Dir(name)
             except TypeError: pass
-        return None
+        node = self.srcdir_duplicate(name)
+        if isinstance(node, File):
+            return None
+        return node
 
     def file_on_disk(self, name):
         if self.entry_exists_on_disk(name) or \
@@ -1515,7 +1883,155 @@ class Dir(Base):
            diskcheck_sccs(self, name):
             try: return self.File(name)
             except TypeError: pass
-        return self.srcdir_duplicate(name)
+        node = self.srcdir_duplicate(name)
+        if isinstance(node, Dir):
+            return None
+        return node
+
+    def walk(self, func, arg):
+        """
+        Walk this directory tree by calling the specified function
+        for each directory in the tree.
+
+        This behaves like the os.path.walk() function, but for in-memory
+        Node.FS.Dir objects.  The function takes the same arguments as
+        the functions passed to os.path.walk():
+
+                func(arg, dirname, fnames)
+
+        Except that "dirname" will actually be the directory *Node*,
+        not the string.  The '.' and '..' entries are excluded from
+        fnames.  The fnames list may be modified in-place to filter the
+        subdirectories visited or otherwise impose a specific order.
+        The "arg" argument is always passed to func() and may be used
+        in any way (or ignored, passing None is common).
+        """
+        entries = self.entries
+        names = entries.keys()
+        names.remove('.')
+        names.remove('..')
+        func(arg, self, names)
+        for dirname in [n for n in names if isinstance(entries[n], Dir)]:
+            entries[dirname].walk(func, arg)
+
+    def glob(self, pathname, ondisk=True, source=False, strings=False):
+        """
+        Returns a list of Nodes (or strings) matching a specified
+        pathname pattern.
+
+        Pathname patterns follow UNIX shell semantics:  * matches
+        any-length strings of any characters, ? matches any character,
+        and [] can enclose lists or ranges of characters.  Matches do
+        not span directory separators.
+
+        The matches take into account Repositories, returning local
+        Nodes if a corresponding entry exists in a Repository (either
+        an in-memory Node or something on disk).
+
+        By defafult, the glob() function matches entries that exist
+        on-disk, in addition to in-memory Nodes.  Setting the "ondisk"
+        argument to False (or some other non-true value) causes the glob()
+        function to only match in-memory Nodes.  The default behavior is
+        to return both the on-disk and in-memory Nodes.
+
+        The "source" argument, when true, specifies that corresponding
+        source Nodes must be returned if you're globbing in a build
+        directory (initialized with VariantDir()).  The default behavior
+        is to return Nodes local to the VariantDir().
+
+        The "strings" argument, when true, returns the matches as strings,
+        not Nodes.  The strings are path names relative to this directory.
+
+        The underlying algorithm is adapted from the glob.glob() function
+        in the Python library (but heavily modified), and uses fnmatch()
+        under the covers.
+        """
+        dirname, basename = os.path.split(pathname)
+        if not dirname:
+            return sorted(self._glob1(basename, ondisk, source, strings),
+                          key=lambda t: str(t))
+        if has_glob_magic(dirname):
+            list = self.glob(dirname, ondisk, source, strings=False)
+        else:
+            list = [self.Dir(dirname, create=True)]
+        result = []
+        for dir in list:
+            r = dir._glob1(basename, ondisk, source, strings)
+            if strings:
+                r = [os.path.join(str(dir), x) for x in r]
+            result.extend(r)
+        result.sort(lambda a, b: cmp(str(a), str(b)))
+        return result
+
+    def _glob1(self, pattern, ondisk=True, source=False, strings=False):
+        """
+        Globs for and returns a list of entry names matching a single
+        pattern in this directory.
+
+        This searches any repositories and source directories for
+        corresponding entries and returns a Node (or string) relative
+        to the current directory if an entry is found anywhere.
+
+        TODO: handle pattern with no wildcard
+        """
+        search_dir_list = self.get_all_rdirs()
+        for srcdir in self.srcdir_list():
+            search_dir_list.extend(srcdir.get_all_rdirs())
+
+        selfEntry = self.Entry
+        names = []
+        for dir in search_dir_list:
+            # We use the .name attribute from the Node because the keys of
+            # the dir.entries dictionary are normalized (that is, all upper
+            # case) on case-insensitive systems like Windows.
+            node_names = [ v.name for k, v in dir.entries.items()
+                           if k not in ('.', '..') ]
+            names.extend(node_names)
+            if not strings:
+                # Make sure the working directory (self) actually has
+                # entries for all Nodes in repositories or variant dirs.
+                for name in node_names: selfEntry(name)
+            if ondisk:
+                try:
+                    disk_names = os.listdir(dir.abspath)
+                except os.error:
+                    continue
+                names.extend(disk_names)
+                if not strings:
+                    # We're going to return corresponding Nodes in
+                    # the local directory, so we need to make sure
+                    # those Nodes exist.  We only want to create
+                    # Nodes for the entries that will match the
+                    # specified pattern, though, which means we
+                    # need to filter the list here, even though
+                    # the overall list will also be filtered later,
+                    # after we exit this loop.
+                    if pattern[0] != '.':
+                        #disk_names = [ d for d in disk_names if d[0] != '.' ]
+                        disk_names = [x for x in disk_names if x[0] != '.']
+                    disk_names = fnmatch.filter(disk_names, pattern)
+                    dirEntry = dir.Entry
+                    for name in disk_names:
+                        # Add './' before disk filename so that '#' at
+                        # beginning of filename isn't interpreted.
+                        name = './' + name
+                        node = dirEntry(name).disambiguate()
+                        n = selfEntry(name)
+                        if n.__class__ != node.__class__:
+                            n.__class__ = node.__class__
+                            n._morph()
+
+        names = set(names)
+        if pattern[0] != '.':
+            #names = [ n for n in names if n[0] != '.' ]
+            names = [x for x in names if x[0] != '.']
+        names = fnmatch.filter(names, pattern)
+
+        if strings:
+            return names
+
+        #return [ self.entries[_my_normcase(n)] for n in names ]
+        return [self.entries[_my_normcase(n)] for n in names]
 
 class RootDir(Dir):
     """A class for the root directory of a file system.
@@ -1531,25 +2047,92 @@ class RootDir(Dir):
         # attribute) so we have to set up some values so Base.__init__()
         # won't gag won't it calls some of our methods.
         self.abspath = ''
+        self.labspath = ''
         self.path = ''
         self.tpath = ''
         self.path_elements = []
         self.duplicate = 0
+        self.root = self
         Base.__init__(self, name, self, fs)
 
         # Now set our paths to what we really want them to be: the
-        # initial drive letter (the name) plus the directory separator.
+        # initial drive letter (the name) plus the directory separator,
+        # except for the "lookup abspath," which does not have the
+        # drive letter.
         self.abspath = name + os.sep
+        self.labspath = ''
         self.path = name + os.sep
         self.tpath = name + os.sep
         self._morph()
 
+        self._lookupDict = {}
+
+        # The // and os.sep + os.sep entries are necessary because
+        # os.path.normpath() seems to preserve double slashes at the
+        # beginning of a path (presumably for UNC path names), but
+        # collapses triple slashes to a single slash.
+        self._lookupDict[''] = self
+        self._lookupDict['/'] = self
+        self._lookupDict['//'] = self
+        self._lookupDict[os.sep] = self
+        self._lookupDict[os.sep + os.sep] = self
+
+    def must_be_same(self, klass):
+        if klass is Dir:
+            return
+        Base.must_be_same(self, klass)
+
+    def _lookup_abs(self, p, klass, create=1):
+        """
+        Fast (?) lookup of a *normalized* absolute path.
+
+        This method is intended for use by internal lookups with
+        already-normalized path data.  For general-purpose lookups,
+        use the FS.Entry(), FS.Dir() or FS.File() methods.
+
+        The caller is responsible for making sure we're passed a
+        normalized absolute path; we merely let Python's dictionary look
+        up and return the One True Node.FS object for the path.
+
+        If no Node for the specified "p" doesn't already exist, and
+        "create" is specified, the Node may be created after recursive
+        invocation to find or create the parent directory or directories.
+        """
+        k = _my_normcase(p)
+        try:
+            result = self._lookupDict[k]
+        except KeyError:
+            if not create:
+                msg = "No such file or directory: '%s' in '%s' (and create is False)" % (p, str(self))
+                raise SCons.Errors.UserError(msg)
+            # There is no Node for this path name, and we're allowed
+            # to create it.
+            dir_name, file_name = os.path.split(p)
+            dir_node = self._lookup_abs(dir_name, Dir)
+            result = klass(file_name, dir_node, self.fs)
+
+            # Double-check on disk (as configured) that the Node we
+            # created matches whatever is out there in the real world.
+            result.diskcheck_match()
+
+            self._lookupDict[k] = result
+            dir_node.entries[_my_normcase(file_name)] = result
+            dir_node.implicit = None
+        else:
+            # There is already a Node for this path name.  Allow it to
+            # complain if we were looking for an inappropriate type.
+            result.must_be_same(klass)
+        return result
+
     def __str__(self):
         return self.abspath
 
     def entry_abspath(self, name):
         return self.abspath + name
 
+    def entry_labspath(self, name):
+        return '/' + name
+
     def entry_path(self, name):
         return self.path + name
 
@@ -1571,61 +2154,112 @@ class RootDir(Dir):
     def src_builder(self):
         return _null
 
-class NodeInfo(SCons.Node.NodeInfo):
-    def __cmp__(self, other):
-        try: return cmp(self.bsig, other.bsig)
-        except AttributeError: return 1
-    def update(self, node):
-        self.timestamp = node.get_timestamp()
-        self.size = node.getsize()
-
-class BuildInfo(SCons.Node.BuildInfo):
-    def __init__(self, node):
-        SCons.Node.BuildInfo.__init__(self, node)
-        self.node = node
+class FileNodeInfo(SCons.Node.NodeInfoBase):
+    current_version_id = 1
+
+    field_list = ['csig', 'timestamp', 'size']
+
+    # This should get reset by the FS initialization.
+    fs = None
+
+    def str_to_node(self, s):
+        top = self.fs.Top
+        root = top.root
+        if do_splitdrive:
+            drive, s = os.path.splitdrive(s)
+            if drive:
+                root = self.fs.get_root(drive)
+        if not os.path.isabs(s):
+            s = top.labspath + '/' + s
+        return root._lookup_abs(s, Entry)
+
+class FileBuildInfo(SCons.Node.BuildInfoBase):
+    current_version_id = 1
+
     def convert_to_sconsign(self):
-        """Convert this BuildInfo object for writing to a .sconsign file
+        """
+        Converts this FileBuildInfo object for writing to a .sconsign file
 
-        We hung onto the node that we refer to so that we can translate
-        the lists of bsources, bdepends and bimplicit Nodes into strings
-        relative to the node, but we don't want to write out that Node
-        itself to the .sconsign file, so we delete the attribute in
-        preparation.
+        This replaces each Node in our various dependency lists with its
+        usual string representation: relative to the top-level SConstruct
+        directory, or an absolute path if it's outside.
         """
-        rel_path = self.node.rel_path
-        delattr(self, 'node')
+        if os.sep == '/':
+            node_to_str = str
+        else:
+            def node_to_str(n):
+                try:
+                    s = n.path
+                except AttributeError:
+                    s = str(n)
+                else:
+                    s = s.replace(os.sep, '/')
+                return s
         for attr in ['bsources', 'bdepends', 'bimplicit']:
             try:
                 val = getattr(self, attr)
             except AttributeError:
                 pass
             else:
-                setattr(self, attr, map(rel_path, val))
+                setattr(self, attr, list(map(node_to_str, val)))
     def convert_from_sconsign(self, dir, name):
-        """Convert a newly-read BuildInfo object for in-SCons use
+        """
+        Converts a newly-read FileBuildInfo object for in-SCons use
 
-        An on-disk BuildInfo comes without a reference to the node
-        for which it's intended, so we have to convert the arguments
-        and add back a self.node attribute.  The bsources, bdepends and
-        bimplicit lists all come from disk as paths relative to that node,
-        so convert them to actual Nodes for use by the rest of SCons.
+        For normal up-to-date checking, we don't have any conversion to
+        perform--but we're leaving this method here to make that clear.
         """
-        self.node = dir.Entry(name)
-        Entry_func = self.node.dir.Entry
-        for attr in ['bsources', 'bdepends', 'bimplicit']:
+        pass
+    def prepare_dependencies(self):
+        """
+        Prepares a FileBuildInfo object for explaining what changed
+
+        The bsources, bdepends and bimplicit lists have all been
+        stored on disk as paths relative to the top-level SConstruct
+        directory.  Convert the strings to actual Nodes (for use by the
+        --debug=explain code and --implicit-cache).
+        """
+        attrs = [
+            ('bsources', 'bsourcesigs'),
+            ('bdepends', 'bdependsigs'),
+            ('bimplicit', 'bimplicitsigs'),
+        ]
+        for (nattr, sattr) in attrs:
             try:
-                val = getattr(self, attr)
+                strings = getattr(self, nattr)
+                nodeinfos = getattr(self, sattr)
             except AttributeError:
-                pass
-            else:
-                setattr(self, attr, map(Entry_func, val))
+                continue
+            nodes = []
+            for s, ni in izip(strings, nodeinfos):
+                if not isinstance(s, SCons.Node.Node):
+                    s = ni.str_to_node(s)
+                nodes.append(s)
+            setattr(self, nattr, nodes)
+    def format(self, names=0):
+        result = []
+        bkids = self.bsources + self.bdepends + self.bimplicit
+        bkidsigs = self.bsourcesigs + self.bdependsigs + self.bimplicitsigs
+        for bkid, bkidsig in izip(bkids, bkidsigs):
+            result.append(str(bkid) + ': ' +
+                          ' '.join(bkidsig.format(names=names)))
+        result.append('%s [%s]' % (self.bactsig, self.bact))
+        return '\n'.join(result)
 
 class File(Base):
     """A class for files in a file system.
     """
+
+    memoizer_counters = []
+
+    NodeInfo = FileNodeInfo
+    BuildInfo = FileBuildInfo
+
+    md5_chunksize = 64
+
     def diskcheck_match(self):
-        diskcheck_match(self, self.fs.isdir,
-                           "Directory %s found where file expected.")
+        diskcheck_match(self, self.isdir,
+                        "Directory %s found where file expected.")
 
     def __init__(self, name, directory, fs):
         if __debug__: logInstanceCreation(self, 'Node.FS.File')
@@ -1634,27 +2268,25 @@ class File(Base):
 
     def Entry(self, name):
         """Create an entry node named 'name' relative to
-        the SConscript directory of this file."""
-        return self.fs.Entry(name, self.cwd)
+        the directory of this file."""
+        return self.dir.Entry(name)
 
-    def Dir(self, name):
+    def Dir(self, name, create=True):
         """Create a directory node named 'name' relative to
-        the SConscript directory of this file."""
-        return self.fs.Dir(name, self.cwd)
+        the directory of this file."""
+        return self.dir.Dir(name, create=create)
 
     def Dirs(self, pathlist):
         """Create a list of directories relative to the SConscript
         directory of this file."""
-        return map(lambda p, s=self: s.Dir(p), pathlist)
+        # TODO(1.5)
+        # return [self.Dir(p) for p in pathlist]
+        return [self.Dir(p) for p in pathlist]
 
     def File(self, name):
         """Create a file node named 'name' relative to
-        the SConscript directory of this file."""
-        return self.fs.File(name, self.cwd)
-
-    def RDirs(self, pathlist):
-        """Search for a list of directories in the Repository list."""
-        return self.fs.Rfindalldirs(pathlist, self.cwd)
+        the directory of this file."""
+        return self.dir.File(name)
 
     #def generate_build_dict(self):
     #    """Return an appropriate dictionary of values for building
@@ -1664,13 +2296,23 @@ class File(Base):
     #            'RDirs' : self.RDirs}
 
     def _morph(self):
-        """Turn a file system node into a File object.  __cache_reset__"""
+        """Turn a file system node into a File object."""
         self.scanner_paths = {}
         if not hasattr(self, '_local'):
             self._local = 0
 
-    def disambiguate(self):
-        return self
+        # If there was already a Builder set on this entry, then
+        # we need to make sure we call the target-decider function,
+        # not the source-decider.  Reaching in and doing this by hand
+        # is a little bogus.  We'd prefer to handle this by adding
+        # an Entry.builder_set() method that disambiguates like the
+        # other methods, but that starts running into problems with the
+        # fragile way we initialize Dir Nodes with their Mkdir builders,
+        # yet still allow them to be overridden by the user.  Since it's
+        # not clear right now how to fix that, stick with what works
+        # until it becomes clear...
+        if self.has_builder():
+            self.changed_since_last_build = self.decide_target
 
     def scanner_key(self):
         return self.get_suffix()
@@ -1678,64 +2320,311 @@ class File(Base):
     def get_contents(self):
         if not self.rexists():
             return ''
-        return open(self.rfile().abspath, "rb").read()
+        fname = self.rfile().abspath
+        try:
+            contents = open(fname, "rb").read()
+        except EnvironmentError, e:
+            if not e.filename:
+                e.filename = fname
+            raise
+        return contents
+
+    try:
+        import codecs
+    except ImportError:
+        get_text_contents = get_contents
+    else:
+        # This attempts to figure out what the encoding of the text is
+        # based upon the BOM bytes, and then decodes the contents so that
+        # it's a valid python string.
+        def get_text_contents(self):
+            contents = self.get_contents()
+            # The behavior of various decode() methods and functions
+            # w.r.t. the initial BOM bytes is different for different
+            # encodings and/or Python versions.  ('utf-8' does not strip
+            # them, but has a 'utf-8-sig' which does; 'utf-16' seems to
+            # strip them; etc.)  Just side step all the complication by
+            # explicitly stripping the BOM before we decode().
+            if contents.startswith(codecs.BOM_UTF8):
+                contents = contents[len(codecs.BOM_UTF8):]
+                # TODO(2.2):  Remove when 2.3 becomes floor.
+                #contents = contents.decode('utf-8')
+                contents = my_decode(contents, 'utf-8')
+            elif contents.startswith(codecs.BOM_UTF16_LE):
+                contents = contents[len(codecs.BOM_UTF16_LE):]
+                # TODO(2.2):  Remove when 2.3 becomes floor.
+                #contents = contents.decode('utf-16-le')
+                contents = my_decode(contents, 'utf-16-le')
+            elif contents.startswith(codecs.BOM_UTF16_BE):
+                contents = contents[len(codecs.BOM_UTF16_BE):]
+                # TODO(2.2):  Remove when 2.3 becomes floor.
+                #contents = contents.decode('utf-16-be')
+                contents = my_decode(contents, 'utf-16-be')
+            return contents
+
+    def get_content_hash(self):
+        """
+        Compute and return the MD5 hash for this file.
+        """
+        if not self.rexists():
+            return SCons.Util.MD5signature('')
+        fname = self.rfile().abspath
+        try:
+            cs = SCons.Util.MD5filesignature(fname,
+                chunksize=SCons.Node.FS.File.md5_chunksize*1024)
+        except EnvironmentError, e:
+            if not e.filename:
+                e.filename = fname
+            raise
+        return cs
+        
+
+    memoizer_counters.append(SCons.Memoize.CountValue('get_size'))
+
+    def get_size(self):
+        try:
+            return self._memo['get_size']
+        except KeyError:
+            pass
+
+        if self.rexists():
+            size = self.rfile().getsize()
+        else:
+            size = 0
+
+        self._memo['get_size'] = size
+
+        return size
+
+    memoizer_counters.append(SCons.Memoize.CountValue('get_timestamp'))
 
     def get_timestamp(self):
+        try:
+            return self._memo['get_timestamp']
+        except KeyError:
+            pass
+
         if self.rexists():
-            return self.rfile().getmtime()
+            timestamp = self.rfile().getmtime()
         else:
-            return 0
+            timestamp = 0
 
-    def store_info(self, obj):
+        self._memo['get_timestamp'] = timestamp
+
+        return timestamp
+
+    def store_info(self):
         # Merge our build information into the already-stored entry.
         # This accomodates "chained builds" where a file that's a target
         # in one build (SConstruct file) is a source in a different build.
         # See test/chained-build.py for the use case.
-        entry = self.get_stored_info()
-        entry.merge(obj)
-        self.dir.sconsign().set_entry(self.name, entry)
+        if do_store_info:
+            self.dir.sconsign().store_info(self.name, self)
+
+    convert_copy_attrs = [
+        'bsources',
+        'bimplicit',
+        'bdepends',
+        'bact',
+        'bactsig',
+        'ninfo',
+    ]
+
+
+    convert_sig_attrs = [
+        'bsourcesigs',
+        'bimplicitsigs',
+        'bdependsigs',
+    ]
+
+    def convert_old_entry(self, old_entry):
+        # Convert a .sconsign entry from before the Big Signature
+        # Refactoring, doing what we can to convert its information
+        # to the new .sconsign entry format.
+        #
+        # The old format looked essentially like this:
+        #
+        #   BuildInfo
+        #       .ninfo (NodeInfo)
+        #           .bsig
+        #           .csig
+        #           .timestamp
+        #           .size
+        #       .bsources
+        #       .bsourcesigs ("signature" list)
+        #       .bdepends
+        #       .bdependsigs ("signature" list)
+        #       .bimplicit
+        #       .bimplicitsigs ("signature" list)
+        #       .bact
+        #       .bactsig
+        #
+        # The new format looks like this:
+        #
+        #   .ninfo (NodeInfo)
+        #       .bsig
+        #       .csig
+        #       .timestamp
+        #       .size
+        #   .binfo (BuildInfo)
+        #       .bsources
+        #       .bsourcesigs (NodeInfo list)
+        #           .bsig
+        #           .csig
+        #           .timestamp
+        #           .size
+        #       .bdepends
+        #       .bdependsigs (NodeInfo list)
+        #           .bsig
+        #           .csig
+        #           .timestamp
+        #           .size
+        #       .bimplicit
+        #       .bimplicitsigs (NodeInfo list)
+        #           .bsig
+        #           .csig
+        #           .timestamp
+        #           .size
+        #       .bact
+        #       .bactsig
+        #
+        # The basic idea of the new structure is that a NodeInfo always
+        # holds all available information about the state of a given Node
+        # at a certain point in time.  The various .b*sigs lists can just
+        # be a list of pointers to the .ninfo attributes of the different
+        # dependent nodes, without any copying of information until it's
+        # time to pickle it for writing out to a .sconsign file.
+        #
+        # The complicating issue is that the *old* format only stored one
+        # "signature" per dependency, based on however the *last* build
+        # was configured.  We don't know from just looking at it whether
+        # it was a build signature, a content signature, or a timestamp
+        # "signature".  Since we no longer use build signatures, the
+        # best we can do is look at the length and if it's thirty two,
+        # assume that it was (or might have been) a content signature.
+        # If it was actually a build signature, then it will cause a
+        # rebuild anyway when it doesn't match the new content signature,
+        # but that's probably the best we can do.
+        import SCons.SConsign
+        new_entry = SCons.SConsign.SConsignEntry()
+        new_entry.binfo = self.new_binfo()
+        binfo = new_entry.binfo
+        for attr in self.convert_copy_attrs:
+            try:
+                value = getattr(old_entry, attr)
+            except AttributeError:
+                continue
+            setattr(binfo, attr, value)
+            delattr(old_entry, attr)
+        for attr in self.convert_sig_attrs:
+            try:
+                sig_list = getattr(old_entry, attr)
+            except AttributeError:
+                continue
+            value = []
+            for sig in sig_list:
+                ninfo = self.new_ninfo()
+                if len(sig) == 32:
+                    ninfo.csig = sig
+                else:
+                    ninfo.timestamp = sig
+                value.append(ninfo)
+            setattr(binfo, attr, value)
+            delattr(old_entry, attr)
+        return new_entry
+
+    memoizer_counters.append(SCons.Memoize.CountValue('get_stored_info'))
 
     def get_stored_info(self):
-        "__cacheable__"
         try:
-            stored = self.dir.sconsign().get_entry(self.name)
-        except (KeyError, OSError):
-            return self.new_binfo()
+            return self._memo['get_stored_info']
+        except KeyError:
+            pass
+
+        try:
+            sconsign_entry = self.dir.sconsign().get_entry(self.name)
+        except (KeyError, EnvironmentError):
+            import SCons.SConsign
+            sconsign_entry = SCons.SConsign.SConsignEntry()
+            sconsign_entry.binfo = self.new_binfo()
+            sconsign_entry.ninfo = self.new_ninfo()
         else:
-            if not hasattr(stored, 'ninfo'):
-                # Transition:  The .sconsign file entry has no NodeInfo
-                # object, which means it's a slightly older BuildInfo.
-                # Copy over the relevant attributes.
-                ninfo = stored.ninfo = self.new_ninfo()
-                for attr in ninfo.__dict__.keys():
-                    try:
-                        setattr(ninfo, attr, getattr(stored, attr))
-                    except AttributeError:
-                        pass
-            return stored
+            if isinstance(sconsign_entry, FileBuildInfo):
+                # This is a .sconsign file from before the Big Signature
+                # Refactoring; convert it as best we can.
+                sconsign_entry = self.convert_old_entry(sconsign_entry)
+            try:
+                delattr(sconsign_entry.ninfo, 'bsig')
+            except AttributeError:
+                pass
+
+        self._memo['get_stored_info'] = sconsign_entry
+
+        return sconsign_entry
 
     def get_stored_implicit(self):
-        binfo = self.get_stored_info()
+        binfo = self.get_stored_info().binfo
+        binfo.prepare_dependencies()
         try: return binfo.bimplicit
         except AttributeError: return None
 
     def rel_path(self, other):
         return self.dir.rel_path(other)
 
+    def _get_found_includes_key(self, env, scanner, path):
+        return (id(env), id(scanner), path)
+
+    memoizer_counters.append(SCons.Memoize.CountDict('get_found_includes', _get_found_includes_key))
+
     def get_found_includes(self, env, scanner, path):
         """Return the included implicit dependencies in this file.
         Cache results so we only scan the file once per path
         regardless of how many times this information is requested.
-        __cacheable__"""
-        if not scanner:
-            return []
-        return scanner(self, env, path)
+        """
+        memo_key = (id(env), id(scanner), path)
+        try:
+            memo_dict = self._memo['get_found_includes']
+        except KeyError:
+            memo_dict = {}
+            self._memo['get_found_includes'] = memo_dict
+        else:
+            try:
+                return memo_dict[memo_key]
+            except KeyError:
+                pass
+
+        if scanner:
+            # result = [n.disambiguate() for n in scanner(self, env, path)]
+            result = scanner(self, env, path)
+            result = [N.disambiguate() for N in result]
+        else:
+            result = []
+
+        memo_dict[memo_key] = result
+
+        return result
 
     def _createDir(self):
         # ensure that the directories for this node are
         # created.
         self.dir._create()
 
+    def push_to_cache(self):
+        """Try to push the node into a cache
+        """
+        # This should get called before the Nodes' .built() method is
+        # called, which would clear the build signature if the file has
+        # a source scanner.
+        #
+        # We have to clear the local memoized values *before* we push
+        # the node to cache so that the memoization of the self.exists()
+        # return value doesn't interfere.
+        if self.nocache:
+            return
+        self.clear_memoized_values()
+        if self.exists():
+            self.get_build_env().get_CacheDir().push(self)
+
     def retrieve_from_cache(self):
         """Try to retrieve the node's content from a cache
 
@@ -1743,53 +2632,56 @@ class File(Base):
         so only do thread safe stuff here. Do thread unsafe stuff in
         built().
 
-        Note that there's a special trick here with the execute flag
-        (one that's not normally done for other actions).  Basically
-        if the user requested a noexec (-n) build, then
-        SCons.Action.execute_actions is set to 0 and when any action
-        is called, it does its showing but then just returns zero
-        instead of actually calling the action execution operation.
-        The problem for caching is that if the file does NOT exist in
-        cache then the CacheRetrieveString won't return anything to
-        show for the task, but the Action.__call__ won't call
-        CacheRetrieveFunc; instead it just returns zero, which makes
-        the code below think that the file *was* successfully
-        retrieved from the cache, therefore it doesn't do any
-        subsequent building.  However, the CacheRetrieveString didn't
-        print anything because it didn't actually exist in the cache,
-        and no more build actions will be performed, so the user just
-        sees nothing.  The fix is to tell Action.__call__ to always
-        execute the CacheRetrieveFunc and then have the latter
-        explicitly check SCons.Action.execute_actions itself.
-
         Returns true iff the node was successfully retrieved.
         """
-        b = self.is_derived()
-        if not b and not self.has_src_builder():
+        if self.nocache:
             return None
-        if b and self.fs.CachePath:
-            if self.fs.cache_show:
-                if CacheRetrieveSilent(self, [], None, execute=1) == 0:
-                    self.build(presub=0, execute=0)
-                    return 1
-            elif CacheRetrieve(self, [], None, execute=1) == 0:
-                return 1
-        return None
-
-    def built(self):
-        """Called just after this node is successfully built.
-        __cache_reset__"""
-        # Push this file out to cache before the superclass Node.built()
-        # method has a chance to clear the build signature, which it
-        # will do if this file has a source scanner.
-        if self.fs.CachePath and self.exists():
-            CachePush(self, [], None)
-        self.fs.clear_cache()
-        SCons.Node.Node.built(self)
+        if not self.is_derived():
+            return None
+        return self.get_build_env().get_CacheDir().retrieve(self)
 
     def visited(self):
-        if self.fs.CachePath and self.fs.cache_force and self.exists():
-            CachePush(self, None, None)
+        if self.exists():
+            self.get_build_env().get_CacheDir().push_if_forced(self)
+
+        ninfo = self.get_ninfo()
+
+        csig = self.get_max_drift_csig()
+        if csig:
+            ninfo.csig = csig
+
+        ninfo.timestamp = self.get_timestamp()
+        ninfo.size      = self.get_size()
+
+        if not self.has_builder():
+            # This is a source file, but it might have been a target file
+            # in another build that included more of the DAG.  Copy
+            # any build information that's stored in the .sconsign file
+            # into our binfo object so it doesn't get lost.
+            old = self.get_stored_info()
+            self.get_binfo().__dict__.update(old.binfo.__dict__)
+
+        self.store_info()
+
+    def find_src_builder(self):
+        if self.rexists():
+            return None
+        scb = self.dir.src_builder()
+        if scb is _null:
+            if diskcheck_sccs(self.dir, self.name):
+                scb = get_DefaultSCCSBuilder()
+            elif diskcheck_rcs(self.dir, self.name):
+                scb = get_DefaultRCSBuilder()
+            else:
+                scb = None
+        if scb is not None:
+            try:
+                b = self.builder
+            except AttributeError:
+                b = None
+            if b is None:
+                self.builder_set(scb)
+        return scb
 
     def has_src_builder(self):
         """Return whether this Node has a source builder or not.
@@ -1805,37 +2697,30 @@ class File(Base):
         try:
             scb = self.sbuilder
         except AttributeError:
-            if self.rexists():
-                scb = None
-            else:
-                scb = self.dir.src_builder()
-                if scb is _null:
-                    if diskcheck_sccs(self.dir, self.name):
-                        scb = get_DefaultSCCSBuilder()
-                    elif diskcheck_rcs(self.dir, self.name):
-                        scb = get_DefaultRCSBuilder()
-                    else:
-                        scb = None
-                if scb is not None:
-                    self.builder_set(scb)
-            self.sbuilder = scb
-        return not scb is None
+            scb = self.sbuilder = self.find_src_builder()
+        return scb is not None
 
     def alter_targets(self):
-        """Return any corresponding targets in a build directory.
+        """Return any corresponding targets in a variant directory.
         """
         if self.is_derived():
             return [], None
-        return self.fs.build_dir_target_climb(self, self.dir, [self.name])
-
-    def is_pseudo_derived(self):
-        "__cacheable__"
-        return self.has_src_builder()
+        return self.fs.variant_dir_target_climb(self, self.dir, [self.name])
 
     def _rmv_existing(self):
-        '__cache_reset__'
-        Unlink(self, [], None)
-        
+        self.clear_memoized_values()
+        e = Unlink(self, [], None)
+        if isinstance(e, SCons.Errors.BuildError):
+            raise e
+
+    #
+    # Taskmaster interface subsystem
+    #
+
+    def make_ready(self):
+        self.has_src_builder()
+        self.get_binfo()
+
     def prepare(self):
         """Prepare for this file to be created."""
         SCons.Node.Node.prepare(self)
@@ -1849,7 +2734,11 @@ class File(Base):
                     self._createDir()
                 except SCons.Errors.StopError, drive:
                     desc = "No drive `%s' for target `%s'." % (drive, self)
-                    raise SCons.Errors.StopError, desc
+                    raise SCons.Errors.StopError(desc)
+
+    #
+    #
+    #
 
     def remove(self):
         """Remove this file."""
@@ -1860,15 +2749,11 @@ class File(Base):
 
     def do_duplicate(self, src):
         self._createDir()
-        try:
-            Unlink(self, None, None)
-        except SCons.Errors.BuildError:
-            pass
-        try:
-            Link(self, src, None)
-        except SCons.Errors.BuildError, e:
+        Unlink(self, None, None)
+        e = Link(self, src, None)
+        if isinstance(e, SCons.Errors.BuildError):
             desc = "Cannot duplicate `%s' in `%s': %s." % (src.path, self.dir.path, e.errstr)
-            raise SCons.Errors.StopError, desc
+            raise SCons.Errors.StopError(desc)
         self.linked = 1
         # The Link() action may or may not have actually
         # created the file, depending on whether the -n
@@ -1876,43 +2761,68 @@ class File(Base):
         # _rexists attributes so they can be reevaluated.
         self.clear()
 
+    memoizer_counters.append(SCons.Memoize.CountValue('exists'))
+
     def exists(self):
-        "__cacheable__"
+        try:
+            return self._memo['exists']
+        except KeyError:
+            pass
         # Duplicate from source path if we are set up to do this.
         if self.duplicate and not self.is_derived() and not self.linked:
             src = self.srcnode()
-            if src is self:
-                return Base.exists(self)
-            # At this point, src is meant to be copied in a build directory.
-            src = src.rfile()
-            if src.abspath != self.abspath:
-                if src.exists():
-                    self.do_duplicate(src)
-                    # Can't return 1 here because the duplication might
-                    # not actually occur if the -n option is being used.
-                else:
-                    # The source file does not exist.  Make sure no old
-                    # copy remains in the build directory.
-                    if Base.exists(self) or self.islink():
-                        self.fs.unlink(self.path)
-                    # Return None explicitly because the Base.exists() call
-                    # above will have cached its value if the file existed.
-                    return None
-        return Base.exists(self)
+            if src is not self:
+                # At this point, src is meant to be copied in a variant directory.
+                src = src.rfile()
+                if src.abspath != self.abspath:
+                    if src.exists():
+                        self.do_duplicate(src)
+                        # Can't return 1 here because the duplication might
+                        # not actually occur if the -n option is being used.
+                    else:
+                        # The source file does not exist.  Make sure no old
+                        # copy remains in the variant directory.
+                        if Base.exists(self) or self.islink():
+                            self.fs.unlink(self.path)
+                        # Return None explicitly because the Base.exists() call
+                        # above will have cached its value if the file existed.
+                        self._memo['exists'] = None
+                        return None
+        result = Base.exists(self)
+        self._memo['exists'] = result
+        return result
 
     #
     # SIGNATURE SUBSYSTEM
     #
 
-    def new_binfo(self):
-        return BuildInfo(self)
+    def get_max_drift_csig(self):
+        """
+        Returns the content signature currently stored for this node
+        if it's been unmodified longer than the max_drift value, or the
+        max_drift value is 0.  Returns None otherwise.
+        """
+        old = self.get_stored_info()
+        mtime = self.get_timestamp()
+
+        max_drift = self.fs.max_drift
+        if max_drift > 0:
+            if (time.time() - mtime) > max_drift:
+                try:
+                    n = old.ninfo
+                    if n.timestamp and n.csig and n.timestamp == mtime:
+                        return n.csig
+                except AttributeError:
+                    pass
+        elif max_drift == 0:
+            try:
+                return old.ninfo.csig
+            except AttributeError:
+                pass
 
-    def new_ninfo(self):
-        ninfo = NodeInfo()
-        ninfo.update(self)
-        return ninfo
+        return None
 
-    def get_csig(self, calc=None):
+    def get_csig(self):
         """
         Generate a node's content signature, the digested signature
         of its content.
@@ -1921,72 +2831,119 @@ class File(Base):
         cache - alternate node to use for the signature cache
         returns - the content signature
         """
+        ninfo = self.get_ninfo()
         try:
-            return self.binfo.ninfo.csig
+            return ninfo.csig
         except AttributeError:
             pass
 
-        if calc is None:
-            calc = self.calculator()
-
-        max_drift = self.fs.max_drift
-        mtime = self.get_timestamp()
-        use_stored = max_drift >= 0 and (time.time() - mtime) > max_drift
+        csig = self.get_max_drift_csig()
+        if csig is None:
 
-        csig = None
-        if use_stored:
-            old = self.get_stored_info().ninfo
             try:
-                if old.timestamp and old.csig and old.timestamp == mtime:
-                    csig = old.csig
-            except AttributeError:
-                pass
-        if csig is None:
-            csig = calc.module.signature(self)
+                if self.get_size() < SCons.Node.FS.File.md5_chunksize:
+                    contents = self.get_contents()
+                else:
+                    csig = self.get_content_hash()
+            except IOError:
+                # This can happen if there's actually a directory on-disk,
+                # which can be the case if they've disabled disk checks,
+                # or if an action with a File target actually happens to
+                # create a same-named directory by mistake.
+                csig = ''
+            else:
+                if not csig:
+                    csig = SCons.Util.MD5signature(contents)
 
-        binfo = self.get_binfo()
-        ninfo = binfo.ninfo
         ninfo.csig = csig
-        ninfo.update(self)
-
-        if use_stored:
-            self.store_info(binfo)
 
         return csig
 
     #
-    #
+    # DECISION SUBSYSTEM
     #
 
-    def current(self, calc=None):
-        self.binfo = self.gen_binfo(calc)
-        return self._cur2()
-    def _cur2(self):
-        "__cacheable__"
-        if self.always_build:
-            return None
+    def builder_set(self, builder):
+        SCons.Node.Node.builder_set(self, builder)
+        self.changed_since_last_build = self.decide_target
+
+    def changed_content(self, target, prev_ni):
+        cur_csig = self.get_csig()
+        try:
+            return cur_csig != prev_ni.csig
+        except AttributeError:
+            return 1
+
+    def changed_state(self, target, prev_ni):
+        return self.state != SCons.Node.up_to_date
+
+    def changed_timestamp_then_content(self, target, prev_ni):
+        if not self.changed_timestamp_match(target, prev_ni):
+            try:
+                self.get_ninfo().csig = prev_ni.csig
+            except AttributeError:
+                pass
+            return False
+        return self.changed_content(target, prev_ni)
+
+    def changed_timestamp_newer(self, target, prev_ni):
+        try:
+            return self.get_timestamp() > target.get_timestamp()
+        except AttributeError:
+            return 1
+
+    def changed_timestamp_match(self, target, prev_ni):
+        try:
+            return self.get_timestamp() != prev_ni.timestamp
+        except AttributeError:
+            return 1
+
+    def decide_source(self, target, prev_ni):
+        return target.get_build_env().decide_source(self, target, prev_ni)
+
+    def decide_target(self, target, prev_ni):
+        return target.get_build_env().decide_target(self, target, prev_ni)
+
+    # Initialize this Node's decider function to decide_source() because
+    # every file is a source file until it has a Builder attached...
+    changed_since_last_build = decide_source
+
+    def is_up_to_date(self):
+        T = 0
+        if T: Trace('is_up_to_date(%s):' % self)
         if not self.exists():
+            if T: Trace(' not self.exists():')
             # The file doesn't exist locally...
             r = self.rfile()
             if r != self:
                 # ...but there is one in a Repository...
-                old = r.get_stored_info()
-                new = self.get_binfo()
-                if new == old:
+                if not self.changed(r):
+                    if T: Trace(' changed(%s):' % r)
                     # ...and it's even up-to-date...
                     if self._local:
                         # ...and they'd like a local copy.
-                        LocalCopy(self, r, None)
-                        self.store_info(new)
+                        e = LocalCopy(self, r, None)
+                        if isinstance(e, SCons.Errors.BuildError):
+                            raise 
+                        self.store_info()
+                    if T: Trace(' 1\n')
                     return 1
+            self.changed()
+            if T: Trace(' None\n')
             return None
         else:
-            old = self.get_stored_info()
-            new = self.get_binfo()
-            return (new == old)
+            r = self.changed()
+            if T: Trace(' self.exists():  %s\n' % r)
+            return not r
+
+    memoizer_counters.append(SCons.Memoize.CountValue('rfile'))
 
     def rfile(self):
-        "__cacheable__"
+        try:
+            return self._memo['rfile']
+        except KeyError:
+            pass
+        result = self
         if not self.exists():
             norm_name = _my_normcase(self.name)
             for dir in self.dir.get_all_rdirs():
@@ -1995,104 +2952,262 @@ class File(Base):
                 if node and node.exists() and \
                    (isinstance(node, File) or isinstance(node, Entry) \
                     or not node.is_derived()):
-                        return node
-        return self
+                        result = node
+                        # Copy over our local attributes to the repository
+                        # Node so we identify shared object files in the
+                        # repository and don't assume they're static.
+                        #
+                        # This isn't perfect; the attribute would ideally
+                        # be attached to the object in the repository in
+                        # case it was built statically in the repository
+                        # and we changed it to shared locally, but that's
+                        # rarely the case and would only occur if you
+                        # intentionally used the same suffix for both
+                        # shared and static objects anyway.  So this
+                        # should work well in practice.
+                        result.attributes = self.attributes
+                        break
+        self._memo['rfile'] = result
+        return result
 
     def rstr(self):
         return str(self.rfile())
 
-    def cachepath(self):
-        if not self.fs.CachePath:
-            return None, None
-        ninfo = self.get_binfo().ninfo
-        if not hasattr(ninfo, 'bsig'):
-            raise SCons.Errors.InternalError, "cachepath(%s) found no bsig" % self.path
-        elif ninfo.bsig is None:
-            raise SCons.Errors.InternalError, "cachepath(%s) found a bsig of None" % self.path
+    def get_cachedir_csig(self):
+        """
+        Fetch a Node's content signature for purposes of computing
+        another Node's cachesig.
+
+        This is a wrapper around the normal get_csig() method that handles
+        the somewhat obscure case of using CacheDir with the -n option.
+        Any files that don't exist would normally be "built" by fetching
+        them from the cache, but the normal get_csig() method will try
+        to open up the local file, which doesn't exist because the -n
+        option meant we didn't actually pull the file from cachedir.
+        But since the file *does* actually exist in the cachedir, we
+        can use its contents for the csig.
+        """
+        try:
+            return self.cachedir_csig
+        except AttributeError:
+            pass
+
+        cachedir, cachefile = self.get_build_env().get_CacheDir().cachepath(self)
+        if not self.exists() and cachefile and os.path.exists(cachefile):
+            self.cachedir_csig = SCons.Util.MD5filesignature(cachefile, \
+                SCons.Node.FS.File.md5_chunksize * 1024)
+        else:
+            self.cachedir_csig = self.get_csig()
+        return self.cachedir_csig
+
+    def get_cachedir_bsig(self):
+        try:
+            return self.cachesig
+        except AttributeError:
+            pass
+
         # Add the path to the cache signature, because multiple
         # targets built by the same action will all have the same
         # build signature, and we have to differentiate them somehow.
-        cache_sig = SCons.Sig.MD5.collect([ninfo.bsig, self.path])
-        subdir = string.upper(cache_sig[0])
-        dir = os.path.join(self.fs.CachePath, subdir)
-        return dir, os.path.join(dir, cache_sig)
-
-    def target_from_source(self, prefix, suffix, splitext=SCons.Util.splitext):
-        return self.dir.File(prefix + splitext(self.name)[0] + suffix)
+        children = self.children()
+        executor = self.get_executor()
+        # sigs = [n.get_cachedir_csig() for n in children]
+        sigs = [n.get_cachedir_csig() for n in children]
+        sigs.append(SCons.Util.MD5signature(executor.get_contents()))
+        sigs.append(self.path)
+        result = self.cachesig = SCons.Util.MD5collect(sigs)
+        return result
 
-    def must_be_a_Dir(self):
-        """Called to make sure a Node is a Dir.  Since we're already a
-        File, this is a TypeError..."""
-        raise TypeError, "Tried to lookup File '%s' as a Dir." % self.path
 
 default_fs = None
 
-def find_file(filename, paths, verbose=None):
+def get_default_fs():
+    global default_fs
+    if not default_fs:
+        default_fs = FS()
+    return default_fs
+
+class FileFinder:
     """
-    find_file(str, [Dir()]) -> [nodes]
+    """
+    if SCons.Memoize.use_memoizer:
+        __metaclass__ = SCons.Memoize.Memoized_Metaclass
 
-    filename - a filename to find
-    paths - a list of directory path *nodes* to search in.  Can be
-            represented as a list, a tuple, or a callable that is
-            called with no arguments and returns the list or tuple.
+    memoizer_counters = []
+
+    def __init__(self):
+        self._memo = {}
+
+    def filedir_lookup(self, p, fd=None):
+        """
+        A helper method for find_file() that looks up a directory for
+        a file we're trying to find.  This only creates the Dir Node if
+        it exists on-disk, since if the directory doesn't exist we know
+        we won't find any files in it...  :-)
+
+        It would be more compact to just use this as a nested function
+        with a default keyword argument (see the commented-out version
+        below), but that doesn't work unless you have nested scopes,
+        so we define it here just so this work under Python 1.5.2.
+        """
+        if fd is None:
+            fd = self.default_filedir
+        dir, name = os.path.split(fd)
+        drive, d = os.path.splitdrive(dir)
+        if not name and d[:1] in ('/', os.sep):
+            #return p.fs.get_root(drive).dir_on_disk(name)
+            return p.fs.get_root(drive)
+        if dir:
+            p = self.filedir_lookup(p, dir)
+            if not p:
+                return None
+        norm_name = _my_normcase(name)
+        try:
+            node = p.entries[norm_name]
+        except KeyError:
+            return p.dir_on_disk(name)
+        if isinstance(node, Dir):
+            return node
+        if isinstance(node, Entry):
+            node.must_be_same(Dir)
+            return node
+        return None
 
-    returns - the node created from the found file.
+    def _find_file_key(self, filename, paths, verbose=None):
+        return (filename, paths)
+        
+    memoizer_counters.append(SCons.Memoize.CountDict('find_file', _find_file_key))
 
-    Find a node corresponding to either a derived file or a file
-    that exists already.
+    def find_file(self, filename, paths, verbose=None):
+        """
+        find_file(str, [Dir()]) -> [nodes]
 
-    Only the first file found is returned, and none is returned
-    if no file is found.
-    __cacheable__
-    """
-    if verbose:
-        if not SCons.Util.is_String(verbose):
-            verbose = "find_file"
-        if not callable(verbose):
-            verbose = '  %s: ' % verbose
-            verbose = lambda s, v=verbose: sys.stdout.write(v + s)
-    else:
-        verbose = lambda x: x
+        filename - a filename to find
+        paths - a list of directory path *nodes* to search in.  Can be
+                represented as a list, a tuple, or a callable that is
+                called with no arguments and returns the list or tuple.
 
-    if callable(paths):
-        paths = paths()
+        returns - the node created from the found file.
 
-    # Give Entries a chance to morph into Dirs.
-    paths = map(lambda p: p.must_be_a_Dir(), paths)
+        Find a node corresponding to either a derived file or a file
+        that exists already.
 
-    filedir, filename = os.path.split(filename)
-    if filedir:
-        def filedir_lookup(p, fd=filedir):
+        Only the first file found is returned, and none is returned
+        if no file is found.
+        """
+        memo_key = self._find_file_key(filename, paths)
+        try:
+            memo_dict = self._memo['find_file']
+        except KeyError:
+            memo_dict = {}
+            self._memo['find_file'] = memo_dict
+        else:
             try:
-                return p.Dir(fd)
-            except TypeError:
-                # We tried to look up a Dir, but it seems there's already
-                # a File (or something else) there.  No big.
-                return None
-        paths = filter(None, map(filedir_lookup, paths))
+                return memo_dict[memo_key]
+            except KeyError:
+                pass
 
-    for dir in paths:
-        verbose("looking for '%s' in '%s' ...\n" % (filename, dir))
-        node, d = dir.srcdir_find_file(filename)
-        if node:
-            verbose("... FOUND '%s' in '%s'\n" % (filename, d))
-            return node
-    return None
+        if verbose and not callable(verbose):
+            if not SCons.Util.is_String(verbose):
+                verbose = "find_file"
+            _verbose = u'  %s: ' % verbose
+            verbose = lambda s: sys.stdout.write(_verbose + s)
 
-def find_files(filenames, paths):
-    """
-    find_files([str], [Dir()]) -> [nodes]
+        filedir, filename = os.path.split(filename)
+        if filedir:
+            # More compact code that we can't use until we drop
+            # support for Python 1.5.2:
+            #
+            #def filedir_lookup(p, fd=filedir):
+            #    """
+            #    A helper function that looks up a directory for a file
+            #    we're trying to find.  This only creates the Dir Node
+            #    if it exists on-disk, since if the directory doesn't
+            #    exist we know we won't find any files in it...  :-)
+            #    """
+            #    dir, name = os.path.split(fd)
+            #    if dir:
+            #        p = filedir_lookup(p, dir)
+            #        if not p:
+            #            return None
+            #    norm_name = _my_normcase(name)
+            #    try:
+            #        node = p.entries[norm_name]
+            #    except KeyError:
+            #        return p.dir_on_disk(name)
+            #    if isinstance(node, Dir):
+            #        return node
+            #    if isinstance(node, Entry):
+            #        node.must_be_same(Dir)
+            #        return node
+            #    if isinstance(node, Dir) or isinstance(node, Entry):
+            #        return node
+            #    return None
+            #paths = filter(None, map(filedir_lookup, paths))
+
+            self.default_filedir = filedir
+            paths = [_f for _f in map(self.filedir_lookup, paths) if _f]
+
+        result = None
+        for dir in paths:
+            if verbose:
+                verbose("looking for '%s' in '%s' ...\n" % (filename, dir))
+            node, d = dir.srcdir_find_file(filename)
+            if node:
+                if verbose:
+                    verbose("... FOUND '%s' in '%s'\n" % (filename, d))
+                result = node
+                break
 
-    filenames - a list of filenames to find
-    paths - a list of directory path *nodes* to search in
+        memo_dict[memo_key] = result
 
-    returns - the nodes created from the found files.
+        return result
+
+find_file = FileFinder().find_file
 
-    Finds nodes corresponding to either derived files or files
-    that exist already.
 
-    Only the first file found is returned for each filename,
-    and any files that aren't found are ignored.
+def invalidate_node_memos(targets):
+    """
+    Invalidate the memoized values of all Nodes (files or directories)
+    that are associated with the given entries. Has been added to
+    clear the cache of nodes affected by a direct execution of an
+    action (e.g.  Delete/Copy/Chmod). Existing Node caches become
+    inconsistent if the action is run through Execute().  The argument
+    `targets` can be a single Node object or filename, or a sequence
+    of Nodes/filenames.
     """
-    nodes = map(lambda x, paths=paths: find_file(x, paths), filenames)
-    return filter(None, nodes)
+    from traceback import extract_stack
+
+    # First check if the cache really needs to be flushed. Only
+    # actions run in the SConscript with Execute() seem to be
+    # affected. XXX The way to check if Execute() is in the stacktrace
+    # is a very dirty hack and should be replaced by a more sensible
+    # solution.
+    for f in extract_stack():
+        if f[2] == 'Execute' and f[0][-14:] == 'Environment.py':
+            break
+    else:
+        # Dont have to invalidate, so return
+        return
+
+    if not SCons.Util.is_List(targets):
+        targets = [targets]
+    
+    for entry in targets:
+        # If the target is a Node object, clear the cache. If it is a
+        # filename, look up potentially existing Node object first.
+        try:
+            entry.clear_memoized_values()
+        except AttributeError:
+            # Not a Node object, try to look up Node by filename.  XXX
+            # This creates Node objects even for those filenames which
+            # do not correspond to an existing Node object.
+            node = get_default_fs().Entry(entry)
+            if node:
+                node.clear_memoized_values()                        
+
+# Local Variables:
+# tab-width:4
+# indent-tabs-mode:nil
+# End:
+# vim: set expandtab tabstop=4 shiftwidth=4: