cmDependsC.cxx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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 "cmFileTimeComparison.h"
  16. #include <ctype.h> // isspace
  17. //----------------------------------------------------------------------------
  18. cmDependsC::cmDependsC():
  19. IncludePath(0), GeneratedFiles(0)
  20. {
  21. }
  22. //----------------------------------------------------------------------------
  23. // yummy look at all those constructor arguments
  24. cmDependsC::cmDependsC(std::vector<std::string> const& includes,
  25. const char* scanRegex, const char* complainRegex,
  26. std::set<cmStdString> const& generatedFiles,
  27. const cmStdString& cacheFileName):
  28. IncludePath(&includes),
  29. IncludeRegexLine("^[ \t]*#[ \t]*include[ \t]*[<\"]([^\">]+)([\">])"),
  30. IncludeRegexScan(scanRegex),
  31. IncludeRegexComplain(complainRegex),
  32. GeneratedFiles(&generatedFiles),
  33. CacheFileName(cacheFileName)
  34. {
  35. this->ReadCacheFile();
  36. }
  37. //----------------------------------------------------------------------------
  38. cmDependsC::~cmDependsC()
  39. {
  40. this->WriteCacheFile();
  41. for (std::map<cmStdString, cmIncludeLines*>::iterator it=
  42. this->fileCache.begin(); it!=this->fileCache.end(); ++it)
  43. {
  44. delete it->second;
  45. }
  46. }
  47. //----------------------------------------------------------------------------
  48. bool cmDependsC::WriteDependencies(const char *src, const char *obj,
  49. std::ostream& makeDepends, std::ostream& internalDepends)
  50. {
  51. // Make sure this is a scanning instance.
  52. if(!src || src[0] == '\0')
  53. {
  54. cmSystemTools::Error("Cannot scan dependencies without a source file.");
  55. return false;
  56. }
  57. if(!obj || obj[0] == '\0')
  58. {
  59. cmSystemTools::Error("Cannot scan dependencies without an object file.");
  60. return false;
  61. }
  62. if(!this->IncludePath)
  63. {
  64. cmSystemTools::Error("Cannot scan dependencies without an include path.");
  65. return false;
  66. }
  67. // Walk the dependency graph starting with the source file.
  68. bool first = true;
  69. UnscannedEntry root;
  70. root.FileName = src;
  71. this->Unscanned.push(root);
  72. this->Encountered.clear();
  73. this->Encountered.insert(src);
  74. std::set<cmStdString> dependencies;
  75. std::set<cmStdString> scanned;
  76. while(!this->Unscanned.empty())
  77. {
  78. // Get the next file to scan.
  79. UnscannedEntry current = this->Unscanned.front();
  80. this->Unscanned.pop();
  81. // If not a full path, find the file in the include path.
  82. std::string fullName;
  83. if(first || cmSystemTools::FileIsFullPath(current.FileName.c_str()))
  84. {
  85. if(this->FileExistsOrIsGenerated(current.FileName, scanned,
  86. dependencies))
  87. {
  88. fullName = current.FileName;
  89. }
  90. }
  91. else if(!current.QuotedLocation.empty() &&
  92. this->FileExistsOrIsGenerated(current.QuotedLocation, scanned,
  93. dependencies))
  94. {
  95. // The include statement producing this entry was a double-quote
  96. // include and the included file is present in the directory of
  97. // the source containing the include statement.
  98. fullName = current.QuotedLocation;
  99. }
  100. else
  101. {
  102. for(std::vector<std::string>::const_iterator i = this->IncludePath->begin();
  103. i != this->IncludePath->end(); ++i)
  104. {
  105. // Construct the name of the file as if it were in the current
  106. // include directory. Avoid using a leading "./".
  107. std::string temp = *i;
  108. if(temp == ".")
  109. {
  110. temp = "";
  111. }
  112. else
  113. {
  114. temp += "/";
  115. }
  116. temp += current.FileName;
  117. // Look for the file in this location.
  118. if(this->FileExistsOrIsGenerated(temp, scanned, dependencies))
  119. {
  120. fullName = temp;
  121. break;
  122. }
  123. }
  124. }
  125. // Complain if the file cannot be found and matches the complain
  126. // regex.
  127. if(fullName.empty() &&
  128. this->IncludeRegexComplain.find(current.FileName.c_str()))
  129. {
  130. cmSystemTools::Error("Cannot find file \"",
  131. current.FileName.c_str(), "\".");
  132. return false;
  133. }
  134. // Scan the file if it was found and has not been scanned already.
  135. if(!fullName.empty() && (scanned.find(fullName) == scanned.end()))
  136. {
  137. // Record scanned files.
  138. scanned.insert(fullName);
  139. // Check whether this file is already in the cache
  140. std::map<cmStdString, cmIncludeLines*>::iterator fileIt=
  141. this->fileCache.find(fullName);
  142. if (fileIt!=this->fileCache.end())
  143. {
  144. fileIt->second->Used=true;
  145. dependencies.insert(fullName);
  146. for (std::vector<UnscannedEntry>::const_iterator incIt=
  147. fileIt->second->UnscannedEntries.begin();
  148. incIt!=fileIt->second->UnscannedEntries.end(); ++incIt)
  149. {
  150. if (this->Encountered.find(incIt->FileName) ==
  151. this->Encountered.end())
  152. {
  153. this->Encountered.insert(incIt->FileName);
  154. this->Unscanned.push(*incIt);
  155. }
  156. }
  157. }
  158. else
  159. {
  160. // Try to scan the file. Just leave it out if we cannot find
  161. // it.
  162. std::ifstream fin(fullName.c_str());
  163. if(fin)
  164. {
  165. // Add this file as a dependency.
  166. dependencies.insert(fullName);
  167. // Scan this file for new dependencies. Pass the directory
  168. // containing the file to handle double-quote includes.
  169. std::string dir = cmSystemTools::GetFilenamePath(fullName);
  170. this->Scan(fin, dir.c_str(), fullName);
  171. }
  172. }
  173. }
  174. first = false;
  175. }
  176. // Write the dependencies to the output stream.
  177. internalDepends << obj << std::endl;
  178. for(std::set<cmStdString>::iterator i=dependencies.begin();
  179. i != dependencies.end(); ++i)
  180. {
  181. makeDepends << obj << ": "
  182. << cmSystemTools::ConvertToOutputPath(i->c_str()).c_str()
  183. << std::endl;
  184. internalDepends << " " << i->c_str() << std::endl;
  185. }
  186. makeDepends << std::endl;
  187. return true;
  188. }
  189. //----------------------------------------------------------------------------
  190. void cmDependsC::ReadCacheFile()
  191. {
  192. if(this->CacheFileName.size() == 0)
  193. {
  194. return;
  195. }
  196. std::ifstream fin(this->CacheFileName.c_str());
  197. if(!fin)
  198. {
  199. return;
  200. }
  201. std::string line;
  202. cmIncludeLines* cacheEntry=0;
  203. bool haveFileName=false;
  204. while(cmSystemTools::GetLineFromStream(fin, line))
  205. {
  206. if (line.empty())
  207. {
  208. cacheEntry=0;
  209. haveFileName=false;
  210. continue;
  211. }
  212. //the first line after an empty line is the name of the parsed file
  213. if (haveFileName==false)
  214. {
  215. haveFileName=true;
  216. int newer=0;
  217. cmFileTimeComparison comp;
  218. bool res=comp.FileTimeCompare(this->CacheFileName.c_str(),
  219. line.c_str(), &newer);
  220. if ((res==true) && (newer==1)) //cache is newer than the parsed file
  221. {
  222. cacheEntry=new cmIncludeLines;
  223. this->fileCache[line]=cacheEntry;
  224. }
  225. }
  226. else if (cacheEntry!=0)
  227. {
  228. UnscannedEntry entry;
  229. entry.FileName = line;
  230. if (cmSystemTools::GetLineFromStream(fin, line))
  231. {
  232. if (line!="-")
  233. {
  234. entry.QuotedLocation=line;
  235. }
  236. cacheEntry->UnscannedEntries.push_back(entry);
  237. }
  238. }
  239. }
  240. }
  241. //----------------------------------------------------------------------------
  242. void cmDependsC::WriteCacheFile() const
  243. {
  244. if(this->CacheFileName.size() == 0)
  245. {
  246. return;
  247. }
  248. std::ofstream cacheOut(this->CacheFileName.c_str());
  249. if(!cacheOut)
  250. {
  251. return;
  252. }
  253. for (std::map<cmStdString, cmIncludeLines*>::const_iterator fileIt=
  254. this->fileCache.begin();
  255. fileIt!=this->fileCache.end(); ++fileIt)
  256. {
  257. if (fileIt->second->Used)
  258. {
  259. cacheOut<<fileIt->first.c_str()<<std::endl;
  260. for (std::vector<UnscannedEntry>::const_iterator
  261. incIt=fileIt->second->UnscannedEntries.begin();
  262. incIt!=fileIt->second->UnscannedEntries.end(); ++incIt)
  263. {
  264. cacheOut<<incIt->FileName.c_str()<<std::endl;
  265. if (incIt->QuotedLocation.empty())
  266. {
  267. cacheOut<<"-"<<std::endl;
  268. }
  269. else
  270. {
  271. cacheOut<<incIt->QuotedLocation.c_str()<<std::endl;
  272. }
  273. }
  274. cacheOut<<std::endl;
  275. }
  276. }
  277. }
  278. //----------------------------------------------------------------------------
  279. void cmDependsC::Scan(std::istream& is, const char* directory,
  280. const cmStdString& fullName)
  281. {
  282. cmIncludeLines* newCacheEntry=new cmIncludeLines;
  283. newCacheEntry->Used=true;
  284. this->fileCache[fullName]=newCacheEntry;
  285. // Read one line at a time.
  286. std::string line;
  287. while(cmSystemTools::GetLineFromStream(is, line))
  288. {
  289. // Match include directives.
  290. if(this->IncludeRegexLine.find(line.c_str()))
  291. {
  292. // Get the file being included.
  293. UnscannedEntry entry;
  294. entry.FileName = this->IncludeRegexLine.match(1);
  295. if(this->IncludeRegexLine.match(2) == "\"" &&
  296. !cmSystemTools::FileIsFullPath(entry.FileName.c_str()))
  297. {
  298. // This was a double-quoted include with a relative path. We
  299. // must check for the file in the directory containing the
  300. // file we are scanning.
  301. entry.QuotedLocation = directory;
  302. entry.QuotedLocation += "/";
  303. entry.QuotedLocation += entry.FileName;
  304. }
  305. // Queue the file if it has not yet been encountered and it
  306. // matches the regular expression for recursive scanning. Note
  307. // that this check does not account for the possibility of two
  308. // headers with the same name in different directories when one
  309. // is included by double-quotes and the other by angle brackets.
  310. // This kind of problem will be fixed when a more
  311. // preprocessor-like implementation of this scanner is created.
  312. if (this->IncludeRegexScan.find(entry.FileName.c_str()))
  313. {
  314. newCacheEntry->UnscannedEntries.push_back(entry);
  315. if(this->Encountered.find(entry.FileName) == this->Encountered.end())
  316. {
  317. this->Encountered.insert(entry.FileName);
  318. this->Unscanned.push(entry);
  319. }
  320. }
  321. }
  322. }
  323. }
  324. //----------------------------------------------------------------------------
  325. bool cmDependsC::FileExistsOrIsGenerated(const std::string& fname,
  326. std::set<cmStdString>& scanned,
  327. std::set<cmStdString>& dependencies)
  328. {
  329. // Check for a generated file.
  330. if(this->FileIsGenerated(fname, scanned, dependencies))
  331. {
  332. return true;
  333. }
  334. else if(cmSystemTools::FileIsFullPath(fname.c_str()))
  335. {
  336. // The generated file may have been listed with a relative path.
  337. std::string dir = cmSystemTools::CollapseFullPath(this->Directory.c_str());
  338. std::string rname =
  339. cmSystemTools::RelativePath(dir.c_str(), fname.c_str());
  340. if(this->FileIsGenerated(rname, scanned, dependencies))
  341. {
  342. return true;
  343. }
  344. }
  345. // Check for an existing file.
  346. return cmSystemTools::FileExists(fname.c_str());
  347. }
  348. //----------------------------------------------------------------------------
  349. bool cmDependsC::FileIsGenerated(const std::string& fname,
  350. std::set<cmStdString>& scanned,
  351. std::set<cmStdString>& dependencies)
  352. {
  353. if(this->GeneratedFiles &&
  354. std::set<cmStdString>::const_iterator(this->GeneratedFiles->find(fname)) !=
  355. this->GeneratedFiles->end())
  356. {
  357. // If the file does not really exist yet pretend it has already
  358. // been scanned. When it exists later then dependencies will be
  359. // rescanned.
  360. if(!cmSystemTools::FileExists(fname.c_str()))
  361. {
  362. scanned.insert(fname);
  363. dependencies.insert(fname);
  364. }
  365. return true;
  366. }
  367. else
  368. {
  369. return false;
  370. }
  371. }