cmDepends.cxx 8.3 KB

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