cmDependsC.cxx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. {
  19. }
  20. //----------------------------------------------------------------------------
  21. // yummy look at all those constructor arguments
  22. cmDependsC::cmDependsC(std::vector<std::string> const& includes,
  23. const char* scanRegex, const char* complainRegex):
  24. m_IncludePath(&includes),
  25. m_IncludeRegexLine("^[ \t]*#[ \t]*include[ \t]*[<\"]([^\">]+)([\">])"),
  26. m_IncludeRegexScan(scanRegex),
  27. m_IncludeRegexComplain(complainRegex)
  28. {
  29. }
  30. //----------------------------------------------------------------------------
  31. cmDependsC::~cmDependsC()
  32. {
  33. }
  34. //----------------------------------------------------------------------------
  35. bool cmDependsC::WriteDependencies(const char *src,
  36. const char *obj, std::ostream& os)
  37. {
  38. // Make sure this is a scanning instance.
  39. if(!src || src[0] == '\0')
  40. {
  41. cmSystemTools::Error("Cannot scan dependencies without a source file.");
  42. return false;
  43. }
  44. if(!obj || obj[0] == '\0')
  45. {
  46. cmSystemTools::Error("Cannot scan dependencies without an object file.");
  47. return false;
  48. }
  49. if(!m_IncludePath)
  50. {
  51. cmSystemTools::Error("Cannot scan dependencies without an include path.");
  52. return false;
  53. }
  54. // Walk the dependency graph starting with the source file.
  55. bool first = true;
  56. UnscannedEntry root;
  57. root.FileName = src;
  58. m_Unscanned.push(root);
  59. m_Encountered.clear();
  60. m_Encountered.insert(src);
  61. std::set<cmStdString> dependencies;
  62. std::set<cmStdString> scanned;
  63. while(!m_Unscanned.empty())
  64. {
  65. // Get the next file to scan.
  66. UnscannedEntry current = m_Unscanned.front();
  67. m_Unscanned.pop();
  68. // If not a full path, find the file in the include path.
  69. std::string fullName;
  70. if(first || cmSystemTools::FileIsFullPath(current.FileName.c_str()))
  71. {
  72. if(cmSystemTools::FileExists(current.FileName.c_str()))
  73. {
  74. fullName = current.FileName;
  75. }
  76. }
  77. else if(!current.QuotedLocation.empty() &&
  78. cmSystemTools::FileExists(current.QuotedLocation.c_str()))
  79. {
  80. // The include statement producing this entry was a double-quote
  81. // include and the included file is present in the directory of
  82. // the source containing the include statement.
  83. fullName = current.QuotedLocation;
  84. }
  85. else
  86. {
  87. for(std::vector<std::string>::const_iterator i = m_IncludePath->begin();
  88. i != m_IncludePath->end(); ++i)
  89. {
  90. // Construct the name of the file as if it were in the current
  91. // include directory. Avoid using a leading "./".
  92. std::string temp = *i;
  93. if(temp == ".")
  94. {
  95. temp = "";
  96. }
  97. else
  98. {
  99. temp += "/";
  100. }
  101. temp += current.FileName;
  102. // Look for the file in this location.
  103. if(cmSystemTools::FileExists(temp.c_str()))
  104. {
  105. fullName = temp;
  106. break;
  107. }
  108. }
  109. }
  110. // Complain if the file cannot be found and matches the complain
  111. // regex.
  112. if(fullName.empty() &&
  113. m_IncludeRegexComplain.find(current.FileName.c_str()))
  114. {
  115. cmSystemTools::Error("Cannot find file \"",
  116. current.FileName.c_str(), "\".");
  117. return false;
  118. }
  119. // Scan the file if it was found and has not been scanned already.
  120. if(!fullName.empty() && (scanned.find(fullName) == scanned.end()))
  121. {
  122. // Record scanned files.
  123. scanned.insert(fullName);
  124. // Try to scan the file. Just leave it out if we cannot find
  125. // it.
  126. std::ifstream fin(fullName.c_str());
  127. if(fin)
  128. {
  129. // Add this file as a dependency.
  130. dependencies.insert(fullName);
  131. // Scan this file for new dependencies. Pass the directory
  132. // containing the file to handle double-quote includes.
  133. std::string dir = cmSystemTools::GetFilenamePath(fullName);
  134. this->Scan(fin, dir.c_str());
  135. }
  136. }
  137. first = false;
  138. }
  139. // Write the dependencies to the output stream.
  140. for(std::set<cmStdString>::iterator i=dependencies.begin();
  141. i != dependencies.end(); ++i)
  142. {
  143. os << obj << ": "
  144. << cmSystemTools::ConvertToOutputPath(i->c_str()).c_str()
  145. << std::endl;
  146. }
  147. os << std::endl;
  148. return true;
  149. }
  150. //----------------------------------------------------------------------------
  151. bool cmDependsC::CheckDependencies(std::istream& is)
  152. {
  153. // Parse dependencies from the stream. If any dependee is missing
  154. // or newer than the depender then dependencies should be
  155. // regenerated.
  156. bool okay = true;
  157. std::string line;
  158. std::string depender;
  159. std::string dependee;
  160. while(cmSystemTools::GetLineFromStream(is, line))
  161. {
  162. // Parse the dependency line.
  163. if(!this->ParseDependency(line.c_str(), depender, dependee))
  164. {
  165. continue;
  166. }
  167. // Dependencies must be regenerated if the dependee does not exist
  168. // or if the depender exists and is older than the dependee.
  169. bool regenerate = false;
  170. if(!cmSystemTools::FileExists(dependee.c_str()))
  171. {
  172. // The dependee does not exist.
  173. regenerate = true;
  174. // Print verbose output.
  175. if(m_Verbose)
  176. {
  177. cmOStringStream msg;
  178. msg << "Dependee \"" << dependee
  179. << "\" does not exist for depender \""
  180. << depender << "\"." << std::endl;
  181. cmSystemTools::Stdout(msg.str().c_str());
  182. }
  183. }
  184. else if(cmSystemTools::FileExists(depender.c_str()))
  185. {
  186. // The dependee and depender both exist. Compare file times.
  187. int result = 0;
  188. if((!cmSystemTools::FileTimeCompare(depender.c_str(), dependee.c_str(),
  189. &result) || result < 0))
  190. {
  191. // The depender is older than the dependee.
  192. regenerate = true;
  193. // Print verbose output.
  194. if(m_Verbose)
  195. {
  196. cmOStringStream msg;
  197. msg << "Dependee \"" << dependee
  198. << "\" is newer than depender \""
  199. << depender << "\"." << std::endl;
  200. cmSystemTools::Stdout(msg.str().c_str());
  201. }
  202. }
  203. }
  204. if(regenerate)
  205. {
  206. // Dependencies must be regenerated.
  207. okay = false;
  208. // Remove the depender to be sure it is rebuilt.
  209. cmSystemTools::RemoveFile(depender.c_str());
  210. }
  211. }
  212. return okay;
  213. }
  214. //----------------------------------------------------------------------------
  215. void cmDependsC::Scan(std::istream& is, const char* directory)
  216. {
  217. // Read one line at a time.
  218. std::string line;
  219. while(cmSystemTools::GetLineFromStream(is, line))
  220. {
  221. // Match include directives.
  222. if(m_IncludeRegexLine.find(line.c_str()))
  223. {
  224. // Get the file being included.
  225. UnscannedEntry entry;
  226. entry.FileName = m_IncludeRegexLine.match(1);
  227. if(m_IncludeRegexLine.match(2) == "\"")
  228. {
  229. // This was a double-quoted include. We must check for the
  230. // file in the directory containing the file we are scanning.
  231. entry.QuotedLocation = directory;
  232. entry.QuotedLocation += "/";
  233. entry.QuotedLocation += entry.FileName;
  234. }
  235. // Queue the file if it has not yet been encountered and it
  236. // matches the regular expression for recursive scanning. Note
  237. // that this check does not account for the possibility of two
  238. // headers with the same name in different directories when one
  239. // is included by double-quotes and the other by angle brackets.
  240. // This kind of problem will be fixed when a more
  241. // preprocessor-like implementation of this scanner is created.
  242. if(m_Encountered.find(entry.FileName) == m_Encountered.end() &&
  243. m_IncludeRegexScan.find(entry.FileName.c_str()))
  244. {
  245. m_Encountered.insert(entry.FileName);
  246. m_Unscanned.push(entry);
  247. }
  248. }
  249. }
  250. }
  251. //----------------------------------------------------------------------------
  252. bool cmDependsC::ParseDependency(const char* line, std::string& depender,
  253. std::string& dependee)
  254. {
  255. // Start with empty names.
  256. depender = "";
  257. dependee = "";
  258. // Get the left-hand-side of the dependency.
  259. const char* c = this->ParseFileName(line, depender);
  260. // Skip the ':' separator.
  261. for(;c && *c && isspace(*c);++c);
  262. if(!c || !*c || *c != ':')
  263. {
  264. return false;
  265. }
  266. ++c;
  267. // Get the right-hand-side of the dependency.
  268. return this->ParseFileName(c, dependee)?true:false;
  269. }
  270. //----------------------------------------------------------------------------
  271. const char* cmDependsC::ParseFileName(const char* in, std::string& name)
  272. {
  273. // Skip leading whitespace.
  274. const char* c = in;
  275. for(;c && *c && isspace(*c);++c);
  276. // If this is an empty line or a comment line return failure.
  277. if(!c || !*c || *c == '#')
  278. {
  279. return 0;
  280. }
  281. // Parse the possibly quoted file name.
  282. bool quoted = false;
  283. char* buf = new char[strlen(in)+1];
  284. char* pos = buf;
  285. // for every character while we haven't hit the end of the string AND we
  286. // are in a quoted string OR the current character isn't a : or the second
  287. // character AND it isn't a space
  288. for(;*c && (quoted ||
  289. ((*c != ':' || pos == buf+1) && !isspace(*c))); ++c)
  290. {
  291. if(*c == '"')
  292. {
  293. quoted = !quoted;
  294. }
  295. // handle unquoted escaped spaces
  296. else if(!quoted && *c == '\\' && isspace(*(c+1)))
  297. {
  298. *pos = *(++c);
  299. pos++;
  300. }
  301. else
  302. {
  303. *pos = *c;
  304. pos++;
  305. }
  306. }
  307. *pos =0;
  308. name += buf;
  309. delete [] buf;
  310. // Return the ending position.
  311. return c;
  312. }