cmDepends.cxx 9.3 KB

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