cmFindPackageCommand.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #ifndef cmFindPackageCommand_h
  4. #define cmFindPackageCommand_h
  5. #include "cmCommand.h"
  6. #include "cmConfigure.h" // IWYU pragma: keep
  7. #include "cmPolicies.h"
  8. #include "cm_kwiml.h"
  9. #include <cstddef>
  10. #include <functional>
  11. #include <map>
  12. #include <set>
  13. #include <string>
  14. #include <vector>
  15. #include "cm_memory.hxx"
  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. #include "cmFindCommon.h"
  27. class cmExecutionStatus;
  28. class cmSearchPath;
  29. /** \class cmFindPackageCommand
  30. * \brief Load settings from an external project.
  31. *
  32. * cmFindPackageCommand
  33. */
  34. class cmFindPackageCommand : public cmFindCommon
  35. {
  36. public:
  37. /*! A sorting order strategy to be applied to recovered package folders (see
  38. * FIND_PACKAGE_SORT_ORDER)*/
  39. enum /*class*/ SortOrderType
  40. {
  41. None,
  42. Name_order,
  43. Natural
  44. };
  45. /*! A sorting direction to be applied to recovered package folders (see
  46. * FIND_PACKAGE_SORT_DIRECTION)*/
  47. enum /*class*/ SortDirectionType
  48. {
  49. Asc,
  50. Dec
  51. };
  52. /*! sorts a given list of string based on the input sort parameters */
  53. static void Sort(std::vector<std::string>::iterator begin,
  54. std::vector<std::string>::iterator end, SortOrderType order,
  55. SortDirectionType dir);
  56. cmFindPackageCommand();
  57. /**
  58. * This is a virtual constructor for the command.
  59. */
  60. std::unique_ptr<cmCommand> Clone() override
  61. {
  62. return cm::make_unique<cmFindPackageCommand>();
  63. }
  64. /**
  65. * This is called when the command is first encountered in
  66. * the CMakeLists.txt file.
  67. */
  68. bool InitialPass(std::vector<std::string> const& args,
  69. cmExecutionStatus& status) override;
  70. private:
  71. class PathLabel : public cmFindCommon::PathLabel
  72. {
  73. protected:
  74. PathLabel();
  75. public:
  76. PathLabel(const std::string& label)
  77. : cmFindCommon::PathLabel(label)
  78. {
  79. }
  80. static PathLabel UserRegistry;
  81. static PathLabel Builds;
  82. static PathLabel SystemRegistry;
  83. };
  84. bool FindPackageUsingModuleMode();
  85. bool FindPackageUsingConfigMode();
  86. // Add additional search path labels and groups not present in the
  87. // parent class
  88. void AppendSearchPathGroups();
  89. void AppendSuccessInformation();
  90. void AppendToFoundProperty(bool found);
  91. void SetModuleVariables(const std::string& components);
  92. bool FindModule(bool& found);
  93. void AddFindDefinition(const std::string& var, const char* val);
  94. void RestoreFindDefinitions();
  95. enum /*class*/ HandlePackageModeType
  96. {
  97. Module,
  98. Config
  99. };
  100. bool HandlePackageMode(HandlePackageModeType type);
  101. bool FindConfig();
  102. bool FindPrefixedConfig();
  103. bool FindFrameworkConfig();
  104. bool FindAppBundleConfig();
  105. enum PolicyScopeRule
  106. {
  107. NoPolicyScope,
  108. DoPolicyScope
  109. };
  110. bool ReadListFile(const std::string& f, PolicyScopeRule psr);
  111. void StoreVersionFound();
  112. void ComputePrefixes();
  113. void FillPrefixesPackageRoot();
  114. void FillPrefixesCMakeEnvironment();
  115. void FillPrefixesCMakeVariable();
  116. void FillPrefixesSystemEnvironment();
  117. void FillPrefixesUserRegistry();
  118. void FillPrefixesSystemRegistry();
  119. void FillPrefixesCMakeSystemVariable();
  120. void FillPrefixesUserGuess();
  121. void FillPrefixesUserHints();
  122. void LoadPackageRegistryDir(std::string const& dir, cmSearchPath& outPaths);
  123. void LoadPackageRegistryWinUser();
  124. void LoadPackageRegistryWinSystem();
  125. void LoadPackageRegistryWin(bool user, unsigned int view,
  126. cmSearchPath& outPaths);
  127. bool CheckPackageRegistryEntry(const std::string& fname,
  128. cmSearchPath& outPaths);
  129. bool SearchDirectory(std::string const& dir);
  130. bool CheckDirectory(std::string const& dir);
  131. bool FindConfigFile(std::string const& dir, std::string& file);
  132. bool CheckVersion(std::string const& config_file);
  133. bool CheckVersionFile(std::string const& version_file,
  134. std::string& result_version);
  135. bool SearchPrefix(std::string const& prefix);
  136. bool SearchFrameworkPrefix(std::string const& prefix_in);
  137. bool SearchAppBundlePrefix(std::string const& prefix_in);
  138. friend class cmFindPackageFileList;
  139. struct OriginalDef
  140. {
  141. bool exists;
  142. std::string value;
  143. };
  144. std::map<std::string, OriginalDef> OriginalDefs;
  145. std::map<std::string, cmPolicies::PolicyID> DeprecatedFindModules;
  146. std::string Name;
  147. std::string Variable;
  148. std::string Version;
  149. unsigned int VersionMajor;
  150. unsigned int VersionMinor;
  151. unsigned int VersionPatch;
  152. unsigned int VersionTweak;
  153. unsigned int VersionCount;
  154. bool VersionExact;
  155. std::string FileFound;
  156. std::string VersionFound;
  157. unsigned int VersionFoundMajor;
  158. unsigned int VersionFoundMinor;
  159. unsigned int VersionFoundPatch;
  160. unsigned int VersionFoundTweak;
  161. unsigned int VersionFoundCount;
  162. KWIML_INT_uint64_t RequiredCMakeVersion;
  163. bool Quiet;
  164. bool Required;
  165. bool UseConfigFiles;
  166. bool UseFindModules;
  167. bool NoUserRegistry;
  168. bool NoSystemRegistry;
  169. bool DebugMode;
  170. bool UseLib32Paths;
  171. bool UseLib64Paths;
  172. bool UseLibx32Paths;
  173. bool UseRealPath;
  174. bool PolicyScope;
  175. std::string LibraryArchitecture;
  176. std::vector<std::string> Names;
  177. std::vector<std::string> Configs;
  178. std::set<std::string> IgnoredPaths;
  179. /*! the selected sortOrder (None by default)*/
  180. SortOrderType SortOrder;
  181. /*! the selected sortDirection (Asc by default)*/
  182. SortDirectionType SortDirection;
  183. struct ConfigFileInfo
  184. {
  185. std::string filename;
  186. std::string version;
  187. bool operator<(ConfigFileInfo const& rhs) const
  188. {
  189. return this->filename < rhs.filename;
  190. }
  191. bool operator==(ConfigFileInfo const& rhs) const
  192. {
  193. return this->filename == rhs.filename;
  194. }
  195. bool operator!=(ConfigFileInfo const& rhs) const
  196. {
  197. return !(*this == rhs);
  198. }
  199. };
  200. std::vector<ConfigFileInfo> ConsideredConfigs;
  201. friend struct std::hash<ConfigFileInfo>;
  202. };
  203. namespace std {
  204. template <>
  205. struct hash<cmFindPackageCommand::ConfigFileInfo>
  206. {
  207. typedef cmFindPackageCommand::ConfigFileInfo argument_type;
  208. typedef size_t result_type;
  209. result_type operator()(argument_type const& s) const noexcept
  210. {
  211. result_type const h(std::hash<std::string>{}(s.filename));
  212. return h;
  213. }
  214. };
  215. }
  216. #endif