resolve cyclic dependency with zstd
[platform/upstream/cmake.git] / Source / cmCacheManager.h
1 /* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2    file Copyright.txt or https://cmake.org/licensing for details.  */
3 #pragma once
4
5 #include "cmConfigure.h" // IWYU pragma: keep
6
7 #include <iosfwd>
8 #include <map>
9 #include <set>
10 #include <string>
11 #include <utility>
12 #include <vector>
13
14 #include "cmPropertyMap.h"
15 #include "cmStateTypes.h"
16 #include "cmValue.h"
17
18 class cmMessenger;
19
20 /** \class cmCacheManager
21  * \brief Control class for cmake's cache
22  *
23  * Load and Save CMake cache files.
24  *
25  */
26 class cmCacheManager
27 {
28   class CacheEntry
29   {
30     friend class cmCacheManager;
31
32   public:
33     const std::string& GetValue() const { return this->Value; }
34     void SetValue(cmValue);
35
36     cmStateEnums::CacheEntryType GetType() const { return this->Type; }
37     void SetType(cmStateEnums::CacheEntryType ty) { this->Type = ty; }
38
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);
46
47   private:
48     std::string Value;
49     cmStateEnums::CacheEntryType Type = cmStateEnums::UNINITIALIZED;
50     cmPropertyMap Properties;
51     bool Initialized = false;
52   };
53
54 public:
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);
59
60   //! Save cache for given makefile.  Saves to output path/CMakeCache.txt
61   bool SaveCache(const std::string& path, cmMessenger* messenger);
62
63   //! Delete the cache given
64   bool DeleteCache(const std::string& path);
65
66   //! Print the cache to a stream
67   void PrintCache(std::ostream&) const;
68
69   //! Get whether or not cache is loaded
70   bool IsCacheLoaded() const { return this->CacheLoaded; }
71
72   //! Get a value from the cache given a key
73   cmValue GetInitializedCacheValue(const std::string& key) const;
74
75   cmValue GetCacheEntryValue(const std::string& key) const
76   {
77     if (const auto* entry = this->GetCacheEntry(key)) {
78       return cmValue(entry->GetValue());
79     }
80     return nullptr;
81   }
82
83   void SetCacheEntryValue(std::string const& key, std::string const& value)
84   {
85     if (auto* entry = this->GetCacheEntry(key)) {
86       entry->SetValue(cmValue(value));
87     }
88   }
89
90   cmStateEnums::CacheEntryType GetCacheEntryType(std::string const& key) const
91   {
92     if (const auto* entry = this->GetCacheEntry(key)) {
93       return entry->GetType();
94     }
95     return cmStateEnums::UNINITIALIZED;
96   }
97
98   std::vector<std::string> GetCacheEntryPropertyList(
99     std::string const& key) const
100   {
101     if (const auto* entry = this->GetCacheEntry(key)) {
102       return entry->GetPropertyList();
103     }
104     return {};
105   }
106
107   cmValue GetCacheEntryProperty(std::string const& key,
108                                 std::string const& propName) const
109   {
110     if (const auto* entry = this->GetCacheEntry(key)) {
111       return entry->GetProperty(propName);
112     }
113     return nullptr;
114   }
115
116   bool GetCacheEntryPropertyAsBool(std::string const& key,
117                                    std::string const& propName) const
118   {
119     if (const auto* entry = this->GetCacheEntry(key)) {
120       return entry->GetPropertyAsBool(propName);
121     }
122     return false;
123   }
124
125   void SetCacheEntryProperty(std::string const& key,
126                              std::string const& propName,
127                              std::string const& value)
128   {
129     if (auto* entry = this->GetCacheEntry(key)) {
130       entry->SetProperty(propName, value.c_str());
131     }
132   }
133
134   void SetCacheEntryBoolProperty(std::string const& key,
135                                  std::string const& propName, bool value)
136   {
137     if (auto* entry = this->GetCacheEntry(key)) {
138       entry->SetProperty(propName, value);
139     }
140   }
141
142   void RemoveCacheEntryProperty(std::string const& key,
143                                 std::string const& propName)
144   {
145     if (auto* entry = this->GetCacheEntry(key)) {
146       entry->SetProperty(propName, nullptr);
147     }
148   }
149
150   void AppendCacheEntryProperty(std::string const& key,
151                                 std::string const& propName,
152                                 std::string const& value,
153                                 bool asString = false)
154   {
155     if (auto* entry = this->GetCacheEntry(key)) {
156       entry->AppendProperty(propName, value, asString);
157     }
158   }
159
160   std::vector<std::string> GetCacheEntryKeys() const
161   {
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);
166     }
167     return definitions;
168   }
169
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; }
173
174   //! Add an entry into the cache
175   void AddCacheEntry(const std::string& key, const char* value,
176                      const char* helpString, cmStateEnums::CacheEntryType type)
177   {
178     this->AddCacheEntry(key,
179                         value ? cmValue(std::string(value)) : cmValue(nullptr),
180                         helpString, type);
181   }
182   void AddCacheEntry(const std::string& key, const std::string& value,
183                      const char* helpString, cmStateEnums::CacheEntryType type)
184   {
185     this->AddCacheEntry(key, cmValue(value), helpString, type);
186   }
187   void AddCacheEntry(const std::string& key, cmValue value,
188                      const char* helpString,
189                      cmStateEnums::CacheEntryType type);
190
191   //! Remove an entry from the cache
192   void RemoveCacheEntry(const std::string& key);
193
194 private:
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;
198
199   //! Clean out the CMakeFiles directory if no CMakeCache.txt
200   void CleanCMakeFiles(const std::string& path);
201
202   static void OutputHelpString(std::ostream& fout,
203                                const std::string& helpString);
204   static void OutputWarningComment(std::ostream& fout,
205                                    std::string const& message,
206                                    bool wrapSpaces);
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);
215
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;
220
221   std::map<std::string, CacheEntry> Cache;
222   bool CacheLoaded = false;
223
224   // Cache version info
225   unsigned int CacheMajorVersion = 0;
226   unsigned int CacheMinorVersion = 0;
227 };