Package SCons :: Package Scanner :: Module C
[hide private]
[frames] | no frames]

Source Code for Module SCons.Scanner.C

  1  """SCons.Scanner.C 
  2   
  3  This module implements the dependency scanner for C/C++ code.  
  4   
  5  """ 
  6   
  7  # 
  8  # Copyright (c) 2001 - 2019 The SCons Foundation 
  9  # 
 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: 
 17  # 
 18  # The above copyright notice and this permission notice shall be included 
 19  # in all copies or substantial portions of the Software. 
 20  # 
 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. 
 28  # 
 29   
 30  __revision__ = "src/engine/SCons/Scanner/C.py 3a41ed6b288cee8d085373ad7fa02894e1903864 2019-01-23 17:30:35 bdeegan" 
 31   
 32  import SCons.Node.FS 
 33  import SCons.Scanner 
 34  import SCons.Util 
 35   
 36  import SCons.cpp 
 37   
38 -class SConsCPPScanner(SCons.cpp.PreProcessor):
39 """ 40 SCons-specific subclass of the cpp.py module's processing. 41 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 44 missing. 45 """
46 - def __init__(self, *args, **kw):
47 SCons.cpp.PreProcessor.__init__(self, *args, **kw) 48 self.missing = []
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]) 56 if not result: 57 self.missing.append((fname, self.current_file)) 58 return result
59 - def read_file(self, file):
60 try: 61 with open(str(file.rfile())) as fp: 62 return fp.read() 63 except EnvironmentError as e: 64 self.missing.append((file, self.current_file)) 65 return ''
66
67 -def dictify_CPPDEFINES(env):
68 cppdefines = env.get('CPPDEFINES', {}) 69 if cppdefines is None: 70 return {} 71 if SCons.Util.is_Sequence(cppdefines): 72 result = {} 73 for c in cppdefines: 74 if SCons.Util.is_Sequence(c): 75 result[c[0]] = c[1] 76 else: 77 result[c] = None 78 return result 79 if not SCons.Util.is_Dict(cppdefines): 80 return {cppdefines : None} 81 return cppdefines
82
83 -class SConsCPPScannerWrapper(object):
84 """ 85 The SCons wrapper around a cpp.py scanner. 86 87 This is the actual glue between the calling conventions of generic 88 SCons scanners, and the (subclass of) cpp.py class that knows how 89 to look for #include lines with reasonably real C-preprocessor-like 90 evaluation of #if/#ifdef/#else/#elif lines. 91 """
92 - def __init__(self, name, variable):
93 self.name = name 94 self.path = SCons.Scanner.FindPathDirs(variable)
95 - def __call__(self, node, env, path = ()):
96 cpp = SConsCPPScanner(current = node.get_dir(), 97 cpppath = path, 98 dict = dictify_CPPDEFINES(env)) 99 result = cpp(node) 100 for included, includer in cpp.missing: 101 fmt = "No dependency generated for file: %s (included from: %s) -- file not found" 102 SCons.Warnings.warn(SCons.Warnings.DependencyWarning, 103 fmt % (included, includer)) 104 return result
105
106 - def recurse_nodes(self, nodes):
107 return nodes
108 - def select(self, node):
109 return self
110
111 -def CScanner():
112 """Return a prototype Scanner instance for scanning source files 113 that use the C pre-processor""" 114 115 # Here's how we would (or might) use the CPP scanner code above that 116 # knows how to evaluate #if/#ifdef/#else/#elif lines when searching 117 # for #includes. This is commented out for now until we add the 118 # right configurability to let users pick between the scanners. 119 #return SConsCPPScannerWrapper("CScanner", "CPPPATH") 120 121 cs = SCons.Scanner.ClassicCPP("CScanner", 122 "$CPPSUFFIXES", 123 "CPPPATH", 124 '^[ \t]*#[ \t]*(?:include|import)[ \t]*(<|")([^>"]+)(>|")') 125 return cs
126 127 # Local Variables: 128 # tab-width:4 129 # indent-tabs-mode:nil 130 # End: 131 # vim: set expandtab tabstop=4 shiftwidth=4: 132