cmFileAPICMakeFiles.cxx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file LICENSE.rst or https://cmake.org/licensing for details. */
  3. #include "cmFileAPICMakeFiles.h"
  4. #include <memory>
  5. #include <string>
  6. #include <utility>
  7. #include <vector>
  8. #include <cm3p/json/value.h>
  9. #include "cmFileAPI.h"
  10. #include "cmGlobCacheEntry.h"
  11. #include "cmGlobalGenerator.h"
  12. #include "cmLocalGenerator.h"
  13. #include "cmMakefile.h"
  14. #include "cmSystemTools.h"
  15. #include "cmake.h"
  16. namespace {
  17. class CMakeFiles
  18. {
  19. cmFileAPI& FileAPI;
  20. unsigned int Version;
  21. std::string CMakeModules;
  22. std::string const& TopSource;
  23. std::string const& TopBuild;
  24. bool OutOfSource;
  25. Json::Value DumpPaths();
  26. Json::Value DumpInputs();
  27. Json::Value DumpInput(std::string const& file);
  28. Json::Value DumpGlobsDependent();
  29. Json::Value DumpGlobDependent(cmGlobCacheEntry const& entry);
  30. public:
  31. CMakeFiles(cmFileAPI& fileAPI, unsigned int version);
  32. Json::Value Dump();
  33. };
  34. CMakeFiles::CMakeFiles(cmFileAPI& fileAPI, unsigned int version)
  35. : FileAPI(fileAPI)
  36. , Version(version)
  37. , CMakeModules(cmSystemTools::GetCMakeRoot() + "/Modules")
  38. , TopSource(this->FileAPI.GetCMakeInstance()->GetHomeDirectory())
  39. , TopBuild(this->FileAPI.GetCMakeInstance()->GetHomeOutputDirectory())
  40. , OutOfSource(this->TopBuild != this->TopSource)
  41. {
  42. static_cast<void>(this->Version);
  43. }
  44. Json::Value CMakeFiles::Dump()
  45. {
  46. Json::Value cmakeFiles = Json::objectValue;
  47. cmakeFiles["paths"] = this->DumpPaths();
  48. cmakeFiles["inputs"] = this->DumpInputs();
  49. Json::Value globsDependent = this->DumpGlobsDependent();
  50. if (!globsDependent.empty()) {
  51. cmakeFiles["globsDependent"] = std::move(globsDependent);
  52. }
  53. return cmakeFiles;
  54. }
  55. Json::Value CMakeFiles::DumpPaths()
  56. {
  57. Json::Value paths = Json::objectValue;
  58. paths["source"] = this->TopSource;
  59. paths["build"] = this->TopBuild;
  60. return paths;
  61. }
  62. Json::Value CMakeFiles::DumpInputs()
  63. {
  64. Json::Value inputs = Json::arrayValue;
  65. cmGlobalGenerator* gg =
  66. this->FileAPI.GetCMakeInstance()->GetGlobalGenerator();
  67. for (auto const& lg : gg->GetLocalGenerators()) {
  68. cmMakefile const* mf = lg->GetMakefile();
  69. for (std::string const& file : mf->GetListFiles()) {
  70. inputs.append(this->DumpInput(file));
  71. }
  72. }
  73. return inputs;
  74. }
  75. Json::Value CMakeFiles::DumpInput(std::string const& file)
  76. {
  77. Json::Value input = Json::objectValue;
  78. bool const isCMake = cmSystemTools::IsSubDirectory(file, this->CMakeModules);
  79. if (isCMake) {
  80. input["isCMake"] = true;
  81. }
  82. if (!cmSystemTools::IsSubDirectory(file, this->TopSource) &&
  83. !cmSystemTools::IsSubDirectory(file, this->TopBuild)) {
  84. input["isExternal"] = true;
  85. }
  86. if (this->OutOfSource &&
  87. cmSystemTools::IsSubDirectory(file, this->TopBuild)) {
  88. input["isGenerated"] = true;
  89. }
  90. std::string path = file;
  91. if (!isCMake && cmSystemTools::IsSubDirectory(path, this->TopSource)) {
  92. // Use a relative path within the source directory.
  93. path = cmSystemTools::RelativePath(this->TopSource, path);
  94. }
  95. input["path"] = path;
  96. return input;
  97. }
  98. Json::Value CMakeFiles::DumpGlobsDependent()
  99. {
  100. Json::Value globsDependent = Json::arrayValue;
  101. for (cmGlobCacheEntry const& entry :
  102. this->FileAPI.GetCMakeInstance()->GetGlobCacheEntries()) {
  103. globsDependent.append(this->DumpGlobDependent(entry));
  104. }
  105. return globsDependent;
  106. }
  107. Json::Value CMakeFiles::DumpGlobDependent(cmGlobCacheEntry const& entry)
  108. {
  109. Json::Value globDependent = Json::objectValue;
  110. globDependent["expression"] = entry.Expression;
  111. if (entry.Recurse) {
  112. globDependent["recurse"] = true;
  113. }
  114. if (entry.ListDirectories) {
  115. globDependent["listDirectories"] = true;
  116. }
  117. if (entry.FollowSymlinks) {
  118. globDependent["followSymlinks"] = true;
  119. }
  120. if (!entry.Relative.empty()) {
  121. globDependent["relative"] = entry.Relative;
  122. }
  123. Json::Value paths = Json::arrayValue;
  124. for (std::string const& file : entry.Files) {
  125. paths.append(file);
  126. }
  127. globDependent["paths"] = std::move(paths);
  128. return globDependent;
  129. }
  130. }
  131. Json::Value cmFileAPICMakeFilesDump(cmFileAPI& fileAPI, unsigned int version)
  132. {
  133. CMakeFiles cmakeFiles(fileAPI, version);
  134. return cmakeFiles.Dump();
  135. }