cmFindPackageCommand.h 12 KB

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