1
0

cmDependsC.cxx 13 KB

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