cmDependsCompiler.cxx 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 "cmDependsCompiler.h"
  4. #include <algorithm>
  5. #include <iterator>
  6. #include <map>
  7. #include <string>
  8. #include <unordered_set>
  9. #include <utility>
  10. #include <cm/optional>
  11. #include <cm/string_view>
  12. #include <cm/vector>
  13. #include <cmext/string_view>
  14. #include "cmsys/FStream.hxx"
  15. #include "cmFileTime.h"
  16. #include "cmGccDepfileReader.h"
  17. #include "cmGccDepfileReaderTypes.h"
  18. #include "cmGlobalUnixMakefileGenerator3.h"
  19. #include "cmLocalUnixMakefileGenerator3.h"
  20. #include "cmStringAlgorithms.h"
  21. #include "cmSystemTools.h"
  22. bool cmDependsCompiler::CheckDependencies(
  23. const std::string& internalDepFile, const std::vector<std::string>& depFiles,
  24. cmDepends::DependencyMap& dependencies,
  25. const std::function<bool(const std::string&)>& isValidPath)
  26. {
  27. bool status = true;
  28. bool forceReadDeps = true;
  29. cmFileTime internalDepFileTime;
  30. // read cached dependencies stored in internal file
  31. if (cmSystemTools::FileExists(internalDepFile)) {
  32. internalDepFileTime.Load(internalDepFile);
  33. forceReadDeps = false;
  34. // read current dependencies
  35. cmsys::ifstream fin(internalDepFile.c_str());
  36. if (fin) {
  37. std::string line;
  38. std::string depender;
  39. std::vector<std::string>* currentDependencies = nullptr;
  40. while (std::getline(fin, line)) {
  41. if (line.empty() || line.front() == '#') {
  42. continue;
  43. }
  44. // Drop carriage return character at the end
  45. if (line.back() == '\r') {
  46. line.pop_back();
  47. if (line.empty()) {
  48. continue;
  49. }
  50. }
  51. // Check if this a depender line
  52. if (line.front() != ' ') {
  53. depender = std::move(line);
  54. currentDependencies = &dependencies[depender];
  55. continue;
  56. }
  57. // This is a dependee line
  58. if (currentDependencies != nullptr) {
  59. currentDependencies->emplace_back(line.substr(1));
  60. }
  61. }
  62. fin.close();
  63. }
  64. }
  65. // Now, update dependencies map with all new compiler generated
  66. // dependencies files
  67. cmFileTime depFileTime;
  68. for (auto dep = depFiles.begin(); dep != depFiles.end(); dep++) {
  69. const auto& source = *dep++;
  70. const auto& target = *dep++;
  71. const auto& format = *dep++;
  72. const auto& depFile = *dep;
  73. if (!cmSystemTools::FileExists(depFile)) {
  74. continue;
  75. }
  76. if (!forceReadDeps) {
  77. depFileTime.Load(depFile);
  78. }
  79. if (forceReadDeps || depFileTime.Compare(internalDepFileTime) >= 0) {
  80. status = false;
  81. if (this->Verbose) {
  82. cmSystemTools::Stdout(cmStrCat("Dependencies file \"", depFile,
  83. "\" is newer than depends file \"",
  84. internalDepFile, "\".\n"));
  85. }
  86. std::vector<std::string> depends;
  87. if (format == "custom"_s) {
  88. auto deps = cmReadGccDepfile(
  89. depFile.c_str(), this->LocalGenerator->GetCurrentBinaryDirectory());
  90. if (!deps) {
  91. continue;
  92. }
  93. for (auto& entry : *deps) {
  94. depends = std::move(entry.paths);
  95. if (isValidPath) {
  96. cm::erase_if(depends, isValidPath);
  97. }
  98. // copy depends for each target, except first one, which can be
  99. // moved
  100. for (auto index = entry.rules.size() - 1; index > 0; --index) {
  101. auto& rule_deps = dependencies[entry.rules[index]];
  102. rule_deps.insert(rule_deps.end(), depends.cbegin(),
  103. depends.cend());
  104. }
  105. auto& rule_deps = dependencies[entry.rules.front()];
  106. std::move(depends.cbegin(), depends.cend(),
  107. std::back_inserter(rule_deps));
  108. }
  109. } else {
  110. if (format == "msvc"_s) {
  111. cmsys::ifstream fin(depFile.c_str());
  112. if (!fin) {
  113. continue;
  114. }
  115. std::string line;
  116. if (!isValidPath && !source.empty()) {
  117. // insert source as first dependency
  118. depends.push_back(source);
  119. }
  120. while (cmSystemTools::GetLineFromStream(fin, line)) {
  121. depends.emplace_back(std::move(line));
  122. }
  123. } else if (format == "gcc"_s) {
  124. auto deps = cmReadGccDepfile(
  125. depFile.c_str(), this->LocalGenerator->GetCurrentBinaryDirectory(),
  126. GccDepfilePrependPaths::Deps);
  127. if (!deps) {
  128. continue;
  129. }
  130. // dependencies generated by the compiler contains only one target
  131. depends = std::move(deps->front().paths);
  132. if (depends.empty()) {
  133. // unexpectedly empty, ignore it and continue
  134. continue;
  135. }
  136. // depending of the effective format of the dependencies file
  137. // generated by the compiler, the target can be wrongly identified
  138. // as a dependency so remove it from the list
  139. if (depends.front() == target) {
  140. depends.erase(depends.begin());
  141. }
  142. // ensure source file is the first dependency
  143. if (!source.empty()) {
  144. if (depends.front() != source) {
  145. cm::erase(depends, source);
  146. if (!isValidPath) {
  147. depends.insert(depends.begin(), source);
  148. }
  149. } else if (isValidPath) {
  150. // remove first dependency because it must not be filtered out
  151. depends.erase(depends.begin());
  152. }
  153. }
  154. } else {
  155. // unknown format, ignore it
  156. continue;
  157. }
  158. if (isValidPath) {
  159. cm::erase_if(depends, isValidPath);
  160. if (!source.empty()) {
  161. // insert source as first dependency
  162. depends.insert(depends.begin(), source);
  163. }
  164. }
  165. dependencies[target] = std::move(depends);
  166. }
  167. }
  168. }
  169. return status;
  170. }
  171. void cmDependsCompiler::WriteDependencies(
  172. const cmDepends::DependencyMap& dependencies, std::ostream& makeDepends,
  173. std::ostream& internalDepends)
  174. {
  175. // dependencies file consumed by make tool
  176. const auto& lineContinue = static_cast<cmGlobalUnixMakefileGenerator3*>(
  177. this->LocalGenerator->GetGlobalGenerator())
  178. ->LineContinueDirective;
  179. bool supportLongLineDepend = static_cast<cmGlobalUnixMakefileGenerator3*>(
  180. this->LocalGenerator->GetGlobalGenerator())
  181. ->SupportsLongLineDependencies();
  182. cmDepends::DependencyMap makeDependencies(dependencies);
  183. std::unordered_set<cm::string_view> phonyTargets;
  184. // external dependencies file
  185. for (auto& node : makeDependencies) {
  186. auto target = this->LocalGenerator->ConvertToMakefilePath(
  187. this->LocalGenerator->MaybeRelativeToTopBinDir(node.first));
  188. auto& deps = node.second;
  189. std::transform(deps.cbegin(), deps.cend(), deps.begin(),
  190. [this](const std::string& dep) {
  191. return this->LocalGenerator->ConvertToMakefilePath(
  192. this->LocalGenerator->MaybeRelativeToTopBinDir(dep));
  193. });
  194. bool first_dep = true;
  195. if (supportLongLineDepend) {
  196. makeDepends << target << ": ";
  197. }
  198. for (const auto& dep : deps) {
  199. if (supportLongLineDepend) {
  200. if (first_dep) {
  201. first_dep = false;
  202. makeDepends << dep;
  203. } else {
  204. makeDepends << ' ' << lineContinue << " " << dep;
  205. }
  206. } else {
  207. makeDepends << target << ": " << dep << std::endl;
  208. }
  209. phonyTargets.emplace(dep.data(), dep.length());
  210. }
  211. makeDepends << std::endl << std::endl;
  212. }
  213. // add phony targets
  214. for (const auto& target : phonyTargets) {
  215. makeDepends << std::endl << target << ':' << std::endl;
  216. }
  217. // internal dependencies file
  218. for (const auto& node : dependencies) {
  219. internalDepends << node.first << std::endl;
  220. for (const auto& dep : node.second) {
  221. internalDepends << ' ' << dep << std::endl;
  222. }
  223. internalDepends << std::endl;
  224. }
  225. }
  226. void cmDependsCompiler::ClearDependencies(
  227. const std::vector<std::string>& depFiles)
  228. {
  229. for (auto dep = depFiles.begin(); dep != depFiles.end(); dep++) {
  230. dep += 3;
  231. cmSystemTools::RemoveFile(*dep);
  232. }
  233. }