cmCacheManager.h 8.1 KB

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