cmDepends.cxx 8.5 KB

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