cmDepends.cxx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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 obj = *si++;
  55. // Make sure the object file is relative to the top of the build tree.
  56. obj = this->LocalGenerator->Convert(obj,
  57. cmLocalGenerator::HOME_OUTPUT,
  58. cmLocalGenerator::MAKEFILE);
  59. dependencies[obj].insert(src);
  60. }
  61. for(std::map<std::string, std::set<std::string> >::const_iterator
  62. it = dependencies.begin(); it != dependencies.end(); ++it)
  63. {
  64. // Write the dependencies for this pair.
  65. if(!this->WriteDependencies(it->second, it->first,
  66. makeDepends, internalDepends))
  67. {
  68. return false;
  69. }
  70. }
  71. return this->Finalize(makeDepends, internalDepends);
  72. }
  73. //----------------------------------------------------------------------------
  74. bool cmDepends::Finalize(std::ostream&,
  75. std::ostream&)
  76. {
  77. return true;
  78. }
  79. //----------------------------------------------------------------------------
  80. bool cmDepends::Check(const char *makeFile, const char *internalFile,
  81. std::map<std::string, DependencyVector>& validDeps)
  82. {
  83. // Dependency checks must be done in proper working directory.
  84. std::string oldcwd = ".";
  85. if(this->CompileDirectory != ".")
  86. {
  87. // Get the CWD but do not call CollapseFullPath because
  88. // we only need it to cd back, and the form does not matter
  89. oldcwd = cmSystemTools::GetCurrentWorkingDirectory(false);
  90. cmSystemTools::ChangeDirectory(this->CompileDirectory.c_str());
  91. }
  92. // Check whether dependencies must be regenerated.
  93. bool okay = true;
  94. cmsys::ifstream fin(internalFile);
  95. if(!(fin && this->CheckDependencies(fin, internalFile, validDeps)))
  96. {
  97. // Clear all dependencies so they will be regenerated.
  98. this->Clear(makeFile);
  99. cmSystemTools::RemoveFile(internalFile);
  100. okay = false;
  101. }
  102. // Restore working directory.
  103. if(oldcwd != ".")
  104. {
  105. cmSystemTools::ChangeDirectory(oldcwd.c_str());
  106. }
  107. return okay;
  108. }
  109. //----------------------------------------------------------------------------
  110. void cmDepends::Clear(const char *file)
  111. {
  112. // Print verbose output.
  113. if(this->Verbose)
  114. {
  115. cmOStringStream msg;
  116. msg << "Clearing dependencies in \"" << file << "\"." << std::endl;
  117. cmSystemTools::Stdout(msg.str().c_str());
  118. }
  119. // Write an empty dependency file.
  120. cmGeneratedFileStream depFileStream(file);
  121. depFileStream
  122. << "# Empty dependencies file\n"
  123. << "# This may be replaced when dependencies are built." << std::endl;
  124. }
  125. //----------------------------------------------------------------------------
  126. bool cmDepends::WriteDependencies(
  127. const std::set<std::string>&, const std::string&,
  128. std::ostream&, std::ostream&)
  129. {
  130. // This should be implemented by the subclass.
  131. return false;
  132. }
  133. //----------------------------------------------------------------------------
  134. bool cmDepends::CheckDependencies(std::istream& internalDepends,
  135. const char* internalDependsFileName,
  136. std::map<std::string, DependencyVector>& validDeps)
  137. {
  138. // Parse dependencies from the stream. If any dependee is missing
  139. // or newer than the depender then dependencies should be
  140. // regenerated.
  141. bool okay = true;
  142. bool dependerExists = false;
  143. DependencyVector* currentDependencies = 0;
  144. while(internalDepends.getline(this->Dependee, this->MaxPath))
  145. {
  146. if ( this->Dependee[0] == 0 || this->Dependee[0] == '#' ||
  147. this->Dependee[0] == '\r' )
  148. {
  149. continue;
  150. }
  151. size_t len = internalDepends.gcount()-1;
  152. if ( this->Dependee[len-1] == '\r' )
  153. {
  154. len --;
  155. this->Dependee[len] = 0;
  156. }
  157. if ( this->Dependee[0] != ' ' )
  158. {
  159. memcpy(this->Depender, this->Dependee, len+1);
  160. // Calling FileExists() for the depender here saves in many cases 50%
  161. // of the calls to FileExists() further down in the loop. E.g. for
  162. // kdelibs/khtml this reduces the number of calls from 184k down to 92k,
  163. // or the time for cmake -E cmake_depends from 0.3 s down to 0.21 s.
  164. dependerExists = cmSystemTools::FileExists(this->Depender);
  165. // If we erase validDeps[this->Depender] by overwriting it with an empty
  166. // vector, we lose dependencies for dependers that have multiple
  167. // entries. No need to initialize the entry, std::map will do so on first
  168. // access.
  169. currentDependencies = &validDeps[this->Depender];
  170. continue;
  171. }
  172. /*
  173. // Parse the dependency line.
  174. if(!this->ParseDependency(line.c_str()))
  175. {
  176. continue;
  177. }
  178. */
  179. // Dependencies must be regenerated
  180. // * if the dependee does not exist
  181. // * if the depender exists and is older than the dependee.
  182. // * if the depender does not exist, but the dependee is newer than the
  183. // depends file
  184. bool regenerate = false;
  185. const char* dependee = this->Dependee+1;
  186. const char* depender = this->Depender;
  187. if (currentDependencies != 0)
  188. {
  189. currentDependencies->push_back(dependee);
  190. }
  191. if(!cmSystemTools::FileExists(dependee))
  192. {
  193. // The dependee does not exist.
  194. regenerate = true;
  195. // Print verbose output.
  196. if(this->Verbose)
  197. {
  198. cmOStringStream msg;
  199. msg << "Dependee \"" << dependee
  200. << "\" does not exist for depender \""
  201. << depender << "\"." << std::endl;
  202. cmSystemTools::Stdout(msg.str().c_str());
  203. }
  204. }
  205. else
  206. {
  207. if(dependerExists)
  208. {
  209. // The dependee and depender both exist. Compare file times.
  210. int result = 0;
  211. if((!this->FileComparison->FileTimeCompare(depender, dependee,
  212. &result) || result < 0))
  213. {
  214. // The depender is older than the dependee.
  215. regenerate = true;
  216. // Print verbose output.
  217. if(this->Verbose)
  218. {
  219. cmOStringStream msg;
  220. msg << "Dependee \"" << dependee
  221. << "\" is newer than depender \""
  222. << depender << "\"." << std::endl;
  223. cmSystemTools::Stdout(msg.str().c_str());
  224. }
  225. }
  226. }
  227. else
  228. {
  229. // The dependee exists, but the depender doesn't. Regenerate if the
  230. // internalDepends file is older than the dependee.
  231. int result = 0;
  232. if((!this->FileComparison->FileTimeCompare(internalDependsFileName,
  233. dependee, &result) || result < 0))
  234. {
  235. // The depends-file is older than the dependee.
  236. regenerate = true;
  237. // Print verbose output.
  238. if(this->Verbose)
  239. {
  240. cmOStringStream msg;
  241. msg << "Dependee \"" << dependee
  242. << "\" is newer than depends file \""
  243. << internalDependsFileName << "\"." << std::endl;
  244. cmSystemTools::Stdout(msg.str().c_str());
  245. }
  246. }
  247. }
  248. }
  249. if(regenerate)
  250. {
  251. // Dependencies must be regenerated.
  252. okay = false;
  253. // Remove the information of this depender from the map, it needs
  254. // to be rescanned
  255. if (currentDependencies != 0)
  256. {
  257. validDeps.erase(this->Depender);
  258. currentDependencies = 0;
  259. }
  260. // Remove the depender to be sure it is rebuilt.
  261. if (dependerExists)
  262. {
  263. cmSystemTools::RemoveFile(depender);
  264. dependerExists = false;
  265. }
  266. }
  267. }
  268. return okay;
  269. }
  270. //----------------------------------------------------------------------------
  271. void cmDepends::SetIncludePathFromLanguage(const std::string& lang)
  272. {
  273. // Look for the new per "TARGET_" variant first:
  274. const char * includePath = 0;
  275. std::string includePathVar = "CMAKE_";
  276. includePathVar += lang;
  277. includePathVar += "_TARGET_INCLUDE_PATH";
  278. cmMakefile* mf = this->LocalGenerator->GetMakefile();
  279. includePath = mf->GetDefinition(includePathVar);
  280. if(includePath)
  281. {
  282. cmSystemTools::ExpandListArgument(includePath, this->IncludePath);
  283. }
  284. else
  285. {
  286. // Fallback to the old directory level variable if no per-target var:
  287. includePathVar = "CMAKE_";
  288. includePathVar += lang;
  289. includePathVar += "_INCLUDE_PATH";
  290. includePath = mf->GetDefinition(includePathVar);
  291. if(includePath)
  292. {
  293. cmSystemTools::ExpandListArgument(includePath, this->IncludePath);
  294. }
  295. }
  296. }