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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
"""SCons.Scanner.C
This module implements the depenency scanner for C/C++ code.
"""
#
# Copyright (c) 2001, 2002 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.Node
import SCons.Node.FS
import SCons.Scanner
import SCons.Util
import SCons.Warnings
include_re = re.compile('^[ \t]*#[ \t]*include[ \t]+(<|")([^>"]+)(>|")', re.M)
def CScan(fs = SCons.Node.FS.default_fs):
"""Return a prototype Scanner instance for scanning source files
that use the C pre-processor"""
cs = SCons.Scanner.Recursive(scan, "CScan", fs,
[".c", ".C", ".cxx", ".cpp", ".c++", ".cc",
".h", ".H", ".hxx", ".hpp", ".hh",
".F", ".fpp", ".FPP"])
return cs
def scan(node, env, target, fs = SCons.Node.FS.default_fs):
"""
scan(node, Environment) -> [node]
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.
"""
# 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'):
def Dir(x, dir=target.cwd, fs=fs): return fs.Dir(x,dir)
try:
target.cpppath = tuple(fs.Rsearchall(env['CPPPATH'], Dir))
except KeyError:
target.cpppath = ()
cpppath = target.cpppath
if not node.found_includes.has_key(cpppath):
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
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):
return str(node)
node.found_includes[cpppath] = st(nodes, normalize)
else:
node.found_includes[cpppath] = []
return node.found_includes[cpppath]
|