cmCacheManager.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #pragma once
  4. #include "cmConfigure.h" // IWYU pragma: keep
  5. #include <iosfwd>
  6. #include <map>
  7. #include <set>
  8. #include <string>
  9. #include <utility>
  10. #include <vector>
  11. #include "cmPropertyMap.h"
  12. #include "cmStateTypes.h"
  13. #include "cmValue.h"
  14. class cmMessenger;
  15. /** \class cmCacheManager
  16. * \brief Control class for cmake's cache
  17. *
  18. * Load and Save CMake cache files.
  19. *
  20. */
  21. class cmCacheManager
  22. {
  23. class CacheEntry
  24. {
  25. friend class cmCacheManager;
  26. public:
  27. const std::string& GetValue() const { return this->Value; }
  28. void SetValue(cmValue);
  29. cmStateEnums::CacheEntryType GetType() const { return this->Type; }
  30. void SetType(cmStateEnums::CacheEntryType ty) { this->Type = ty; }
  31. std::vector<std::string> GetPropertyList() const;
  32. cmValue GetProperty(const std::string& property) const;
  33. bool GetPropertyAsBool(const std::string& property) const;
  34. void SetProperty(const std::string& property, const char* value);
  35. void SetProperty(const std::string& property, bool value);
  36. void AppendProperty(const std::string& property, const std::string& value,
  37. bool asString = false);
  38. private:
  39. std::string Value;
  40. cmStateEnums::CacheEntryType Type = cmStateEnums::UNINITIALIZED;
  41. cmPropertyMap Properties;
  42. bool Initialized = false;
  43. };
  44. public:
  45. //! Load a cache for given makefile. Loads from path/CMakeCache.txt.
  46. bool LoadCache(const std::string& path, bool internal,
  47. std::set<std::string>& excludes,
  48. std::set<std::string>& includes);
  49. //! Save cache for given makefile. Saves to output path/CMakeCache.txt
  50. bool SaveCache(const std::string& path, cmMessenger* messenger);
  51. //! Delete the cache given
  52. bool DeleteCache(const std::string& path);
  53. //! Print the cache to a stream
  54. void PrintCache(std::ostream&) const;
  55. //! Get whether or not cache is loaded
  56. bool IsCacheLoaded() const { return this->CacheLoaded; }
  57. //! Get a value from the cache given a key
  58. cmValue GetInitializedCacheValue(const std::string& key) const;
  59. cmValue GetCacheEntryValue(const std::string& key) const
  60. {
  61. if (const auto* entry = this->GetCacheEntry(key)) {
  62. return cmValue(entry->GetValue());
  63. }
  64. return nullptr;
  65. }
  66. void SetCacheEntryValue(std::string const& key, std::string const& value)
  67. {
  68. if (auto* entry = this->GetCacheEntry(key)) {
  69. entry->SetValue(cmValue(value));
  70. }
  71. }
  72. cmStateEnums::CacheEntryType GetCacheEntryType(std::string const& key) const
  73. {
  74. if (const auto* entry = this->GetCacheEntry(key)) {
  75. return entry->GetType();
  76. }
  77. return cmStateEnums::UNINITIALIZED;
  78. }
  79. std::vector<std::string> GetCacheEntryPropertyList(
  80. std::string const& key) const
  81. {
  82. if (const auto* entry = this->GetCacheEntry(key)) {
  83. return entry->GetPropertyList();
  84. }
  85. return {};
  86. }
  87. cmValue GetCacheEntryProperty(std::string const& key,
  88. std::string const& propName) const
  89. {
  90. if (const auto* entry = this->GetCacheEntry(key)) {
  91. return entry->GetProperty(propName);
  92. }
  93. return nullptr;
  94. }
  95. bool GetCacheEntryPropertyAsBool(std::string const& key,
  96. std::string const& propName) const
  97. {
  98. if (const auto* entry = this->GetCacheEntry(key)) {
  99. return entry->GetPropertyAsBool(propName);
  100. }
  101. return false;
  102. }
  103. void SetCacheEntryProperty(std::string const& key,
  104. std::string const& propName,
  105. std::string const& value)
  106. {
  107. if (auto* entry = this->GetCacheEntry(key)) {
  108. entry->SetProperty(propName, value.c_str());
  109. }
  110. }
  111. void SetCacheEntryBoolProperty(std::string const& key,
  112. std::string const& propName, bool value)
  113. {
  114. if (auto* entry = this->GetCacheEntry(key)) {
  115. entry->SetProperty(propName, value);
  116. }
  117. }
  118. void RemoveCacheEntryProperty(std::string const& key,
  119. std::string const& propName)
  120. {
  121. if (auto* entry = this->GetCacheEntry(key)) {
  122. entry->SetProperty(propName, nullptr);
  123. }
  124. }
  125. void AppendCacheEntryProperty(std::string const& key,
  126. std::string const& propName,
  127. std::string const& value,
  128. bool asString = false)
  129. {
  130. if (auto* entry = this->GetCacheEntry(key)) {
  131. entry->AppendProperty(propName, value, asString);
  132. }
  133. }
  134. std::vector<std::string> GetCacheEntryKeys() const
  135. {
  136. std::vector<std::string> definitions;
  137. definitions.reserve(this->Cache.size());
  138. for (auto const& i : this->Cache) {
  139. definitions.push_back(i.first);
  140. }
  141. return definitions;
  142. }
  143. /** Get the version of CMake that wrote the cache. */
  144. unsigned int GetCacheMajorVersion() const { return this->CacheMajorVersion; }
  145. unsigned int GetCacheMinorVersion() const { return this->CacheMinorVersion; }
  146. //! Add an entry into the cache
  147. void AddCacheEntry(const std::string& key, const char* value,
  148. const char* helpString, cmStateEnums::CacheEntryType type)
  149. {
  150. this->AddCacheEntry(key,
  151. value ? cmValue(std::string(value)) : cmValue(nullptr),
  152. helpString, type);
  153. }
  154. void AddCacheEntry(const std::string& key, const std::string& value,
  155. const char* helpString, cmStateEnums::CacheEntryType type)
  156. {
  157. this->AddCacheEntry(key, cmValue(value), helpString, type);
  158. }
  159. void AddCacheEntry(const std::string& key, cmValue value,
  160. const char* helpString,
  161. cmStateEnums::CacheEntryType type);
  162. //! Remove an entry from the cache
  163. void RemoveCacheEntry(const std::string& key);
  164. private:
  165. //! Get a cache entry object for a key
  166. CacheEntry* GetCacheEntry(const std::string& key);
  167. const CacheEntry* GetCacheEntry(const std::string& key) const;
  168. //! Clean out the CMakeFiles directory if no CMakeCache.txt
  169. void CleanCMakeFiles(const std::string& path);
  170. static void OutputHelpString(std::ostream& fout,
  171. const std::string& helpString);
  172. static void OutputWarningComment(std::ostream& fout,
  173. std::string const& message,
  174. bool wrapSpaces);
  175. static void OutputNewlineTruncationWarning(std::ostream& fout,
  176. std::string const& key,
  177. std::string const& value,
  178. cmMessenger* messenger);
  179. static void OutputKey(std::ostream& fout, std::string const& key);
  180. static void OutputValue(std::ostream& fout, std::string const& value);
  181. static void OutputValueNoNewlines(std::ostream& fout,
  182. std::string const& value);
  183. static const char* PersistentProperties[];
  184. bool ReadPropertyEntry(const std::string& key, const CacheEntry& e);
  185. void WritePropertyEntries(std::ostream& os, const std::string& entryKey,
  186. const CacheEntry& e, cmMessenger* messenger) const;
  187. std::map<std::string, CacheEntry> Cache;
  188. bool CacheLoaded = false;
  189. // Cache version info
  190. unsigned int CacheMajorVersion = 0;
  191. unsigned int CacheMinorVersion = 0;
  192. };