cmState.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. #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(const cmState&) = delete;
  57. cmState& operator=(const cmState&) = delete;
  58. static const std::string& 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. const std::string&);
  80. static bool StringToCacheEntryType(const std::string&,
  81. cmStateEnums::CacheEntryType& type);
  82. static const std::string& CacheEntryTypeToString(
  83. cmStateEnums::CacheEntryType);
  84. static bool IsCacheEntryType(std::string const& key);
  85. bool LoadCache(const std::string& path, bool internal,
  86. std::set<std::string>& excludes,
  87. std::set<std::string>& includes);
  88. bool SaveCache(const std::string& path, cmMessenger* messenger);
  89. bool DeleteCache(const std::string& 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. const std::string& property,
  110. const std::string& 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(const std::string& entry, std::string& var,
  116. std::string& value,
  117. cmStateEnums::CacheEntryType& type);
  118. cmStateSnapshot Reset();
  119. // Define a property
  120. void DefineProperty(const std::string& name, cmProperty::ScopeType scope,
  121. const std::string& ShortDescription,
  122. const std::string& FullDescription, bool chain = false,
  123. const std::string& initializeFromVariable = "");
  124. // get property definition
  125. cmPropertyDefinition const* GetPropertyDefinition(
  126. const std::string& name, cmProperty::ScopeType scope) const;
  127. const cmPropertyDefinitionMap& GetPropertyDefinitions() const
  128. {
  129. return this->PropertyDefinitions;
  130. }
  131. bool IsPropertyChained(const std::string& 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, const char* message,
  154. const char* additionalWarning = nullptr);
  155. void AddUnexpectedCommand(std::string const& name, const char* error);
  156. void AddUnexpectedFlowControlCommand(std::string const& name,
  157. const char* error);
  158. bool AddScriptedCommand(std::string const& name, BT<Command> command,
  159. cmMakefile& mf);
  160. void RemoveBuiltinCommand(std::string const& name);
  161. void RemoveUserDefinedCommands();
  162. std::vector<std::string> GetCommandNames() const;
  163. void SetGlobalProperty(const std::string& prop, const std::string& value);
  164. void SetGlobalProperty(const std::string& prop, cmValue value);
  165. void AppendGlobalProperty(const std::string& prop, const std::string& value,
  166. bool asString = false);
  167. cmValue GetGlobalProperty(const std::string& prop);
  168. bool GetGlobalPropertyAsBool(const std::string& prop);
  169. std::string const& GetSourceDirectory() const;
  170. void SetSourceDirectory(std::string const& sourceDirectory);
  171. std::string const& GetBinaryDirectory() const;
  172. void SetBinaryDirectory(std::string const& binaryDirectory);
  173. void SetWindowsShell(bool windowsShell);
  174. bool UseWindowsShell() const;
  175. void SetWindowsVSIDE(bool windowsVSIDE);
  176. bool UseWindowsVSIDE() const;
  177. void SetGhsMultiIDE(bool ghsMultiIDE);
  178. bool UseGhsMultiIDE() const;
  179. void SetBorlandMake(bool borlandMake);
  180. bool UseBorlandMake() const;
  181. void SetWatcomWMake(bool watcomWMake);
  182. bool UseWatcomWMake() const;
  183. void SetMinGWMake(bool minGWMake);
  184. bool UseMinGWMake() const;
  185. void SetNMake(bool nMake);
  186. bool UseNMake() const;
  187. void SetMSYSShell(bool mSYSShell);
  188. bool UseMSYSShell() const;
  189. void SetNinja(bool ninja);
  190. bool UseNinja() const;
  191. void SetNinjaMulti(bool ninjaMulti);
  192. bool UseNinjaMulti() const;
  193. unsigned int GetCacheMajorVersion() const;
  194. unsigned int GetCacheMinorVersion() const;
  195. Mode GetMode() const;
  196. std::string GetModeString() const;
  197. static std::string ModeToString(Mode mode);
  198. ProjectKind GetProjectKind() const;
  199. void ClearDependencyProvider() { this->DependencyProvider.reset(); }
  200. void SetDependencyProvider(cmDependencyProvider provider)
  201. {
  202. this->DependencyProvider = std::move(provider);
  203. }
  204. cm::optional<cmDependencyProvider> const& GetDependencyProvider() const
  205. {
  206. return this->DependencyProvider;
  207. }
  208. Command GetDependencyProviderCommand(
  209. cmDependencyProvider::Method method) const;
  210. void SetInTopLevelIncludes(bool inTopLevelIncludes)
  211. {
  212. this->ProcessingTopLevelIncludes = inTopLevelIncludes;
  213. }
  214. bool InTopLevelIncludes() const { return this->ProcessingTopLevelIncludes; }
  215. private:
  216. friend class cmake;
  217. void AddCacheEntry(const std::string& key, cmValue value,
  218. const std::string& helpString,
  219. cmStateEnums::CacheEntryType type);
  220. bool DoWriteGlobVerifyTarget() const;
  221. std::string const& GetGlobVerifyScript() const;
  222. std::string const& GetGlobVerifyStamp() const;
  223. bool SaveVerificationScript(const std::string& path, cmMessenger* messenger);
  224. void AddGlobCacheEntry(const cmGlobCacheEntry& entry,
  225. const std::string& variable,
  226. cmListFileBacktrace const& bt,
  227. cmMessenger* messenger);
  228. std::vector<cmGlobCacheEntry> GetGlobCacheEntries() const;
  229. cmPropertyDefinitionMap PropertyDefinitions;
  230. std::vector<std::string> EnabledLanguages;
  231. std::unordered_map<std::string, Command> BuiltinCommands;
  232. std::unordered_map<std::string, Command> ScriptedCommands;
  233. std::unordered_set<std::string> FlowControlCommands;
  234. cmPropertyMap GlobalProperties;
  235. std::unique_ptr<cmCacheManager> CacheManager;
  236. std::unique_ptr<cmGlobVerificationManager> GlobVerificationManager;
  237. cmLinkedTree<cmStateDetail::BuildsystemDirectoryStateType>
  238. BuildsystemDirectory;
  239. cmLinkedTree<std::string> ExecutionListFiles;
  240. cmLinkedTree<cmStateDetail::PolicyStackEntry> PolicyStack;
  241. cmLinkedTree<cmStateDetail::SnapshotDataType> SnapshotData;
  242. cmLinkedTree<cmDefinitions> VarTree;
  243. std::string SourceDirectory;
  244. std::string BinaryDirectory;
  245. bool IsGeneratorMultiConfig = false;
  246. bool WindowsShell = false;
  247. bool WindowsVSIDE = false;
  248. bool GhsMultiIDE = false;
  249. bool BorlandMake = false;
  250. bool WatcomWMake = false;
  251. bool MinGWMake = false;
  252. bool NMake = false;
  253. bool MSYSShell = false;
  254. bool Ninja = false;
  255. bool NinjaMulti = false;
  256. Mode StateMode = Unknown;
  257. ProjectKind StateProjectKind = ProjectKind::Normal;
  258. cm::optional<cmDependencyProvider> DependencyProvider;
  259. bool ProcessingTopLevelIncludes = false;
  260. };