cmDepends.cxx 8.9 KB

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