cmDependsC.cxx 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmDependsC.h"
  14. #include "cmSystemTools.h"
  15. #include <ctype.h> // isspace
  16. //----------------------------------------------------------------------------
  17. cmDependsC::cmDependsC():
  18. m_IncludePath(0), m_GeneratedFiles(0)
  19. {
  20. }
  21. //----------------------------------------------------------------------------
  22. // yummy look at all those constructor arguments
  23. cmDependsC::cmDependsC(std::vector<std::string> const& includes,
  24. const char* scanRegex, const char* complainRegex,
  25. std::set<cmStdString> const& generatedFiles):
  26. m_IncludePath(&includes),
  27. m_IncludeRegexLine("^[ \t]*#[ \t]*include[ \t]*[<\"]([^\">]+)([\">])"),
  28. m_IncludeRegexScan(scanRegex),
  29. m_IncludeRegexComplain(complainRegex),
  30. m_GeneratedFiles(&generatedFiles)
  31. {
  32. }
  33. //----------------------------------------------------------------------------
  34. cmDependsC::~cmDependsC()
  35. {
  36. }
  37. //----------------------------------------------------------------------------
  38. bool cmDependsC::WriteDependencies(const char *src, const char *obj,
  39. std::ostream& makeDepends, std::ostream& internalDepends)
  40. {
  41. // Make sure this is a scanning instance.
  42. if(!src || src[0] == '\0')
  43. {
  44. cmSystemTools::Error("Cannot scan dependencies without a source file.");
  45. return false;
  46. }
  47. if(!obj || obj[0] == '\0')
  48. {
  49. cmSystemTools::Error("Cannot scan dependencies without an object file.");
  50. return false;
  51. }
  52. if(!m_IncludePath)
  53. {
  54. cmSystemTools::Error("Cannot scan dependencies without an include path.");
  55. return false;
  56. }
  57. // Walk the dependency graph starting with the source file.
  58. bool first = true;
  59. UnscannedEntry root;
  60. root.FileName = src;
  61. m_Unscanned.push(root);
  62. m_Encountered.clear();
  63. m_Encountered.insert(src);
  64. std::set<cmStdString> dependencies;
  65. std::set<cmStdString> scanned;
  66. while(!m_Unscanned.empty())
  67. {
  68. // Get the next file to scan.
  69. UnscannedEntry current = m_Unscanned.front();
  70. m_Unscanned.pop();
  71. // If not a full path, find the file in the include path.
  72. std::string fullName;
  73. if(first || cmSystemTools::FileIsFullPath(current.FileName.c_str()))
  74. {
  75. if(this->FileExistsOrIsGenerated(current.FileName, scanned,
  76. dependencies))
  77. {
  78. fullName = current.FileName;
  79. }
  80. }
  81. else if(!current.QuotedLocation.empty() &&
  82. this->FileExistsOrIsGenerated(current.QuotedLocation, scanned,
  83. dependencies))
  84. {
  85. // The include statement producing this entry was a double-quote
  86. // include and the included file is present in the directory of
  87. // the source containing the include statement.
  88. fullName = current.QuotedLocation;
  89. }
  90. else
  91. {
  92. for(std::vector<std::string>::const_iterator i = m_IncludePath->begin();
  93. i != m_IncludePath->end(); ++i)
  94. {
  95. // Construct the name of the file as if it were in the current
  96. // include directory. Avoid using a leading "./".
  97. std::string temp = *i;
  98. if(temp == ".")
  99. {
  100. temp = "";
  101. }
  102. else
  103. {
  104. temp += "/";
  105. }
  106. temp += current.FileName;
  107. // Look for the file in this location.
  108. if(this->FileExistsOrIsGenerated(temp, scanned, dependencies))
  109. {
  110. fullName = temp;
  111. break;
  112. }
  113. }
  114. }
  115. // Complain if the file cannot be found and matches the complain
  116. // regex.
  117. if(fullName.empty() &&
  118. m_IncludeRegexComplain.find(current.FileName.c_str()))
  119. {
  120. cmSystemTools::Error("Cannot find file \"",
  121. current.FileName.c_str(), "\".");
  122. return false;
  123. }
  124. // Scan the file if it was found and has not been scanned already.
  125. if(!fullName.empty() && (scanned.find(fullName) == scanned.end()))
  126. {
  127. // Record scanned files.
  128. scanned.insert(fullName);
  129. // Try to scan the file. Just leave it out if we cannot find
  130. // it.
  131. std::ifstream fin(fullName.c_str());
  132. if(fin)
  133. {
  134. // Add this file as a dependency.
  135. dependencies.insert(fullName);
  136. // Scan this file for new dependencies. Pass the directory
  137. // containing the file to handle double-quote includes.
  138. std::string dir = cmSystemTools::GetFilenamePath(fullName);
  139. this->Scan(fin, dir.c_str());
  140. }
  141. }
  142. first = false;
  143. }
  144. // Write the dependencies to the output stream.
  145. internalDepends << obj << std::endl;
  146. for(std::set<cmStdString>::iterator i=dependencies.begin();
  147. i != dependencies.end(); ++i)
  148. {
  149. makeDepends << obj << ": "
  150. << cmSystemTools::ConvertToOutputPath(i->c_str()).c_str()
  151. << std::endl;
  152. internalDepends << " " << i->c_str() << std::endl;
  153. }
  154. makeDepends << std::endl;
  155. return true;
  156. }
  157. //----------------------------------------------------------------------------
  158. void cmDependsC::Scan(std::istream& is, const char* directory)
  159. {
  160. // Read one line at a time.
  161. std::string line;
  162. while(cmSystemTools::GetLineFromStream(is, line))
  163. {
  164. // Match include directives.
  165. if(m_IncludeRegexLine.find(line.c_str()))
  166. {
  167. // Get the file being included.
  168. UnscannedEntry entry;
  169. entry.FileName = m_IncludeRegexLine.match(1);
  170. if(m_IncludeRegexLine.match(2) == "\"")
  171. {
  172. // This was a double-quoted include. We must check for the
  173. // file in the directory containing the file we are scanning.
  174. entry.QuotedLocation = directory;
  175. entry.QuotedLocation += "/";
  176. entry.QuotedLocation += entry.FileName;
  177. }
  178. // Queue the file if it has not yet been encountered and it
  179. // matches the regular expression for recursive scanning. Note
  180. // that this check does not account for the possibility of two
  181. // headers with the same name in different directories when one
  182. // is included by double-quotes and the other by angle brackets.
  183. // This kind of problem will be fixed when a more
  184. // preprocessor-like implementation of this scanner is created.
  185. if(m_Encountered.find(entry.FileName) == m_Encountered.end() &&
  186. m_IncludeRegexScan.find(entry.FileName.c_str()))
  187. {
  188. m_Encountered.insert(entry.FileName);
  189. m_Unscanned.push(entry);
  190. }
  191. }
  192. }
  193. }
  194. //----------------------------------------------------------------------------
  195. bool cmDependsC::FileExistsOrIsGenerated(const std::string& fname,
  196. std::set<cmStdString>& scanned,
  197. std::set<cmStdString>& dependencies)
  198. {
  199. // Check first for a generated file.
  200. if(m_GeneratedFiles &&
  201. std::set<cmStdString>::const_iterator(m_GeneratedFiles->find(fname)) !=
  202. m_GeneratedFiles->end())
  203. {
  204. // If the file does not really exist yet pretend it has already
  205. // been scanned. When it exists later then dependencies will be
  206. // rescanned.
  207. if(!cmSystemTools::FileExists(fname.c_str()))
  208. {
  209. scanned.insert(fname);
  210. dependencies.insert(fname);
  211. }
  212. return true;
  213. }
  214. else
  215. {
  216. return cmSystemTools::FileExists(fname.c_str());
  217. }
  218. }