cmFindPackageCommand.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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 <cstddef>
  6. #include <functional>
  7. #include <map>
  8. #include <memory>
  9. #include <set>
  10. #include <string>
  11. #include <utility>
  12. #include <vector>
  13. #include <cm/string_view>
  14. #include <cm3p/kwiml/int.h>
  15. #include "cmFindCommon.h"
  16. #include "cmPackageInfoReader.h"
  17. #include "cmPolicies.h"
  18. // IWYU insists we should forward-declare instead of including <functional>,
  19. // but we cannot forward-declare reliably because some C++ standard libraries
  20. // put the template in an inline namespace.
  21. #ifdef CMAKE_IWYU_FORWARD_STD_HASH
  22. /* clang-format off */
  23. namespace std {
  24. template <class T> struct hash;
  25. }
  26. /* clang-format on */
  27. #endif
  28. class cmExecutionStatus;
  29. class cmSearchPath;
  30. /** \class cmFindPackageCommand
  31. * \brief Load settings from an external project.
  32. *
  33. * cmFindPackageCommand
  34. */
  35. class cmFindPackageCommand : public cmFindCommon
  36. {
  37. public:
  38. /*! A sorting order strategy to be applied to recovered package folders (see
  39. * FIND_PACKAGE_SORT_ORDER)*/
  40. enum /*class*/ SortOrderType
  41. {
  42. None,
  43. Name_order,
  44. Natural
  45. };
  46. /*! A sorting direction to be applied to recovered package folders (see
  47. * FIND_PACKAGE_SORT_DIRECTION)*/
  48. enum /*class*/ SortDirectionType
  49. {
  50. Asc,
  51. Dec
  52. };
  53. enum class PackageDescriptionType
  54. {
  55. Any,
  56. CMake,
  57. Cps,
  58. };
  59. /*! sorts a given list of string based on the input sort parameters */
  60. static void Sort(std::vector<std::string>::iterator begin,
  61. std::vector<std::string>::iterator end, SortOrderType order,
  62. SortDirectionType dir);
  63. cmFindPackageCommand(cmExecutionStatus& status);
  64. bool InitialPass(std::vector<std::string> const& args);
  65. private:
  66. class PathLabel : public cmFindCommon::PathLabel
  67. {
  68. protected:
  69. PathLabel();
  70. public:
  71. PathLabel(const std::string& label)
  72. : cmFindCommon::PathLabel(label)
  73. {
  74. }
  75. static PathLabel PackageRedirect;
  76. static PathLabel UserRegistry;
  77. static PathLabel Builds;
  78. static PathLabel SystemRegistry;
  79. };
  80. // Try to find a package, assuming most state has already been set up. This
  81. // is used for recursive dependency solving, particularly when importing
  82. // packages via CPS. Bypasses providers if argsForProvider is empty.
  83. bool FindPackage(std::vector<std::string> const& argsForProvider);
  84. bool FindPackageUsingModuleMode();
  85. bool FindPackageUsingConfigMode();
  86. // Add additional search path labels and groups not present in the
  87. // parent class
  88. void AppendSearchPathGroups();
  89. void AppendSuccessInformation();
  90. void AppendToFoundProperty(bool found);
  91. void SetVersionVariables(
  92. const std::function<void(const std::string&, cm::string_view)>&
  93. addDefinition,
  94. const std::string& prefix, const std::string& version, unsigned int count,
  95. unsigned int major, unsigned int minor, unsigned int patch,
  96. unsigned int tweak);
  97. void SetModuleVariables();
  98. bool FindModule(bool& found);
  99. void AddFindDefinition(const std::string& var, cm::string_view value);
  100. void RestoreFindDefinitions();
  101. class SetRestoreFindDefinitions;
  102. enum /*class*/ HandlePackageModeType
  103. {
  104. Module,
  105. Config
  106. };
  107. bool HandlePackageMode(HandlePackageModeType type);
  108. bool FindConfig();
  109. bool FindPrefixedConfig();
  110. bool FindFrameworkConfig();
  111. bool FindAppBundleConfig();
  112. bool FindEnvironmentConfig();
  113. enum PolicyScopeRule
  114. {
  115. NoPolicyScope,
  116. DoPolicyScope
  117. };
  118. bool ReadListFile(const std::string& f, PolicyScopeRule psr);
  119. void StoreVersionFound();
  120. void SetConfigDirCacheVariable(const std::string& value);
  121. void PushFindPackageRootPathStack();
  122. void PopFindPackageRootPathStack();
  123. class PushPopRootPathStack;
  124. void ComputePrefixes();
  125. void FillPrefixesPackageRedirect();
  126. void FillPrefixesPackageRoot();
  127. void FillPrefixesCMakeEnvironment();
  128. void FillPrefixesCMakeVariable();
  129. void FillPrefixesSystemEnvironment();
  130. void FillPrefixesUserRegistry();
  131. void FillPrefixesSystemRegistry();
  132. void FillPrefixesCMakeSystemVariable();
  133. void FillPrefixesUserGuess();
  134. void FillPrefixesUserHints();
  135. void LoadPackageRegistryDir(std::string const& dir, cmSearchPath& outPaths);
  136. void LoadPackageRegistryWinUser();
  137. void LoadPackageRegistryWinSystem();
  138. void LoadPackageRegistryWin(bool user, unsigned int view,
  139. cmSearchPath& outPaths);
  140. bool CheckPackageRegistryEntry(const std::string& fname,
  141. cmSearchPath& outPaths);
  142. bool SearchDirectory(std::string const& dir, PackageDescriptionType type);
  143. bool CheckDirectory(std::string const& dir, PackageDescriptionType type);
  144. bool FindConfigFile(std::string const& dir, PackageDescriptionType type,
  145. std::string& file);
  146. bool CheckVersion(std::string const& config_file);
  147. bool CheckVersionFile(std::string const& version_file,
  148. std::string& result_version);
  149. bool SearchPrefix(std::string const& prefix);
  150. bool SearchFrameworkPrefix(std::string const& prefix_in);
  151. bool SearchAppBundlePrefix(std::string const& prefix_in);
  152. bool SearchEnvironmentPrefix(std::string const& prefix_in);
  153. struct OriginalDef
  154. {
  155. bool exists;
  156. std::string value;
  157. };
  158. std::map<std::string, OriginalDef> OriginalDefs;
  159. std::map<std::string, cmPolicies::PolicyID> DeprecatedFindModules;
  160. static const cm::string_view VERSION_ENDPOINT_INCLUDED;
  161. static const cm::string_view VERSION_ENDPOINT_EXCLUDED;
  162. std::string Name;
  163. std::string Variable;
  164. std::string VersionComplete;
  165. std::string VersionRange;
  166. cm::string_view VersionRangeMin;
  167. cm::string_view VersionRangeMax;
  168. std::string Version;
  169. unsigned int VersionMajor = 0;
  170. unsigned int VersionMinor = 0;
  171. unsigned int VersionPatch = 0;
  172. unsigned int VersionTweak = 0;
  173. unsigned int VersionCount = 0;
  174. std::string VersionMax;
  175. unsigned int VersionMaxMajor = 0;
  176. unsigned int VersionMaxMinor = 0;
  177. unsigned int VersionMaxPatch = 0;
  178. unsigned int VersionMaxTweak = 0;
  179. unsigned int VersionMaxCount = 0;
  180. bool VersionExact = false;
  181. std::string FileFound;
  182. std::string VersionFound;
  183. unsigned int VersionFoundMajor = 0;
  184. unsigned int VersionFoundMinor = 0;
  185. unsigned int VersionFoundPatch = 0;
  186. unsigned int VersionFoundTweak = 0;
  187. unsigned int VersionFoundCount = 0;
  188. KWIML_INT_uint64_t RequiredCMakeVersion = 0;
  189. bool Quiet = false;
  190. bool Required = false;
  191. bool UseCpsFiles = false;
  192. bool UseConfigFiles = true;
  193. bool UseFindModules = true;
  194. bool NoUserRegistry = false;
  195. bool NoSystemRegistry = false;
  196. bool UseLib32Paths = false;
  197. bool UseLib64Paths = false;
  198. bool UseLibx32Paths = false;
  199. bool UseRealPath = false;
  200. bool PolicyScope = true;
  201. bool GlobalScope = false;
  202. bool RegistryViewDefined = false;
  203. std::string LibraryArchitecture;
  204. std::vector<std::string> Names;
  205. std::set<std::string> IgnoredPaths;
  206. std::set<std::string> IgnoredPrefixPaths;
  207. std::string Components;
  208. std::set<std::string> RequiredComponents;
  209. std::set<std::string> OptionalComponents;
  210. std::string DebugBuffer;
  211. struct ConfigName
  212. {
  213. ConfigName(std::string const& name, PackageDescriptionType type)
  214. : Name{ name }
  215. , Type{ type }
  216. {
  217. }
  218. ConfigName(std::string&& name, PackageDescriptionType type)
  219. : Name{ std::move(name) }
  220. , Type{ type }
  221. {
  222. }
  223. ConfigName(ConfigName const&) = default;
  224. ConfigName(ConfigName&&) = default;
  225. std::string Name;
  226. PackageDescriptionType Type;
  227. };
  228. std::vector<ConfigName> Configs;
  229. class FlushDebugBufferOnExit;
  230. /*! the selected sortOrder (None by default)*/
  231. SortOrderType SortOrder = None;
  232. /*! the selected sortDirection (Asc by default)*/
  233. SortDirectionType SortDirection = Asc;
  234. struct ConfigFileInfo
  235. {
  236. std::string filename;
  237. std::string version;
  238. bool operator<(ConfigFileInfo const& rhs) const
  239. {
  240. return this->filename < rhs.filename;
  241. }
  242. bool operator==(ConfigFileInfo const& rhs) const
  243. {
  244. return this->filename == rhs.filename;
  245. }
  246. bool operator!=(ConfigFileInfo const& rhs) const
  247. {
  248. return !(*this == rhs);
  249. }
  250. };
  251. std::vector<ConfigFileInfo> ConsideredConfigs;
  252. std::unique_ptr<cmPackageInfoReader> CpsReader;
  253. friend struct std::hash<ConfigFileInfo>;
  254. };
  255. namespace std {
  256. template <>
  257. struct hash<cmFindPackageCommand::ConfigFileInfo>
  258. {
  259. using argument_type = cmFindPackageCommand::ConfigFileInfo;
  260. using result_type = size_t;
  261. result_type operator()(argument_type const& s) const noexcept
  262. {
  263. result_type const h(std::hash<std::string>{}(s.filename));
  264. return h;
  265. }
  266. };
  267. }
  268. bool cmFindPackage(std::vector<std::string> const& args,
  269. cmExecutionStatus& status);