cmDependsC.cxx 13 KB

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