cmFindBase.cxx 10.3 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.  */
Bill Hoffman's avatar
Bill Hoffman committed
3
#include "cmFindBase.h"
4

5
#include <deque>
6 7 8 9
#include <iostream>
#include <map>
#include <stddef.h>

10
#include "cmAlgorithms.h"
11
#include "cmMakefile.h"
12
#include "cmRange.h"
13
#include "cmSearchPath.h"
14
#include "cmState.h"
15
#include "cmStateTypes.h"
16
#include "cmSystemTools.h"
17

Bill Hoffman's avatar
Bill Hoffman committed
18 19 20
cmFindBase::cmFindBase()
{
  this->AlreadyInCache = false;
21
  this->AlreadyInCacheWithoutMetaInfo = false;
22 23
  this->NamesPerDir = false;
  this->NamesPerDirAllowed = false;
Brad King's avatar
Brad King committed
24 25
}

Bill Hoffman's avatar
Bill Hoffman committed
26 27
bool cmFindBase::ParseArguments(std::vector<std::string> const& argsIn)
{
28
  if (argsIn.size() < 2) {
Bill Hoffman's avatar
Bill Hoffman committed
29 30
    this->SetError("called with incorrect number of arguments");
    return false;
31
  }
32

Bill Hoffman's avatar
Bill Hoffman committed
33
  // copy argsIn into args so it can be modified,
34
  // in the process extract the DOC "documentation"
Bill Hoffman's avatar
Bill Hoffman committed
35 36 37
  size_t size = argsIn.size();
  std::vector<std::string> args;
  bool foundDoc = false;
38 39 40 41
  for (unsigned int j = 0; j < size; ++j) {
    if (foundDoc || argsIn[j] != "DOC") {
      if (argsIn[j] == "ENV") {
        if (j + 1 < size) {
42 43 44
          j++;
          cmSystemTools::GetPath(args, argsIn[j].c_str());
        }
45
      } else {
46
        args.push_back(argsIn[j]);
Bill Hoffman's avatar
Bill Hoffman committed
47
      }
48 49
    } else {
      if (j + 1 < size) {
Bill Hoffman's avatar
Bill Hoffman committed
50
        foundDoc = true;
51
        this->VariableDocumentation = argsIn[j + 1];
Bill Hoffman's avatar
Bill Hoffman committed
52
        j++;
53
        if (j >= size) {
Bill Hoffman's avatar
Bill Hoffman committed
54 55 56 57
          break;
        }
      }
    }
58 59
  }
  if (args.size() < 2) {
60 61
    this->SetError("called with incorrect number of arguments");
    return false;
62
  }
Bill Hoffman's avatar
Bill Hoffman committed
63
  this->VariableName = args[0];
64
  if (this->CheckForVariableInCache()) {
Bill Hoffman's avatar
Bill Hoffman committed
65 66
    this->AlreadyInCache = true;
    return true;
67
  }
68
  this->AlreadyInCache = false;
69

70 71 72
  // Find what search path locations have been enabled/disable
  this->SelectDefaultSearchModes();

73 74 75 76 77
  // Find the current root path mode.
  this->SelectDefaultRootPathMode();

  // Find the current bundle/framework search policy.
  this->SelectDefaultMacMode();
Alexander Neundorf's avatar
 
Alexander Neundorf committed
78

Bill Hoffman's avatar
Bill Hoffman committed
79
  bool newStyle = false;
80 81 82 83 84 85 86 87
  enum Doing
  {
    DoingNone,
    DoingNames,
    DoingPaths,
    DoingPathSuffixes,
    DoingHints
  };
88
  Doing doing = DoingNames; // assume it starts with a name
89 90
  for (unsigned int j = 1; j < args.size(); ++j) {
    if (args[j] == "NAMES") {
91
      doing = DoingNames;
Bill Hoffman's avatar
Bill Hoffman committed
92
      newStyle = true;
93
    } else if (args[j] == "PATHS") {
94 95
      doing = DoingPaths;
      newStyle = true;
96
    } else if (args[j] == "HINTS") {
97
      doing = DoingHints;
Bill Hoffman's avatar
Bill Hoffman committed
98
      newStyle = true;
99
    } else if (args[j] == "PATH_SUFFIXES") {
100
      doing = DoingPathSuffixes;
Bill Hoffman's avatar
Bill Hoffman committed
101
      newStyle = true;
102
    } else if (args[j] == "NAMES_PER_DIR") {
103
      doing = DoingNone;
104
      if (this->NamesPerDirAllowed) {
105
        this->NamesPerDir = true;
106
      } else {
107 108 109
        this->SetError("does not support NAMES_PER_DIR");
        return false;
      }
110
    } else if (args[j] == "NO_SYSTEM_PATH") {
111
      doing = DoingNone;
112
      this->NoDefaultPath = true;
113
    } else if (this->CheckCommonArgument(args[j])) {
114
      doing = DoingNone;
115 116 117
      // Some common arguments were accidentally supported by CMake
      // 2.4 and 2.6.0 in the short-hand form of the command, so we
      // must support it even though it is not documented.
118
    } else if (doing == DoingNames) {
119
      this->Names.push_back(args[j]);
120
    } else if (doing == DoingPaths) {
121
      this->UserGuessArgs.push_back(args[j]);
122
    } else if (doing == DoingHints) {
123
      this->UserHintsArgs.push_back(args[j]);
124
    } else if (doing == DoingPathSuffixes) {
125
      this->AddPathSuffix(args[j]);
Bill Hoffman's avatar
Bill Hoffman committed
126
    }
127
  }
128

129
  if (this->VariableDocumentation.empty()) {
Alexander Neundorf's avatar
 
Alexander Neundorf committed
130
    this->VariableDocumentation = "Where can ";
131
    if (this->Names.empty()) {
Bill Hoffman's avatar
Bill Hoffman committed
132
      this->VariableDocumentation += "the (unknown) library be found";
133 134
    } else if (this->Names.size() == 1) {
      this->VariableDocumentation +=
135
        "the " + this->Names.front() + " library be found";
136
    } else {
137
      this->VariableDocumentation += "one of the ";
138 139
      this->VariableDocumentation +=
        cmJoin(cmMakeRange(this->Names).retreat(1), ", ");
140
      this->VariableDocumentation +=
141
        " or " + this->Names.back() + " libraries be found";
Bill Hoffman's avatar
Bill Hoffman committed
142
    }
143
  }
Bill Hoffman's avatar
Bill Hoffman committed
144 145 146

  // look for old style
  // FIND_*(VAR name path1 path2 ...)
147
  if (!newStyle) {
148 149
    // All the short-hand arguments have been recorded as names.
    std::vector<std::string> shortArgs = this->Names;
150
    this->Names.clear(); // clear out any values in Names
151
    this->Names.push_back(shortArgs[0]);
152
    cmAppend(this->UserGuessArgs, shortArgs.begin() + 1, shortArgs.end());
153
  }
154
  this->ExpandPaths();
155

156
  this->ComputeFinalPaths();
157

Bill Hoffman's avatar
Bill Hoffman committed
158 159 160
  return true;
}

161
void cmFindBase::ExpandPaths()
Bill Hoffman's avatar
Bill Hoffman committed
162
{
163
  if (!this->NoDefaultPath) {
164 165 166
    if (!this->NoPackageRootPath) {
      this->FillPackageRootPath();
    }
167
    if (!this->NoCMakePath) {
168
      this->FillCMakeVariablePath();
169 170
    }
    if (!this->NoCMakeEnvironmentPath) {
171
      this->FillCMakeEnvironmentPath();
172
    }
173
  }
174
  this->FillUserHintsPath();
175 176
  if (!this->NoDefaultPath) {
    if (!this->NoSystemEnvironmentPath) {
177
      this->FillSystemEnvironmentPath();
178 179
    }
    if (!this->NoCMakeSystemPath) {
180 181
      this->FillCMakeSystemVariablePath();
    }
182
  }
183
  this->FillUserGuessPath();
184 185
}

186
void cmFindBase::FillCMakeEnvironmentPath()
187
{
188
  cmSearchPath& paths = this->LabeledPaths[PathLabel::CMakeEnvironment];
189

190 191 192 193
  // Add CMAKE_*_PATH environment variables
  std::string var = "CMAKE_";
  var += this->CMakePathName;
  var += "_PATH";
194 195
  paths.AddEnvPrefixPath("CMAKE_PREFIX_PATH");
  paths.AddEnvPath(var);
196

197
  if (this->CMakePathName == "PROGRAM") {
198
    paths.AddEnvPath("CMAKE_APPBUNDLE_PATH");
199
  } else {
200
    paths.AddEnvPath("CMAKE_FRAMEWORK_PATH");
201
  }
202
  paths.AddSuffixes(this->SearchPathSuffixes);
Bill Hoffman's avatar
Bill Hoffman committed
203 204
}

205 206 207 208
void cmFindBase::FillPackageRootPath()
{
  cmSearchPath& paths = this->LabeledPaths[PathLabel::PackageRoot];

209
  // Add the PACKAGE_ROOT_PATH from each enclosing find_package call.
210 211 212
  for (std::vector<std::string> const& pkgPaths :
       cmReverseRange(this->Makefile->FindPackageRootPathStack)) {
    paths.AddPrefixPaths(pkgPaths);
213
  }
214 215

  paths.AddSuffixes(this->SearchPathSuffixes);
216 217
}

218
void cmFindBase::FillCMakeVariablePath()
219
{
220
  cmSearchPath& paths = this->LabeledPaths[PathLabel::CMake];
221

luz.paz's avatar
luz.paz committed
222 223
  // Add CMake variables of the same name as the previous environment
  // variables CMAKE_*_PATH to be used most of the time with -D
224 225 226 227
  // command line options
  std::string var = "CMAKE_";
  var += this->CMakePathName;
  var += "_PATH";
228 229
  paths.AddCMakePrefixPath("CMAKE_PREFIX_PATH");
  paths.AddCMakePath(var);
230

231
  if (this->CMakePathName == "PROGRAM") {
232
    paths.AddCMakePath("CMAKE_APPBUNDLE_PATH");
233
  } else {
234
    paths.AddCMakePath("CMAKE_FRAMEWORK_PATH");
235
  }
236
  paths.AddSuffixes(this->SearchPathSuffixes);
Bill Hoffman's avatar
Bill Hoffman committed
237 238
}

239
void cmFindBase::FillSystemEnvironmentPath()
Bill Hoffman's avatar
Bill Hoffman committed
240
{
241
  cmSearchPath& paths = this->LabeledPaths[PathLabel::SystemEnvironment];
242

243
  // Add LIB or INCLUDE
244
  if (!this->EnvironmentPath.empty()) {
245
    paths.AddEnvPath(this->EnvironmentPath);
246
#if defined(_WIN32) || defined(__CYGWIN__)
247
    paths.AddEnvPrefixPath("PATH", true);
248
#endif
249
  }
250 251
  // Add PATH
  paths.AddEnvPath("PATH");
252
  paths.AddSuffixes(this->SearchPathSuffixes);
253 254
}

255
void cmFindBase::FillCMakeSystemVariablePath()
256
{
257
  cmSearchPath& paths = this->LabeledPaths[PathLabel::CMakeSystem];
258

259 260 261
  std::string var = "CMAKE_SYSTEM_";
  var += this->CMakePathName;
  var += "_PATH";
262 263
  paths.AddCMakePrefixPath("CMAKE_SYSTEM_PREFIX_PATH");
  paths.AddCMakePath(var);
264

265
  if (this->CMakePathName == "PROGRAM") {
266
    paths.AddCMakePath("CMAKE_SYSTEM_APPBUNDLE_PATH");
267
  } else {
268
    paths.AddCMakePath("CMAKE_SYSTEM_FRAMEWORK_PATH");
269
  }
270
  paths.AddSuffixes(this->SearchPathSuffixes);
271 272
}

273
void cmFindBase::FillUserHintsPath()
274
{
275
  cmSearchPath& paths = this->LabeledPaths[PathLabel::Hints];
276

277 278
  for (std::string const& p : this->UserHintsArgs) {
    paths.AddUserPath(p);
279
  }
280
  paths.AddSuffixes(this->SearchPathSuffixes);
281 282
}

283
void cmFindBase::FillUserGuessPath()
284
{
285
  cmSearchPath& paths = this->LabeledPaths[PathLabel::Guess];
286

287 288
  for (std::string const& p : this->UserGuessArgs) {
    paths.AddUserPath(p);
289
  }
290
  paths.AddSuffixes(this->SearchPathSuffixes);
Bill Hoffman's avatar
Bill Hoffman committed
291 292 293 294
}

void cmFindBase::PrintFindStuff()
{
295 296 297
  std::cerr << "SearchFrameworkLast: " << this->SearchFrameworkLast << "\n";
  std::cerr << "SearchFrameworkOnly: " << this->SearchFrameworkOnly << "\n";
  std::cerr << "SearchFrameworkFirst: " << this->SearchFrameworkFirst << "\n";
298 299 300
  std::cerr << "SearchAppBundleLast: " << this->SearchAppBundleLast << "\n";
  std::cerr << "SearchAppBundleOnly: " << this->SearchAppBundleOnly << "\n";
  std::cerr << "SearchAppBundleFirst: " << this->SearchAppBundleFirst << "\n";
Bill Hoffman's avatar
Bill Hoffman committed
301
  std::cerr << "VariableName " << this->VariableName << "\n";
302
  std::cerr << "VariableDocumentation " << this->VariableDocumentation << "\n";
303
  std::cerr << "NoDefaultPath " << this->NoDefaultPath << "\n";
304 305
  std::cerr << "NoCMakeEnvironmentPath " << this->NoCMakeEnvironmentPath
            << "\n";
306
  std::cerr << "NoCMakePath " << this->NoCMakePath << "\n";
307 308
  std::cerr << "NoSystemEnvironmentPath " << this->NoSystemEnvironmentPath
            << "\n";
309
  std::cerr << "NoCMakeSystemPath " << this->NoCMakeSystemPath << "\n";
Bill Hoffman's avatar
Bill Hoffman committed
310 311
  std::cerr << "EnvironmentPath " << this->EnvironmentPath << "\n";
  std::cerr << "CMakePathName " << this->CMakePathName << "\n";
Stephen Kelly's avatar
Stephen Kelly committed
312
  std::cerr << "Names  " << cmJoin(this->Names, " ") << "\n";
Bill Hoffman's avatar
Bill Hoffman committed
313 314
  std::cerr << "\n";
  std::cerr << "SearchPathSuffixes  ";
Stephen Kelly's avatar
Stephen Kelly committed
315
  std::cerr << cmJoin(this->SearchPathSuffixes, "\n") << "\n";
Bill Hoffman's avatar
Bill Hoffman committed
316
  std::cerr << "SearchPaths\n";
317
  std::cerr << cmWrap("[", this->SearchPaths, "]", "\n") << "\n";
Bill Hoffman's avatar
Bill Hoffman committed
318 319 320 321
}

bool cmFindBase::CheckForVariableInCache()
{
322 323
  if (const char* cacheValue =
        this->Makefile->GetDefinition(this->VariableName)) {
Stephen Kelly's avatar
Stephen Kelly committed
324 325
    cmState* state = this->Makefile->GetState();
    const char* cacheEntry = state->GetCacheEntryValue(this->VariableName);
326
    bool found = !cmSystemTools::IsNOTFOUND(cacheValue);
Daniel Pfeifer's avatar
Daniel Pfeifer committed
327
    bool cached = cacheEntry != nullptr;
328
    if (found) {
329 330 331 332
      // If the user specifies the entry on the command line without a
      // type we should add the type and docstring but keep the
      // original value.  Tell the subclass implementations to do
      // this.
333 334
      if (cached &&
          state->GetCacheEntryType(this->VariableName) ==
335
            cmStateEnums::UNINITIALIZED) {
336
        this->AlreadyInCacheWithoutMetaInfo = true;
Bill Hoffman's avatar
Bill Hoffman committed
337
      }
338
      return true;
Daniel Pfeifer's avatar
Daniel Pfeifer committed
339 340
    }
    if (cached) {
341 342 343
      const char* hs =
        state->GetCacheEntryProperty(this->VariableName, "HELPSTRING");
      this->VariableDocumentation = hs ? hs : "(none)";
Bill Hoffman's avatar
Bill Hoffman committed
344
    }
345
  }
Bill Hoffman's avatar
Bill Hoffman committed
346 347
  return false;
}