cmFileAPICache.cxx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 "cmFileAPICache.h"
  4. #include <algorithm>
  5. #include <string>
  6. #include <utility>
  7. #include <vector>
  8. #include "cm_jsoncpp_value.h"
  9. #include "cmFileAPI.h"
  10. #include "cmState.h"
  11. #include "cmake.h"
  12. namespace {
  13. class Cache
  14. {
  15. cmFileAPI& FileAPI;
  16. unsigned long Version;
  17. cmState* State;
  18. Json::Value DumpEntries();
  19. Json::Value DumpEntry(std::string const& name);
  20. Json::Value DumpEntryProperties(std::string const& name);
  21. Json::Value DumpEntryProperty(std::string const& name,
  22. std::string const& prop);
  23. public:
  24. Cache(cmFileAPI& fileAPI, unsigned long version);
  25. Json::Value Dump();
  26. };
  27. Cache::Cache(cmFileAPI& fileAPI, unsigned long version)
  28. : FileAPI(fileAPI)
  29. , Version(version)
  30. , State(this->FileAPI.GetCMakeInstance()->GetState())
  31. {
  32. static_cast<void>(this->Version);
  33. }
  34. Json::Value Cache::Dump()
  35. {
  36. Json::Value cache = Json::objectValue;
  37. cache["entries"] = DumpEntries();
  38. return cache;
  39. }
  40. Json::Value Cache::DumpEntries()
  41. {
  42. Json::Value entries = Json::arrayValue;
  43. std::vector<std::string> names = this->State->GetCacheEntryKeys();
  44. std::sort(names.begin(), names.end());
  45. for (std::string const& name : names) {
  46. entries.append(this->DumpEntry(name));
  47. }
  48. return entries;
  49. }
  50. Json::Value Cache::DumpEntry(std::string const& name)
  51. {
  52. Json::Value entry = Json::objectValue;
  53. entry["name"] = name;
  54. entry["type"] =
  55. cmState::CacheEntryTypeToString(this->State->GetCacheEntryType(name));
  56. entry["value"] = this->State->GetSafeCacheEntryValue(name);
  57. Json::Value properties = this->DumpEntryProperties(name);
  58. if (!properties.empty()) {
  59. entry["properties"] = std::move(properties);
  60. }
  61. return entry;
  62. }
  63. Json::Value Cache::DumpEntryProperties(std::string const& name)
  64. {
  65. Json::Value properties = Json::arrayValue;
  66. std::vector<std::string> props =
  67. this->State->GetCacheEntryPropertyList(name);
  68. std::sort(props.begin(), props.end());
  69. for (std::string const& prop : props) {
  70. properties.append(this->DumpEntryProperty(name, prop));
  71. }
  72. return properties;
  73. }
  74. Json::Value Cache::DumpEntryProperty(std::string const& name,
  75. std::string const& prop)
  76. {
  77. Json::Value property = Json::objectValue;
  78. property["name"] = prop;
  79. cmProp p = this->State->GetCacheEntryProperty(name, prop);
  80. property["value"] = p ? *p : "";
  81. return property;
  82. }
  83. }
  84. Json::Value cmFileAPICacheDump(cmFileAPI& fileAPI, unsigned long version)
  85. {
  86. Cache cache(fileAPI, version);
  87. return cache.Dump();
  88. }