cmDepends.cxx 7.7 KB

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