cmCacheManager.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 <iosfwd>
  6. #include <map>
  7. #include <set>
  8. #include <string>
  9. #include <utility>
  10. #include <vector>
  11. #include "cmPropertyMap.h"
  12. #include "cmStateTypes.h"
  13. #include "cmValue.h"
  14. class cmMessenger;
  15. /** \class cmCacheManager
  16. * \brief Control class for cmake's cache
  17. *
  18. * Load and Save CMake cache files.
  19. *
  20. */
  21. class cmCacheManager
  22. {
  23. class CacheEntry
  24. {
  25. friend class cmCacheManager;
  26. public:
  27. const std::string& GetValue() const { return this->Value; }
  28. void SetValue(cmValue);
  29. cmStateEnums::CacheEntryType GetType() const { return this->Type; }
  30. void SetType(cmStateEnums::CacheEntryType ty) { this->Type = ty; }
  31. std::vector<std::string> GetPropertyList() const;
  32. cmValue GetProperty(const std::string& property) const;
  33. bool GetPropertyAsBool(const std::string& property) const;
  34. void SetProperty(const std::string& property, const std::string& value);
  35. void SetProperty(const std::string& property, bool value);
  36. void SetProperty(const std::string& property, std::nullptr_t);
  37. void AppendProperty(const std::string& property, const std::string& value,
  38. bool asString = false);
  39. private:
  40. std::string Value;
  41. cmStateEnums::CacheEntryType Type = cmStateEnums::UNINITIALIZED;
  42. cmPropertyMap Properties;
  43. bool Initialized = false;
  44. };
  45. public:
  46. //! Load a cache for given makefile. Loads from path/CMakeCache.txt.
  47. bool LoadCache(const std::string& path, bool internal,
  48. std::set<std::string>& excludes,
  49. std::set<std::string>& includes);
  50. //! Save cache for given makefile. Saves to output path/CMakeCache.txt
  51. bool SaveCache(const std::string& path, cmMessenger* messenger);
  52. //! Delete the cache given
  53. bool DeleteCache(const std::string& path);
  54. //! Print the cache to a stream
  55. void PrintCache(std::ostream&) const;
  56. //! Get whether or not cache is loaded
  57. bool IsCacheLoaded() const { return this->CacheLoaded; }
  58. //! Get a value from the cache given a key
  59. cmValue GetInitializedCacheValue(const std::string& key) const;
  60. cmValue GetCacheEntryValue(const std::string& key) const
  61. {
  62. if (const auto* entry = this->GetCacheEntry(key)) {
  63. return cmValue(entry->GetValue());
  64. }
  65. return nullptr;
  66. }
  67. void SetCacheEntryValue(std::string const& key, std::string const& value)
  68. {
  69. if (auto* entry = this->GetCacheEntry(key)) {
  70. entry->SetValue(cmValue(value));
  71. }
  72. }
  73. cmStateEnums::CacheEntryType GetCacheEntryType(std::string const& key) const
  74. {
  75. if (const auto* entry = this->GetCacheEntry(key)) {
  76. return entry->GetType();
  77. }
  78. return cmStateEnums::UNINITIALIZED;
  79. }
  80. std::vector<std::string> GetCacheEntryPropertyList(
  81. std::string const& key) const
  82. {
  83. if (const auto* entry = this->GetCacheEntry(key)) {
  84. return entry->GetPropertyList();
  85. }
  86. return {};
  87. }
  88. cmValue GetCacheEntryProperty(std::string const& key,
  89. std::string const& propName) const
  90. {
  91. if (const auto* entry = this->GetCacheEntry(key)) {
  92. return entry->GetProperty(propName);
  93. }
  94. return nullptr;
  95. }
  96. bool GetCacheEntryPropertyAsBool(std::string const& key,
  97. std::string const& propName) const
  98. {
  99. if (const auto* entry = this->GetCacheEntry(key)) {
  100. return entry->GetPropertyAsBool(propName);
  101. }
  102. return false;
  103. }
  104. void SetCacheEntryProperty(std::string const& key,
  105. std::string const& propName,
  106. std::string const& value)
  107. {
  108. if (auto* entry = this->GetCacheEntry(key)) {
  109. entry->SetProperty(propName, value);
  110. }
  111. }
  112. void SetCacheEntryBoolProperty(std::string const& key,
  113. std::string const& propName, bool value)
  114. {
  115. if (auto* entry = this->GetCacheEntry(key)) {
  116. entry->SetProperty(propName, value);
  117. }
  118. }
  119. void RemoveCacheEntryProperty(std::string const& key,
  120. std::string const& propName)
  121. {
  122. if (auto* entry = this->GetCacheEntry(key)) {
  123. entry->SetProperty(propName, nullptr);
  124. }
  125. }
  126. void AppendCacheEntryProperty(std::string const& key,
  127. std::string const& propName,
  128. std::string const& value,
  129. bool asString = false)
  130. {
  131. if (auto* entry = this->GetCacheEntry(key)) {
  132. entry->AppendProperty(propName, value, asString);
  133. }
  134. }
  135. std::vector<std::string> GetCacheEntryKeys() const
  136. {
  137. std::vector<std::string> definitions;
  138. definitions.reserve(this->Cache.size());
  139. for (auto const& i : this->Cache) {
  140. definitions.push_back(i.first);
  141. }
  142. return definitions;
  143. }
  144. /** Get the version of CMake that wrote the cache. */
  145. unsigned int GetCacheMajorVersion() const { return this->CacheMajorVersion; }
  146. unsigned int GetCacheMinorVersion() const { return this->CacheMinorVersion; }
  147. //! Add an entry into the cache
  148. void AddCacheEntry(const std::string& key, const std::string& value,
  149. const std::string& helpString,
  150. cmStateEnums::CacheEntryType type)
  151. {
  152. this->AddCacheEntry(key, cmValue{ value }, cmValue{ helpString }, type);
  153. }
  154. void AddCacheEntry(const std::string& key, cmValue value,
  155. const std::string& helpString,
  156. cmStateEnums::CacheEntryType type)
  157. {
  158. this->AddCacheEntry(key, value, cmValue{ helpString }, type);
  159. }
  160. void AddCacheEntry(const std::string& key, cmValue value, cmValue helpString,
  161. cmStateEnums::CacheEntryType type);
  162. //! Remove an entry from the cache
  163. void RemoveCacheEntry(const std::string& key);
  164. private:
  165. //! Get a cache entry object for a key
  166. CacheEntry* GetCacheEntry(const std::string& key);
  167. const CacheEntry* GetCacheEntry(const std::string& key) const;
  168. //! Clean out the CMakeFiles directory if no CMakeCache.txt
  169. void CleanCMakeFiles(const std::string& path);
  170. static void OutputHelpString(std::ostream& fout,
  171. const std::string& helpString);
  172. static void OutputWarningComment(std::ostream& fout,
  173. std::string const& message,
  174. bool wrapSpaces);
  175. static void OutputNewlineTruncationWarning(std::ostream& fout,
  176. std::string const& key,
  177. std::string const& value,
  178. cmMessenger* messenger);
  179. static void OutputKey(std::ostream& fout, std::string const& key);
  180. static void OutputValue(std::ostream& fout, std::string const& value);
  181. static void OutputValueNoNewlines(std::ostream& fout,
  182. std::string const& value);
  183. static const char* PersistentProperties[];
  184. bool ReadPropertyEntry(const std::string& key, const CacheEntry& e);
  185. void WritePropertyEntries(std::ostream& os, const std::string& entryKey,
  186. const CacheEntry& e, cmMessenger* messenger) const;
  187. std::map<std::string, CacheEntry> Cache;
  188. bool CacheLoaded = false;
  189. // Cache version info
  190. unsigned int CacheMajorVersion = 0;
  191. unsigned int CacheMinorVersion = 0;
  192. };