cmFindPackageCommand.h 7.1 KB

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