Coverage Report

Created: 2026-02-09 06:05

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/CMake/Source/cmCacheManager.h
Line
Count
Source
1
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2
   file LICENSE.rst 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
0
    std::string const& GetValue() const { return this->Value; }
34
    void SetValue(cmValue);
35
36
0
    cmStateEnums::CacheEntryType GetType() const { return this->Type; }
37
0
    void SetType(cmStateEnums::CacheEntryType ty) { this->Type = ty; }
38
39
    std::vector<std::string> GetPropertyList() const;
40
    cmValue GetProperty(std::string const& property) const;
41
    bool GetPropertyAsBool(std::string const& property) const;
42
    void SetProperty(std::string const& property, std::string const& value);
43
    void SetProperty(std::string const& property, bool value);
44
    void RemoveProperty(std::string const& property);
45
    void AppendProperty(std::string const& property, std::string const& value,
46
                        bool asString = false);
47
48
  private:
49
    std::string Value;
50
    cmStateEnums::CacheEntryType Type = cmStateEnums::UNINITIALIZED;
51
    cmPropertyMap Properties;
52
    bool Initialized = false;
53
  };
54
55
public:
56
  //! Load a cache for given makefile.  Loads from path/CMakeCache.txt.
57
  bool LoadCache(std::string const& path, bool internal,
58
                 std::set<std::string>& excludes,
59
                 std::set<std::string>& includes);
60
61
  //! Save cache for given makefile.  Saves to output path/CMakeCache.txt
62
  bool SaveCache(std::string const& path, cmMessenger* messenger);
63
64
  //! Delete the cache given
65
  bool DeleteCache(std::string const& path);
66
67
  //! Print the cache to a stream
68
  void PrintCache(std::ostream&) const;
69
70
  //! Get whether or not cache is loaded
71
0
  bool IsCacheLoaded() const { return this->CacheLoaded; }
72
73
  //! Get a value from the cache given a key
74
  cmValue GetInitializedCacheValue(std::string const& key) const;
75
76
  cmValue GetCacheEntryValue(std::string const& key) const
77
0
  {
78
0
    if (auto const* entry = this->GetCacheEntry(key)) {
79
0
      return cmValue(entry->GetValue());
80
0
    }
81
0
    return nullptr;
82
0
  }
83
84
  void SetCacheEntryValue(std::string const& key, std::string const& value)
85
0
  {
86
0
    if (auto* entry = this->GetCacheEntry(key)) {
87
0
      entry->SetValue(cmValue(value));
88
0
    }
89
0
  }
90
91
  cmStateEnums::CacheEntryType GetCacheEntryType(std::string const& key) const
92
0
  {
93
0
    if (auto const* entry = this->GetCacheEntry(key)) {
94
0
      return entry->GetType();
95
0
    }
96
0
    return cmStateEnums::UNINITIALIZED;
97
0
  }
98
99
  std::vector<std::string> GetCacheEntryPropertyList(
100
    std::string const& key) const
101
0
  {
102
0
    if (auto const* entry = this->GetCacheEntry(key)) {
103
0
      return entry->GetPropertyList();
104
0
    }
105
0
    return {};
106
0
  }
107
108
  cmValue GetCacheEntryProperty(std::string const& key,
109
                                std::string const& propName) const
110
0
  {
111
0
    if (auto const* entry = this->GetCacheEntry(key)) {
112
0
      return entry->GetProperty(propName);
113
0
    }
114
0
    return nullptr;
115
0
  }
116
117
  bool GetCacheEntryPropertyAsBool(std::string const& key,
118
                                   std::string const& propName) const
119
0
  {
120
0
    if (auto const* entry = this->GetCacheEntry(key)) {
121
0
      return entry->GetPropertyAsBool(propName);
122
0
    }
123
0
    return false;
124
0
  }
125
126
  void SetCacheEntryProperty(std::string const& key,
127
                             std::string const& propName,
128
                             std::string const& value)
129
0
  {
130
0
    if (auto* entry = this->GetCacheEntry(key)) {
131
0
      entry->SetProperty(propName, value);
132
0
    }
133
0
  }
134
135
  void SetCacheEntryBoolProperty(std::string const& key,
136
                                 std::string const& propName, bool value)
137
0
  {
138
0
    if (auto* entry = this->GetCacheEntry(key)) {
139
0
      entry->SetProperty(propName, value);
140
0
    }
141
0
  }
142
143
  void RemoveCacheEntryProperty(std::string const& key,
144
                                std::string const& propName)
145
0
  {
146
0
    if (auto* entry = this->GetCacheEntry(key)) {
147
0
      entry->RemoveProperty(propName);
148
0
    }
149
0
  }
150
151
  void AppendCacheEntryProperty(std::string const& key,
152
                                std::string const& propName,
153
                                std::string const& value,
154
                                bool asString = false)
155
0
  {
156
0
    if (auto* entry = this->GetCacheEntry(key)) {
157
0
      entry->AppendProperty(propName, value, asString);
158
0
    }
159
0
  }
160
161
  std::vector<std::string> GetCacheEntryKeys() const
162
0
  {
163
0
    std::vector<std::string> definitions;
164
0
    definitions.reserve(this->Cache.size());
165
0
    for (auto const& i : this->Cache) {
166
0
      definitions.push_back(i.first);
167
0
    }
168
0
    return definitions;
169
0
  }
170
171
  /** Get the version of CMake that wrote the cache.  */
172
0
  unsigned int GetCacheMajorVersion() const { return this->CacheMajorVersion; }
173
0
  unsigned int GetCacheMinorVersion() const { return this->CacheMinorVersion; }
174
175
  //! Add an entry into the cache
176
  void AddCacheEntry(std::string const& key, std::string const& value,
177
                     std::string const& helpString,
178
                     cmStateEnums::CacheEntryType type)
179
0
  {
180
0
    this->AddCacheEntry(key, cmValue{ value }, cmValue{ helpString }, type);
181
0
  }
182
  void AddCacheEntry(std::string const& key, cmValue value,
183
                     std::string const& helpString,
184
                     cmStateEnums::CacheEntryType type)
185
3
  {
186
3
    this->AddCacheEntry(key, value, cmValue{ helpString }, type);
187
3
  }
188
  void AddCacheEntry(std::string const& key, cmValue value, cmValue helpString,
189
                     cmStateEnums::CacheEntryType type);
190
191
  //! Remove an entry from the cache
192
  void RemoveCacheEntry(std::string const& key);
193
194
private:
195
  //! Get a cache entry object for a key
196
  CacheEntry* GetCacheEntry(std::string const& key);
197
  CacheEntry const* GetCacheEntry(std::string const& key) const;
198
199
  //! Clean out the CMakeFiles directory if no CMakeCache.txt
200
  void CleanCMakeFiles(std::string const& path);
201
202
  static void OutputHelpString(std::ostream& fout,
203
                               std::string const& 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 char const* PersistentProperties[];
217
  bool ReadPropertyEntry(std::string const& key, CacheEntry const& e);
218
  void WritePropertyEntries(std::ostream& os, std::string const& entryKey,
219
                            CacheEntry const& 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
};