Coverage Report

Created: 2026-04-29 07:01

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/CMake/Source/cmState.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 <functional>
8
#include <memory>
9
#include <set>
10
#include <string>
11
#include <unordered_map>
12
#include <unordered_set>
13
#include <utility>
14
#include <vector>
15
16
#include <cm/optional>
17
18
#include "cmDefinitions.h"
19
#include "cmDependencyProvider.h"
20
#include "cmLinkedTree.h"
21
#include "cmPolicies.h"
22
#include "cmProperty.h"
23
#include "cmPropertyDefinition.h"
24
#include "cmPropertyMap.h"
25
#include "cmStatePrivate.h"
26
#include "cmStateTypes.h"
27
#include "cmValue.h"
28
29
class cmCacheManager;
30
class cmGlobVerificationManager;
31
class cmMakefile;
32
class cmStateSnapshot;
33
class cmMessenger;
34
class cmExecutionStatus;
35
class cmListFileBacktrace;
36
struct cmGlobCacheEntry;
37
struct cmListFileArgument;
38
39
template <typename T>
40
class BT;
41
42
class cmState
43
{
44
  friend class cmStateSnapshot;
45
46
public:
47
  enum class Role
48
  {
49
    Internal,
50
    Project,
51
    Script,
52
    FindPackage,
53
    CTest,
54
    CPack,
55
    Help,
56
  };
57
58
  enum class TryCompile
59
  {
60
    No,
61
    Yes,
62
  };
63
64
  cmState(Role role, TryCompile isTryCompile = TryCompile::No);
65
  ~cmState();
66
67
  cmState(cmState const&) = delete;
68
  cmState& operator=(cmState const&) = delete;
69
70
  static std::string const& GetTargetTypeName(
71
    cmStateEnums::TargetType targetType);
72
73
  cmStateSnapshot CreateBaseSnapshot();
74
  cmStateSnapshot CreateBuildsystemDirectorySnapshot(
75
    cmStateSnapshot const& originSnapshot);
76
  cmStateSnapshot CreateDeferCallSnapshot(
77
    cmStateSnapshot const& originSnapshot, std::string const& fileName);
78
  cmStateSnapshot CreateFunctionCallSnapshot(
79
    cmStateSnapshot const& originSnapshot, std::string const& fileName);
80
  cmStateSnapshot CreateMacroCallSnapshot(
81
    cmStateSnapshot const& originSnapshot, std::string const& fileName);
82
  cmStateSnapshot CreateIncludeFileSnapshot(
83
    cmStateSnapshot const& originSnapshot, std::string const& fileName);
84
  cmStateSnapshot CreateVariableScopeSnapshot(
85
    cmStateSnapshot const& originSnapshot);
86
  cmStateSnapshot CreateInlineListFileSnapshot(
87
    cmStateSnapshot const& originSnapshot, std::string const& fileName);
88
  cmStateSnapshot CreatePolicyScopeSnapshot(
89
    cmStateSnapshot const& originSnapshot);
90
  cmStateSnapshot Pop(cmStateSnapshot const& originSnapshot);
91
92
  static cmStateEnums::CacheEntryType StringToCacheEntryType(
93
    std::string const&);
94
  static bool StringToCacheEntryType(std::string const&,
95
                                     cmStateEnums::CacheEntryType& type);
96
  static std::string const& CacheEntryTypeToString(
97
    cmStateEnums::CacheEntryType);
98
  static bool IsCacheEntryType(std::string const& key);
99
100
  bool LoadCache(std::string const& path, bool internal,
101
                 std::set<std::string>& excludes,
102
                 std::set<std::string>& includes);
103
104
  bool SaveCache(std::string const& path, cmMessenger* messenger);
105
106
  bool DeleteCache(std::string const& path);
107
108
  bool IsCacheLoaded() const;
109
110
  std::vector<std::string> GetCacheEntryKeys() const;
111
  cmValue GetCacheEntryValue(std::string const& key) const;
112
  std::string GetSafeCacheEntryValue(std::string const& key) const;
113
  cmValue GetInitializedCacheValue(std::string const& key) const;
114
  cmStateEnums::CacheEntryType GetCacheEntryType(std::string const& key) const;
115
  void SetCacheEntryValue(std::string const& key, std::string const& value);
116
117
  void RemoveCacheEntry(std::string const& key);
118
119
  void SetCacheEntryProperty(std::string const& key,
120
                             std::string const& propertyName,
121
                             std::string const& value);
122
  void SetCacheEntryBoolProperty(std::string const& key,
123
                                 std::string const& propertyName, bool value);
124
  std::vector<std::string> GetCacheEntryPropertyList(std::string const& key);
125
  cmValue GetCacheEntryProperty(std::string const& key,
126
                                std::string const& propertyName);
127
  bool GetCacheEntryPropertyAsBool(std::string const& key,
128
                                   std::string const& propertyName);
129
  void AppendCacheEntryProperty(std::string const& key,
130
                                std::string const& property,
131
                                std::string const& value,
132
                                bool asString = false);
133
  void RemoveCacheEntryProperty(std::string const& key,
134
                                std::string const& propertyName);
135
136
  //! Break up a line like VAR:type="value" into var, type and value
137
  static bool ParseCacheEntry(std::string const& entry, std::string& var,
138
                              std::string& value,
139
                              cmStateEnums::CacheEntryType& type);
140
141
  // Define a property
142
  void DefineProperty(std::string const& name, cmProperty::ScopeType scope,
143
                      std::string const& ShortDescription,
144
                      std::string const& FullDescription, bool chain = false,
145
                      std::string const& initializeFromVariable = "");
146
147
  // get property definition
148
  cmPropertyDefinition const* GetPropertyDefinition(
149
    std::string const& name, cmProperty::ScopeType scope) const;
150
151
  cmPropertyDefinitionMap const& GetPropertyDefinitions() const
152
0
  {
153
0
    return this->PropertyDefinitions;
154
0
  }
155
156
  bool IsPropertyChained(std::string const& name,
157
                         cmProperty::ScopeType scope) const;
158
159
  void SetLanguageEnabled(std::string const& l);
160
  bool GetLanguageEnabled(std::string const& l) const;
161
  std::vector<std::string> GetEnabledLanguages() const;
162
  void SetEnabledLanguages(std::vector<std::string> const& langs);
163
  void ClearEnabledLanguages();
164
165
  bool GetIsGeneratorMultiConfig() const;
166
  void SetIsGeneratorMultiConfig(bool b);
167
168
  using Command = std::function<bool(std::vector<cmListFileArgument> const&,
169
                                     cmExecutionStatus&)>;
170
  using BuiltinCommand = bool (*)(std::vector<std::string> const&,
171
                                  cmExecutionStatus&);
172
173
  // Returns a command from its name, case insensitive, or nullptr
174
  Command GetCommand(std::string const& name) const;
175
  // Returns a command from its name, or nullptr
176
  Command GetCommandByExactName(std::string const& name) const;
177
178
  void AddBuiltinCommand(std::string const& name, Command command);
179
  void AddBuiltinCommand(std::string const& name, BuiltinCommand command);
180
  void AddFlowControlCommand(std::string const& name, Command command);
181
  void AddFlowControlCommand(std::string const& name, BuiltinCommand command);
182
  void AddDisallowedCommand(std::string const& name, BuiltinCommand command,
183
                            cmPolicies::PolicyID policy, char const* message,
184
                            char const* additionalWarning = nullptr);
185
  void AddRemovedCommand(std::string const& name, std::string const& message);
186
  void AddUnexpectedCommand(std::string const& name, char const* error);
187
  void AddUnexpectedFlowControlCommand(std::string const& name,
188
                                       char const* error);
189
  bool AddScriptedCommand(std::string const& name, BT<Command> command,
190
                          cmMakefile& mf);
191
  void RemoveBuiltinCommand(std::string const& name);
192
  void RemoveUserDefinedCommands();
193
  std::vector<std::string> GetCommandNames() const;
194
195
  void SetGlobalProperty(std::string const& prop, std::string const& value);
196
  void SetGlobalProperty(std::string const& prop, cmValue value);
197
  void AppendGlobalProperty(std::string const& prop, std::string const& value,
198
                            bool asString = false);
199
  cmValue GetGlobalProperty(std::string const& prop);
200
  bool GetGlobalPropertyAsBool(std::string const& prop);
201
202
  std::string const& GetSourceDirectory() const;
203
  void SetSourceDirectory(std::string const& sourceDirectory);
204
  std::string const& GetBinaryDirectory() const;
205
  void SetBinaryDirectory(std::string const& binaryDirectory);
206
207
  void SetWindowsShell(bool windowsShell);
208
  bool UseWindowsShell() const;
209
  void SetWindowsVSIDE(bool windowsVSIDE);
210
  bool UseWindowsVSIDE() const;
211
  void SetGhsMultiIDE(bool ghsMultiIDE);
212
  bool UseGhsMultiIDE() const;
213
  void SetBorlandMake(bool borlandMake);
214
  bool UseBorlandMake() const;
215
  void SetWatcomWMake(bool watcomWMake);
216
  bool UseWatcomWMake() const;
217
  void SetMinGWMake(bool minGWMake);
218
  bool UseMinGWMake() const;
219
  void SetNMake(bool nMake);
220
  bool UseNMake() const;
221
  void SetMSYSShell(bool mSYSShell);
222
  bool UseMSYSShell() const;
223
  void SetNinja(bool ninja);
224
  bool UseNinja() const;
225
  void SetNinjaMulti(bool ninjaMulti);
226
  bool UseNinjaMulti() const;
227
  void SetFastbuildMake(bool fastbuildMake);
228
  bool UseFastbuildMake() const;
229
230
  unsigned int GetCacheMajorVersion() const;
231
  unsigned int GetCacheMinorVersion() const;
232
233
  void SetRoleToProjectForCMakeBuildVsReconfigure();
234
  void SetRoleToHelpForListPresets();
235
  Role GetRole() const;
236
  std::string GetRoleString() const;
237
238
  static std::string RoleToString(Role role);
239
240
  TryCompile GetIsTryCompile() const;
241
242
0
  void ClearDependencyProvider() { this->DependencyProvider.reset(); }
243
  void SetDependencyProvider(cmDependencyProvider provider)
244
0
  {
245
0
    this->DependencyProvider = std::move(provider);
246
0
  }
247
  cm::optional<cmDependencyProvider> const& GetDependencyProvider() const
248
0
  {
249
0
    return this->DependencyProvider;
250
0
  }
251
  Command GetDependencyProviderCommand(
252
    cmDependencyProvider::Method method) const;
253
254
  void SetInTopLevelIncludes(bool inTopLevelIncludes)
255
0
  {
256
0
    this->ProcessingTopLevelIncludes = inTopLevelIncludes;
257
0
  }
258
0
  bool InTopLevelIncludes() const { return this->ProcessingTopLevelIncludes; }
259
260
private:
261
  friend class cmake;
262
  cmStateSnapshot Reset(cmStateSnapshot const& diagnosticState);
263
264
  void AddCacheEntry(std::string const& key, cmValue value,
265
                     std::string const& helpString,
266
                     cmStateEnums::CacheEntryType type);
267
268
  bool DoWriteGlobVerifyTarget() const;
269
  std::string const& GetGlobVerifyScript() const;
270
  std::string const& GetGlobVerifyStamp() const;
271
  bool SaveVerificationScript(std::string const& path, cmMessenger* messenger);
272
  void AddGlobCacheEntry(cmGlobCacheEntry const& entry,
273
                         std::string const& variable,
274
                         cmListFileBacktrace const& bt,
275
                         cmMessenger* messenger);
276
  std::vector<cmGlobCacheEntry> GetGlobCacheEntries() const;
277
278
  cmPropertyDefinitionMap PropertyDefinitions;
279
  std::vector<std::string> EnabledLanguages;
280
  std::unordered_map<std::string, Command> BuiltinCommands;
281
  std::unordered_map<std::string, Command> ScriptedCommands;
282
  std::unordered_set<std::string> FlowControlCommands;
283
  cmPropertyMap GlobalProperties;
284
  std::unique_ptr<cmCacheManager> CacheManager;
285
  std::unique_ptr<cmGlobVerificationManager> GlobVerificationManager;
286
287
  cmLinkedTree<cmStateDetail::BuildsystemDirectoryStateType>
288
    BuildsystemDirectory;
289
290
  cmLinkedTree<std::string> ExecutionListFiles;
291
292
  cmLinkedTree<cmStateDetail::DiagnosticStackEntry> DiagnosticStack;
293
  cmLinkedTree<cmStateDetail::PolicyStackEntry> PolicyStack;
294
  cmLinkedTree<cmStateDetail::SnapshotDataType> SnapshotData;
295
  cmLinkedTree<cmDefinitions> VarTree;
296
297
  std::string SourceDirectory;
298
  std::string BinaryDirectory;
299
  bool IsGeneratorMultiConfig = false;
300
  bool WindowsShell = false;
301
  bool WindowsVSIDE = false;
302
  bool GhsMultiIDE = false;
303
  bool BorlandMake = false;
304
  bool WatcomWMake = false;
305
  bool MinGWMake = false;
306
  bool NMake = false;
307
  bool MSYSShell = false;
308
  bool Ninja = false;
309
  bool NinjaMulti = false;
310
  bool FastbuildMake = false;
311
  Role StateRole = Role::Internal;
312
  TryCompile IsTryCompile = TryCompile::No;
313
  cm::optional<cmDependencyProvider> DependencyProvider;
314
  bool ProcessingTopLevelIncludes = false;
315
};