resolve cyclic dependency with zstd
[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   struct OriginalDef
157   {
158     bool exists;
159     std::string value;
160   };
161   std::map<std::string, OriginalDef> OriginalDefs;
162
163   std::map<std::string, cmPolicies::PolicyID> DeprecatedFindModules;
164
165   static const cm::string_view VERSION_ENDPOINT_INCLUDED;
166   static const cm::string_view VERSION_ENDPOINT_EXCLUDED;
167
168   std::string Name;
169   std::string Variable;
170   std::string VersionComplete;
171   std::string VersionRange;
172   cm::string_view VersionRangeMin;
173   cm::string_view VersionRangeMax;
174   std::string Version;
175   unsigned int VersionMajor = 0;
176   unsigned int VersionMinor = 0;
177   unsigned int VersionPatch = 0;
178   unsigned int VersionTweak = 0;
179   unsigned int VersionCount = 0;
180   std::string VersionMax;
181   unsigned int VersionMaxMajor = 0;
182   unsigned int VersionMaxMinor = 0;
183   unsigned int VersionMaxPatch = 0;
184   unsigned int VersionMaxTweak = 0;
185   unsigned int VersionMaxCount = 0;
186   bool VersionExact = false;
187   std::string FileFound;
188   std::string VersionFound;
189   unsigned int VersionFoundMajor = 0;
190   unsigned int VersionFoundMinor = 0;
191   unsigned int VersionFoundPatch = 0;
192   unsigned int VersionFoundTweak = 0;
193   unsigned int VersionFoundCount = 0;
194   KWIML_INT_uint64_t RequiredCMakeVersion = 0;
195   bool Quiet = false;
196   bool Required = false;
197   bool UseConfigFiles = true;
198   bool UseFindModules = true;
199   bool NoUserRegistry = false;
200   bool NoSystemRegistry = false;
201   bool UseLib32Paths = false;
202   bool UseLib64Paths = false;
203   bool UseLibx32Paths = false;
204   bool UseRealPath = false;
205   bool PolicyScope = true;
206   bool GlobalScope = false;
207   bool RegistryViewDefined = false;
208   std::string LibraryArchitecture;
209   std::vector<std::string> Names;
210   std::vector<std::string> Configs;
211   std::set<std::string> IgnoredPaths;
212   std::set<std::string> IgnoredPrefixPaths;
213   std::string DebugBuffer;
214
215   /*! the selected sortOrder (None by default)*/
216   SortOrderType SortOrder = None;
217   /*! the selected sortDirection (Asc by default)*/
218   SortDirectionType SortDirection = Asc;
219
220   struct ConfigFileInfo
221   {
222     std::string filename;
223     std::string version;
224
225     bool operator<(ConfigFileInfo const& rhs) const
226     {
227       return this->filename < rhs.filename;
228     }
229
230     bool operator==(ConfigFileInfo const& rhs) const
231     {
232       return this->filename == rhs.filename;
233     }
234
235     bool operator!=(ConfigFileInfo const& rhs) const
236     {
237       return !(*this == rhs);
238     }
239   };
240   std::vector<ConfigFileInfo> ConsideredConfigs;
241
242   friend struct std::hash<ConfigFileInfo>;
243 };
244
245 namespace std {
246
247 template <>
248 struct hash<cmFindPackageCommand::ConfigFileInfo>
249 {
250   using argument_type = cmFindPackageCommand::ConfigFileInfo;
251   using result_type = size_t;
252
253   result_type operator()(argument_type const& s) const noexcept
254   {
255     result_type const h(std::hash<std::string>{}(s.filename));
256     return h;
257   }
258 };
259 }
260
261 bool cmFindPackage(std::vector<std::string> const& args,
262                    cmExecutionStatus& status);