cmCacheManager.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #ifndef cmCacheManager_h
  4. #define cmCacheManager_h
  5. #include "cmConfigure.h" // IWYU pragma: keep
  6. #include <iosfwd>
  7. #include <map>
  8. #include <set>
  9. #include <string>
  10. #include <utility>
  11. #include <vector>
  12. #include "cmPropertyMap.h"
  13. #include "cmStateTypes.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. public:
  24. cmCacheManager();
  25. class CacheIterator;
  26. friend class cmCacheManager::CacheIterator;
  27. private:
  28. struct CacheEntry
  29. {
  30. std::string Value;
  31. cmStateEnums::CacheEntryType Type = cmStateEnums::UNINITIALIZED;
  32. cmPropertyMap Properties;
  33. std::vector<std::string> GetPropertyList() const;
  34. const char* GetProperty(const std::string&) const;
  35. void SetProperty(const std::string& property, const char* value);
  36. void AppendProperty(const std::string& property, const char* value,
  37. bool asString = false);
  38. bool Initialized = false;
  39. };
  40. public:
  41. class CacheIterator
  42. {
  43. public:
  44. void Begin();
  45. bool Find(const std::string&);
  46. bool IsAtEnd() const;
  47. void Next();
  48. std::string GetName() const { return this->Position->first; }
  49. std::vector<std::string> GetPropertyList() const;
  50. const char* GetProperty(const std::string&) const;
  51. bool GetPropertyAsBool(const std::string&) const;
  52. bool PropertyExists(const std::string&) const;
  53. void SetProperty(const std::string& property, const char* value);
  54. void AppendProperty(const std::string& property, const char* value,
  55. bool asString = false);
  56. void SetProperty(const std::string& property, bool value);
  57. const char* GetValue() const { return this->GetEntry().Value.c_str(); }
  58. bool GetValueAsBool() const;
  59. void SetValue(const char*);
  60. cmStateEnums::CacheEntryType GetType() const
  61. {
  62. return this->GetEntry().Type;
  63. }
  64. void SetType(cmStateEnums::CacheEntryType ty)
  65. {
  66. this->GetEntry().Type = ty;
  67. }
  68. bool Initialized() { return this->GetEntry().Initialized; }
  69. cmCacheManager& Container;
  70. std::map<std::string, CacheEntry>::iterator Position;
  71. CacheIterator(cmCacheManager& cm)
  72. : Container(cm)
  73. {
  74. this->Begin();
  75. }
  76. CacheIterator(cmCacheManager& cm, const char* key)
  77. : Container(cm)
  78. {
  79. if (key) {
  80. this->Find(key);
  81. }
  82. }
  83. private:
  84. CacheEntry const& GetEntry() const { return this->Position->second; }
  85. CacheEntry& GetEntry() { return this->Position->second; }
  86. };
  87. //! return an iterator to iterate through the cache map
  88. cmCacheManager::CacheIterator NewIterator() { return { *this }; }
  89. //! Load a cache for given makefile. Loads from path/CMakeCache.txt.
  90. bool LoadCache(const std::string& path, bool internal,
  91. std::set<std::string>& excludes,
  92. std::set<std::string>& includes);
  93. //! Save cache for given makefile. Saves to output path/CMakeCache.txt
  94. bool SaveCache(const std::string& path, cmMessenger* messenger);
  95. //! Delete the cache given
  96. bool DeleteCache(const std::string& path);
  97. //! Print the cache to a stream
  98. void PrintCache(std::ostream&) const;
  99. //! Get the iterator for an entry with a given key.
  100. cmCacheManager::CacheIterator GetCacheIterator(const char* key = nullptr);
  101. //! Remove an entry from the cache
  102. void RemoveCacheEntry(const std::string& key);
  103. //! Get the number of entries in the cache
  104. int GetSize() { return static_cast<int>(this->Cache.size()); }
  105. //! Get a value from the cache given a key
  106. const std::string* GetInitializedCacheValue(const std::string& key) const;
  107. const char* GetCacheEntryValue(const std::string& key)
  108. {
  109. cmCacheManager::CacheIterator it = this->GetCacheIterator(key.c_str());
  110. if (it.IsAtEnd()) {
  111. return nullptr;
  112. }
  113. return it.GetValue();
  114. }
  115. const char* GetCacheEntryProperty(std::string const& key,
  116. std::string const& propName)
  117. {
  118. return this->GetCacheIterator(key.c_str()).GetProperty(propName);
  119. }
  120. cmStateEnums::CacheEntryType GetCacheEntryType(std::string const& key)
  121. {
  122. return this->GetCacheIterator(key.c_str()).GetType();
  123. }
  124. bool GetCacheEntryPropertyAsBool(std::string const& key,
  125. std::string const& propName)
  126. {
  127. return this->GetCacheIterator(key.c_str()).GetPropertyAsBool(propName);
  128. }
  129. void SetCacheEntryProperty(std::string const& key,
  130. std::string const& propName,
  131. std::string const& value)
  132. {
  133. this->GetCacheIterator(key.c_str()).SetProperty(propName, value.c_str());
  134. }
  135. void SetCacheEntryBoolProperty(std::string const& key,
  136. std::string const& propName, bool value)
  137. {
  138. this->GetCacheIterator(key.c_str()).SetProperty(propName, value);
  139. }
  140. void SetCacheEntryValue(std::string const& key, std::string const& value)
  141. {
  142. this->GetCacheIterator(key.c_str()).SetValue(value.c_str());
  143. }
  144. void RemoveCacheEntryProperty(std::string const& key,
  145. std::string const& propName)
  146. {
  147. this->GetCacheIterator(key.c_str()).SetProperty(propName, nullptr);
  148. }
  149. void AppendCacheEntryProperty(std::string const& key,
  150. std::string const& propName,
  151. std::string const& value,
  152. bool asString = false)
  153. {
  154. this->GetCacheIterator(key.c_str())
  155. .AppendProperty(propName, value.c_str(), asString);
  156. }
  157. std::vector<std::string> GetCacheEntryKeys()
  158. {
  159. std::vector<std::string> definitions;
  160. definitions.reserve(this->GetSize());
  161. cmCacheManager::CacheIterator cit = this->GetCacheIterator();
  162. for (cit.Begin(); !cit.IsAtEnd(); cit.Next()) {
  163. definitions.push_back(cit.GetName());
  164. }
  165. return definitions;
  166. }
  167. /** Get the version of CMake that wrote the cache. */
  168. unsigned int GetCacheMajorVersion() const { return this->CacheMajorVersion; }
  169. unsigned int GetCacheMinorVersion() const { return this->CacheMinorVersion; }
  170. protected:
  171. //! Add an entry into the cache
  172. void AddCacheEntry(const std::string& key, const char* value,
  173. const char* helpString,
  174. cmStateEnums::CacheEntryType type);
  175. //! Get a cache entry object for a key
  176. CacheEntry* GetCacheEntry(const std::string& key);
  177. //! Clean out the CMakeFiles directory if no CMakeCache.txt
  178. void CleanCMakeFiles(const std::string& path);
  179. // Cache version info
  180. unsigned int CacheMajorVersion;
  181. unsigned int CacheMinorVersion;
  182. private:
  183. using CacheEntryMap = std::map<std::string, CacheEntry>;
  184. static void OutputHelpString(std::ostream& fout,
  185. const std::string& helpString);
  186. static void OutputWarningComment(std::ostream& fout,
  187. std::string const& message,
  188. bool wrapSpaces);
  189. static void OutputNewlineTruncationWarning(std::ostream& fout,
  190. std::string const& key,
  191. std::string const& value,
  192. cmMessenger* messenger);
  193. static void OutputKey(std::ostream& fout, std::string const& key);
  194. static void OutputValue(std::ostream& fout, std::string const& value);
  195. static void OutputValueNoNewlines(std::ostream& fout,
  196. std::string const& value);
  197. static const char* PersistentProperties[];
  198. bool ReadPropertyEntry(std::string const& key, CacheEntry& e);
  199. void WritePropertyEntries(std::ostream& os, CacheIterator i,
  200. cmMessenger* messenger);
  201. CacheEntryMap Cache;
  202. // Only cmake and cmState should be able to add cache values
  203. // the commands should never use the cmCacheManager directly
  204. friend class cmState; // allow access to add cache values
  205. friend class cmake; // allow access to add cache values
  206. };
  207. #endif