cmFindPackageCommand.h 7.4 KB

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