cmDependsC.cxx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. m_IncludePath(0), m_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. m_IncludePath(&includes),
  29. m_IncludeRegexLine("^[ \t]*#[ \t]*include[ \t]*[<\"]([^\">]+)([\">])"),
  30. m_IncludeRegexScan(scanRegex),
  31. m_IncludeRegexComplain(complainRegex),
  32. m_GeneratedFiles(&generatedFiles),
  33. m_cacheFileName(cacheFileName)
  34. {
  35. this->ReadCacheFile();
  36. }
  37. //----------------------------------------------------------------------------
  38. cmDependsC::~cmDependsC()
  39. {
  40. this->WriteCacheFile();
  41. for (std::map<cmStdString, cmIncludeLines*>::iterator it=m_fileCache.begin();
  42. it!=m_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(!m_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. m_Unscanned.push(root);
  72. m_Encountered.clear();
  73. m_Encountered.insert(src);
  74. std::set<cmStdString> dependencies;
  75. std::set<cmStdString> scanned;
  76. while(!m_Unscanned.empty())
  77. {
  78. // Get the next file to scan.
  79. UnscannedEntry current = m_Unscanned.front();
  80. m_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 = m_IncludePath->begin();
  103. i != m_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. m_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=m_fileCache.find(fullName);
  141. if (fileIt!=m_fileCache.end())
  142. {
  143. fileIt->second->m_Used=true;
  144. dependencies.insert(fullName);
  145. for (std::list<UnscannedEntry>::const_iterator incIt=
  146. fileIt->second->m_UnscannedEntries.begin();
  147. incIt!=fileIt->second->m_UnscannedEntries.end(); ++incIt)
  148. {
  149. if (m_Encountered.find(incIt->FileName) == m_Encountered.end())
  150. {
  151. m_Encountered.insert(incIt->FileName);
  152. m_Unscanned.push(*incIt);
  153. }
  154. }
  155. }
  156. else
  157. {
  158. // Try to scan the file. Just leave it out if we cannot find
  159. // it.
  160. std::ifstream fin(fullName.c_str());
  161. if(fin)
  162. {
  163. // Add this file as a dependency.
  164. dependencies.insert(fullName);
  165. // Scan this file for new dependencies. Pass the directory
  166. // containing the file to handle double-quote includes.
  167. std::string dir = cmSystemTools::GetFilenamePath(fullName);
  168. this->Scan(fin, dir.c_str(), fullName);
  169. }
  170. }
  171. }
  172. first = false;
  173. }
  174. // Write the dependencies to the output stream.
  175. internalDepends << obj << std::endl;
  176. for(std::set<cmStdString>::iterator i=dependencies.begin();
  177. i != dependencies.end(); ++i)
  178. {
  179. makeDepends << obj << ": "
  180. << cmSystemTools::ConvertToOutputPath(i->c_str()).c_str()
  181. << std::endl;
  182. internalDepends << " " << i->c_str() << std::endl;
  183. }
  184. makeDepends << std::endl;
  185. return true;
  186. }
  187. //----------------------------------------------------------------------------
  188. void cmDependsC::ReadCacheFile()
  189. {
  190. if(m_cacheFileName.size() == 0)
  191. {
  192. return;
  193. }
  194. std::ifstream fin(m_cacheFileName.c_str());
  195. if(!fin)
  196. {
  197. return;
  198. }
  199. std::string line;
  200. cmIncludeLines* cacheEntry=0;
  201. bool haveFileName=false;
  202. while(cmSystemTools::GetLineFromStream(fin, line))
  203. {
  204. if (line.empty())
  205. {
  206. cacheEntry=0;
  207. haveFileName=false;
  208. continue;
  209. }
  210. //the first line after an empty line is the name of the parsed file
  211. if (haveFileName==false)
  212. {
  213. haveFileName=true;
  214. int newer=0;
  215. cmFileTimeComparison comp;
  216. bool res=comp.FileTimeCompare(m_cacheFileName.c_str(), line.c_str(), &newer);
  217. if ((res==true) && (newer==1)) //cache is newer than the parsed file
  218. {
  219. cacheEntry=new cmIncludeLines;
  220. m_fileCache[line]=cacheEntry;
  221. }
  222. }
  223. else if (cacheEntry!=0)
  224. {
  225. UnscannedEntry entry;
  226. entry.FileName = line;
  227. if (cmSystemTools::GetLineFromStream(fin, line))
  228. {
  229. if (line!="-")
  230. {
  231. entry.QuotedLocation=line;
  232. }
  233. cacheEntry->m_UnscannedEntries.push_back(entry);
  234. }
  235. }
  236. }
  237. }
  238. //----------------------------------------------------------------------------
  239. void cmDependsC::WriteCacheFile() const
  240. {
  241. if(m_cacheFileName.size() == 0)
  242. {
  243. return;
  244. }
  245. std::ofstream cacheOut(m_cacheFileName.c_str());
  246. if(!cacheOut)
  247. {
  248. return;
  249. }
  250. for (std::map<cmStdString, cmIncludeLines*>::const_iterator fileIt=m_fileCache.begin();
  251. fileIt!=m_fileCache.end(); ++fileIt)
  252. {
  253. if (fileIt->second->m_Used)
  254. {
  255. cacheOut<<fileIt->first.c_str()<<std::endl;
  256. for (std::list<UnscannedEntry>::const_iterator
  257. incIt=fileIt->second->m_UnscannedEntries.begin();
  258. incIt!=fileIt->second->m_UnscannedEntries.end(); ++incIt)
  259. {
  260. cacheOut<<incIt->FileName.c_str()<<std::endl;
  261. if (incIt->QuotedLocation.empty())
  262. {
  263. cacheOut<<"-"<<std::endl;
  264. }
  265. else
  266. {
  267. cacheOut<<incIt->QuotedLocation.c_str()<<std::endl;
  268. }
  269. }
  270. cacheOut<<std::endl;
  271. }
  272. }
  273. }
  274. //----------------------------------------------------------------------------
  275. void cmDependsC::Scan(std::istream& is, const char* directory, const cmStdString& fullName)
  276. {
  277. cmIncludeLines* newCacheEntry=new cmIncludeLines;
  278. newCacheEntry->m_Used=true;
  279. m_fileCache[fullName]=newCacheEntry;
  280. // Read one line at a time.
  281. std::string line;
  282. while(cmSystemTools::GetLineFromStream(is, line))
  283. {
  284. // Match include directives.
  285. if(m_IncludeRegexLine.find(line.c_str()))
  286. {
  287. // Get the file being included.
  288. UnscannedEntry entry;
  289. entry.FileName = m_IncludeRegexLine.match(1);
  290. if(m_IncludeRegexLine.match(2) == "\"")
  291. {
  292. // This was a double-quoted include. We must check for the
  293. // file in the directory containing the file we are scanning.
  294. entry.QuotedLocation = directory;
  295. entry.QuotedLocation += "/";
  296. entry.QuotedLocation += entry.FileName;
  297. }
  298. // Queue the file if it has not yet been encountered and it
  299. // matches the regular expression for recursive scanning. Note
  300. // that this check does not account for the possibility of two
  301. // headers with the same name in different directories when one
  302. // is included by double-quotes and the other by angle brackets.
  303. // This kind of problem will be fixed when a more
  304. // preprocessor-like implementation of this scanner is created.
  305. if (m_IncludeRegexScan.find(entry.FileName.c_str()))
  306. {
  307. newCacheEntry->m_UnscannedEntries.push_back(entry);
  308. if(m_Encountered.find(entry.FileName) == m_Encountered.end())
  309. {
  310. m_Encountered.insert(entry.FileName);
  311. m_Unscanned.push(entry);
  312. }
  313. }
  314. }
  315. }
  316. }
  317. //----------------------------------------------------------------------------
  318. bool cmDependsC::FileExistsOrIsGenerated(const std::string& fname,
  319. std::set<cmStdString>& scanned,
  320. std::set<cmStdString>& dependencies)
  321. {
  322. // Check first for a generated file.
  323. if(m_GeneratedFiles &&
  324. std::set<cmStdString>::const_iterator(m_GeneratedFiles->find(fname)) !=
  325. 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. }