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