1
0

cmCacheManager.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. class cmMakefile;
  15. class cmMarkAsAdvancedCommand;
  16. class cmake;
  17. /** \class cmCacheManager
  18. * \brief Control class for cmake's cache
  19. *
  20. * Load and Save CMake cache files.
  21. *
  22. */
  23. class cmCacheManager
  24. {
  25. public:
  26. cmCacheManager(cmake* cm);
  27. class CacheIterator;
  28. friend class cmCacheManager::CacheIterator;
  29. enum CacheEntryType{ BOOL=0, PATH, FILEPATH, STRING, INTERNAL,STATIC,
  30. UNINITIALIZED };
  31. private:
  32. struct CacheEntry
  33. {
  34. std::string Value;
  35. CacheEntryType Type;
  36. cmPropertyMap Properties;
  37. const char* GetProperty(const char*) const;
  38. void SetProperty(const char* property, const char* value);
  39. void AppendProperty(const char* property, const char* value,
  40. bool asString=false);
  41. bool Initialized;
  42. CacheEntry() : Value(""), Type(UNINITIALIZED), Initialized(false)
  43. {}
  44. };
  45. public:
  46. class CacheIterator
  47. {
  48. public:
  49. void Begin();
  50. bool Find(const char*);
  51. bool IsAtEnd() const;
  52. void Next();
  53. const char *GetName() const {
  54. return this->Position->first.c_str(); }
  55. const char* GetProperty(const char*) const ;
  56. bool GetPropertyAsBool(const char*) const ;
  57. bool PropertyExists(const char*) const;
  58. void SetProperty(const char* property, const char* value);
  59. void AppendProperty(const char* property, const char* value,
  60. bool asString=false);
  61. void SetProperty(const char* property, bool value);
  62. const char* GetValue() const { return this->GetEntry().Value.c_str(); }
  63. bool GetValueAsBool() const;
  64. void SetValue(const char*);
  65. CacheEntryType GetType() const { return this->GetEntry().Type; }
  66. void SetType(CacheEntryType ty) { this->GetEntry().Type = ty; }
  67. bool Initialized() { return this->GetEntry().Initialized; }
  68. cmCacheManager &Container;
  69. std::map<cmStdString, CacheEntry>::iterator Position;
  70. CacheIterator(cmCacheManager &cm) : Container(cm) {
  71. this->Begin();
  72. }
  73. CacheIterator(cmCacheManager &cm, const char* key) : Container(cm)
  74. {
  75. if ( key )
  76. {
  77. this->Find(key);
  78. }
  79. }
  80. private:
  81. CacheEntry const& GetEntry() const { return this->Position->second; }
  82. CacheEntry& GetEntry() { return this->Position->second; }
  83. };
  84. ///! return an iterator to iterate through the cache map
  85. cmCacheManager::CacheIterator NewIterator()
  86. {
  87. return CacheIterator(*this);
  88. }
  89. /**
  90. * Types for the cache entries. These are useful as
  91. * hints for a cache editor program. Path should bring
  92. * up a file chooser, BOOL a check box, and STRING a
  93. * text entry box, FILEPATH is a full path to a file which
  94. * can be different than just a path input
  95. */
  96. static CacheEntryType StringToType(const char*);
  97. static const char* TypeToString(CacheEntryType);
  98. static bool IsType(const char*);
  99. ///! Load a cache for given makefile. Loads from ouput home.
  100. bool LoadCache(cmMakefile*);
  101. ///! Load a cache for given makefile. Loads from path/CMakeCache.txt.
  102. bool LoadCache(const char* path);
  103. bool LoadCache(const char* path, bool internal);
  104. bool LoadCache(const char* path, bool internal,
  105. std::set<cmStdString>& excludes,
  106. std::set<cmStdString>& includes);
  107. ///! Save cache for given makefile. Saves to ouput home CMakeCache.txt.
  108. bool SaveCache(cmMakefile*) ;
  109. ///! Save cache for given makefile. Saves to ouput path/CMakeCache.txt
  110. bool SaveCache(const char* path) ;
  111. ///! Delete the cache given
  112. bool DeleteCache(const char* path);
  113. ///! Print the cache to a stream
  114. void PrintCache(std::ostream&) const;
  115. ///! Get the iterator for an entry with a given key.
  116. cmCacheManager::CacheIterator GetCacheIterator(const char *key=0);
  117. ///! Remove an entry from the cache
  118. void RemoveCacheEntry(const char* key);
  119. ///! Get the number of entries in the cache
  120. int GetSize() {
  121. return static_cast<int>(this->Cache.size()); }
  122. ///! Break up a line like VAR:type="value" into var, type and value
  123. static bool ParseEntry(const char* entry,
  124. std::string& var,
  125. std::string& value,
  126. CacheEntryType& type);
  127. ///! Get a value from the cache given a key
  128. const char* GetCacheValue(const char* key) const;
  129. /** Get the version of CMake that wrote the cache. */
  130. unsigned int GetCacheMajorVersion() { return this->CacheMajorVersion; }
  131. unsigned int GetCacheMinorVersion() { return this->CacheMinorVersion; }
  132. bool NeedCacheCompatibility(int major, int minor);
  133. /** Define and document CACHE entry properties. */
  134. static void DefineProperties(cmake *cm);
  135. protected:
  136. ///! Add an entry into the cache
  137. void AddCacheEntry(const char* key, const char* value,
  138. const char* helpString, CacheEntryType type);
  139. ///! Get a cache entry object for a key
  140. CacheEntry *GetCacheEntry(const char *key);
  141. ///! Clean out the CMakeFiles directory if no CMakeCache.txt
  142. void CleanCMakeFiles(const char* path);
  143. // Cache version info
  144. unsigned int CacheMajorVersion;
  145. unsigned int CacheMinorVersion;
  146. private:
  147. cmake* CMakeInstance;
  148. typedef std::map<cmStdString, CacheEntry> CacheEntryMap;
  149. static void OutputHelpString(std::ostream& fout,
  150. const std::string& helpString);
  151. static void OutputKey(std::ostream& fout, std::string const& key);
  152. static void OutputValue(std::ostream& fout, std::string const& value);
  153. static const char* PersistentProperties[];
  154. bool ReadPropertyEntry(std::string const& key, CacheEntry& e);
  155. void WritePropertyEntries(std::ostream& os, CacheIterator const& i);
  156. CacheEntryMap Cache;
  157. // Only cmake and cmMakefile should be able to add cache values
  158. // the commands should never use the cmCacheManager directly
  159. friend class cmMakefile; // allow access to add cache values
  160. friend class cmake; // allow access to add cache values
  161. friend class cmakewizard; // allow access to add cache values
  162. friend class cmMarkAsAdvancedCommand; // allow access to add cache values
  163. };
  164. #endif