summaryrefslogtreecommitdiffstats
path: root/src/engine/SCons/Scanner/C.py
blob: a5aa02d66c98706b0012fb0e2fda2a4705fdc6c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
"""SCons.Scanner.C

This module implements the depenency scanner for C/C++ code. 

"""

#
# Copyright (c) 2001 Steven Knight
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#

__revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"


import copy
import os.path
import re
import SCons.Scanner
import SCons.Util

angle_re = re.compile('^[ \t]*#[ \t]*include[ \t]+<([\\w./\\\\]+)>', re.M)
quote_re = re.compile('^[ \t]*#[ \t]*include[ \t]+"([\\w./\\\\]+)"', re.M)

def CScan(fs = SCons.Node.FS.default_fs):
    "Return a prototype Scanner instance for scanning C/C++ source files"
    cs = CScanner(scan, "CScan", [fs, ()],
                  [".c", ".C", ".cxx", ".cpp", ".c++",
                   ".h", ".H", ".hxx", ".hpp"])
    cs.fs = fs
    return cs

class CScanner(SCons.Scanner.Recursive):
    def __init__(self, *args, **kw):
        apply(SCons.Scanner.Recursive.__init__, (self,) + args, kw)
        self.hash = None
        self.pathscanners = {}

    def instance(self, env):
        """
        Return a unique instance of a C scanner object for a
        given environment.
        """
        try:
            dirs = tuple(SCons.Util.scons_str2nodes(env.Dictionary('CPPPATH'),
                                                    self.fs.Dir))
        except:
            dirs = ()
        if not self.pathscanners.has_key(dirs):
            clone = copy.copy(self)
            clone.hash = dirs
            clone.argument = [self.fs, dirs]	# XXX reaching into object
            self.pathscanners[dirs] = clone
        return self.pathscanners[dirs]

    def __hash__(self):
        return hash(self.hash)

def scan(filename, env, args = [SCons.Node.FS.default_fs, ()]):
    """
    scan(str, Environment) -> [str]

    the C/C++ dependency scanner function

    This function is intentionally simple. There are two rules it
    follows:
    
    1) #include <foo.h> - search for foo.h in CPPPATH followed by the
        directory 'filename' is in
    2) #include \"foo.h\" - search for foo.h in the directory 'filename' is
       in followed by CPPPATH

    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.
    """

    fs, cpppath = args

    if fs.File(filename, fs.Top).exists():
        file = open(filename)
        contents = file.read()
        file.close()

        angle_includes = angle_re.findall(contents)
        quote_includes = quote_re.findall(contents)

        dir = os.path.dirname(filename)
        if dir:
            source_dir = (fs.Dir(dir, fs.Top),)
        else:
            source_dir = ( fs.Top, )

        return (SCons.Util.find_files(angle_includes, cpppath + source_dir,
                                      fs.File)
                + SCons.Util.find_files(quote_includes, source_dir + cpppath,
                                        fs.File))
    return []