cmDependsCompiler.cxx 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 <map>
  6. #include <memory>
  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. std::string prefix;
  89. if (this->LocalGenerator->GetCurrentBinaryDirectory() !=
  90. this->LocalGenerator->GetBinaryDirectory()) {
  91. prefix =
  92. cmStrCat(this->LocalGenerator->MaybeConvertToRelativePath(
  93. this->LocalGenerator->GetBinaryDirectory(),
  94. this->LocalGenerator->GetCurrentBinaryDirectory()),
  95. '/');
  96. }
  97. auto deps = cmReadGccDepfile(depFile.c_str(), prefix);
  98. if (!deps) {
  99. continue;
  100. }
  101. for (auto& entry : *deps) {
  102. depends = std::move(entry.paths);
  103. if (isValidPath) {
  104. cm::erase_if(depends, isValidPath);
  105. }
  106. // copy depends for each target, except first one, which can be
  107. // moved
  108. for (auto index = entry.rules.size() - 1; index > 0; --index) {
  109. dependencies[entry.rules[index]] = depends;
  110. }
  111. dependencies[entry.rules.front()] = std::move(depends);
  112. }
  113. } else {
  114. if (format == "msvc"_s) {
  115. cmsys::ifstream fin(depFile.c_str());
  116. if (!fin) {
  117. continue;
  118. }
  119. std::string line;
  120. if (!isValidPath) {
  121. // insert source as first dependency
  122. depends.push_back(source);
  123. }
  124. while (cmSystemTools::GetLineFromStream(fin, line)) {
  125. depends.emplace_back(std::move(line));
  126. }
  127. } else if (format == "gcc"_s) {
  128. auto deps = cmReadGccDepfile(depFile.c_str());
  129. if (!deps) {
  130. continue;
  131. }
  132. // dependencies generated by the compiler contains only one target
  133. depends = std::move(deps->front().paths);
  134. if (depends.empty()) {
  135. // unexpectedly empty, ignore it and continue
  136. continue;
  137. }
  138. // depending of the effective format of the dependencies file
  139. // generated by the compiler, the target can be wrongly identified
  140. // as a dependency so remove it from the list
  141. if (depends.front() == target) {
  142. depends.erase(depends.begin());
  143. }
  144. // ensure source file is the first dependency
  145. if (depends.front() != source) {
  146. cm::erase(depends, source);
  147. if (!isValidPath) {
  148. depends.insert(depends.begin(), source);
  149. }
  150. } else if (isValidPath) {
  151. // remove first dependency because it must not be filtered out
  152. depends.erase(depends.begin());
  153. }
  154. } else {
  155. // unknown format, ignore it
  156. continue;
  157. }
  158. if (isValidPath) {
  159. cm::erase_if(depends, isValidPath);
  160. // insert source as first dependency
  161. depends.insert(depends.begin(), source);
  162. }
  163. dependencies[target] = std::move(depends);
  164. }
  165. }
  166. }
  167. return status;
  168. }
  169. void cmDependsCompiler::WriteDependencies(
  170. const cmDepends::DependencyMap& dependencies, std::ostream& makeDepends,
  171. std::ostream& internalDepends)
  172. {
  173. // dependencies file consumed by make tool
  174. const auto& lineContinue = static_cast<cmGlobalUnixMakefileGenerator3*>(
  175. this->LocalGenerator->GetGlobalGenerator())
  176. ->LineContinueDirective;
  177. bool supportLongLineDepend = static_cast<cmGlobalUnixMakefileGenerator3*>(
  178. this->LocalGenerator->GetGlobalGenerator())
  179. ->SupportsLongLineDependencies();
  180. const auto& binDir = this->LocalGenerator->GetBinaryDirectory();
  181. cmDepends::DependencyMap makeDependencies(dependencies);
  182. std::unordered_set<cm::string_view> phonyTargets;
  183. // external dependencies file
  184. for (auto& node : makeDependencies) {
  185. auto target = this->LocalGenerator->ConvertToMakefilePath(
  186. this->LocalGenerator->MaybeConvertToRelativePath(binDir, node.first));
  187. auto& deps = node.second;
  188. std::transform(
  189. deps.cbegin(), deps.cend(), deps.begin(),
  190. [this, &binDir](const std::string& dep) {
  191. return this->LocalGenerator->ConvertToMakefilePath(
  192. this->LocalGenerator->MaybeConvertToRelativePath(binDir, 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. }