3 This module implements the depenency scanner for C/C++ code.
8 # Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 The SCons Foundation
10 # Permission is hereby granted, free of charge, to any person obtaining
11 # a copy of this software and associated documentation files (the
12 # "Software"), to deal in the Software without restriction, including
13 # without limitation the rights to use, copy, modify, merge, publish,
14 # distribute, sublicense, and/or sell copies of the Software, and to
15 # permit persons to whom the Software is furnished to do so, subject to
16 # the following conditions:
18 # The above copyright notice and this permission notice shall be included
19 # in all copies or substantial portions of the Software.
21 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
22 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
23 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 __revision__ = "src/engine/SCons/Scanner/C.py 3842 2008/12/20 22:59:52 scons"
38 class SConsCPPScanner(SCons.cpp.PreProcessor):
40 SCons-specific subclass of the cpp.py module's processing.
42 We subclass this so that: 1) we can deal with files represented
43 by Nodes, not strings; 2) we can keep track of the files that are
46 def __init__(self, *args, **kw):
47 apply(SCons.cpp.PreProcessor.__init__, (self,)+args, kw)
49 def initialize_result(self, fname):
50 self.result = SCons.Util.UniqueList([fname])
51 def finalize_result(self, fname):
52 return self.result[1:]
53 def find_include_file(self, t):
54 keyword, quote, fname = t
55 result = SCons.Node.FS.find_file(fname, self.searchpath[quote])
57 self.missing.append((fname, self.current_file))
59 def read_file(self, file):
61 fp = open(str(file.rfile()))
62 except EnvironmentError, e:
63 self.missing.append((file, self.current_file))
68 def dictify_CPPDEFINES(env):
69 cppdefines = env.get('CPPDEFINES', {})
70 if cppdefines is None:
72 if SCons.Util.is_Sequence(cppdefines):
75 if SCons.Util.is_Sequence(c):
80 if not SCons.Util.is_Dict(cppdefines):
81 return {cppdefines : None}
84 class SConsCPPScannerWrapper:
86 The SCons wrapper around a cpp.py scanner.
88 This is the actual glue between the calling conventions of generic
89 SCons scanners, and the (subclass of) cpp.py class that knows how
90 to look for #include lines with reasonably real C-preprocessor-like
91 evaluation of #if/#ifdef/#else/#elif lines.
93 def __init__(self, name, variable):
95 self.path = SCons.Scanner.FindPathDirs(variable)
96 def __call__(self, node, env, path = ()):
97 cpp = SConsCPPScanner(current = node.get_dir(),
99 dict = dictify_CPPDEFINES(env))
101 for included, includer in cpp.missing:
102 fmt = "No dependency generated for file: %s (included from: %s) -- file not found"
103 SCons.Warnings.warn(SCons.Warnings.DependencyWarning,
104 fmt % (included, includer))
107 def recurse_nodes(self, nodes):
109 def select(self, node):
113 """Return a prototype Scanner instance for scanning source files
114 that use the C pre-processor"""
116 # Here's how we would (or might) use the CPP scanner code above that
117 # knows how to evaluate #if/#ifdef/#else/#elif lines when searching
118 # for #includes. This is commented out for now until we add the
119 # right configurability to let users pick between the scanners.
120 #return SConsCPPScannerWrapper("CScanner", "CPPPATH")
122 cs = SCons.Scanner.ClassicCPP("CScanner",
125 '^[ \t]*#[ \t]*(?:include|import)[ \t]*(<|")([^>"]+)(>|")')