cmFindPackageCommand.h 8.5 KB

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