cmDependsC.cxx 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. //----------------------------------------------------------------------------
  16. cmDependsC::cmDependsC(const char* dir, const char* targetFile):
  17. cmDepends(dir, targetFile),
  18. m_SourceFile(),
  19. m_IncludePath(0),
  20. m_IncludeRegexLine(),
  21. m_IncludeRegexScan(),
  22. m_IncludeRegexComplain()
  23. {
  24. }
  25. //----------------------------------------------------------------------------
  26. cmDependsC::cmDependsC(const char* dir, const char* targetFile,
  27. const char* sourceFile,
  28. std::vector<std::string> const& includes,
  29. const char* scanRegex, const char* complainRegex):
  30. cmDepends(dir, targetFile),
  31. m_SourceFile(sourceFile),
  32. m_IncludePath(&includes),
  33. m_IncludeRegexLine("^[ \t]*#[ \t]*include[ \t]*[<\"]([^\">]+)([\">])"),
  34. m_IncludeRegexScan(scanRegex),
  35. m_IncludeRegexComplain(complainRegex)
  36. {
  37. }
  38. //----------------------------------------------------------------------------
  39. cmDependsC::~cmDependsC()
  40. {
  41. }
  42. //----------------------------------------------------------------------------
  43. bool cmDependsC::WriteDependencies(std::ostream& os)
  44. {
  45. // Make sure this is a scanning instance.
  46. if(m_SourceFile == "")
  47. {
  48. cmSystemTools::Error("Cannot scan dependencies without an source file.");
  49. return false;
  50. }
  51. if(!m_IncludePath)
  52. {
  53. cmSystemTools::Error("Cannot scan dependencies without an include path.");
  54. return false;
  55. }
  56. // Walk the dependency graph starting with the source file.
  57. bool first = true;
  58. UnscannedEntry root;
  59. root.FileName = m_SourceFile;
  60. m_Unscanned.push(root);
  61. m_Encountered.clear();
  62. m_Encountered.insert(m_SourceFile);
  63. std::set<cmStdString> dependencies;
  64. std::set<cmStdString> scanned;
  65. while(!m_Unscanned.empty())
  66. {
  67. // Get the next file to scan.
  68. UnscannedEntry current = m_Unscanned.front();
  69. m_Unscanned.pop();
  70. // If not a full path, find the file in the include path.
  71. std::string fullName;
  72. if(first || cmSystemTools::FileIsFullPath(current.FileName.c_str()))
  73. {
  74. if(cmSystemTools::FileExists(current.FileName.c_str()))
  75. {
  76. fullName = current.FileName;
  77. }
  78. }
  79. else if(!current.QuotedLocation.empty() &&
  80. cmSystemTools::FileExists(current.QuotedLocation.c_str()))
  81. {
  82. // The include statement producing this entry was a double-quote
  83. // include and the included file is present in the directory of
  84. // the source containing the include statement.
  85. fullName = current.QuotedLocation;
  86. }
  87. else
  88. {
  89. for(std::vector<std::string>::const_iterator i = m_IncludePath->begin();
  90. i != m_IncludePath->end(); ++i)
  91. {
  92. // Construct the name of the file as if it were in the current
  93. // include directory. Avoid using a leading "./".
  94. std::string temp = *i;
  95. if(temp == ".")
  96. {
  97. temp = "";
  98. }
  99. else
  100. {
  101. temp += "/";
  102. }
  103. temp += current.FileName;
  104. // Look for the file in this location.
  105. if(cmSystemTools::FileExists(temp.c_str()))
  106. {
  107. fullName = temp;
  108. break;
  109. }
  110. }
  111. }
  112. // Complain if the file cannot be found and matches the complain
  113. // regex.
  114. if(fullName.empty() &&
  115. m_IncludeRegexComplain.find(current.FileName.c_str()))
  116. {
  117. cmSystemTools::Error("Cannot find file \"",
  118. current.FileName.c_str(), "\".");
  119. return false;
  120. }
  121. // Scan the file if it was found and has not been scanned already.
  122. if(!fullName.empty() && (scanned.find(fullName) == scanned.end()))
  123. {
  124. // Record scanned files.
  125. scanned.insert(fullName);
  126. // Try to scan the file. Just leave it out if we cannot find
  127. // it.
  128. std::ifstream fin(fullName.c_str());
  129. if(fin)
  130. {
  131. // Add this file as a dependency.
  132. dependencies.insert(fullName);
  133. // Scan this file for new dependencies. Pass the directory
  134. // containing the file to handle double-quote includes.
  135. std::string dir = cmSystemTools::GetFilenamePath(fullName);
  136. this->Scan(fin, dir.c_str());
  137. }
  138. }
  139. first = false;
  140. }
  141. // Write the dependencies to the output stream.
  142. for(std::set<cmStdString>::iterator i=dependencies.begin();
  143. i != dependencies.end(); ++i)
  144. {
  145. os << m_TargetFile.c_str() << ": "
  146. << cmSystemTools::ConvertToOutputPath(i->c_str()).c_str()
  147. << std::endl;
  148. }
  149. os << std::endl;
  150. return true;
  151. }
  152. //----------------------------------------------------------------------------
  153. bool cmDependsC::CheckDependencies(std::istream& is)
  154. {
  155. // Parse dependencies from the stream. If any dependee is missing
  156. // or newer than the depender then dependencies should be
  157. // regenerated.
  158. bool okay = true;
  159. std::string line;
  160. std::string depender;
  161. std::string dependee;
  162. while(cmSystemTools::GetLineFromStream(is, line))
  163. {
  164. // Skip empty lines and comments.
  165. std::string::size_type pos = line.find_first_not_of(" \t\r\n");
  166. if(pos == std::string::npos || line[pos] == '#')
  167. {
  168. continue;
  169. }
  170. // Strip leading whitespace.
  171. if(pos > 0)
  172. {
  173. line = line.substr(pos);
  174. }
  175. // Skip lines too short to have a dependency.
  176. if(line.size() < 3)
  177. {
  178. continue;
  179. }
  180. // Find the colon on the line. Skip the first two characters to
  181. // avoid finding the colon in a drive letter on Windows. Ignore
  182. // the line if a colon cannot be found.
  183. if((pos = line.find(':', 2)) == std::string::npos)
  184. {
  185. continue;
  186. }
  187. // Split the line into depender and dependee.
  188. depender = line.substr(0, pos);
  189. dependee = line.substr(pos+1);
  190. // Strip whitespace from the dependee.
  191. if((pos = dependee.find_first_not_of(" \t\r\n")) != std::string::npos &&
  192. pos > 0)
  193. {
  194. dependee = dependee.substr(pos);
  195. }
  196. if((pos = dependee.find_last_not_of(" \t\r\n")) != std::string::npos)
  197. {
  198. dependee = dependee.substr(0, pos+1);
  199. }
  200. // Strip whitespace from the depender.
  201. if((pos = depender.find_last_not_of(" \t\r\n")) != std::string::npos)
  202. {
  203. depender = depender.substr(0, pos+1);
  204. }
  205. // Dependencies must be regenerated if the dependee does not exist
  206. // or if the depender exists and is older than the dependee.
  207. int result = 0;
  208. if(!cmSystemTools::FileExists(dependee.c_str()) ||
  209. (cmSystemTools::FileExists(depender.c_str()) &&
  210. (!cmSystemTools::FileTimeCompare(depender.c_str(), dependee.c_str(),
  211. &result) || result < 0)))
  212. {
  213. // Dependencies must be regenerated.
  214. okay = false;
  215. // Remove the depender to be sure it is rebuilt.
  216. cmSystemTools::RemoveFile(depender.c_str());
  217. }
  218. }
  219. return okay;
  220. }
  221. //----------------------------------------------------------------------------
  222. void cmDependsC::Scan(std::istream& is, const char* directory)
  223. {
  224. // Read one line at a time.
  225. std::string line;
  226. while(cmSystemTools::GetLineFromStream(is, line))
  227. {
  228. // Match include directives.
  229. if(m_IncludeRegexLine.find(line.c_str()))
  230. {
  231. // Get the file being included.
  232. UnscannedEntry entry;
  233. entry.FileName = m_IncludeRegexLine.match(1);
  234. if(m_IncludeRegexLine.match(2) == "\"")
  235. {
  236. // This was a double-quoted include. We must check for the
  237. // file in the directory containing the file we are scanning.
  238. entry.QuotedLocation = directory;
  239. entry.QuotedLocation += "/";
  240. entry.QuotedLocation += entry.FileName;
  241. }
  242. // Queue the file if it has not yet been encountered and it
  243. // matches the regular expression for recursive scanning. Note
  244. // that this check does not account for the possibility of two
  245. // headers with the same name in different directories when one
  246. // is included by double-quotes and the other by angle brackets.
  247. // This kind of problem will be fixed when a more
  248. // preprocessor-like implementation of this scanner is created.
  249. if(m_Encountered.find(entry.FileName) == m_Encountered.end() &&
  250. m_IncludeRegexScan.find(entry.FileName.c_str()))
  251. {
  252. m_Encountered.insert(entry.FileName);
  253. m_Unscanned.push(entry);
  254. }
  255. }
  256. }
  257. }