1# MIT License 2# 3# Copyright The SCons Foundation 4# 5# Permission is hereby granted, free of charge, to any person obtaining 6# a copy of this software and associated documentation files (the 7# "Software"), to deal in the Software without restriction, including 8# without limitation the rights to use, copy, modify, merge, publish, 9# distribute, sublicense, and/or sell copies of the Software, and to 10# permit persons to whom the Software is furnished to do so, subject to 11# the following conditions: 12# 13# The above copyright notice and this permission notice shall be included 14# in all copies or substantial portions of the Software. 15# 16# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY 17# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE 18# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 20# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 21# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 22# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 24"""Dependency scanner for C/C++ code.""" 25 26import SCons.Node.FS 27import SCons.Scanner 28import SCons.Util 29 30import SCons.cpp 31 32class SConsCPPScanner(SCons.cpp.PreProcessor): 33 """SCons-specific subclass of the cpp.py module's processing. 34 35 We subclass this so that: 1) we can deal with files represented 36 by Nodes, not strings; 2) we can keep track of the files that are 37 missing. 38 """ 39 def __init__(self, *args, **kw): 40 SCons.cpp.PreProcessor.__init__(self, *args, **kw) 41 self.missing = [] 42 def initialize_result(self, fname): 43 self.result = SCons.Util.UniqueList([fname]) 44 def finalize_result(self, fname): 45 return self.result[1:] 46 def find_include_file(self, t): 47 keyword, quote, fname = t 48 result = SCons.Node.FS.find_file(fname, self.searchpath[quote]) 49 if not result: 50 self.missing.append((fname, self.current_file)) 51 return result 52 def read_file(self, file): 53 try: 54 with open(str(file.rfile())) as fp: 55 return fp.read() 56 except EnvironmentError as e: 57 self.missing.append((file, self.current_file)) 58 return '' 59 60def dictify_CPPDEFINES(env): 61 cppdefines = env.get('CPPDEFINES', {}) 62 if cppdefines is None: 63 return {} 64 if SCons.Util.is_Sequence(cppdefines): 65 result = {} 66 for c in cppdefines: 67 if SCons.Util.is_Sequence(c): 68 result[c[0]] = c[1] 69 else: 70 result[c] = None 71 return result 72 if not SCons.Util.is_Dict(cppdefines): 73 return {cppdefines : None} 74 return cppdefines 75 76class SConsCPPScannerWrapper: 77 """The SCons wrapper around a cpp.py scanner. 78 79 This is the actual glue between the calling conventions of generic 80 SCons scanners, and the (subclass of) cpp.py class that knows how 81 to look for #include lines with reasonably real C-preprocessor-like 82 evaluation of #if/#ifdef/#else/#elif lines. 83 """ 84 def __init__(self, name, variable): 85 self.name = name 86 self.path = SCons.Scanner.FindPathDirs(variable) 87 def __call__(self, node, env, path = ()): 88 cpp = SConsCPPScanner(current = node.get_dir(), 89 cpppath = path, 90 dict = dictify_CPPDEFINES(env)) 91 result = cpp(node) 92 for included, includer in cpp.missing: 93 fmt = "No dependency generated for file: %s (included from: %s) -- file not found" 94 SCons.Warnings.warn(SCons.Warnings.DependencyWarning, 95 fmt % (included, includer)) 96 return result 97 98 def recurse_nodes(self, nodes): 99 return nodes 100 def select(self, node): 101 return self 102 103def CScanner(): 104 """Return a prototype Scanner instance for scanning source files 105 that use the C pre-processor""" 106 107 # Here's how we would (or might) use the CPP scanner code above that 108 # knows how to evaluate #if/#ifdef/#else/#elif lines when searching 109 # for #includes. This is commented out for now until we add the 110 # right configurability to let users pick between the scanners. 111 # return SConsCPPScannerWrapper("CScanner", "CPPPATH") 112 113 cs = SCons.Scanner.ClassicCPP( 114 "CScanner", 115 "$CPPSUFFIXES", 116 "CPPPATH", 117 r'^[ \t]*#[ \t]*(?:include|import)[ \t]*(<|")([^>"]+)(>|")', 118 ) 119 return cs 120 121 122# 123# ConditionalScanner 124# 125 126 127class SConsCPPConditionalScanner(SCons.cpp.PreProcessor): 128 """SCons-specific subclass of the cpp.py module's processing. 129 130 We subclass this so that: 1) we can deal with files represented 131 by Nodes, not strings; 2) we can keep track of the files that are 132 missing. 133 """ 134 135 def __init__(self, *args, **kw): 136 SCons.cpp.PreProcessor.__init__(self, *args, **kw) 137 self.missing = [] 138 self._known_paths = [] 139 140 def initialize_result(self, fname): 141 self.result = SCons.Util.UniqueList([fname]) 142 143 def find_include_file(self, t): 144 keyword, quote, fname = t 145 paths = tuple(self._known_paths) + self.searchpath[quote] 146 if quote == '"': 147 paths = (self.current_file.dir,) + paths 148 result = SCons.Node.FS.find_file(fname, paths) 149 if result: 150 result_path = result.get_abspath() 151 for p in self.searchpath[quote]: 152 if result_path.startswith(p.get_abspath()): 153 self._known_paths.append(p) 154 break 155 else: 156 self.missing.append((fname, self.current_file)) 157 return result 158 159 def read_file(self, file): 160 try: 161 with open(str(file.rfile())) as fp: 162 return fp.read() 163 except EnvironmentError: 164 self.missing.append((file, self.current_file)) 165 return "" 166 167 168class SConsCPPConditionalScannerWrapper: 169 """ 170 The SCons wrapper around a cpp.py scanner. 171 172 This is the actual glue between the calling conventions of generic 173 SCons scanners, and the (subclass of) cpp.py class that knows how 174 to look for #include lines with reasonably real C-preprocessor-like 175 evaluation of #if/#ifdef/#else/#elif lines. 176 """ 177 178 def __init__(self, name, variable): 179 self.name = name 180 self.path = SCons.Scanner.FindPathDirs(variable) 181 182 def __call__(self, node, env, path=(), depth=-1): 183 cpp = SConsCPPConditionalScanner( 184 current=node.get_dir(), 185 cpppath=path, 186 dict=dictify_CPPDEFINES(env), 187 depth=depth, 188 ) 189 result = cpp(node) 190 for included, includer in cpp.missing: 191 fmt = "No dependency generated for file: %s (included from: %s) -- file not found" 192 SCons.Warnings.warn( 193 SCons.Warnings.DependencyWarning, fmt % (included, includer) 194 ) 195 return result 196 197 def recurse_nodes(self, nodes): 198 return nodes 199 200 def select(self, node): 201 return self 202 203 204def CConditionalScanner(): 205 """ 206 Return an advanced conditional Scanner instance for scanning source files 207 208 Interprets C/C++ Preprocessor conditional syntax 209 (#ifdef, #if, defined, #else, #elif, etc.). 210 """ 211 return SConsCPPConditionalScannerWrapper("CConditionalScanner", "CPPPATH") 212 213 214# Local Variables: 215# tab-width:4 216# indent-tabs-mode:nil 217# End: 218# vim: set expandtab tabstop=4 shiftwidth=4: 219