cmDepends.cxx 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 <utility>
  5. #include "cmsys/FStream.hxx"
  6. #include "cmFileTime.h"
  7. #include "cmFileTimeCache.h"
  8. #include "cmGeneratedFileStream.h"
  9. #include "cmList.h"
  10. #include "cmLocalUnixMakefileGenerator3.h"
  11. #include "cmMakefile.h"
  12. #include "cmStringAlgorithms.h"
  13. #include "cmSystemTools.h"
  14. #include "cmValue.h"
  15. cmDepends::cmDepends(cmLocalUnixMakefileGenerator3* lg, std::string targetDir)
  16. : LocalGenerator(lg)
  17. , TargetDirectory(std::move(targetDir))
  18. {
  19. }
  20. cmDepends::~cmDepends() = default;
  21. bool cmDepends::Write(std::ostream& makeDepends, std::ostream& internalDepends)
  22. {
  23. std::map<std::string, std::set<std::string>> dependencies;
  24. {
  25. // Lookup the set of sources to scan.
  26. cmList pairs;
  27. {
  28. std::string const srcLang = "CMAKE_DEPENDS_CHECK_" + this->Language;
  29. cmMakefile* mf = this->LocalGenerator->GetMakefile();
  30. pairs.assign(mf->GetSafeDefinition(srcLang));
  31. }
  32. for (auto si = pairs.begin(); si != pairs.end();) {
  33. // Get the source and object file.
  34. std::string const& src = *si++;
  35. if (si == pairs.end()) {
  36. break;
  37. }
  38. std::string const& obj = *si++;
  39. dependencies[obj].insert(src);
  40. }
  41. }
  42. for (auto const& d : dependencies) {
  43. // Write the dependencies for this pair.
  44. if (!this->WriteDependencies(d.second, d.first, makeDepends,
  45. internalDepends)) {
  46. return false;
  47. }
  48. }
  49. return this->Finalize(makeDepends, internalDepends);
  50. }
  51. bool cmDepends::Finalize(std::ostream& /*unused*/, std::ostream& /*unused*/)
  52. {
  53. return true;
  54. }
  55. bool cmDepends::Check(const std::string& makeFile,
  56. const std::string& internalFile,
  57. DependencyMap& validDeps)
  58. {
  59. // Check whether dependencies must be regenerated.
  60. bool okay = true;
  61. cmsys::ifstream fin(internalFile.c_str());
  62. if (!(fin && this->CheckDependencies(fin, internalFile, validDeps))) {
  63. // Clear all dependencies so they will be regenerated.
  64. this->Clear(makeFile);
  65. cmSystemTools::RemoveFile(internalFile);
  66. this->FileTimeCache->Remove(internalFile);
  67. okay = false;
  68. }
  69. return okay;
  70. }
  71. void cmDepends::Clear(const std::string& file) const
  72. {
  73. // Print verbose output.
  74. if (this->Verbose) {
  75. cmSystemTools::Stdout(
  76. cmStrCat("Clearing dependencies in \"", file, "\".\n"));
  77. }
  78. // Write an empty dependency file.
  79. cmGeneratedFileStream depFileStream(file);
  80. depFileStream << "# Empty dependencies file\n"
  81. "# This may be replaced when dependencies are built.\n";
  82. }
  83. bool cmDepends::WriteDependencies(const std::set<std::string>& /*unused*/,
  84. const std::string& /*unused*/,
  85. std::ostream& /*unused*/,
  86. std::ostream& /*unused*/)
  87. {
  88. // This should be implemented by the subclass.
  89. return false;
  90. }
  91. bool cmDepends::CheckDependencies(std::istream& internalDepends,
  92. const std::string& internalDependsFileName,
  93. DependencyMap& validDeps)
  94. {
  95. // Read internal depends file time
  96. cmFileTime internalDependsTime;
  97. if (!this->FileTimeCache->Load(internalDependsFileName,
  98. internalDependsTime)) {
  99. return false;
  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. std::string line;
  107. line.reserve(1024);
  108. std::string depender;
  109. std::string dependee;
  110. cmFileTime dependerTime;
  111. cmFileTime dependeeTime;
  112. std::vector<std::string>* currentDependencies = nullptr;
  113. while (std::getline(internalDepends, line)) {
  114. // Check if this an empty or a comment line
  115. if (line.empty() || line.front() == '#') {
  116. continue;
  117. }
  118. // Drop carriage return character at the end
  119. if (line.back() == '\r') {
  120. line.pop_back();
  121. if (line.empty()) {
  122. continue;
  123. }
  124. }
  125. // Check if this a depender line
  126. if (line.front() != ' ') {
  127. depender = line;
  128. dependerExists = this->FileTimeCache->Load(depender, dependerTime);
  129. // If we erase validDeps[this->Depender] by overwriting it with an empty
  130. // vector, we lose dependencies for dependers that have multiple
  131. // entries. No need to initialize the entry, std::map will do so on first
  132. // access.
  133. currentDependencies = &validDeps[depender];
  134. continue;
  135. }
  136. // This is a dependee line
  137. dependee = line.substr(1);
  138. // Add dependee to depender's list
  139. if (currentDependencies) {
  140. currentDependencies->push_back(dependee);
  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. bool dependeeExists = this->FileTimeCache->Load(dependee, dependeeTime);
  149. if (!dependeeExists) {
  150. // The dependee does not exist.
  151. regenerate = true;
  152. // Print verbose output.
  153. if (this->Verbose) {
  154. cmSystemTools::Stdout(cmStrCat("Dependee \"", dependee,
  155. "\" does not exist for depender \"",
  156. depender, "\".\n"));
  157. }
  158. } else if (dependerExists) {
  159. // The dependee and depender both exist. Compare file times.
  160. if (dependerTime.Older(dependeeTime)) {
  161. // The depender is older than the dependee.
  162. regenerate = true;
  163. // Print verbose output.
  164. if (this->Verbose) {
  165. cmSystemTools::Stdout(cmStrCat("Dependee \"", dependee,
  166. "\" is newer than depender \"",
  167. depender, "\".\n"));
  168. }
  169. }
  170. } else {
  171. // The dependee exists, but the depender doesn't. Regenerate if the
  172. // internalDepends file is older than the dependee.
  173. if (internalDependsTime.Older(dependeeTime)) {
  174. // The depends-file is older than the dependee.
  175. regenerate = true;
  176. // Print verbose output.
  177. if (this->Verbose) {
  178. cmSystemTools::Stdout(cmStrCat("Dependee \"", dependee,
  179. "\" is newer than depends file \"",
  180. internalDependsFileName, "\".\n"));
  181. }
  182. }
  183. }
  184. if (regenerate) {
  185. // Dependencies must be regenerated.
  186. okay = false;
  187. // Remove the information of this depender from the map, it needs
  188. // to be rescanned
  189. if (currentDependencies) {
  190. validDeps.erase(depender);
  191. currentDependencies = nullptr;
  192. }
  193. // Remove the depender to be sure it is rebuilt.
  194. if (dependerExists) {
  195. cmSystemTools::RemoveFile(depender);
  196. this->FileTimeCache->Remove(depender);
  197. dependerExists = false;
  198. }
  199. }
  200. }
  201. return okay;
  202. }
  203. void cmDepends::SetIncludePathFromLanguage(const std::string& lang)
  204. {
  205. // Look for the new per "TARGET_" variant first:
  206. std::string includePathVar =
  207. cmStrCat("CMAKE_", lang, "_TARGET_INCLUDE_PATH");
  208. cmMakefile* mf = this->LocalGenerator->GetMakefile();
  209. cmValue includePath = mf->GetDefinition(includePathVar);
  210. if (includePath) {
  211. cmExpandList(*includePath, this->IncludePath);
  212. } else {
  213. // Fallback to the old directory level variable if no per-target var:
  214. includePathVar = cmStrCat("CMAKE_", lang, "_INCLUDE_PATH");
  215. includePath = mf->GetDefinition(includePathVar);
  216. if (includePath) {
  217. cmExpandList(*includePath, this->IncludePath);
  218. }
  219. }
  220. }