cmState.h 11 KB

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