cmDependsC.cxx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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,
  39. const char *obj, std::ostream& os)
  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. for(std::set<cmStdString>::iterator i=dependencies.begin();
  146. i != dependencies.end(); ++i)
  147. {
  148. os << obj << ": "
  149. << cmSystemTools::ConvertToOutputPath(i->c_str()).c_str()
  150. << std::endl;
  151. }
  152. os << std::endl;
  153. return true;
  154. }
  155. //----------------------------------------------------------------------------
  156. bool cmDependsC::CheckDependencies(std::istream& is)
  157. {
  158. // Parse dependencies from the stream. If any dependee is missing
  159. // or newer than the depender then dependencies should be
  160. // regenerated.
  161. bool okay = true;
  162. std::string line;
  163. std::string depender;
  164. std::string dependee;
  165. while(cmSystemTools::GetLineFromStream(is, line))
  166. {
  167. // Parse the dependency line.
  168. if(!this->ParseDependency(line.c_str(), depender, dependee))
  169. {
  170. continue;
  171. }
  172. // Dependencies must be regenerated if the dependee does not exist
  173. // or if the depender exists and is older than the dependee.
  174. bool regenerate = false;
  175. if(!cmSystemTools::FileExists(dependee.c_str()))
  176. {
  177. // The dependee does not exist.
  178. regenerate = true;
  179. // Print verbose output.
  180. if(m_Verbose)
  181. {
  182. cmOStringStream msg;
  183. msg << "Dependee \"" << dependee
  184. << "\" does not exist for depender \""
  185. << depender << "\"." << std::endl;
  186. cmSystemTools::Stdout(msg.str().c_str());
  187. }
  188. }
  189. else if(cmSystemTools::FileExists(depender.c_str()))
  190. {
  191. // The dependee and depender both exist. Compare file times.
  192. int result = 0;
  193. if((!cmSystemTools::FileTimeCompare(depender.c_str(), dependee.c_str(),
  194. &result) || result < 0))
  195. {
  196. // The depender is older than the dependee.
  197. regenerate = true;
  198. // Print verbose output.
  199. if(m_Verbose)
  200. {
  201. cmOStringStream msg;
  202. msg << "Dependee \"" << dependee
  203. << "\" is newer than depender \""
  204. << depender << "\"." << std::endl;
  205. cmSystemTools::Stdout(msg.str().c_str());
  206. }
  207. }
  208. }
  209. if(regenerate)
  210. {
  211. // Dependencies must be regenerated.
  212. okay = false;
  213. // Remove the depender to be sure it is rebuilt.
  214. cmSystemTools::RemoveFile(depender.c_str());
  215. }
  216. }
  217. return okay;
  218. }
  219. //----------------------------------------------------------------------------
  220. void cmDependsC::Scan(std::istream& is, const char* directory)
  221. {
  222. // Read one line at a time.
  223. std::string line;
  224. while(cmSystemTools::GetLineFromStream(is, line))
  225. {
  226. // Match include directives.
  227. if(m_IncludeRegexLine.find(line.c_str()))
  228. {
  229. // Get the file being included.
  230. UnscannedEntry entry;
  231. entry.FileName = m_IncludeRegexLine.match(1);
  232. if(m_IncludeRegexLine.match(2) == "\"")
  233. {
  234. // This was a double-quoted include. We must check for the
  235. // file in the directory containing the file we are scanning.
  236. entry.QuotedLocation = directory;
  237. entry.QuotedLocation += "/";
  238. entry.QuotedLocation += entry.FileName;
  239. }
  240. // Queue the file if it has not yet been encountered and it
  241. // matches the regular expression for recursive scanning. Note
  242. // that this check does not account for the possibility of two
  243. // headers with the same name in different directories when one
  244. // is included by double-quotes and the other by angle brackets.
  245. // This kind of problem will be fixed when a more
  246. // preprocessor-like implementation of this scanner is created.
  247. if(m_Encountered.find(entry.FileName) == m_Encountered.end() &&
  248. m_IncludeRegexScan.find(entry.FileName.c_str()))
  249. {
  250. m_Encountered.insert(entry.FileName);
  251. m_Unscanned.push(entry);
  252. }
  253. }
  254. }
  255. }
  256. //----------------------------------------------------------------------------
  257. bool cmDependsC::ParseDependency(const char* line, std::string& depender,
  258. std::string& dependee)
  259. {
  260. // Start with empty names.
  261. depender = "";
  262. dependee = "";
  263. // Get the left-hand-side of the dependency.
  264. const char* c = this->ParseFileName(line, depender);
  265. // Skip the ':' separator.
  266. for(;c && *c && isspace(*c);++c);
  267. if(!c || !*c || *c != ':')
  268. {
  269. return false;
  270. }
  271. ++c;
  272. // Get the right-hand-side of the dependency.
  273. return this->ParseFileName(c, dependee)?true:false;
  274. }
  275. //----------------------------------------------------------------------------
  276. const char* cmDependsC::ParseFileName(const char* in, std::string& name)
  277. {
  278. // Skip leading whitespace.
  279. const char* c = in;
  280. for(;c && *c && isspace(*c);++c);
  281. // If this is an empty line or a comment line return failure.
  282. if(!c || !*c || *c == '#')
  283. {
  284. return 0;
  285. }
  286. // Parse the possibly quoted file name.
  287. bool quoted = false;
  288. char* buf = new char[strlen(in)+1];
  289. char* pos = buf;
  290. // for every character while we haven't hit the end of the string AND we
  291. // are in a quoted string OR the current character isn't a : or the second
  292. // character AND it isn't a space
  293. for(;*c && (quoted ||
  294. ((*c != ':' || pos == buf+1) && !isspace(*c))); ++c)
  295. {
  296. if(*c == '"')
  297. {
  298. quoted = !quoted;
  299. }
  300. // handle unquoted escaped spaces
  301. else if(!quoted && *c == '\\' && isspace(*(c+1)))
  302. {
  303. *pos = *(++c);
  304. pos++;
  305. }
  306. else
  307. {
  308. *pos = *c;
  309. pos++;
  310. }
  311. }
  312. *pos =0;
  313. name += buf;
  314. delete [] buf;
  315. // Return the ending position.
  316. return c;
  317. }
  318. //----------------------------------------------------------------------------
  319. bool cmDependsC::FileExistsOrIsGenerated(const std::string& fname,
  320. std::set<cmStdString>& scanned,
  321. std::set<cmStdString>& dependencies)
  322. {
  323. // Check first for a generated file.
  324. if(m_GeneratedFiles &&
  325. m_GeneratedFiles->find(fname) != m_GeneratedFiles->end())
  326. {
  327. // If the file does not really exist yet pretend it has already
  328. // been scanned. When it exists later then dependencies will be
  329. // rescanned.
  330. if(!cmSystemTools::FileExists(fname.c_str()))
  331. {
  332. scanned.insert(fname);
  333. dependencies.insert(fname);
  334. }
  335. return true;
  336. }
  337. else
  338. {
  339. return cmSystemTools::FileExists(fname.c_str());
  340. }
  341. }