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