cmDepends.cxx 8.6 KB

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