You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 lines
4.7 KiB

6 years ago
  1. """SCons.Scanner.C
  2. This module implements the dependency scanner for C/C++ code.
  3. """
  4. #
  5. # Copyright (c) 2001 - 2017 The SCons Foundation
  6. #
  7. # Permission is hereby granted, free of charge, to any person obtaining
  8. # a copy of this software and associated documentation files (the
  9. # "Software"), to deal in the Software without restriction, including
  10. # without limitation the rights to use, copy, modify, merge, publish,
  11. # distribute, sublicense, and/or sell copies of the Software, and to
  12. # permit persons to whom the Software is furnished to do so, subject to
  13. # the following conditions:
  14. #
  15. # The above copyright notice and this permission notice shall be included
  16. # in all copies or substantial portions of the Software.
  17. #
  18. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
  19. # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  20. # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  21. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  22. # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  23. # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  24. # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. #
  26. __revision__ = "src/engine/SCons/Scanner/C.py rel_3.0.0:4395:8972f6a2f699 2017/09/18 12:59:24 bdbaddog"
  27. import SCons.Node.FS
  28. import SCons.Scanner
  29. import SCons.Util
  30. import SCons.cpp
  31. class SConsCPPScanner(SCons.cpp.PreProcessor):
  32. """
  33. SCons-specific subclass of the cpp.py module's processing.
  34. We subclass this so that: 1) we can deal with files represented
  35. by Nodes, not strings; 2) we can keep track of the files that are
  36. missing.
  37. """
  38. def __init__(self, *args, **kw):
  39. SCons.cpp.PreProcessor.__init__(self, *args, **kw)
  40. self.missing = []
  41. def initialize_result(self, fname):
  42. self.result = SCons.Util.UniqueList([fname])
  43. def finalize_result(self, fname):
  44. return self.result[1:]
  45. def find_include_file(self, t):
  46. keyword, quote, fname = t
  47. result = SCons.Node.FS.find_file(fname, self.searchpath[quote])
  48. if not result:
  49. self.missing.append((fname, self.current_file))
  50. return result
  51. def read_file(self, file):
  52. try:
  53. with open(str(file.rfile())) as fp:
  54. return fp.read()
  55. except EnvironmentError as e:
  56. self.missing.append((file, self.current_file))
  57. return ''
  58. def dictify_CPPDEFINES(env):
  59. cppdefines = env.get('CPPDEFINES', {})
  60. if cppdefines is None:
  61. return {}
  62. if SCons.Util.is_Sequence(cppdefines):
  63. result = {}
  64. for c in cppdefines:
  65. if SCons.Util.is_Sequence(c):
  66. result[c[0]] = c[1]
  67. else:
  68. result[c] = None
  69. return result
  70. if not SCons.Util.is_Dict(cppdefines):
  71. return {cppdefines : None}
  72. return cppdefines
  73. class SConsCPPScannerWrapper(object):
  74. """
  75. The SCons wrapper around a cpp.py scanner.
  76. This is the actual glue between the calling conventions of generic
  77. SCons scanners, and the (subclass of) cpp.py class that knows how
  78. to look for #include lines with reasonably real C-preprocessor-like
  79. evaluation of #if/#ifdef/#else/#elif lines.
  80. """
  81. def __init__(self, name, variable):
  82. self.name = name
  83. self.path = SCons.Scanner.FindPathDirs(variable)
  84. def __call__(self, node, env, path = ()):
  85. cpp = SConsCPPScanner(current = node.get_dir(),
  86. cpppath = path,
  87. dict = dictify_CPPDEFINES(env))
  88. result = cpp(node)
  89. for included, includer in cpp.missing:
  90. fmt = "No dependency generated for file: %s (included from: %s) -- file not found"
  91. SCons.Warnings.warn(SCons.Warnings.DependencyWarning,
  92. fmt % (included, includer))
  93. return result
  94. def recurse_nodes(self, nodes):
  95. return nodes
  96. def select(self, node):
  97. return self
  98. def CScanner():
  99. """Return a prototype Scanner instance for scanning source files
  100. that use the C pre-processor"""
  101. # Here's how we would (or might) use the CPP scanner code above that
  102. # knows how to evaluate #if/#ifdef/#else/#elif lines when searching
  103. # for #includes. This is commented out for now until we add the
  104. # right configurability to let users pick between the scanners.
  105. #return SConsCPPScannerWrapper("CScanner", "CPPPATH")
  106. cs = SCons.Scanner.ClassicCPP("CScanner",
  107. "$CPPSUFFIXES",
  108. "CPPPATH",
  109. '^[ \t]*#[ \t]*(?:include|import)[ \t]*(<|")([^>"]+)(>|")')
  110. return cs
  111. # Local Variables:
  112. # tab-width:4
  113. # indent-tabs-mode:nil
  114. # End:
  115. # vim: set expandtab tabstop=4 shiftwidth=4: