cmDepends.cxx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmDepends.h"
  11. #include "cmLocalGenerator.h"
  12. #include "cmMakefile.h"
  13. #include "cmGeneratedFileStream.h"
  14. #include "cmSystemTools.h"
  15. #include "cmFileTimeComparison.h"
  16. #include <string.h>
  17. #include <cmsys/FStream.hxx>
  18. //----------------------------------------------------------------------------
  19. cmDepends::cmDepends(cmLocalGenerator* lg, const char* targetDir):
  20. CompileDirectory(),
  21. LocalGenerator(lg),
  22. Verbose(false),
  23. FileComparison(0),
  24. TargetDirectory(targetDir),
  25. MaxPath(16384),
  26. Dependee(new char[MaxPath]),
  27. Depender(new char[MaxPath])
  28. {
  29. }
  30. //----------------------------------------------------------------------------
  31. cmDepends::~cmDepends()
  32. {
  33. delete [] this->Dependee;
  34. delete [] this->Depender;
  35. }
  36. //----------------------------------------------------------------------------
  37. bool cmDepends::Write(std::ostream &makeDepends,
  38. std::ostream &internalDepends)
  39. {
  40. // Lookup the set of sources to scan.
  41. std::string srcLang = "CMAKE_DEPENDS_CHECK_";
  42. srcLang += this->Language;
  43. cmMakefile* mf = this->LocalGenerator->GetMakefile();
  44. const char* srcStr = mf->GetSafeDefinition(srcLang);
  45. std::vector<std::string> pairs;
  46. cmSystemTools::ExpandListArgument(srcStr, pairs);
  47. std::map<std::string, std::set<std::string> > dependencies;
  48. for(std::vector<std::string>::iterator si = pairs.begin();
  49. si != pairs.end();)
  50. {
  51. // Get the source and object file.
  52. std::string const& src = *si++;
  53. if(si == pairs.end()) { break; }
  54. std::string const& obj = *si++;
  55. dependencies[obj].insert(src);
  56. }
  57. for(std::map<std::string, std::set<std::string> >::const_iterator
  58. it = dependencies.begin(); it != dependencies.end(); ++it)
  59. {
  60. // Write the dependencies for this pair.
  61. if(!this->WriteDependencies(it->second, it->first,
  62. makeDepends, internalDepends))
  63. {
  64. return false;
  65. }
  66. }
  67. return this->Finalize(makeDepends, internalDepends);
  68. }
  69. //----------------------------------------------------------------------------
  70. bool cmDepends::Finalize(std::ostream&,
  71. std::ostream&)
  72. {
  73. return true;
  74. }
  75. //----------------------------------------------------------------------------
  76. bool cmDepends::Check(const char *makeFile, const char *internalFile,
  77. std::map<std::string, DependencyVector>& validDeps)
  78. {
  79. // Dependency checks must be done in proper working directory.
  80. std::string oldcwd = ".";
  81. if(this->CompileDirectory != ".")
  82. {
  83. // Get the CWD but do not call CollapseFullPath because
  84. // we only need it to cd back, and the form does not matter
  85. oldcwd = cmSystemTools::GetCurrentWorkingDirectory(false);
  86. cmSystemTools::ChangeDirectory(this->CompileDirectory);
  87. }
  88. // Check whether dependencies must be regenerated.
  89. bool okay = true;
  90. cmsys::ifstream fin(internalFile);
  91. if(!(fin && this->CheckDependencies(fin, internalFile, validDeps)))
  92. {
  93. // Clear all dependencies so they will be regenerated.
  94. this->Clear(makeFile);
  95. cmSystemTools::RemoveFile(internalFile);
  96. okay = false;
  97. }
  98. // Restore working directory.
  99. if(oldcwd != ".")
  100. {
  101. cmSystemTools::ChangeDirectory(oldcwd);
  102. }
  103. return okay;
  104. }
  105. //----------------------------------------------------------------------------
  106. void cmDepends::Clear(const char *file)
  107. {
  108. // Print verbose output.
  109. if(this->Verbose)
  110. {
  111. cmOStringStream msg;
  112. msg << "Clearing dependencies in \"" << file << "\"." << std::endl;
  113. cmSystemTools::Stdout(msg.str().c_str());
  114. }
  115. // Write an empty dependency file.
  116. cmGeneratedFileStream depFileStream(file);
  117. depFileStream
  118. << "# Empty dependencies file\n"
  119. << "# This may be replaced when dependencies are built." << std::endl;
  120. }
  121. //----------------------------------------------------------------------------
  122. bool cmDepends::WriteDependencies(
  123. const std::set<std::string>&, const std::string&,
  124. std::ostream&, std::ostream&)
  125. {
  126. // This should be implemented by the subclass.
  127. return false;
  128. }
  129. //----------------------------------------------------------------------------
  130. bool cmDepends::CheckDependencies(std::istream& internalDepends,
  131. const char* internalDependsFileName,
  132. std::map<std::string, DependencyVector>& validDeps)
  133. {
  134. // Parse dependencies from the stream. If any dependee is missing
  135. // or newer than the depender then dependencies should be
  136. // regenerated.
  137. bool okay = true;
  138. bool dependerExists = false;
  139. DependencyVector* currentDependencies = 0;
  140. while(internalDepends.getline(this->Dependee, this->MaxPath))
  141. {
  142. if ( this->Dependee[0] == 0 || this->Dependee[0] == '#' ||
  143. this->Dependee[0] == '\r' )
  144. {
  145. continue;
  146. }
  147. size_t len = internalDepends.gcount()-1;
  148. if ( this->Dependee[len-1] == '\r' )
  149. {
  150. len --;
  151. this->Dependee[len] = 0;
  152. }
  153. if ( this->Dependee[0] != ' ' )
  154. {
  155. memcpy(this->Depender, this->Dependee, len+1);
  156. // Calling FileExists() for the depender here saves in many cases 50%
  157. // of the calls to FileExists() further down in the loop. E.g. for
  158. // kdelibs/khtml this reduces the number of calls from 184k down to 92k,
  159. // or the time for cmake -E cmake_depends from 0.3 s down to 0.21 s.
  160. dependerExists = cmSystemTools::FileExists(this->Depender);
  161. // If we erase validDeps[this->Depender] by overwriting it with an empty
  162. // vector, we lose dependencies for dependers that have multiple
  163. // entries. No need to initialize the entry, std::map will do so on first
  164. // access.
  165. currentDependencies = &validDeps[this->Depender];
  166. continue;
  167. }
  168. /*
  169. // Parse the dependency line.
  170. if(!this->ParseDependency(line.c_str()))
  171. {
  172. continue;
  173. }
  174. */
  175. // Dependencies must be regenerated
  176. // * if the dependee does not exist
  177. // * if the depender exists and is older than the dependee.
  178. // * if the depender does not exist, but the dependee is newer than the
  179. // depends file
  180. bool regenerate = false;
  181. const char* dependee = this->Dependee+1;
  182. const char* depender = this->Depender;
  183. if (currentDependencies != 0)
  184. {
  185. currentDependencies->push_back(dependee);
  186. }
  187. if(!cmSystemTools::FileExists(dependee))
  188. {
  189. // The dependee does not exist.
  190. regenerate = true;
  191. // Print verbose output.
  192. if(this->Verbose)
  193. {
  194. cmOStringStream msg;
  195. msg << "Dependee \"" << dependee
  196. << "\" does not exist for depender \""
  197. << depender << "\"." << std::endl;
  198. cmSystemTools::Stdout(msg.str().c_str());
  199. }
  200. }
  201. else
  202. {
  203. if(dependerExists)
  204. {
  205. // The dependee and depender both exist. Compare file times.
  206. int result = 0;
  207. if((!this->FileComparison->FileTimeCompare(depender, dependee,
  208. &result) || result < 0))
  209. {
  210. // The depender is older than the dependee.
  211. regenerate = true;
  212. // Print verbose output.
  213. if(this->Verbose)
  214. {
  215. cmOStringStream msg;
  216. msg << "Dependee \"" << dependee
  217. << "\" is newer than depender \""
  218. << depender << "\"." << std::endl;
  219. cmSystemTools::Stdout(msg.str().c_str());
  220. }
  221. }
  222. }
  223. else
  224. {
  225. // The dependee exists, but the depender doesn't. Regenerate if the
  226. // internalDepends file is older than the dependee.
  227. int result = 0;
  228. if((!this->FileComparison->FileTimeCompare(internalDependsFileName,
  229. dependee, &result) || result < 0))
  230. {
  231. // The depends-file is older than the dependee.
  232. regenerate = true;
  233. // Print verbose output.
  234. if(this->Verbose)
  235. {
  236. cmOStringStream msg;
  237. msg << "Dependee \"" << dependee
  238. << "\" is newer than depends file \""
  239. << internalDependsFileName << "\"." << std::endl;
  240. cmSystemTools::Stdout(msg.str().c_str());
  241. }
  242. }
  243. }
  244. }
  245. if(regenerate)
  246. {
  247. // Dependencies must be regenerated.
  248. okay = false;
  249. // Remove the information of this depender from the map, it needs
  250. // to be rescanned
  251. if (currentDependencies != 0)
  252. {
  253. validDeps.erase(this->Depender);
  254. currentDependencies = 0;
  255. }
  256. // Remove the depender to be sure it is rebuilt.
  257. if (dependerExists)
  258. {
  259. cmSystemTools::RemoveFile(depender);
  260. dependerExists = false;
  261. }
  262. }
  263. }
  264. return okay;
  265. }
  266. //----------------------------------------------------------------------------
  267. void cmDepends::SetIncludePathFromLanguage(const std::string& lang)
  268. {
  269. // Look for the new per "TARGET_" variant first:
  270. const char * includePath = 0;
  271. std::string includePathVar = "CMAKE_";
  272. includePathVar += lang;
  273. includePathVar += "_TARGET_INCLUDE_PATH";
  274. cmMakefile* mf = this->LocalGenerator->GetMakefile();
  275. includePath = mf->GetDefinition(includePathVar);
  276. if(includePath)
  277. {
  278. cmSystemTools::ExpandListArgument(includePath, this->IncludePath);
  279. }
  280. else
  281. {
  282. // Fallback to the old directory level variable if no per-target var:
  283. includePathVar = "CMAKE_";
  284. includePathVar += lang;
  285. includePathVar += "_INCLUDE_PATH";
  286. includePath = mf->GetDefinition(includePathVar);
  287. if(includePath)
  288. {
  289. cmSystemTools::ExpandListArgument(includePath, this->IncludePath);
  290. }
  291. }
  292. }