1 /*============================================================================
2 CMake - Cross Platform Makefile Generator
3 Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
5 Distributed under the OSI-approved BSD License (the "License");
6 see accompanying file Copyright.txt for details.
8 This software is distributed WITHOUT ANY WARRANTY; without even the
9 implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the License for more information.
11 ============================================================================*/
12 #ifndef cmCacheManager_h
13 #define cmCacheManager_h
15 #include "cmStandardIncludes.h"
16 #include "cmPropertyMap.h"
18 class cmMarkAsAdvancedCommand;
21 /** \class cmCacheManager
22 * \brief Control class for cmake's cache
24 * Load and Save CMake cache files.
30 cmCacheManager(cmake* cm);
32 friend class cmCacheManager::CacheIterator;
33 enum CacheEntryType{ BOOL=0, PATH, FILEPATH, STRING, INTERNAL,STATIC,
41 cmPropertyMap Properties;
42 const char* GetProperty(const char*) const;
43 void SetProperty(const char* property, const char* value);
44 void AppendProperty(const char* property, const char* value,
47 CacheEntry() : Value(""), Type(UNINITIALIZED), Initialized(false)
56 bool Find(const char*);
59 const char *GetName() const {
60 return this->Position->first.c_str(); }
61 const char* GetProperty(const char*) const ;
62 bool GetPropertyAsBool(const char*) const ;
63 bool PropertyExists(const char*) const;
64 void SetProperty(const char* property, const char* value);
65 void AppendProperty(const char* property, const char* value,
67 void SetProperty(const char* property, bool value);
68 const char* GetValue() const { return this->GetEntry().Value.c_str(); }
69 bool GetValueAsBool() const;
70 void SetValue(const char*);
71 CacheEntryType GetType() const { return this->GetEntry().Type; }
72 void SetType(CacheEntryType ty) { this->GetEntry().Type = ty; }
73 bool Initialized() { return this->GetEntry().Initialized; }
74 cmCacheManager &Container;
75 std::map<cmStdString, CacheEntry>::iterator Position;
76 CacheIterator(cmCacheManager &cm) : Container(cm) {
79 CacheIterator(cmCacheManager &cm, const char* key) : Container(cm)
87 CacheEntry const& GetEntry() const { return this->Position->second; }
88 CacheEntry& GetEntry() { return this->Position->second; }
91 ///! return an iterator to iterate through the cache map
92 cmCacheManager::CacheIterator NewIterator()
94 return CacheIterator(*this);
98 * Types for the cache entries. These are useful as
99 * hints for a cache editor program. Path should bring
100 * up a file chooser, BOOL a check box, and STRING a
101 * text entry box, FILEPATH is a full path to a file which
102 * can be different than just a path input
104 static CacheEntryType StringToType(const char*);
105 static const char* TypeToString(CacheEntryType);
106 static bool IsType(const char*);
108 ///! Load a cache for given makefile. Loads from ouput home.
109 bool LoadCache(cmMakefile*);
110 ///! Load a cache for given makefile. Loads from path/CMakeCache.txt.
111 bool LoadCache(const char* path);
112 bool LoadCache(const char* path, bool internal);
113 bool LoadCache(const char* path, bool internal,
114 std::set<cmStdString>& excludes,
115 std::set<cmStdString>& includes);
117 ///! Save cache for given makefile. Saves to ouput home CMakeCache.txt.
118 bool SaveCache(cmMakefile*) ;
119 ///! Save cache for given makefile. Saves to ouput path/CMakeCache.txt
120 bool SaveCache(const char* path) ;
122 ///! Delete the cache given
123 bool DeleteCache(const char* path);
125 ///! Print the cache to a stream
126 void PrintCache(std::ostream&) const;
128 ///! Get the iterator for an entry with a given key.
129 cmCacheManager::CacheIterator GetCacheIterator(const char *key=0);
131 ///! Remove an entry from the cache
132 void RemoveCacheEntry(const char* key);
134 ///! Get the number of entries in the cache
136 return static_cast<int>(this->Cache.size()); }
138 ///! Break up a line like VAR:type="value" into var, type and value
139 static bool ParseEntry(const char* entry,
142 CacheEntryType& type);
144 ///! Get a value from the cache given a key
145 const char* GetCacheValue(const char* key) const;
147 /** Get the version of CMake that wrote the cache. */
148 unsigned int GetCacheMajorVersion() { return this->CacheMajorVersion; }
149 unsigned int GetCacheMinorVersion() { return this->CacheMinorVersion; }
150 bool NeedCacheCompatibility(int major, int minor);
152 /** Define and document CACHE entry properties. */
153 static void DefineProperties(cmake *cm);
156 ///! Add an entry into the cache
157 void AddCacheEntry(const char* key, const char* value,
158 const char* helpString, CacheEntryType type);
160 ///! Get a cache entry object for a key
161 CacheEntry *GetCacheEntry(const char *key);
162 ///! Clean out the CMakeFiles directory if no CMakeCache.txt
163 void CleanCMakeFiles(const char* path);
165 // Cache version info
166 unsigned int CacheMajorVersion;
167 unsigned int CacheMinorVersion;
169 cmake* CMakeInstance;
170 typedef std::map<cmStdString, CacheEntry> CacheEntryMap;
171 static void OutputHelpString(std::ostream& fout,
172 const std::string& helpString);
173 static void OutputKey(std::ostream& fout, std::string const& key);
174 static void OutputValue(std::ostream& fout, std::string const& value);
176 static const char* PersistentProperties[];
177 bool ReadPropertyEntry(std::string const& key, CacheEntry& e);
178 void WritePropertyEntries(std::ostream& os, CacheIterator const& i);
181 // Only cmake and cmMakefile should be able to add cache values
182 // the commands should never use the cmCacheManager directly
183 friend class cmMakefile; // allow access to add cache values
184 friend class cmake; // allow access to add cache values
185 friend class cmakewizard; // allow access to add cache values
186 friend class cmMarkAsAdvancedCommand; // allow access to add cache values