1 /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 file Copyright.txt or https://cmake.org/licensing for details. */
5 #include "cmConfigure.h" // IWYU pragma: keep
15 #include <cm/string_view>
17 #include <cm3p/kwiml/int.h>
19 #include "cmFindCommon.h"
20 #include "cmPolicies.h"
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 */
28 template <class T> struct hash;
33 class cmExecutionStatus;
36 /** \class cmFindPackageCommand
37 * \brief Load settings from an external project.
39 * cmFindPackageCommand
41 class cmFindPackageCommand : public cmFindCommon
44 /*! A sorting order strategy to be applied to recovered package folders (see
45 * FIND_PACKAGE_SORT_ORDER)*/
46 enum /*class*/ SortOrderType
52 /*! A sorting direction to be applied to recovered package folders (see
53 * FIND_PACKAGE_SORT_DIRECTION)*/
54 enum /*class*/ SortDirectionType
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);
65 cmFindPackageCommand(cmExecutionStatus& status);
67 bool InitialPass(std::vector<std::string> const& args);
70 class PathLabel : public cmFindCommon::PathLabel
76 PathLabel(const std::string& label)
77 : cmFindCommon::PathLabel(label)
80 static PathLabel PackageRedirect;
81 static PathLabel UserRegistry;
82 static PathLabel Builds;
83 static PathLabel SystemRegistry;
86 bool FindPackageUsingModuleMode();
87 bool FindPackageUsingConfigMode();
89 // Add additional search path labels and groups not present in the
91 void AppendSearchPathGroups();
93 void AppendSuccessInformation();
94 void AppendToFoundProperty(bool found);
95 void SetVersionVariables(
96 const std::function<void(const std::string&, cm::string_view)>&
98 const std::string& prefix, const std::string& version, unsigned int count,
99 unsigned int major, unsigned int minor, unsigned int patch,
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();
108 enum /*class*/ HandlePackageModeType
113 bool HandlePackageMode(HandlePackageModeType type);
116 bool FindPrefixedConfig();
117 bool FindFrameworkConfig();
118 bool FindAppBundleConfig();
124 bool ReadListFile(const std::string& f, PolicyScopeRule psr);
125 void StoreVersionFound();
126 void SetConfigDirCacheVariable(const std::string& value);
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);
161 std::map<std::string, OriginalDef> OriginalDefs;
163 std::map<std::string, cmPolicies::PolicyID> DeprecatedFindModules;
165 static const cm::string_view VERSION_ENDPOINT_INCLUDED;
166 static const cm::string_view VERSION_ENDPOINT_EXCLUDED;
169 std::string Variable;
170 std::string VersionComplete;
171 std::string VersionRange;
172 cm::string_view VersionRangeMin;
173 cm::string_view VersionRangeMax;
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;
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;
215 /*! the selected sortOrder (None by default)*/
216 SortOrderType SortOrder = None;
217 /*! the selected sortDirection (Asc by default)*/
218 SortDirectionType SortDirection = Asc;
220 struct ConfigFileInfo
222 std::string filename;
225 bool operator<(ConfigFileInfo const& rhs) const
227 return this->filename < rhs.filename;
230 bool operator==(ConfigFileInfo const& rhs) const
232 return this->filename == rhs.filename;
235 bool operator!=(ConfigFileInfo const& rhs) const
237 return !(*this == rhs);
240 std::vector<ConfigFileInfo> ConsideredConfigs;
242 friend struct std::hash<ConfigFileInfo>;
248 struct hash<cmFindPackageCommand::ConfigFileInfo>
250 using argument_type = cmFindPackageCommand::ConfigFileInfo;
251 using result_type = size_t;
253 result_type operator()(argument_type const& s) const noexcept
255 result_type const h(std::hash<std::string>{}(s.filename));
261 bool cmFindPackage(std::vector<std::string> const& args,
262 cmExecutionStatus& status);