Refactor the Scanner interface to eliminate unnecessary scanning and make it easier...
authorstevenknight <stevenknight@fdb21ef1-2011-0410-befe-b5e4ea1792b1>
Mon, 6 Jan 2003 18:42:37 +0000 (18:42 +0000)
committerstevenknight <stevenknight@fdb21ef1-2011-0410-befe-b5e4ea1792b1>
Mon, 6 Jan 2003 18:42:37 +0000 (18:42 +0000)
git-svn-id: http://scons.tigris.org/svn/scons/trunk@536 fdb21ef1-2011-0410-befe-b5e4ea1792b1

17 files changed:
doc/man/scons.1
src/CHANGES.txt
src/RELEASE.txt
src/engine/SCons/Defaults.py
src/engine/SCons/EnvironmentTests.py
src/engine/SCons/Node/FS.py
src/engine/SCons/Node/FSTests.py
src/engine/SCons/Node/__init__.py
src/engine/SCons/Scanner/C.py
src/engine/SCons/Scanner/CTests.py
src/engine/SCons/Scanner/Fortran.py
src/engine/SCons/Scanner/FortranTests.py
src/engine/SCons/Scanner/Prog.py
src/engine/SCons/Scanner/ProgTests.py
src/engine/SCons/Scanner/ScannerTests.py
src/engine/SCons/Scanner/__init__.py
test/scan-once.py

index 33718bd0f303df7620eacc86abe6ce46e7157308..fb5af362162636a074672ccf6f0fd698c217e1aa 100644 (file)
@@ -3144,24 +3144,6 @@ objects to scan
 new file types for implicit dependencies.
 Scanner accepts the following arguments:
 
-.IP name
-The name of the Scanner.
-This is mainly used
-to identify the Scanner internally.
-
-.IP argument
-An optional argument that, if specified,
-will be passed to the scanner function.
-
-.IP skeys
-An optional list that can be used to
-determine which scanner should be used for
-a given Node.
-In the usual case of scanning for file names,
-this array can be a list of suffixes
-for the different file types that this
-Scanner knows how to scan.
-
 .IP function
 A Python function that will process
 the Node (file)
@@ -3170,9 +3152,9 @@ representing the implicit
 dependencies found in the contents.
 The function takes three or four arguments:
 
-    def scanner_function(node, env, target):
+    def scanner_function(node, env, path):
 
-    def scanner_function(node, env, target, arg):
+    def scanner_function(node, env, path, arg):
 
 The
 .B node
@@ -3192,15 +3174,68 @@ Fetch values from it using the
 method.
 
 The
-.B target
-argument is the internal
-SCons node representing the target file.
+.B path
+argument is a tuple (or list)
+of directories that can be searched
+for files.
+This will usually be the tuple returned by the
+.B path_function
+argument (see below).
 
 The
 .B arg
 argument is the argument supplied
 when the scanner was created, if any.
 
+.IP name
+The name of the Scanner.
+This is mainly used
+to identify the Scanner internally.
+
+.IP argument
+An optional argument that, if specified,
+will be passed to the scanner function
+(described above)
+and the path function
+(specified below).
+
+.IP skeys
+An optional list that can be used to
+determine which scanner should be used for
+a given Node.
+In the usual case of scanning for file names,
+this array will be a list of suffixes
+for the different file types that this
+Scanner knows how to scan.
+
+.IP path_function
+A Python function that takes
+two or three arguments:
+a construction environment, directory Node,
+and optional argument supplied
+when the scanner was created.
+The
+.B path_function
+returns a tuple of directories
+that can be searched for files to be returned
+by this Scanner object.
+
+.IP node_class
+The class of Node that should be returned
+by this Scanner object.
+Any strings or other objects returned
+by the scanner function
+that are not of this class
+will be run through the
+.B node_factory
+function.
+
+.IP node_factory
+A Python function that will take a string
+or other object
+and turn it into the appropriate class of Node
+to be returned by this Scanner object.
+
 .IP scan_check
 An optional Python function that takes a Node (file)
 as an argument and returns whether the
@@ -3429,7 +3464,7 @@ import re
 
 include_re = re.compile(r'^include\\s+(\\S+)$', re.M)
 
-def kfile_scan(node, env, target, arg):
+def kfile_scan(node, env, path, arg):
     contents = node.get_contents()
     includes = include_re.findall(contents)
     return includes
@@ -3595,7 +3630,7 @@ CC = 'my_cc'
 or get documentation on the options:
 
 .ES
-> scons -h
+$ scons -h
 
 CC: The C compiler.
     default: None
index 88ee9e86962e2a4f456ff6f110010a70ad37ee06..0c608da5351f9fb1c2bc47c9d26c106bf851cf71 100644 (file)
@@ -28,8 +28,8 @@ RELEASE 0.10 - XXX
   - Don't create duplicate source files in a BuildDir when the -n
     option is used.
 
-  - Fix SCons not exiting with the appropriate status on build errors
-    (and probably in other situations).
+  - Refactor the Scanner interface to eliminate unnecessary Scanner
+    calls and make it easier to write efficient scanners.
 
   - Significant performance improvement from using a more efficient
     check, throughout the code, for whether a Node has a Builder.
index 0280fedad3566a4710e30d52874d101b67c2e6d2..efb455bc47af711f75e0ff2f65f5798ef5a14ec2 100644 (file)
@@ -27,11 +27,16 @@ RELEASE 0.10 - XXX
 
   Please note the following important changes since release 0.09:
 
+    - The Scanner interface has been changed to make it easier to
+      write user-defined scanners and to eliminate unnecessary
+      scanner calls.  This will require changing your user-defined
+      SCanner definitions.  XXX
+
     - The .sconsign file format has been changed from ASCII to a pickled
       Python data structure.  This improves performance and future
       extensibility, but means that the first time you execute SCons
-      0.10 on an already-existing source tree, for every .sconsign
-      file in the tree it will report:
+      0.10 on an already-existing source tree built with SCons 0.09 or
+      earlier, SCons will report for every .sconsign file in the tree:
 
        SCons warning: Ignoring corrupt .sconsign file: xxx
 
index e50be98586478ab0fca9ec94386cb2c75d9ccb65..201669450a921ca4fd98219f30f697323f9fdd0b 100644 (file)
@@ -111,10 +111,10 @@ class SharedFlagChecker:
                 elif not self.shared and src.attributes.shared:
                     raise SCons.Errors.UserError, "Source file: %s is shared and is not compatible with static target: %s" % (src, target[0])
 
-SharedCheck = SCons.Action.Action(SharedFlagChecker(1, 0))
-StaticCheck = SCons.Action.Action(SharedFlagChecker(0, 0))
-SharedCheckSet = SCons.Action.Action(SharedFlagChecker(1, 1))
-StaticCheckSet = SCons.Action.Action(SharedFlagChecker(0, 1))
+SharedCheck = SCons.Action.Action(SharedFlagChecker(1, 0), None)
+StaticCheck = SCons.Action.Action(SharedFlagChecker(0, 0), None)
+SharedCheckSet = SCons.Action.Action(SharedFlagChecker(1, 1), None)
+StaticCheckSet = SCons.Action.Action(SharedFlagChecker(0, 1), None)
 
 CAction = SCons.Action.Action([ StaticCheckSet, "$CCCOM" ])
 ShCAction = SCons.Action.Action([ SharedCheckSet, "$SHCCCOM" ])
index ff8942473698d7fecbc1a806526cf51f84a5182b..0ec3b4e67cf3a06a05d33353e2bba268fa942572 100644 (file)
@@ -84,7 +84,7 @@ class Scanner:
         self.name = name
         self.skeys = skeys
 
-    def scan(self, filename):
+    def __call__(self, filename):
         scanned_it[filename] = 1
 
     def __cmp__(self, other):
@@ -195,20 +195,20 @@ class EnvironmentTestCase(unittest.TestCase):
 
        scanned_it = {}
        env1 = Environment(SCANNERS = s1)
-       env1.scanner1.scan(filename = 'out1')
+        env1.scanner1(filename = 'out1')
        assert scanned_it['out1']
 
        scanned_it = {}
        env2 = Environment(SCANNERS = [s1])
-       env1.scanner1.scan(filename = 'out1')
+        env1.scanner1(filename = 'out1')
        assert scanned_it['out1']
 
        scanned_it = {}
         env3 = Environment()
         env3.Replace(SCANNERS = [s1, s2])
-       env3.scanner1.scan(filename = 'out1')
-       env3.scanner2.scan(filename = 'out2')
-       env3.scanner1.scan(filename = 'out3')
+        env3.scanner1(filename = 'out1')
+        env3.scanner2(filename = 'out2')
+        env3.scanner1(filename = 'out3')
        assert scanned_it['out1']
        assert scanned_it['out2']
        assert scanned_it['out3']
index 826307b1d3fd5d3c9c64314a44f9e76c7c00ee64..aa7f973607a06327311154e479557fe2fbacfaa3 100644 (file)
@@ -790,6 +790,8 @@ class File(Entry):
     def _morph(self):
         """Turn a file system node into a File object."""
         self.linked = 0
+        self.scanner_paths = {}
+        self.found_includes = {}
         if not hasattr(self, '_local'):
             self._local = 0
 
@@ -856,11 +858,23 @@ class File(Entry):
         return self.dir.sconsign().get_implicit(self.name)
 
     def get_implicit_deps(self, env, scanner, target):
-        if scanner:
-            return scanner.scan(self, env, target)
-        else:
+        if not scanner:
             return []
-        
+
+        try:
+            path = target.scanner_paths[scanner]
+        except KeyError:
+            path = scanner.path(env, target.cwd)
+            target.scanner_paths[scanner] = path
+
+        try:
+            includes = self.found_includes[path]
+        except KeyError:
+            includes = scanner(self, env, path)
+            self.found_includes[path] = includes
+
+        return includes
+
     def scanner_key(self):
         return os.path.splitext(self.name)[1]
 
@@ -895,6 +909,7 @@ class File(Entry):
 
     def built(self):
         SCons.Node.Node.built(self)
+        self.found_includes = {}
         if hasattr(self, '_exists'):
             delattr(self, '_exists')
         if hasattr(self, '_rexists'):
index a624d9781b35d62a32314aafb19b6a301f74559b..e23178cf072b279c9858b80c505d62bb379acad2 100644 (file)
@@ -69,7 +69,9 @@ class Scanner:
         global scanner_count
         scanner_count = scanner_count + 1
         self.hash = scanner_count
-    def scan(self, node, env, target):
+    def path(self, env, target):
+        return ()
+    def __call__(self, node, env, path):
         return [node]
     def __hash__(self):
         return self.hash
@@ -669,6 +671,35 @@ class FSTestCase(unittest.TestCase):
         f1.store_implicit()
         assert f1.get_stored_implicit()[0] == os.path.join("d1", "f1")
 
+        # Test underlying scanning functionality in get_implicit_deps()
+        env = Environment()
+        f12 = fs.File("f12")
+        t1 = fs.File("t1")
+
+        deps = f12.get_implicit_deps(env, None, t1)
+        assert deps == [], deps
+
+        class MyScanner(Scanner):
+            call_count = 0
+            def __call__(self, node, env, path):
+                self.call_count = self.call_count + 1
+                return [node]
+        s = MyScanner()
+
+        deps = f12.get_implicit_deps(env, s, t1)
+        assert deps == [f12], deps
+        assert s.call_count == 1, s.call_count
+
+        deps = f12.get_implicit_deps(env, s, t1)
+        assert deps == [f12], deps
+        assert s.call_count == 1, s.call_count
+
+        f12.built()
+
+        deps = f12.get_implicit_deps(env, s, t1)
+        assert deps == [f12], deps
+        assert s.call_count == 2, s.call_count
+
         # Test building a file whose directory is not there yet...
         f1 = fs.File(test.workpath("foo/bar/baz/ack"))
         assert not f1.dir.exists()
index 3bafb9cce1bf3b0c1746b218aa4ba19252280007..16e28e22705447670a6fe40af63abc2b5e6eb69d 100644 (file)
@@ -221,15 +221,17 @@ class Node:
                     self.implicit = []
                     self.del_bsig()
 
+        build_env = self.generate_build_env()
+
         for child in self.children(scan=0):
             self._add_child(self.implicit,
-                            child.get_implicit_deps(self.generate_build_env(),
+                            child.get_implicit_deps(build_env,
                                                     child.source_scanner,
                                                     self))
 
         # scan this node itself for implicit dependencies
         self._add_child(self.implicit,
-                        self.get_implicit_deps(self.generate_build_env(),
+                        self.get_implicit_deps(build_env,
                                                self.target_scanner,
                                                self))
 
@@ -384,7 +386,23 @@ class Node:
 
     def all_children(self, scan=1):
         """Return a list of all the node's direct children."""
-        #XXX Need to remove duplicates from this
+        # The return list may contain duplicate Nodes, especially in
+        # source trees where there are a lot of repeated #includes
+        # of a tangle of .h files.  Profiling shows, however, that
+        # eliminating the duplicates with a brute-force approach that
+        # preserves the order (that is, something like:
+        #
+        #       u = []
+        #       for n in list:
+        #           if n not in u:
+        #               u.append(n)"
+        #
+        # takes more cycles than just letting the underlying methods
+        # hand back cached values if a Node's information is requested
+        # multiple times.  (Other methods of removing duplicates, like
+        # using dictionary keys, lose the order, and the only ordered
+        # dictionary patterns I found all ended up using "not in"
+        # internally anyway...)
         if scan:
             self.scan()
         if self.implicit is None:
index 6e7db584497a7dde6c30673f3c21dfbda3cd3abd..cbcf1c6a5aa4ff97f57aae5cd88c2676dacf2ce2 100644 (file)
@@ -48,10 +48,20 @@ def CScan(fs = SCons.Node.FS.default_fs):
     cs = SCons.Scanner.Recursive(scan, "CScan", fs,
                                  [".c", ".C", ".cxx", ".cpp", ".c++", ".cc",
                                   ".h", ".H", ".hxx", ".hpp", ".hh",
-                                  ".F", ".fpp", ".FPP"])
+                                  ".F", ".fpp", ".FPP"],
+                                 path_function = path)
     return cs
 
-def scan(node, env, target, fs = SCons.Node.FS.default_fs):
+def path(env, dir, fs = SCons.Node.FS.default_fs):
+    try:
+        cpppath = env['CPPPATH']
+    except KeyError:
+        return ()
+    return tuple(fs.Rsearchall(SCons.Util.mapPaths(cpppath, dir, env),
+                               clazz = SCons.Node.FS.Dir,
+                               must_exist = 0))
+
+def scan(node, env, cpppath = (), fs = SCons.Node.FS.default_fs):
     """
     scan(node, Environment) -> [node]
 
@@ -72,70 +82,54 @@ def scan(node, env, target, fs = SCons.Node.FS.default_fs):
     dependencies.
     """
 
-    # This function caches various information in node and target:
-    # target.cpppath - env['CPPPATH'] converted to nodes
-    # node.found_includes - include files found by previous call to scan, 
-    #     keyed on cpppath
-    # node.includes - the result of include_re.findall()
-
-    if not hasattr(target, 'cpppath'):
-        try:
-            target.cpppath = tuple(fs.Rsearchall(SCons.Util.mapPaths(env['CPPPATH'], target.cwd, env), clazz=SCons.Node.FS.Dir, must_exist=0))
-        except KeyError:
-            target.cpppath = ()
-
-    cpppath = target.cpppath
-
     node = node.rfile()
-    if not node.found_includes.has_key(cpppath):
-        if node.exists():
-
-            # cache the includes list in node so we only scan it once:
-            if node.includes != None:
-                includes = node.includes
-            else:
-                includes = include_re.findall(node.get_contents())
-                node.includes = includes
-
-            nodes = []
-            source_dir = node.get_dir()
-            for include in includes:
-                if include[0] == '"':
-                    n = SCons.Node.FS.find_file(include[1],
-                                                (source_dir,) + cpppath,
-                                                fs.File)
-                else:
-                    n = SCons.Node.FS.find_file(include[1],
-                                                cpppath + (source_dir,),
-                                                fs.File)
-
-                if not n is None:
-                    nodes.append(n)
-                else:
-                    SCons.Warnings.warn(SCons.Warnings.DependencyWarning,
-                                        "No dependency generated for file: %s (included from: %s) -- file not found" % (include[1], node))
-
-            # Schwartzian transform from the Python FAQ Wizard
-            def st(List, Metric):
-                def pairing(element, M = Metric):
-                    return (M(element), element)
-                def stripit(pair):
-                    return pair[1]
-                paired = map(pairing, List)
-                paired.sort()
-                return map(stripit, paired)
-    
-            def normalize(node):
-                # We don't want the order of includes to be 
-                # modified by case changes on case insensitive OSes, so
-                # normalize the case of the filename here:
-                # (see test/win32pathmadness.py for a test of this)
-                return SCons.Node.FS._my_normcase(str(node))
 
-            node.found_includes[cpppath] = st(nodes, normalize)
+    # This function caches the following information:
+    # node.includes - the result of include_re.findall()
 
+    if not node.exists():
+        return []
+
+    # cache the includes list in node so we only scan it once:
+    if node.includes != None:
+        includes = node.includes
+    else:
+        includes = include_re.findall(node.get_contents())
+        node.includes = includes
+
+    nodes = []
+    source_dir = node.get_dir()
+    for include in includes:
+        if include[0] == '"':
+            n = SCons.Node.FS.find_file(include[1],
+                                        (source_dir,) + cpppath,
+                                        fs.File)
         else:
+            n = SCons.Node.FS.find_file(include[1],
+                                        cpppath + (source_dir,),
+                                        fs.File)
 
-            node.found_includes[cpppath] = []
-
-    return node.found_includes[cpppath]
+        if not n is None:
+            nodes.append(n)
+        else:
+            SCons.Warnings.warn(SCons.Warnings.DependencyWarning,
+                                "No dependency generated for file: %s (included from: %s) -- file not found" % (include[1], node))
+
+    # Schwartzian transform from the Python FAQ Wizard
+    def st(List, Metric):
+        def pairing(element, M = Metric):
+            return (M(element), element)
+        def stripit(pair):
+            return pair[1]
+        paired = map(pairing, List)
+        paired.sort()
+        return map(stripit, paired)
+    
+    def normalize(node):
+        # We don't want the order of includes to be 
+        # modified by case changes on case insensitive OSes, so
+        # normalize the case of the filename here:
+        # (see test/win32pathmadness.py for a test of this)
+        return SCons.Node.FS._my_normcase(str(node))
+
+    return st(nodes, normalize)
index 28a5f520fd0b9db4fd5e42d30ccb9f009fc23c21..f02474cbb407de3d078acc993e3f63e2bb54f6cd 100644 (file)
@@ -159,10 +159,6 @@ test.write([ 'repository', 'src', 'ddd.h'], "\n")
 
 # define some helpers:
 
-class DummyTarget:
-    def __init__(self, cwd=None):
-        self.cwd = cwd
-
 class DummyEnvironment:
     def __init__(self, listCppPath):
         self.path = listCppPath
@@ -178,6 +174,9 @@ class DummyEnvironment:
     def subst(self, arg):
         return arg
 
+    def has_key(self, key):
+        return self.Dictionary().has_key(key)
+
     def __getitem__(self,key):
         return self.Dictionary()[key]
 
@@ -207,7 +206,8 @@ class CScannerTestCase1(unittest.TestCase):
     def runTest(self):
         env = DummyEnvironment([])
         s = SCons.Scanner.C.CScan()
-        deps = s.scan(make_node('f1.cpp'), env, DummyTarget())
+        path = s.path(env)
+        deps = s(make_node('f1.cpp'), env, path)
         headers = ['f1.h', 'f2.h', 'fi.h']
         deps_match(self, deps, map(test.workpath, headers))
 
@@ -215,7 +215,8 @@ class CScannerTestCase2(unittest.TestCase):
     def runTest(self):
         env = DummyEnvironment([test.workpath("d1")])
         s = SCons.Scanner.C.CScan()
-        deps = s.scan(make_node('f1.cpp'), env, DummyTarget())
+        path = s.path(env)
+        deps = s(make_node('f1.cpp'), env, path)
         headers = ['d1/f2.h', 'f1.h']
         deps_match(self, deps, map(test.workpath, headers))
 
@@ -223,7 +224,8 @@ class CScannerTestCase3(unittest.TestCase):
     def runTest(self):
         env = DummyEnvironment([test.workpath("d1")])
         s = SCons.Scanner.C.CScan()
-        deps = s.scan(make_node('f2.cpp'), env, DummyTarget())
+        path = s.path(env)
+        deps = s(make_node('f2.cpp'), env, path)
         headers = ['d1/d2/f1.h', 'd1/f1.h', 'f1.h']
         deps_match(self, deps, map(test.workpath, headers))
 
@@ -231,7 +233,8 @@ class CScannerTestCase4(unittest.TestCase):
     def runTest(self):
         env = DummyEnvironment([test.workpath("d1"), test.workpath("d1/d2")])
         s = SCons.Scanner.C.CScan()
-        deps = s.scan(make_node('f2.cpp'), env, DummyTarget())
+        path = s.path(env)
+        deps = s(make_node('f2.cpp'), env, path)
         headers =  ['d1/d2/f1.h', 'd1/d2/f4.h', 'd1/f1.h', 'f1.h']
         deps_match(self, deps, map(test.workpath, headers))
         
@@ -239,6 +242,7 @@ class CScannerTestCase5(unittest.TestCase):
     def runTest(self):
         env = DummyEnvironment([])
         s = SCons.Scanner.C.CScan()
+        path = s.path(env)
 
         n = make_node('f3.cpp')
         def my_rexists(s=n):
@@ -247,7 +251,7 @@ class CScannerTestCase5(unittest.TestCase):
         setattr(n, 'old_rexists', n.rexists)
         setattr(n, 'rexists', my_rexists)
 
-        deps = s.scan(n, env, DummyTarget())
+        deps = s(n, env, path)
 
         # Make sure rexists() got called on the file node being
         # scanned, essential for cooperation with BuildDir functionality.
@@ -261,10 +265,11 @@ class CScannerTestCase6(unittest.TestCase):
     def runTest(self):
         env1 = DummyEnvironment([test.workpath("d1")])
         env2 = DummyEnvironment([test.workpath("d1/d2")])
-        env3 = DummyEnvironment([test.workpath("d1/../d1")])
         s = SCons.Scanner.C.CScan()
-        deps1 = s.scan(make_node('f1.cpp'), env1, DummyTarget())
-        deps2 = s.scan(make_node('f1.cpp'), env2, DummyTarget())
+        path1 = s.path(env1)
+        path2 = s.path(env2)
+        deps1 = s(make_node('f1.cpp'), env1, path1)
+        deps2 = s(make_node('f1.cpp'), env2, path2)
         headers1 =  ['d1/f2.h', 'f1.h']
         headers2 =  ['d1/d2/f2.h', 'f1.h']
         deps_match(self, deps1, map(test.workpath, headers1))
@@ -275,11 +280,13 @@ class CScannerTestCase8(unittest.TestCase):
         fs = SCons.Node.FS.FS(test.workpath(''))
         env = DummyEnvironment(["include"])
         s = SCons.Scanner.C.CScan(fs = fs)
-        deps1 = s.scan(fs.File('fa.cpp'), env, DummyTarget())
+        path = s.path(env)
+        deps1 = s(fs.File('fa.cpp'), env, path)
         fs.chdir(fs.Dir('subdir'))
-        target = DummyTarget(fs.getcwd())
+        dir = fs.getcwd()
         fs.chdir(fs.Dir('..'))
-        deps2 = s.scan(fs.File('#fa.cpp'), env, target)
+        path = s.path(env, dir)
+        deps2 = s(fs.File('#fa.cpp'), env, path)
         headers1 =  ['include/fa.h', 'include/fb.h']
         headers2 =  ['subdir/include/fa.h', 'subdir/include/fb.h']
         deps_match(self, deps1, headers1)
@@ -297,9 +304,10 @@ class CScannerTestCase9(unittest.TestCase):
         SCons.Warnings._warningOut = to
         test.write('fa.h','\n')
         fs = SCons.Node.FS.FS(test.workpath(''))
-        s = SCons.Scanner.C.CScan(fs=fs)
         env = DummyEnvironment([])
-        deps = s.scan(fs.File('fa.cpp'), env, DummyTarget())
+        s = SCons.Scanner.C.CScan(fs=fs)
+        path = s.path(env)
+        deps = s(fs.File('fa.cpp'), env, path)
 
         # Did we catch the warning associated with not finding fb.h?
         assert to.out
@@ -311,10 +319,11 @@ class CScannerTestCase10(unittest.TestCase):
     def runTest(self):
         fs = SCons.Node.FS.FS(test.workpath(''))
         fs.chdir(fs.Dir('include'))
-        s = SCons.Scanner.C.CScan(fs=fs)
         env = DummyEnvironment([])
+        s = SCons.Scanner.C.CScan(fs=fs)
+        path = s.path(env)
         test.write('include/fa.cpp', test.read('fa.cpp'))
-        deps = s.scan(fs.File('#include/fa.cpp'), env, DummyTarget())
+        deps = s(fs.File('#include/fa.cpp'), env, path)
         deps_match(self, deps, [ 'include/fa.h', 'include/fb.h' ])
         test.unlink('include/fa.cpp')
 
@@ -328,9 +337,10 @@ class CScannerTestCase11(unittest.TestCase):
         # This was a bug at one time.
         f1=fs.File('include2/jjj.h')
         f1.builder=1
-        s = SCons.Scanner.C.CScan(fs=fs)
         env = DummyEnvironment(['include', 'include2'])
-        deps = s.scan(fs.File('src/fff.c'), env, DummyTarget())
+        s = SCons.Scanner.C.CScan(fs=fs)
+        path = s.path(env)
+        deps = s(fs.File('src/fff.c'), env, path)
         deps_match(self, deps, [ test.workpath('repository/include/iii.h'), 'include2/jjj.h' ])
         os.chdir(test.workpath(''))
 
@@ -343,13 +353,14 @@ class CScannerTestCase12(unittest.TestCase):
         fs.Repository(test.workpath('repository'))
         env = DummyEnvironment([])
         s = SCons.Scanner.C.CScan(fs = fs)
-        deps1 = s.scan(fs.File('build1/aaa.c'), env, DummyTarget())
+        path = s.path(env)
+        deps1 = s(fs.File('build1/aaa.c'), env, path)
         deps_match(self, deps1, [ 'build1/bbb.h' ])
-        deps2 = s.scan(fs.File('build2/aaa.c'), env, DummyTarget())
+        deps2 = s(fs.File('build2/aaa.c'), env, path)
         deps_match(self, deps2, [ 'src/bbb.h' ])
-        deps3 = s.scan(fs.File('build1/ccc.c'), env, DummyTarget())
+        deps3 = s(fs.File('build1/ccc.c'), env, path)
         deps_match(self, deps3, [ 'build1/ddd.h' ])
-        deps4 = s.scan(fs.File('build2/ccc.c'), env, DummyTarget())
+        deps4 = s(fs.File('build2/ccc.c'), env, path)
         deps_match(self, deps4, [ test.workpath('repository/src/ddd.h') ])
         os.chdir(test.workpath(''))
 
@@ -360,7 +371,8 @@ class CScannerTestCase13(unittest.TestCase):
                 return test.workpath("d1")
         env = SubstEnvironment(["blah"])
         s = SCons.Scanner.C.CScan()
-        deps = s.scan(make_node('f1.cpp'), env, DummyTarget())
+        path = s.path(env)
+        deps = s(make_node('f1.cpp'), env, path)
         headers = ['d1/f2.h', 'f1.h']
         deps_match(self, deps, map(test.workpath, headers))
         
index 5d908f71e5bbbfc892ce061e645d81206533acfe..e23c7a58b815433858465f3c151bb9c1ba9616dc 100644 (file)
@@ -46,71 +46,53 @@ def FortranScan(fs = SCons.Node.FS.default_fs):
     """Return a prototype Scanner instance for scanning source files
     for Fortran INCLUDE statements"""
     scanner = SCons.Scanner.Recursive(scan, "FortranScan", fs,
-                                      [".f", ".F", ".for", ".FOR"])
+                                      [".f", ".F", ".for", ".FOR"],
+                                      path_function = path)
     return scanner
 
-def scan(node, env, target, fs = SCons.Node.FS.default_fs):
+def path(env, dir, fs = SCons.Node.FS.default_fs):
+    try:
+        f77path = env['F77PATH']
+    except KeyError:
+        return ()
+    return tuple(fs.Rsearchall(SCons.Util.mapPaths(f77path, dir, env),
+                               clazz = SCons.Node.FS.Dir,
+                               must_exist = 0))
+
+def scan(node, env, f77path = (), fs = SCons.Node.FS.default_fs):
     """
     scan(node, Environment) -> [node]
 
     the Fortran dependency scanner function
-
-    This function is intentionally simple. There are two rules it
-    follows:
-    
-    1) #include <foo.h> - search for foo.h in F77PATH followed by the
-        directory 'filename' is in
-    2) #include \"foo.h\" - search for foo.h in the directory 'filename' is
-       in followed by F77PATH
-
-    These rules approximate the behaviour of most C/C++ compilers.
-
-    This scanner also ignores #ifdef and other preprocessor conditionals, so
-    it may find more depencies than there really are, but it never misses
-    dependencies.
     """
 
-    # This function caches various information in node and target:
-    # target.f77path - env['F77PATH'] converted to nodes
-    # node.found_includes - include files found by previous call to scan, 
-    #     keyed on f77path
+    node = node.rfile()
+
+    # This function caches the following information:
     # node.includes - the result of include_re.findall()
 
-    if not hasattr(target, 'f77path'):
-        try:
-            target.f77path = tuple(fs.Rsearchall(SCons.Util.mapPaths(env['F77PATH'], target.cwd, env), clazz=SCons.Node.FS.Dir, must_exist=0))
-        except KeyError:
-            target.f77path = ()
+    if not node.exists():
+        return []
 
-    f77path = target.f77path
+    # cache the includes list in node so we only scan it once:
+    if node.includes != None:
+        includes = node.includes
+    else:
+        includes = include_re.findall(node.get_contents())
+        node.includes = includes
 
+    source_dir = node.get_dir()
+    
     nodes = []
-
-    node = node.rfile()
-    try:
-        nodes = node.found_includes[f77path]
-    except KeyError:
-        if node.rexists():
-
-            # cache the includes list in node so we only scan it once:
-            if node.includes != None:
-                includes = node.includes
-            else:
-                includes = include_re.findall(node.get_contents())
-                node.includes = includes
-
-            source_dir = node.get_dir()
-            
-            for include in includes:
-                n = SCons.Node.FS.find_file(include,
-                                            (source_dir,) + f77path,
-                                            fs.File)
-                if not n is None:
-                    nodes.append(n)
-                else:
-                    SCons.Warnings.warn(SCons.Warnings.DependencyWarning,
-                                        "No dependency generated for file: %s (included from: %s) -- file not found" % (include, node))
-        node.found_includes[f77path] = nodes
+    for include in includes:
+        n = SCons.Node.FS.find_file(include,
+                                    (source_dir,) + f77path,
+                                    fs.File)
+        if not n is None:
+            nodes.append(n)
+        else:
+            SCons.Warnings.warn(SCons.Warnings.DependencyWarning,
+                                "No dependency generated for file: %s (included from: %s) -- file not found" % (include, node))
 
     # Schwartzian transform from the Python FAQ Wizard
     def st(List, Metric):
index e2cfcd6da7854ed1f346129bedb0ee74e59bea37..f721d8946bf72dedc6497700fb94b73ee4296fb5 100644 (file)
@@ -127,10 +127,6 @@ test.write([ 'repository', 'src', 'ddd.f'], "\n")
 
 # define some helpers:
 
-class DummyTarget:
-    def __init__(self, cwd=None):
-        self.cwd = cwd
-
 class DummyEnvironment:
     def __init__(self, listCppPath):
         self.path = listCppPath
@@ -143,6 +139,9 @@ class DummyEnvironment:
         else:
             raise KeyError, "Dummy environment only has F77PATH attribute."
 
+    def has_key(self, key):
+        return self.Dictionary().has_key(key)
+
     def __getitem__(self,key):
         return self.Dictionary()[key]
 
@@ -171,12 +170,13 @@ class FortranScannerTestCase1(unittest.TestCase):
         test.write('f2.f', "      INCLUDE 'fi.f'\n")
         env = DummyEnvironment([])
         s = SCons.Scanner.Fortran.FortranScan()
-       fs = SCons.Node.FS.FS(original)
-        deps = s.scan(make_node('fff1.f', fs), env, DummyTarget())
+        path = s.path(env)
+        fs = SCons.Node.FS.FS(original)
+        deps = s(make_node('fff1.f', fs), env, path)
         headers = ['f1.f', 'f2.f', 'fi.f']
         deps_match(self, deps, map(test.workpath, headers))
-       test.unlink('f1.f')
-       test.unlink('f2.f')
+        test.unlink('f1.f')
+        test.unlink('f2.f')
 
 class FortranScannerTestCase2(unittest.TestCase):
     def runTest(self):
@@ -184,19 +184,21 @@ class FortranScannerTestCase2(unittest.TestCase):
         test.write('f2.f', "      INCLUDE 'fi.f'\n")
         env = DummyEnvironment([test.workpath("d1")])
         s = SCons.Scanner.Fortran.FortranScan()
-       fs = SCons.Node.FS.FS(original)
-        deps = s.scan(make_node('fff1.f', fs), env, DummyTarget())
+        path = s.path(env)
+        fs = SCons.Node.FS.FS(original)
+        deps = s(make_node('fff1.f', fs), env, path)
         headers = ['f1.f', 'f2.f', 'fi.f']
         deps_match(self, deps, map(test.workpath, headers))
-       test.unlink('f1.f')
-       test.unlink('f2.f')
+        test.unlink('f1.f')
+        test.unlink('f2.f')
 
 class FortranScannerTestCase3(unittest.TestCase):
     def runTest(self):
         env = DummyEnvironment([test.workpath("d1")])
         s = SCons.Scanner.Fortran.FortranScan()
-       fs = SCons.Node.FS.FS(original)
-        deps = s.scan(make_node('fff1.f', fs), env, DummyTarget())
+        path = s.path(env)
+        fs = SCons.Node.FS.FS(original)
+        deps = s(make_node('fff1.f', fs), env, path)
         headers = ['d1/f1.f', 'd1/f2.f']
         deps_match(self, deps, map(test.workpath, headers))
 
@@ -205,8 +207,9 @@ class FortranScannerTestCase4(unittest.TestCase):
         test.write(['d1', 'f2.f'], "      INCLUDE 'fi.f'\n")
         env = DummyEnvironment([test.workpath("d1")])
         s = SCons.Scanner.Fortran.FortranScan()
-       fs = SCons.Node.FS.FS(original)
-        deps = s.scan(make_node('fff1.f', fs), env, DummyTarget())
+        path = s.path(env)
+        fs = SCons.Node.FS.FS(original)
+        deps = s(make_node('fff1.f', fs), env, path)
         headers = ['d1/f1.f', 'd1/f2.f']
         deps_match(self, deps, map(test.workpath, headers))
         test.write(['d1', 'f2.f'], "\n")
@@ -215,8 +218,9 @@ class FortranScannerTestCase5(unittest.TestCase):
     def runTest(self):
         env = DummyEnvironment([test.workpath("d1")])
         s = SCons.Scanner.Fortran.FortranScan()
-       fs = SCons.Node.FS.FS(original)
-        deps = s.scan(make_node('fff2.f', fs), env, DummyTarget())
+        path = s.path(env)
+        fs = SCons.Node.FS.FS(original)
+        deps = s(make_node('fff2.f', fs), env, path)
         headers = ['d1/d2/f2.f', 'd1/f2.f', 'd1/f2.f']
         deps_match(self, deps, map(test.workpath, headers))
 
@@ -225,8 +229,9 @@ class FortranScannerTestCase6(unittest.TestCase):
         test.write('f2.f', "\n")
         env = DummyEnvironment([test.workpath("d1")])
         s = SCons.Scanner.Fortran.FortranScan()
-       fs = SCons.Node.FS.FS(original)
-        deps = s.scan(make_node('fff2.f', fs), env, DummyTarget())
+        path = s.path(env)
+        fs = SCons.Node.FS.FS(original)
+        deps = s(make_node('fff2.f', fs), env, path)
         headers =  ['d1/d2/f2.f', 'd1/f2.f', 'f2.f']
         deps_match(self, deps, map(test.workpath, headers))
         test.unlink('f2.f')
@@ -235,8 +240,9 @@ class FortranScannerTestCase7(unittest.TestCase):
     def runTest(self):
         env = DummyEnvironment([test.workpath("d1/d2"), test.workpath("d1")])
         s = SCons.Scanner.Fortran.FortranScan()
-       fs = SCons.Node.FS.FS(original)
-        deps = s.scan(make_node('fff2.f', fs), env, DummyTarget())
+        path = s.path(env)
+        fs = SCons.Node.FS.FS(original)
+        deps = s(make_node('fff2.f', fs), env, path)
         headers =  ['d1/d2/f2.f', 'd1/d2/f2.f', 'd1/f2.f']
         deps_match(self, deps, map(test.workpath, headers))
 
@@ -245,8 +251,9 @@ class FortranScannerTestCase8(unittest.TestCase):
         test.write('f2.f', "\n")
         env = DummyEnvironment([test.workpath("d1/d2"), test.workpath("d1")])
         s = SCons.Scanner.Fortran.FortranScan()
-       fs = SCons.Node.FS.FS(original)
-        deps = s.scan(make_node('fff2.f', fs), env, DummyTarget())
+        path = s.path(env)
+        fs = SCons.Node.FS.FS(original)
+        deps = s(make_node('fff2.f', fs), env, path)
         headers =  ['d1/d2/f2.f', 'd1/f2.f', 'f2.f']
         deps_match(self, deps, map(test.workpath, headers))
         test.unlink('f2.f')
@@ -256,6 +263,7 @@ class FortranScannerTestCase9(unittest.TestCase):
         test.write('f3.f', "\n")
         env = DummyEnvironment([])
         s = SCons.Scanner.Fortran.FortranScan()
+        path = s.path(env)
 
         n = make_node('fff3.f')
         def my_rexists(s=n):
@@ -264,7 +272,7 @@ class FortranScannerTestCase9(unittest.TestCase):
         setattr(n, 'old_rexists', n.rexists)
         setattr(n, 'rexists', my_rexists)
 
-        deps = s.scan(n, env, DummyTarget())
+        deps = s(n, env, path)
         
         # Make sure rexists() got called on the file node being
         # scanned, essential for cooperation with BuildDir functionality.
@@ -279,11 +287,13 @@ class FortranScannerTestCase10(unittest.TestCase):
         fs = SCons.Node.FS.FS(test.workpath(''))
         env = DummyEnvironment(["include"])
         s = SCons.Scanner.Fortran.FortranScan(fs = fs)
-        deps1 = s.scan(fs.File('fff4.f'), env, DummyTarget())
+        path = s.path(env)
+        deps1 = s(fs.File('fff4.f'), env, path)
         fs.chdir(fs.Dir('subdir'))
-        target = DummyTarget(fs.getcwd())
+        dir = fs.getcwd()
         fs.chdir(fs.Dir('..'))
-        deps2 = s.scan(fs.File('#fff4.f'), env, target)
+        path = s.path(env, dir)
+        deps2 = s(fs.File('#fff4.f'), env, path)
         headers1 =  ['include/f4.f']
         headers2 =  ['subdir/include/f4.f']
         deps_match(self, deps1, headers1)
@@ -301,9 +311,10 @@ class FortranScannerTestCase11(unittest.TestCase):
         SCons.Warnings._warningOut = to
         test.write('f4.f',"      INCLUDE 'not_there.f'\n")
         fs = SCons.Node.FS.FS(test.workpath(''))
-        s = SCons.Scanner.Fortran.FortranScan(fs=fs)
         env = DummyEnvironment([])
-        deps = s.scan(fs.File('fff4.f'), env, DummyTarget())
+        s = SCons.Scanner.Fortran.FortranScan(fs=fs)
+        path = s.path(env)
+        deps = s(fs.File('fff4.f'), env, path)
 
         # Did we catch the warning from not finding not_there.f?
         assert to.out
@@ -315,10 +326,11 @@ class FortranScannerTestCase12(unittest.TestCase):
     def runTest(self):
         fs = SCons.Node.FS.FS(test.workpath(''))
         fs.chdir(fs.Dir('include'))
-        s = SCons.Scanner.Fortran.FortranScan(fs=fs)
         env = DummyEnvironment([])
+        s = SCons.Scanner.Fortran.FortranScan(fs=fs)
+        path = s.path(env)
         test.write('include/fff4.f', test.read('fff4.f'))
-        deps = s.scan(fs.File('#include/fff4.f'), env, DummyTarget())
+        deps = s(fs.File('#include/fff4.f'), env, path)
         deps_match(self, deps, ['include/f4.f'])
         test.unlink('include/fff4.f')
 
@@ -332,9 +344,10 @@ class FortranScannerTestCase13(unittest.TestCase):
         # This was a bug at one time.
         f1=fs.File('include2/jjj.f')
         f1.builder=1
-        s = SCons.Scanner.Fortran.FortranScan(fs=fs)
         env = DummyEnvironment(['include','include2'])
-        deps = s.scan(fs.File('src/fff.f'), env, DummyTarget())
+        s = SCons.Scanner.Fortran.FortranScan(fs=fs)
+        path = s.path(env)
+        deps = s(fs.File('src/fff.f'), env, path)
         deps_match(self, deps, [test.workpath('repository/include/iii.f'), 'include2/jjj.f'])
         os.chdir(test.workpath(''))
 
@@ -347,13 +360,14 @@ class FortranScannerTestCase14(unittest.TestCase):
         fs.Repository(test.workpath('repository'))
         env = DummyEnvironment([])
         s = SCons.Scanner.Fortran.FortranScan(fs = fs)
-        deps1 = s.scan(fs.File('build1/aaa.f'), env, DummyTarget())
+        path = s.path(env)
+        deps1 = s(fs.File('build1/aaa.f'), env, path)
         deps_match(self, deps1, [ 'build1/bbb.f' ])
-        deps2 = s.scan(fs.File('build2/aaa.f'), env, DummyTarget())
+        deps2 = s(fs.File('build2/aaa.f'), env, path)
         deps_match(self, deps2, [ 'src/bbb.f' ])
-        deps3 = s.scan(fs.File('build1/ccc.f'), env, DummyTarget())
+        deps3 = s(fs.File('build1/ccc.f'), env, path)
         deps_match(self, deps3, [ 'build1/ddd.f' ])
-        deps4 = s.scan(fs.File('build2/ccc.f'), env, DummyTarget())
+        deps4 = s(fs.File('build2/ccc.f'), env, path)
         deps_match(self, deps4, [ test.workpath('repository/src/ddd.f') ])
         os.chdir(test.workpath(''))
 
@@ -365,8 +379,9 @@ class FortranScannerTestCase15(unittest.TestCase):
         test.write(['d1', 'f2.f'], "      INCLUDE 'fi.f'\n")
         env = SubstEnvironment(["junk"])
         s = SCons.Scanner.Fortran.FortranScan()
-       fs = SCons.Node.FS.FS(original)
-        deps = s.scan(make_node('fff1.f', fs), env, DummyTarget())
+        path = s.path(env)
+        fs = SCons.Node.FS.FS(original)
+        deps = s(make_node('fff1.f', fs), env, path)
         headers = ['d1/f1.f', 'd1/f2.f']
         deps_match(self, deps, map(test.workpath, headers))
         test.write(['d1', 'f2.f'], "\n")
index 7cfd9f41ca605caf6b79e5e65a824597705f8527..081fd5cb3df3e47b49d456b7b4f6ffeaa45ef181 100644 (file)
@@ -34,10 +34,19 @@ import SCons.Util
 def ProgScan(fs = SCons.Node.FS.default_fs):
     """Return a prototype Scanner instance for scanning executable
     files for static-lib dependencies"""
-    ps = SCons.Scanner.Base(scan, "ProgScan", fs)
+    ps = SCons.Scanner.Base(scan, "ProgScan", fs, path_function = path)
     return ps
 
-def scan(node, env, target, fs):
+def path(env, dir, fs = SCons.Node.FS.default_fs):
+    try:
+        libpath = env['LIBPATH']
+    except KeyError:
+        return ()
+    return tuple(fs.Rsearchall(SCons.Util.mapPaths(libpath, dir, env),
+                               clazz = SCons.Node.FS.Dir,
+                               must_exist = 0))
+
+def scan(node, env, libpath = (), fs = SCons.Node.FS.default_fs):
     """
     This scanner scans program files for static-library
     dependencies.  It will search the LIBPATH environment variable
@@ -45,17 +54,6 @@ def scan(node, env, target, fs):
     files it finds as dependencies.
     """
 
-    # This function caches information in target:
-    # target.libpath - env['LIBPATH'] converted to nodes
-
-    if not hasattr(target, 'libpath'):
-        try:
-            target.libpath = tuple(fs.Rsearchall(SCons.Util.mapPaths(env['LIBPATH'], target.cwd, env), clazz=SCons.Node.FS.Dir, must_exist=0))
-        except KeyError:
-            target.libpath = ()
-    libpath = target.libpath
-
     try:
         libs = env.Dictionary('LIBS')
     except KeyError:
index 1302e467e09fb8a40aa55e467851fab7a9ba02b9..0d0e5ad7387a4bfda22883883749b70e017cc61e 100644 (file)
@@ -43,10 +43,6 @@ for h in libs:
 
 # define some helpers:
 
-class DummyTarget:
-    def __init__(self, cwd=None):
-        self.cwd = cwd
-
 class DummyEnvironment:
     def __init__(self, **kw):
         self._dict = kw
@@ -59,6 +55,10 @@ class DummyEnvironment:
             return self._dict[args[0]]
         else:
             return map(lambda x, s=self: s._dict[x], args)
+
+    def has_key(self, key):
+        return self.Dictionary().has_key(key)
+
     def __getitem__(self,key):
         return self.Dictionary()[key]
 
@@ -86,7 +86,8 @@ class ProgScanTestCase1(unittest.TestCase):
         env = DummyEnvironment(LIBPATH=[ test.workpath("") ],
                                LIBS=[ 'l1', 'l2', 'l3' ])
         s = SCons.Scanner.Prog.ProgScan()
-        deps = s.scan('dummy', env, DummyTarget())
+        path = s.path(env)
+        deps = s('dummy', env, path)
         assert deps_match(deps, ['l1.lib']), map(str, deps)
 
 class ProgScanTestCase2(unittest.TestCase):
@@ -95,7 +96,8 @@ class ProgScanTestCase2(unittest.TestCase):
                                            ["", "d1", "d1/d2" ]),
                                LIBS=[ 'l1', 'l2', 'l3' ])
         s = SCons.Scanner.Prog.ProgScan()
-        deps = s.scan('dummy', env, DummyTarget())
+        path = s.path(env)
+        deps = s('dummy', env, path)
         assert deps_match(deps, ['l1.lib', 'd1/l2.lib', 'd1/d2/l3.lib' ]), map(str, deps)
 
 class ProgScanTestCase3(unittest.TestCase):
@@ -104,7 +106,8 @@ class ProgScanTestCase3(unittest.TestCase):
                                         test.workpath("d1")],
                                LIBS=string.split('l2 l3'))
         s = SCons.Scanner.Prog.ProgScan()
-        deps = s.scan('dummy', env, DummyTarget())
+        path = s.path(env)
+        deps = s('dummy', env, path)
         assert deps_match(deps, ['d1/l2.lib', 'd1/d2/l3.lib']), map(str, deps)
 
 class ProgScanTestCase5(unittest.TestCase):
@@ -118,7 +121,8 @@ class ProgScanTestCase5(unittest.TestCase):
         env = SubstEnvironment(LIBPATH=[ "blah" ],
                                LIBS=string.split('l2 l3'))
         s = SCons.Scanner.Prog.ProgScan()
-        deps = s.scan('dummy', env, DummyTarget())
+        path = s.path(env)
+        deps = s('dummy', env, path)
         assert deps_match(deps, [ 'd1/l2.lib' ]), map(str, deps)
 
 def suite():
@@ -135,7 +139,8 @@ def suite():
                                                     test.workpath("d1")],
                                            LIBS=string.split(u'l2 l3'))
                     s = SCons.Scanner.Prog.ProgScan()
-                    deps = s.scan('dummy', env, DummyTarget())
+                    path = s.path(env)
+                    deps = s('dummy', env, path)
                     assert deps_match(deps, ['d1/l2.lib', 'd1/d2/l3.lib']), map(str, deps)
             suite.addTest(ProgScanTestCase4())
             \n"""
index 2d0e47a5a8f9ea51f998e75e5bdad71393afe4cc..7280c2f28d122ba268580bad0141159f12418b04 100644 (file)
@@ -27,10 +27,6 @@ import unittest
 import SCons.Scanner
 import sys
 
-class DummyTarget:
-    cwd = None
-    
-
 class ScannerTestBase:
     
     def func(self, filename, env, target, *args):
@@ -46,7 +42,8 @@ class ScannerTestBase:
 
     def test(self, scanner, env, filename, deps, *args):
         self.deps = deps
-        scanned = scanner.scan(filename, env, DummyTarget())
+        path = scanner.path(env)
+        scanned = scanner(filename, env, path)
         scanned_strs = map(lambda x: str(x), scanned)
 
         self.failUnless(self.filename == filename, "the filename was passed incorrectly")
@@ -121,7 +118,7 @@ class ScannerHashTestCase(ScannerTestBase, unittest.TestCase):
         s = SCons.Scanner.Base(self.func, "Hash")
         dict = {}
         dict[s] = 777
-        self.failUnless(hash(dict.keys()[0]) == hash(None),
+        self.failUnless(hash(dict.keys()[0]) == hash(repr(s)),
                         "did not hash Scanner base class as expected")
 
 class ScannerCheckTestCase(unittest.TestCase):
@@ -134,8 +131,10 @@ class ScannerCheckTestCase(unittest.TestCase):
         def check(node, s=self):
             s.checked[node] = 1
             return 1
+        env = DummyEnvironment()
         s = SCons.Scanner.Base(my_scan, "Check", scan_check = check)
-        scanned = s.scan('x', DummyEnvironment(), DummyTarget())
+        path = s.path(env)
+        scanned = s('x', env, path)
         self.failUnless(self.checked['x'] == 1,
                         "did not call check function")
 
index aef5d728fd380807712bcf84429a7c0475495e6a..c27c762d70b1bcd95b9f53ea7a37237304c25bc7 100644 (file)
@@ -52,23 +52,42 @@ class Base:
                  name = "NONE",
                  argument = _null,
                  skeys = [],
+                 path_function = None,
+                 node_class = SCons.Node.FS.Entry,
                  node_factory = SCons.Node.FS.default_fs.File,
                  scan_check = None):
         """
         Construct a new scanner object given a scanner function.
 
-        'function' - a scanner function taking two or three arguments and
-        returning a list of strings.
+        'function' - a scanner function taking two or three
+        arguments and returning a list of strings.
 
         'name' - a name for identifying this scanner object.
 
-        'argument' - an optional argument that will be passed to the
-        scanner function if it is given.
+        'argument' - an optional argument that, if specified, will be
+        passed to both the scanner function and the path_function.
 
-        'skeys; - an optional list argument that can be used to determine
+        'skeys' - an optional list argument that can be used to determine
         which scanner should be used for a given Node. In the case of File
         nodes, for example, the 'skeys' would be file suffixes.
 
+        'path_function' - a function that takes one to three arguments
+        (a construction environment, optional directory, and optional
+        argument for this instance) and returns a tuple of the
+        directories that can be searched for implicit dependency files.
+
+        'node_class' - the class of Nodes which this scan will return.
+        If node_class is None, then this scanner will not enforce any
+        Node conversion and will return the raw results from the
+        underlying scanner function.
+
+        'node_factory' - the factory function to be called to translate
+        the raw results returned by the scanner function into the
+        expected node_class objects.
+
+        'scan_check' - a function to be called to first check whether
+        this node really needs to be scanned.
+
         The scanner function's first argument will be the name of a file
         that should be scanned for dependencies, the second argument will
         be an Environment object, the third argument will be the value
@@ -91,13 +110,23 @@ class Base:
         # would need to be changed is the documentation.
 
         self.function = function
+        self.path_function = path_function
         self.name = name
         self.argument = argument
         self.skeys = skeys
+        self.node_class = node_class
         self.node_factory = node_factory
         self.scan_check = scan_check
 
-    def scan(self, node, env, target):
+    def path(self, env, dir = None):
+        if not self.path_function:
+            return ()
+        if not self.argument is _null:
+            return self.path_function(env, dir, self.argument)
+        else:
+            return self.path_function(env, dir)
+
+    def __call__(self, node, env, path = ()):
         """
         This method scans a single object. 'node' is the node
         that will be passed to the scanner function, and 'env' is the
@@ -108,15 +137,15 @@ class Base:
             return []
 
         if not self.argument is _null:
-            list = self.function(node, env, target, self.argument)
+            list = self.function(node, env, path, self.argument)
         else:
-            list = self.function(node, env, target)
+            list = self.function(node, env, path)
         kw = {}
         if hasattr(node, 'dir'):
             kw['directory'] = node.dir
         nodes = []
         for l in list:
-            if not isinstance(l, SCons.Node.FS.Entry):
+            if self.node_class and not isinstance(l, self.node_class):
                 l = apply(self.node_factory, (l,), kw)
             nodes.append(l)
         return nodes
@@ -125,7 +154,7 @@ class Base:
         return cmp(self.__dict__, other.__dict__)
 
     def __hash__(self):
-        return hash(None)
+        return hash(repr(self))
 
     def add_skey(self, skey):
         """Add a skey to the list of skeys"""
@@ -149,7 +178,7 @@ class Recursive(RExists):
     list of all dependencies.
     """
 
-    def scan(self, node, env, target):
+    def __call__(self, node, env, path = ()):
         """
         This method does the actual scanning. 'node' is the node
         that will be passed to the scanner function, and 'env' is the
@@ -164,7 +193,7 @@ class Recursive(RExists):
         while nodes:
             n = nodes.pop(0)
             d = filter(lambda x, seen=seen: not seen.has_key(x),
-                       Base.scan(self, n, env, target))
+                       Base.__call__(self, n, env, path))
             if d:
                 deps.extend(d)
                 nodes.extend(d)
index a1674e641aa5f37bace2c7b3c0fb42a578ca1bce..a2aaf53e4c2fd0c4e047b4ab4fb66029abd2bc4e 100644 (file)
 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 #
 
+"""
+This test verifies that Scanners are called just once.
+
+This is actually a shotgun marriage of two separate tests, the simple
+test originally created for this, plus a more complicated test based
+on a real-life bug report submitted by Scott Lystig Fritchie.  Both
+have value: the simple test will be easier to debug if there are basic
+scanning problems, while Scott's test has a lot of cool real-world
+complexity that is valuable in its own right, including scanning of
+generated .h files.
+
+"""
+
 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
 
+import os.path
+import sys
+
+import TestCmd
 import TestSCons
 
+#test = TestSCons.TestSCons(match = TestCmd.match_re)
 test = TestSCons.TestSCons()
 
-test.write('SConstruct', r"""
+test.subdir('simple',
+            'SLF',
+            ['SLF', 'reftree'], ['SLF', 'reftree', 'include'],
+            ['SLF', 'src'], ['SLF', 'src', 'lib_geng'])
+
+test.write('SConstruct', """\
+SConscript('simple/SConscript')
+SConscript('SLF/SConscript')
+""")
+
+test.write(['simple', 'SConscript'], r"""
 import os.path
 
-def scan(node, env, target, arg):
-    print 'scanning',node,'for',target
+def scan(node, env, envkey, arg):
+    print 'XScanner: node =', os.path.split(str(node))[1]
     return []
 
 def exists_check(node):
     return os.path.exists(str(node))
 
-PScanner = Scanner(name = 'PScanner',
+XScanner = Scanner(name = 'XScanner',
                    function = scan,
                    argument = None,
                    scan_check = exists_check,
-                  skeys = ['.s'])
+                  skeys = ['.x'])
 
 def echo(env, target, source):
-    print 'create %s from %s' % (str(target[0]), str(source[0]))
+    t = os.path.split(str(target[0]))[1]
+    s = os.path.split(str(source[0]))[1]
+    print 'create %s from %s' % (t, s)
 
-Echo = Builder(action = echo,
-               src_suffix = '.s',
-              suffix = '.s')
+Echo = Builder(action = Action(echo, None),
+               src_suffix = '.x',
+              suffix = '.x')
 
-env = Environment(BUILDERS = {'Echo':Echo}, SCANNERS = [PScanner])
+env = Environment(BUILDERS = {'Echo':Echo}, SCANNERS = [XScanner])
 
 f1 = env.Echo(source=['file1'], target=['file2'])
 f2 = env.Echo(source=['file2'], target=['file3'])
 f3 = env.Echo(source=['file3'], target=['file4'])
+""")
+
+test.write(['simple', 'file1.x'], 'simple/file1.x\n')
+
+test.write(['SLF', 'SConscript'], """\
+###
+### QQQ !@#$!@#$!  I need to move the SConstruct file to be "above"
+### both the source and install dirs, or the install dependencies
+### don't seem to work well!  ARRGH!!!!
+###
+
+experimenttop = "%s"
+
+import os
+import os.path
+import string
+import Mylib
+
+BStaticLibMerge = Builder(generator = Mylib.Gen_StaticLibMerge)
+builders = Environment().Dictionary('BUILDERS')
+builders["StaticLibMerge"] = BStaticLibMerge
+
+env = Environment(BUILDERS = builders)
+e = env.Dictionary()   # Slightly easier to type
+
+Scanned = {}
+
+def write_out(file, dict):
+    keys = dict.keys()
+    keys.sort()
+    f = open(file, 'wb')
+    for k in keys:
+        file = os.path.split(k)[1]
+        f.write(file + ": " + str(dict[k]) + "\\n")
+    f.close()
+
+import SCons.Scanner.C
+def MyCScan(node, env, target):
+    deps = SCons.Scanner.C.scan(node, env, target)
+
+    global Scanned
+    n = str(node)
+    try:
+        Scanned[n] = Scanned[n] + 1
+    except KeyError:
+        Scanned[n] = 1
+    write_out('MyCScan.out', Scanned)
+
+    return deps
+S_MyCScan = Scanner(skeys = [".c", ".C", ".cxx", ".cpp", ".c++", ".cc",
+                             ".h", ".H", ".hxx", ".hpp", ".h++", ".hh"],
+                    function = MyCScan)
+# QQQ Yes, this is manner of fixing the SCANNERS list is fragile.
+env["SCANNERS"] = [S_MyCScan] + env["SCANNERS"][1:]
+
+global_env = env
+e["GlobalEnv"] = global_env
+
+e["REF_INCLUDE"] = os.path.join(experimenttop, "reftree", "include")
+e["REF_LIB"] = os.path.join(experimenttop, "reftree", "lib")
+e["EXPORT_INCLUDE"] = os.path.join(experimenttop, "export", "include")
+e["EXPORT_LIB"] = os.path.join(experimenttop, "export", "lib")
+e["INSTALL_BIN"] = os.path.join(experimenttop, "install", "bin")
+
+build_dir = os.path.join(experimenttop, "tmp-bld-dir")
+src_dir = os.path.join(experimenttop, "src")
+
+env.Append(CPPPATH = [e["EXPORT_INCLUDE"]])
+env.Append(CPPPATH = [e["REF_INCLUDE"]])
+Mylib.AddLibDirs(env, "/via/Mylib.AddLibPath")
+env.Append(LIBPATH = [e["EXPORT_LIB"]])
+env.Append(LIBPATH = [e["REF_LIB"]])
+
+Mylib.Subdirs("src")
+""" % test.workpath('SLF'))
+
+test.write(['SLF', 'Mylib.py'], """\
+import os
+import string
+import re
+import SCons.Environment
+import SCons.Script.SConscript
+
+def Subdirs(dirlist):
+    for file in _subconf_list(dirlist):
+        SCons.Script.SConscript.SConscript(file, "env")
+
+def _subconf_list(dirlist):
+    return map(lambda x: os.path.join(x, "SConscript"), string.split(dirlist))
+
+def StaticLibMergeMembers(local_env, libname, hackpath, files):
+    for file in string.split(files):
+        # QQQ Fix limits in grok'ed regexp
+        tmp = re.sub(".c$", ".o", file)
+        objname = re.sub(".cpp", ".o", tmp)
+        local_env.Object(target = objname, source = file)
+        e = 'local_env["GlobalEnv"].Append(%s = ["%s"])' % (libname, os.path.join(hackpath, objname))
+        exec(e)
+
+def CreateMergedStaticLibrary(env, libname):
+    objpaths = env["GlobalEnv"][libname]
+    libname = "lib%s.a" % (libname)
+    env.StaticLibMerge(target = libname, source = objpaths)
+
+# I put the main body of the generator code here to avoid
+# namespace problems
+def Gen_StaticLibMerge(source, target, env, for_signature):
+    target_string = ""
+    for t in target:
+        target_string = str(t)
+    subdir = os.path.dirname(target_string)
+    srclist = []
+    for src in source:
+        srclist.append(src)
+    return [["ar", "cq"] + target + srclist, ["ranlib"] + target]
+
+def StaticLibrary(env, target, source):
+    env.StaticLibrary(target, string.split(source))
+
+def SharedLibrary(env, target, source):
+    env.SharedLibrary(target, string.split(source))
+
+def ExportHeader(env, headers):
+    env.Install(dir = env["EXPORT_INCLUDE"], source = string.split(headers))
+
+def ExportLib(env, libs):
+    env.Install(dir = env["EXPORT_LIB"], source = string.split(libs))
+
+def InstallBin(env, bins):
+    env.Install(dir = env["INSTALL_BIN"], source = string.split(bins))
+
+def Program(env, target, source):
+    env.Program(target, string.split(source))
+
+def AddCFlags(env, str):
+    env.Append(CPPFLAGS = " " + str)
+
+# QQQ Synonym needed?
+#def AddCFLAGS(env, str):
+#    AddCFlags(env, str)
+
+def AddIncludeDirs(env, str):
+    env.Append(CPPPATH = string.split(str))
+
+def AddLibs(env, str):
+    env.Append(LIBS = string.split(str))
+
+def AddLibDirs(env, str):
+    env.Append(LIBPATH = string.split(str))
+
+""")
+
+test.write(['SLF', 'reftree', 'include', 'lib_a.h'], """\
+char *a_letter(void);
+""")
+
+test.write(['SLF', 'reftree', 'include', 'lib_b.h'], """\
+char *b_letter(void);
+""")
+
+test.write(['SLF', 'reftree', 'include', 'lib_ja.h'], """\
+char *j_letter_a(void);
+""")
 
-Default(f3)
+test.write(['SLF', 'reftree', 'include', 'lib_jb.h.intentionally-moved'], """\
+char *j_letter_b(void);
 """)
 
-test.write('file1.s', 'file1.s\n')
+test.write(['SLF', 'src', 'SConscript'], """\
+# --- Begin SConscript boilerplate ---
+import Mylib
+Import("env")
 
-test.run(arguments = '.',
-         stdout = test.wrap_stdout("""scanning file1.s for file2.s
-echo("file2.s", "file1.s")
-create file2.s from file1.s
-scanning file1.s for file2.s
-echo("file3.s", "file2.s")
-create file3.s from file2.s
-echo("file4.s", "file3.s")
-create file4.s from file3.s
+#env = env.Copy()    # Yes, clobber intentionally
+#Make environment changes, such as: Mylib.AddCFlags(env, "-g -D_TEST")
+#Mylib.Subdirs("lib_a lib_b lib_mergej prog_x")
+Mylib.Subdirs("lib_geng")
+
+env = env.Copy()    # Yes, clobber intentionally
+# --- End SConscript boilerplate ---
+
+""")
+
+test.write(['SLF', 'src', 'lib_geng', 'SConscript'], """\
+# --- Begin SConscript boilerplate ---
+import string
+import sys
+import Mylib
+Import("env")
+
+#env = env.Copy()    # Yes, clobber intentionally
+#Make environment changes, such as: Mylib.AddCFlags(env, "-g -D_TEST")
+#Mylib.Subdirs("foo_dir")
+
+env = env.Copy()    # Yes, clobber intentionally
+# --- End SConscript boilerplate ---
+
+Mylib.AddCFlags(env, "-DGOOFY_DEMO")
+Mylib.AddIncludeDirs(env, ".")
+
+# Icky code to set up process environment for "make"
+# I really ought to drop this into Mylib....
+
+fromdict = env.Dictionary()
+todict = env["ENV"]
+import SCons.Util
+import re
+for k in fromdict.keys():
+    if k != "ENV" and k != "SCANNERS" and k != "CFLAGS" and k != "CXXFLAGS" \
+    and not SCons.Util.is_Dict(fromdict[k]):
+        todict[k] = SCons.Util.scons_subst(str(fromdict[k]), fromdict, {})
+todict["CFLAGS"] = fromdict["CPPFLAGS"] + " " + \
+    string.join(map(lambda x: "-I" + x, env["CPPPATH"])) + " " + \
+    string.join(map(lambda x: "-L" + x, env["LIBPATH"])) 
+todict["CXXFLAGS"] = todict["CFLAGS"]
+
+generated_hdrs = "libg_gx.h libg_gy.h libg_gz.h"
+static_hdrs = "libg_w.h"
+#exported_hdrs = generated_hdrs + " " + static_hdrs
+exported_hdrs = static_hdrs
+lib_name = "g"
+lib_fullname = "libg.a"
+lib_srcs = string.split("libg_1.c libg_2.c libg_3.c")
+import re
+lib_objs = map(lambda x: re.sub("\.c$", ".o", x), lib_srcs)
+
+Mylib.ExportHeader(env, exported_hdrs)
+Mylib.ExportLib(env, lib_fullname)
+
+# The following were the original commands from SLF, making use of
+# a shell script and a Makefile to build the library.  These have
+# been preserved, commented out below, but in order to make this
+# test portable, we've replaced them with a Python script and a
+# recursive invocation of SCons (!).
+#cmd_both = "cd %s ; make generated ; make" % Dir(".")
+#cmd_generated = "cd %s ; sh MAKE-HEADER.sh" % Dir(".")
+#cmd_justlib = "cd %s ; make" % Dir(".")
+
+cmd_generated = "%s $SOURCE" % (sys.executable,)
+cmd_justlib = "%s %s -C %s" % (sys.executable, sys.argv[0], Dir("."))
+
+##### Deps appear correct ... but wacky scanning?
+# Why?
+#
+# SCons bug??
+
+env.Command(string.split(generated_hdrs),
+            ["MAKE-HEADER.py"],
+            cmd_generated)
+env.Command([lib_fullname] + lib_objs,
+            lib_srcs + string.split(generated_hdrs + " " + static_hdrs),
+            cmd_justlib) 
+""")
+
+test.write(['SLF', 'src', 'lib_geng', 'MAKE-HEADER.py'], """\
+#!/usr/bin/env python
+
+import os
+import os.path
+import sys
+
+# chdir to the directory in which this script lives
+os.chdir(os.path.split(sys.argv[0])[0])
+
+for h in ['libg_gx.h', 'libg_gy.h', 'libg_gz.h']:
+    open(h, 'w').write('')
+""")
+
+test.write(['SLF', 'src', 'lib_geng', 'SConstruct'], """\
+env = Environment(CPPPATH = ".")
+l = env.StaticLibrary("g", Split("libg_1.c libg_2.c libg_3.c"))
+Default(l)
+""")
+
+# These were the original shell script and Makefile from SLF's original
+# bug report.  We're not using them--in order to make this script as
+# portable as possible, we're using a Python script and a recursive
+# invocation of SCons--but we're preserving them here for history.
+#test.write(['SLF', 'src', 'lib_geng', 'MAKE-HEADER.sh'], """\
+##!/bin/sh
+#
+#exec touch $*
+#""")
+#
+#test.write(['SLF', 'src', 'lib_geng', 'Makefile'], """\
+#all: libg.a
+#
+#GEN_HDRS = libg_gx.h libg_gy.h libg_gz.h
+#STATIC_HDRS = libg_w.h
+#
+#$(GEN_HDRS): generated
+#
+#generated: MAKE-HEADER.sh
+#      sh ./MAKE-HEADER.sh $(GEN_HDRS)
+#
+#libg.a: libg_1.o libg_2.o libg_3.o
+#      ar r libg.a libg_1.o libg_2.o libg_3.o
+#
+#libg_1.c: $(STATIC_HDRS) $(GEN_HDRS)
+#libg_2.c: $(STATIC_HDRS) $(GEN_HDRS)
+#libg_3.c: $(STATIC_HDRS) $(GEN_HDRS)
+#
+#clean:
+#      -rm -f $(GEN_HDRS)
+#      -rm -f libg.a *.o core core.*
+#""")
+
+test.write(['SLF', 'src', 'lib_geng', 'libg_w.h'], """\
+""")
+
+test.write(['SLF', 'src', 'lib_geng', 'libg_1.c'], """\
+#include <libg_w.h>
+#include <libg_gx.h>
+
+int g_1()
+{
+    return 1;
+}
+""")
+
+test.write(['SLF', 'src', 'lib_geng', 'libg_2.c'], """\
+#include <libg_w.h>
+#include <libg_gx.h> 
+#include <libg_gy.h>
+#include <libg_gz.h>
+
+int g_2()
+{
+        return 2;
+}
+""")
+
+test.write(['SLF', 'src', 'lib_geng', 'libg_3.c'], """\
+#include <libg_w.h>
+#include <libg_gx.h>
+
+int g_3()
+{
+    return 3;
+}
+""")
+
+test.run(arguments = 'simple',
+         stdout = test.wrap_stdout("""\
+XScanner: node = file1.x
+create file2.x from file1.x
+create file3.x from file2.x
+create file4.x from file3.x
 """))
 
-test.write('file2.s', 'file2.s\n')
+test.write(['simple', 'file2.x'], 'simple/file2.x\n')
 
-test.run(arguments = '.',
-         stdout = test.wrap_stdout("""scanning file1.s for file2.s
-scanning file2.s for file3.s
-echo("file3.s", "file2.s")
-create file3.s from file2.s
-scanning file2.s for file3.s
-echo("file4.s", "file3.s")
-create file4.s from file3.s
+test.run(arguments = 'simple',
+         stdout = test.wrap_stdout("""\
+XScanner: node = file1.x
+XScanner: node = file2.x
+create file3.x from file2.x
+create file4.x from file3.x
 """))
 
-test.write('file3.s', 'file3.s\n')
+test.write(['simple', 'file3.x'], 'simple/file3.x\n')
 
-test.run(arguments = '.',
-         stdout = test.wrap_stdout("""scanning file1.s for file2.s
-scanning file2.s for file3.s
-scanning file3.s for file4.s
-echo("file4.s", "file3.s")
-create file4.s from file3.s
+test.run(arguments = 'simple',
+         stdout = test.wrap_stdout("""\
+XScanner: node = file1.x
+XScanner: node = file2.x
+XScanner: node = file3.x
+create file4.x from file3.x
 """))
 
+test.run(arguments = 'SLF')
+
+# XXX Note that the generated .h files still get scanned twice,
+# once before they're generated and once after.  That's the
+# next thing to fix here.
+test.fail_test(test.read("MyCScan.out", "rb") != """\
+libg_1.c: 1
+libg_2.c: 1
+libg_3.c: 1
+libg_gx.h: 2
+libg_gy.h: 2
+libg_gz.h: 2
+libg_w.h: 1
+""")
+
 test.pass_test()