cmFindPackageCommand.h 10 KB

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