cmCacheManager.h 8.1 KB

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