cmCacheManager.h 7.8 KB

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