1 /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 file Copyright.txt or https://cmake.org/licensing for details. */
5 #include "cmConfigure.h" // IWYU pragma: keep
14 #include "cmPropertyMap.h"
15 #include "cmStateTypes.h"
20 /** \class cmCacheManager
21 * \brief Control class for cmake's cache
23 * Load and Save CMake cache files.
30 friend class cmCacheManager;
33 const std::string& GetValue() const { return this->Value; }
34 void SetValue(cmValue);
36 cmStateEnums::CacheEntryType GetType() const { return this->Type; }
37 void SetType(cmStateEnums::CacheEntryType ty) { this->Type = ty; }
39 std::vector<std::string> GetPropertyList() const;
40 cmValue GetProperty(const std::string& property) const;
41 bool GetPropertyAsBool(const std::string& property) const;
42 void SetProperty(const std::string& property, const char* value);
43 void SetProperty(const std::string& property, bool value);
44 void AppendProperty(const std::string& property, const std::string& value,
45 bool asString = false);
49 cmStateEnums::CacheEntryType Type = cmStateEnums::UNINITIALIZED;
50 cmPropertyMap Properties;
51 bool Initialized = false;
55 //! Load a cache for given makefile. Loads from path/CMakeCache.txt.
56 bool LoadCache(const std::string& path, bool internal,
57 std::set<std::string>& excludes,
58 std::set<std::string>& includes);
60 //! Save cache for given makefile. Saves to output path/CMakeCache.txt
61 bool SaveCache(const std::string& path, cmMessenger* messenger);
63 //! Delete the cache given
64 bool DeleteCache(const std::string& path);
66 //! Print the cache to a stream
67 void PrintCache(std::ostream&) const;
69 //! Get whether or not cache is loaded
70 bool IsCacheLoaded() const { return this->CacheLoaded; }
72 //! Get a value from the cache given a key
73 cmValue GetInitializedCacheValue(const std::string& key) const;
75 cmValue GetCacheEntryValue(const std::string& key) const
77 if (const auto* entry = this->GetCacheEntry(key)) {
78 return cmValue(entry->GetValue());
83 void SetCacheEntryValue(std::string const& key, std::string const& value)
85 if (auto* entry = this->GetCacheEntry(key)) {
86 entry->SetValue(cmValue(value));
90 cmStateEnums::CacheEntryType GetCacheEntryType(std::string const& key) const
92 if (const auto* entry = this->GetCacheEntry(key)) {
93 return entry->GetType();
95 return cmStateEnums::UNINITIALIZED;
98 std::vector<std::string> GetCacheEntryPropertyList(
99 std::string const& key) const
101 if (const auto* entry = this->GetCacheEntry(key)) {
102 return entry->GetPropertyList();
107 cmValue GetCacheEntryProperty(std::string const& key,
108 std::string const& propName) const
110 if (const auto* entry = this->GetCacheEntry(key)) {
111 return entry->GetProperty(propName);
116 bool GetCacheEntryPropertyAsBool(std::string const& key,
117 std::string const& propName) const
119 if (const auto* entry = this->GetCacheEntry(key)) {
120 return entry->GetPropertyAsBool(propName);
125 void SetCacheEntryProperty(std::string const& key,
126 std::string const& propName,
127 std::string const& value)
129 if (auto* entry = this->GetCacheEntry(key)) {
130 entry->SetProperty(propName, value.c_str());
134 void SetCacheEntryBoolProperty(std::string const& key,
135 std::string const& propName, bool value)
137 if (auto* entry = this->GetCacheEntry(key)) {
138 entry->SetProperty(propName, value);
142 void RemoveCacheEntryProperty(std::string const& key,
143 std::string const& propName)
145 if (auto* entry = this->GetCacheEntry(key)) {
146 entry->SetProperty(propName, nullptr);
150 void AppendCacheEntryProperty(std::string const& key,
151 std::string const& propName,
152 std::string const& value,
153 bool asString = false)
155 if (auto* entry = this->GetCacheEntry(key)) {
156 entry->AppendProperty(propName, value, asString);
160 std::vector<std::string> GetCacheEntryKeys() const
162 std::vector<std::string> definitions;
163 definitions.reserve(this->Cache.size());
164 for (auto const& i : this->Cache) {
165 definitions.push_back(i.first);
170 /** Get the version of CMake that wrote the cache. */
171 unsigned int GetCacheMajorVersion() const { return this->CacheMajorVersion; }
172 unsigned int GetCacheMinorVersion() const { return this->CacheMinorVersion; }
174 //! Add an entry into the cache
175 void AddCacheEntry(const std::string& key, const char* value,
176 const char* helpString, cmStateEnums::CacheEntryType type)
178 this->AddCacheEntry(key,
179 value ? cmValue(std::string(value)) : cmValue(nullptr),
182 void AddCacheEntry(const std::string& key, const std::string& value,
183 const char* helpString, cmStateEnums::CacheEntryType type)
185 this->AddCacheEntry(key, cmValue(value), helpString, type);
187 void AddCacheEntry(const std::string& key, cmValue value,
188 const char* helpString,
189 cmStateEnums::CacheEntryType type);
191 //! Remove an entry from the cache
192 void RemoveCacheEntry(const std::string& key);
195 //! Get a cache entry object for a key
196 CacheEntry* GetCacheEntry(const std::string& key);
197 const CacheEntry* GetCacheEntry(const std::string& key) const;
199 //! Clean out the CMakeFiles directory if no CMakeCache.txt
200 void CleanCMakeFiles(const std::string& path);
202 static void OutputHelpString(std::ostream& fout,
203 const std::string& helpString);
204 static void OutputWarningComment(std::ostream& fout,
205 std::string const& message,
207 static void OutputNewlineTruncationWarning(std::ostream& fout,
208 std::string const& key,
209 std::string const& value,
210 cmMessenger* messenger);
211 static void OutputKey(std::ostream& fout, std::string const& key);
212 static void OutputValue(std::ostream& fout, std::string const& value);
213 static void OutputValueNoNewlines(std::ostream& fout,
214 std::string const& value);
216 static const char* PersistentProperties[];
217 bool ReadPropertyEntry(const std::string& key, const CacheEntry& e);
218 void WritePropertyEntries(std::ostream& os, const std::string& entryKey,
219 const CacheEntry& e, cmMessenger* messenger) const;
221 std::map<std::string, CacheEntry> Cache;
222 bool CacheLoaded = false;
224 // Cache version info
225 unsigned int CacheMajorVersion = 0;
226 unsigned int CacheMinorVersion = 0;