cmFindPackageCommand.h 6.38 KB
Newer Older
1
2
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
   file Copyright.txt or https://cmake.org/licensing for details.  */
3
4
5
#ifndef cmFindPackageCommand_h
#define cmFindPackageCommand_h

6
#include "cmConfigure.h" // IWYU pragma: keep
7
#include "cmPolicies.h"
8

9
#include "cm_kwiml.h"
10
#include <cstddef>
11
#include <functional>
12
13
14
15
16
#include <map>
#include <set>
#include <string>
#include <vector>

17
18
19
// IWYU insists we should forward-declare instead of including <functional>,
// but we cannot forward-declare reliably because some C++ standard libraries
// put the template in an inline namespace.
20
#ifdef CMAKE_IWYU_FORWARD_STD_HASH
21
22
23
24
25
26
27
/* clang-format off */
namespace std {
  template <class T> struct hash;
}
/* clang-format on */
#endif

28
29
#include "cmFindCommon.h"

30
31
32
class cmCommand;
class cmExecutionStatus;
class cmSearchPath;
33
34
35
36
37
38

/** \class cmFindPackageCommand
 * \brief Load settings from an external project.
 *
 * cmFindPackageCommand
 */
39
class cmFindPackageCommand : public cmFindCommon
40
41
{
public:
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
  /*! A sorting order strategy to be applied to recovered package folders (see
   * FIND_PACKAGE_SORT_ORDER)*/
  enum /*class*/ SortOrderType
  {
    None,
    Name_order,
    Natural
  };
  /*! A sorting direction to be applied to recovered package folders (see
   * FIND_PACKAGE_SORT_DIRECTION)*/
  enum /*class*/ SortDirectionType
  {
    Asc,
    Dec
  };

  /*! sorts a given list of string based on the input sort parameters */
  static void Sort(std::vector<std::string>::iterator begin,
                   std::vector<std::string>::iterator end, SortOrderType order,
                   SortDirectionType dir);

63
64
  cmFindPackageCommand();

65
66
67
  /**
   * This is a virtual constructor for the command.
   */
68
  cmCommand* Clone() override { return new cmFindPackageCommand; }
69
70
71
72
73

  /**
   * This is called when the command is first encountered in
   * the CMakeLists.txt file.
   */
74
  bool InitialPass(std::vector<std::string> const& args,
75
                   cmExecutionStatus& status) override;
76
77

private:
78
79
80
81
  class PathLabel : public cmFindCommon::PathLabel
  {
  protected:
    PathLabel();
82

83
  public:
84
85
86
87
    PathLabel(const std::string& label)
      : cmFindCommon::PathLabel(label)
    {
    }
88
89
90
91
92
    static PathLabel UserRegistry;
    static PathLabel Builds;
    static PathLabel SystemRegistry;
  };

93
94
95
  bool FindPackageUsingModuleMode();
  bool FindPackageUsingConfigMode();

96
97
98
99
  // Add additional search path labels and groups not present in the
  // parent class
  void AppendSearchPathGroups();

100
  void AppendSuccessInformation();
101
  void AppendToFoundProperty(bool found);
102
  void SetModuleVariables(const std::string& components);
103
  bool FindModule(bool& found);
104
  void AddFindDefinition(const std::string& var, const char* val);
105
  void RestoreFindDefinitions();
106
107
108
109
110
111
112
113

  enum /*class*/ HandlePackageModeType
  {
    Module,
    Config
  };
  bool HandlePackageMode(HandlePackageModeType type);

114
  bool FindConfig();
115
116
117
  bool FindPrefixedConfig();
  bool FindFrameworkConfig();
  bool FindAppBundleConfig();
118
119
120
121
122
  enum PolicyScopeRule
  {
    NoPolicyScope,
    DoPolicyScope
  };
123
  bool ReadListFile(const std::string& f, PolicyScopeRule psr);
124
  void StoreVersionFound();
125

126
  void ComputePrefixes();
127
  void FillPrefixesPackageRoot();
128
129
130
131
132
133
134
135
  void FillPrefixesCMakeEnvironment();
  void FillPrefixesCMakeVariable();
  void FillPrefixesSystemEnvironment();
  void FillPrefixesUserRegistry();
  void FillPrefixesSystemRegistry();
  void FillPrefixesCMakeSystemVariable();
  void FillPrefixesUserGuess();
  void FillPrefixesUserHints();
136
  void LoadPackageRegistryDir(std::string const& dir, cmSearchPath& outPaths);
137
138
  void LoadPackageRegistryWinUser();
  void LoadPackageRegistryWinSystem();
139
  void LoadPackageRegistryWin(bool user, unsigned int view,
140
                              cmSearchPath& outPaths);
141
  bool CheckPackageRegistryEntry(const std::string& fname,
142
                                 cmSearchPath& outPaths);
143
144
145
  bool SearchDirectory(std::string const& dir);
  bool CheckDirectory(std::string const& dir);
  bool FindConfigFile(std::string const& dir, std::string& file);
146
  bool CheckVersion(std::string const& config_file);
147
148
  bool CheckVersionFile(std::string const& version_file,
                        std::string& result_version);
149
150
151
152
153
154
  bool SearchPrefix(std::string const& prefix);
  bool SearchFrameworkPrefix(std::string const& prefix_in);
  bool SearchAppBundlePrefix(std::string const& prefix_in);

  friend class cmFindPackageFileList;

155
156
157
158
159
  struct OriginalDef
  {
    bool exists;
    std::string value;
  };
160
  std::map<std::string, OriginalDef> OriginalDefs;
161

162
163
  std::map<std::string, cmPolicies::PolicyID> DeprecatedFindModules;

164
165
166
  std::string Name;
  std::string Variable;
  std::string Version;
167
168
169
  unsigned int VersionMajor;
  unsigned int VersionMinor;
  unsigned int VersionPatch;
170
  unsigned int VersionTweak;
171
  unsigned int VersionCount;
172
  bool VersionExact;
173
174
  std::string FileFound;
  std::string VersionFound;
175
176
177
  unsigned int VersionFoundMajor;
  unsigned int VersionFoundMinor;
  unsigned int VersionFoundPatch;
178
  unsigned int VersionFoundTweak;
179
  unsigned int VersionFoundCount;
Brad King's avatar
Brad King committed
180
  KWIML_INT_uint64_t RequiredCMakeVersion;
181
182
  bool Quiet;
  bool Required;
183
  bool UseConfigFiles;
184
  bool UseFindModules;
185
  bool NoUserRegistry;
186
  bool NoSystemRegistry;
187
  bool DebugMode;
188
  bool UseLib32Paths;
189
  bool UseLib64Paths;
Steven Newbury's avatar
Steven Newbury committed
190
  bool UseLibx32Paths;
191
  bool UseRealPath;
192
  bool PolicyScope;
193
  std::string LibraryArchitecture;
194
195
  std::vector<std::string> Names;
  std::vector<std::string> Configs;
196
  std::set<std::string> IgnoredPaths;
197

198
199
200
201
202
  /*! the selected sortOrder (None by default)*/
  SortOrderType SortOrder;
  /*! the selected sortDirection (Asc by default)*/
  SortDirectionType SortDirection;

203
204
205
206
  struct ConfigFileInfo
  {
    std::string filename;
    std::string version;
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221

    bool operator<(ConfigFileInfo const& rhs) const
    {
      return this->filename < rhs.filename;
    }

    bool operator==(ConfigFileInfo const& rhs) const
    {
      return this->filename == rhs.filename;
    }

    bool operator!=(ConfigFileInfo const& rhs) const
    {
      return !(*this == rhs);
    }
222
  };
223
  std::vector<ConfigFileInfo> ConsideredConfigs;
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240

  friend struct std::hash<ConfigFileInfo>;
};

namespace std {

template <>
struct hash<cmFindPackageCommand::ConfigFileInfo>
{
  typedef cmFindPackageCommand::ConfigFileInfo argument_type;
  typedef size_t result_type;

  result_type operator()(argument_type const& s) const noexcept
  {
    result_type const h(std::hash<std::string>{}(s.filename));
    return h;
  }
241
};
242
}
243
244

#endif