cmFindPackageCommand.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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 <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(std::string const& 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. void InheritOptions(cmFindPackageCommand* other);
  81. // Try to find a package, assuming most state has already been set up. This
  82. // is used for recursive dependency solving, particularly when importing
  83. // packages via CPS. Bypasses providers if argsForProvider is empty.
  84. bool FindPackage(std::vector<std::string> const& argsForProvider = {});
  85. bool FindPackageUsingModuleMode();
  86. bool FindPackageUsingConfigMode();
  87. // Add additional search path labels and groups not present in the
  88. // parent class
  89. void AppendSearchPathGroups();
  90. void AppendSuccessInformation();
  91. void AppendToFoundProperty(bool found);
  92. void SetVersionVariables(
  93. std::function<void(std::string const&, cm::string_view)> const&
  94. addDefinition,
  95. std::string const& prefix, std::string const& version, unsigned int count,
  96. unsigned int major, unsigned int minor, unsigned int patch,
  97. unsigned int tweak);
  98. void SetModuleVariables();
  99. bool FindModule(bool& found);
  100. void AddFindDefinition(std::string const& var, cm::string_view value);
  101. void RestoreFindDefinitions();
  102. class SetRestoreFindDefinitions;
  103. enum /*class*/ HandlePackageModeType
  104. {
  105. Module,
  106. Config
  107. };
  108. bool HandlePackageMode(HandlePackageModeType type);
  109. bool FindConfig();
  110. bool FindPrefixedConfig();
  111. bool FindFrameworkConfig();
  112. bool FindAppBundleConfig();
  113. bool FindEnvironmentConfig();
  114. enum PolicyScopeRule
  115. {
  116. NoPolicyScope,
  117. DoPolicyScope
  118. };
  119. bool ReadListFile(std::string const& f, PolicyScopeRule psr);
  120. bool ReadPackage();
  121. struct Appendix
  122. {
  123. std::unique_ptr<cmPackageInfoReader> Reader;
  124. std::vector<std::string> Components;
  125. operator cmPackageInfoReader&() const { return *this->Reader; }
  126. };
  127. using AppendixMap = std::map<std::string, Appendix>;
  128. AppendixMap FindAppendices(std::string const& base,
  129. cmPackageInfoReader const& baseReader) const;
  130. enum RequiredStatus
  131. {
  132. Optional,
  133. OptionalExplicit,
  134. RequiredExplicit,
  135. RequiredFromPackageVar,
  136. RequiredFromFindVar
  137. };
  138. bool FindPackageDependencies(std::string const& fileName,
  139. cmPackageInfoReader const& reader,
  140. RequiredStatus required);
  141. bool ImportPackageTargets(std::string const& fileName,
  142. cmPackageInfoReader& reader);
  143. void StoreVersionFound();
  144. void SetConfigDirCacheVariable(std::string const& value);
  145. void PushFindPackageRootPathStack();
  146. void PopFindPackageRootPathStack();
  147. class PushPopRootPathStack;
  148. void ComputePrefixes();
  149. void FillPrefixesPackageRedirect();
  150. void FillPrefixesPackageRoot();
  151. void FillPrefixesCMakeEnvironment();
  152. void FillPrefixesCMakeVariable();
  153. void FillPrefixesSystemEnvironment();
  154. void FillPrefixesUserRegistry();
  155. void FillPrefixesSystemRegistry();
  156. void FillPrefixesCMakeSystemVariable();
  157. void FillPrefixesUserGuess();
  158. void FillPrefixesUserHints();
  159. void LoadPackageRegistryDir(std::string const& dir, cmSearchPath& outPaths);
  160. void LoadPackageRegistryWinUser();
  161. void LoadPackageRegistryWinSystem();
  162. void LoadPackageRegistryWin(bool user, unsigned int view,
  163. cmSearchPath& outPaths);
  164. bool CheckPackageRegistryEntry(std::string const& fname,
  165. cmSearchPath& outPaths);
  166. bool SearchDirectory(std::string const& dir, PackageDescriptionType type);
  167. bool CheckDirectory(std::string const& dir, PackageDescriptionType type);
  168. bool FindConfigFile(std::string const& dir, PackageDescriptionType type,
  169. std::string& file);
  170. bool CheckVersion(std::string const& config_file);
  171. bool CheckVersionFile(std::string const& version_file,
  172. std::string& result_version);
  173. bool SearchPrefix(std::string const& prefix);
  174. bool SearchFrameworkPrefix(std::string const& prefix);
  175. bool SearchAppBundlePrefix(std::string const& prefix);
  176. bool SearchEnvironmentPrefix(std::string const& prefix);
  177. bool IsRequired() const;
  178. struct OriginalDef
  179. {
  180. bool exists;
  181. std::string value;
  182. };
  183. std::map<std::string, OriginalDef> OriginalDefs;
  184. std::map<std::string, cmPolicies::PolicyID> DeprecatedFindModules;
  185. static cm::string_view const VERSION_ENDPOINT_INCLUDED;
  186. static cm::string_view const VERSION_ENDPOINT_EXCLUDED;
  187. std::string Name;
  188. std::string Variable;
  189. std::string VersionComplete;
  190. std::string VersionRange;
  191. cm::string_view VersionRangeMin;
  192. cm::string_view VersionRangeMax;
  193. std::string Version;
  194. unsigned int VersionMajor = 0;
  195. unsigned int VersionMinor = 0;
  196. unsigned int VersionPatch = 0;
  197. unsigned int VersionTweak = 0;
  198. unsigned int VersionCount = 0;
  199. std::string VersionMax;
  200. unsigned int VersionMaxMajor = 0;
  201. unsigned int VersionMaxMinor = 0;
  202. unsigned int VersionMaxPatch = 0;
  203. unsigned int VersionMaxTweak = 0;
  204. unsigned int VersionMaxCount = 0;
  205. bool VersionExact = false;
  206. std::string FileFound;
  207. std::string VersionFound;
  208. unsigned int VersionFoundMajor = 0;
  209. unsigned int VersionFoundMinor = 0;
  210. unsigned int VersionFoundPatch = 0;
  211. unsigned int VersionFoundTweak = 0;
  212. unsigned int VersionFoundCount = 0;
  213. KWIML_INT_uint64_t RequiredCMakeVersion = 0;
  214. bool Quiet = false;
  215. RequiredStatus Required = RequiredStatus::Optional;
  216. bool UseCpsFiles = false;
  217. bool UseConfigFiles = true;
  218. bool UseFindModules = true;
  219. bool NoUserRegistry = false;
  220. bool NoSystemRegistry = false;
  221. bool UseLib32Paths = false;
  222. bool UseLib64Paths = false;
  223. bool UseLibx32Paths = false;
  224. bool UseRealPath = false;
  225. bool PolicyScope = true;
  226. bool GlobalScope = false;
  227. bool RegistryViewDefined = false;
  228. std::string LibraryArchitecture;
  229. std::vector<std::string> Names;
  230. std::set<std::string> IgnoredPaths;
  231. std::set<std::string> IgnoredPrefixPaths;
  232. std::string Components;
  233. std::set<std::string> RequiredComponents;
  234. std::set<std::string> OptionalComponents;
  235. std::set<std::string> RequiredTargets;
  236. std::string DebugBuffer;
  237. struct ConfigName
  238. {
  239. ConfigName(std::string const& name, PackageDescriptionType type)
  240. : Name{ name }
  241. , Type{ type }
  242. {
  243. }
  244. ConfigName(std::string&& name, PackageDescriptionType type)
  245. : Name{ std::move(name) }
  246. , Type{ type }
  247. {
  248. }
  249. ConfigName(ConfigName const&) = default;
  250. ConfigName(ConfigName&&) = default;
  251. std::string Name;
  252. PackageDescriptionType Type;
  253. };
  254. std::vector<ConfigName> Configs;
  255. class FlushDebugBufferOnExit;
  256. /*! the selected sortOrder (None by default)*/
  257. SortOrderType SortOrder = None;
  258. /*! the selected sortDirection (Asc by default)*/
  259. SortDirectionType SortDirection = Asc;
  260. struct ConfigFileInfo
  261. {
  262. std::string filename;
  263. std::string version;
  264. bool operator<(ConfigFileInfo const& rhs) const
  265. {
  266. return this->filename < rhs.filename;
  267. }
  268. bool operator==(ConfigFileInfo const& rhs) const
  269. {
  270. return this->filename == rhs.filename;
  271. }
  272. bool operator!=(ConfigFileInfo const& rhs) const
  273. {
  274. return !(*this == rhs);
  275. }
  276. };
  277. std::vector<ConfigFileInfo> ConsideredConfigs;
  278. std::unique_ptr<cmPackageInfoReader> CpsReader;
  279. AppendixMap CpsAppendices;
  280. friend struct std::hash<ConfigFileInfo>;
  281. };
  282. namespace std {
  283. template <>
  284. struct hash<cmFindPackageCommand::ConfigFileInfo>
  285. {
  286. using argument_type = cmFindPackageCommand::ConfigFileInfo;
  287. using result_type = size_t;
  288. result_type operator()(argument_type const& s) const noexcept
  289. {
  290. result_type const h(std::hash<std::string>{}(s.filename));
  291. return h;
  292. }
  293. };
  294. }
  295. bool cmFindPackage(std::vector<std::string> const& args,
  296. cmExecutionStatus& status);