cmCacheManager.h 6.5 KB

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