cmDependsC.cxx 9.9 KB

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