cmFindPackageCommand.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 PackageRedirect;
  67. static PathLabel UserRegistry;
  68. static PathLabel Builds;
  69. static PathLabel SystemRegistry;
  70. };
  71. bool FindPackageUsingModuleMode();
  72. bool FindPackageUsingConfigMode();
  73. // Add additional search path labels and groups not present in the
  74. // parent class
  75. void AppendSearchPathGroups();
  76. void AppendSuccessInformation();
  77. void AppendToFoundProperty(bool found);
  78. void SetVersionVariables(
  79. const std::function<void(const std::string&, cm::string_view)>&
  80. addDefinition,
  81. const std::string& prefix, const std::string& version, unsigned int count,
  82. unsigned int major, unsigned int minor, unsigned int patch,
  83. unsigned int tweak);
  84. void SetModuleVariables(const std::string& components);
  85. bool FindModule(bool& found);
  86. void AddFindDefinition(const std::string& var, cm::string_view value);
  87. void RestoreFindDefinitions();
  88. enum /*class*/ HandlePackageModeType
  89. {
  90. Module,
  91. Config
  92. };
  93. bool HandlePackageMode(HandlePackageModeType type);
  94. bool FindConfig();
  95. bool FindPrefixedConfig();
  96. bool FindFrameworkConfig();
  97. bool FindAppBundleConfig();
  98. enum PolicyScopeRule
  99. {
  100. NoPolicyScope,
  101. DoPolicyScope
  102. };
  103. bool ReadListFile(const std::string& f, PolicyScopeRule psr);
  104. void StoreVersionFound();
  105. void SetConfigDirCacheVariable(const std::string& value);
  106. void ComputePrefixes();
  107. void FillPrefixesPackageRedirect();
  108. void FillPrefixesPackageRoot();
  109. void FillPrefixesCMakeEnvironment();
  110. void FillPrefixesCMakeVariable();
  111. void FillPrefixesSystemEnvironment();
  112. void FillPrefixesUserRegistry();
  113. void FillPrefixesSystemRegistry();
  114. void FillPrefixesCMakeSystemVariable();
  115. void FillPrefixesUserGuess();
  116. void FillPrefixesUserHints();
  117. void LoadPackageRegistryDir(std::string const& dir, cmSearchPath& outPaths);
  118. void LoadPackageRegistryWinUser();
  119. void LoadPackageRegistryWinSystem();
  120. void LoadPackageRegistryWin(bool user, unsigned int view,
  121. cmSearchPath& outPaths);
  122. bool CheckPackageRegistryEntry(const std::string& fname,
  123. cmSearchPath& outPaths);
  124. bool SearchDirectory(std::string const& dir);
  125. bool CheckDirectory(std::string const& dir);
  126. bool FindConfigFile(std::string const& dir, std::string& file);
  127. bool CheckVersion(std::string const& config_file);
  128. bool CheckVersionFile(std::string const& version_file,
  129. std::string& result_version);
  130. bool SearchPrefix(std::string const& prefix);
  131. bool SearchFrameworkPrefix(std::string const& prefix_in);
  132. bool SearchAppBundlePrefix(std::string const& prefix_in);
  133. friend class cmFindPackageFileList;
  134. struct OriginalDef
  135. {
  136. bool exists;
  137. std::string value;
  138. };
  139. std::map<std::string, OriginalDef> OriginalDefs;
  140. std::map<std::string, cmPolicies::PolicyID> DeprecatedFindModules;
  141. static const cm::string_view VERSION_ENDPOINT_INCLUDED;
  142. static const cm::string_view VERSION_ENDPOINT_EXCLUDED;
  143. std::string Name;
  144. std::string Variable;
  145. std::string VersionComplete;
  146. std::string VersionRange;
  147. cm::string_view VersionRangeMin;
  148. cm::string_view VersionRangeMax;
  149. std::string Version;
  150. unsigned int VersionMajor = 0;
  151. unsigned int VersionMinor = 0;
  152. unsigned int VersionPatch = 0;
  153. unsigned int VersionTweak = 0;
  154. unsigned int VersionCount = 0;
  155. std::string VersionMax;
  156. unsigned int VersionMaxMajor = 0;
  157. unsigned int VersionMaxMinor = 0;
  158. unsigned int VersionMaxPatch = 0;
  159. unsigned int VersionMaxTweak = 0;
  160. unsigned int VersionMaxCount = 0;
  161. bool VersionExact = false;
  162. std::string FileFound;
  163. std::string VersionFound;
  164. unsigned int VersionFoundMajor = 0;
  165. unsigned int VersionFoundMinor = 0;
  166. unsigned int VersionFoundPatch = 0;
  167. unsigned int VersionFoundTweak = 0;
  168. unsigned int VersionFoundCount = 0;
  169. KWIML_INT_uint64_t RequiredCMakeVersion = 0;
  170. bool Quiet = false;
  171. bool Required = false;
  172. bool UseConfigFiles = true;
  173. bool UseFindModules = true;
  174. bool NoUserRegistry = false;
  175. bool NoSystemRegistry = false;
  176. bool UseLib32Paths = false;
  177. bool UseLib64Paths = false;
  178. bool UseLibx32Paths = false;
  179. bool UseRealPath = false;
  180. bool PolicyScope = true;
  181. bool GlobalScope = false;
  182. std::string LibraryArchitecture;
  183. std::vector<std::string> Names;
  184. std::vector<std::string> Configs;
  185. std::set<std::string> IgnoredPaths;
  186. std::set<std::string> IgnoredPrefixPaths;
  187. std::string DebugBuffer;
  188. /*! the selected sortOrder (None by default)*/
  189. SortOrderType SortOrder = None;
  190. /*! the selected sortDirection (Asc by default)*/
  191. SortDirectionType SortDirection = Asc;
  192. struct ConfigFileInfo
  193. {
  194. std::string filename;
  195. std::string version;
  196. bool operator<(ConfigFileInfo const& rhs) const
  197. {
  198. return this->filename < rhs.filename;
  199. }
  200. bool operator==(ConfigFileInfo const& rhs) const
  201. {
  202. return this->filename == rhs.filename;
  203. }
  204. bool operator!=(ConfigFileInfo const& rhs) const
  205. {
  206. return !(*this == rhs);
  207. }
  208. };
  209. std::vector<ConfigFileInfo> ConsideredConfigs;
  210. friend struct std::hash<ConfigFileInfo>;
  211. };
  212. namespace std {
  213. template <>
  214. struct hash<cmFindPackageCommand::ConfigFileInfo>
  215. {
  216. using argument_type = cmFindPackageCommand::ConfigFileInfo;
  217. using result_type = size_t;
  218. result_type operator()(argument_type const& s) const noexcept
  219. {
  220. result_type const h(std::hash<std::string>{}(s.filename));
  221. return h;
  222. }
  223. };
  224. }
  225. bool cmFindPackage(std::vector<std::string> const& args,
  226. cmExecutionStatus& status);