cmCacheManager.h 7.7 KB

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