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