cmake/Source/cmFindBase.cxx

359 lines
11 KiB
C++
Raw Normal View History

2016-10-30 18:24:19 +01:00
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
#include "cmFindBase.h"
2013-03-16 19:13:01 +02:00
2017-07-20 19:35:53 +02:00
#include "cmConfigure.h"
#include <deque>
2017-04-14 19:02:05 +02:00
#include <iostream>
2017-07-20 19:35:53 +02:00
#include <iterator>
2017-04-14 19:02:05 +02:00
#include <map>
#include <stddef.h>
2015-08-17 11:37:30 +02:00
#include "cmAlgorithms.h"
2017-04-14 19:02:05 +02:00
#include "cmMakefile.h"
#include "cmSearchPath.h"
2015-08-17 11:37:30 +02:00
#include "cmState.h"
2017-04-14 19:02:05 +02:00
#include "cmStateTypes.h"
#include "cmSystemTools.h"
2015-08-17 11:37:30 +02:00
cmFindBase::cmFindBase()
{
this->AlreadyInCache = false;
this->AlreadyInCacheWithoutMetaInfo = false;
2013-03-16 19:13:01 +02:00
this->NamesPerDir = false;
this->NamesPerDirAllowed = false;
2011-01-16 11:35:12 +01:00
}
bool cmFindBase::ParseArguments(std::vector<std::string> const& argsIn)
{
2016-07-09 11:21:54 +02:00
if (argsIn.size() < 2) {
this->SetError("called with incorrect number of arguments");
return false;
2016-07-09 11:21:54 +02:00
}
// copy argsIn into args so it can be modified,
2013-03-16 19:13:01 +02:00
// in the process extract the DOC "documentation"
size_t size = argsIn.size();
std::vector<std::string> args;
bool foundDoc = false;
2016-07-09 11:21:54 +02:00
for (unsigned int j = 0; j < size; ++j) {
if (foundDoc || argsIn[j] != "DOC") {
if (argsIn[j] == "ENV") {
if (j + 1 < size) {
j++;
cmSystemTools::GetPath(args, argsIn[j].c_str());
}
2016-07-09 11:21:54 +02:00
} else {
args.push_back(argsIn[j]);
}
2016-07-09 11:21:54 +02:00
} else {
if (j + 1 < size) {
foundDoc = true;
2016-07-09 11:21:54 +02:00
this->VariableDocumentation = argsIn[j + 1];
j++;
2016-07-09 11:21:54 +02:00
if (j >= size) {
break;
}
}
}
2016-07-09 11:21:54 +02:00
}
if (args.size() < 2) {
2011-01-16 11:35:12 +01:00
this->SetError("called with incorrect number of arguments");
return false;
2016-07-09 11:21:54 +02:00
}
this->VariableName = args[0];
2016-07-09 11:21:54 +02:00
if (this->CheckForVariableInCache()) {
this->AlreadyInCache = true;
return true;
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
this->AlreadyInCache = false;
2017-08-11 09:33:23 +02:00
this->SelectDefaultNoPackageRootPath();
// Find the current root path mode.
this->SelectDefaultRootPathMode();
// Find the current bundle/framework search policy.
this->SelectDefaultMacMode();
bool newStyle = false;
2016-07-09 11:21:54 +02:00
enum Doing
{
DoingNone,
DoingNames,
DoingPaths,
DoingPathSuffixes,
DoingHints
};
Doing doing = DoingNames; // assume it starts with a name
2016-07-09 11:21:54 +02:00
for (unsigned int j = 1; j < args.size(); ++j) {
if (args[j] == "NAMES") {
doing = DoingNames;
newStyle = true;
2016-07-09 11:21:54 +02:00
} else if (args[j] == "PATHS") {
doing = DoingPaths;
newStyle = true;
2016-07-09 11:21:54 +02:00
} else if (args[j] == "HINTS") {
doing = DoingHints;
newStyle = true;
2016-07-09 11:21:54 +02:00
} else if (args[j] == "PATH_SUFFIXES") {
doing = DoingPathSuffixes;
newStyle = true;
2016-07-09 11:21:54 +02:00
} else if (args[j] == "NAMES_PER_DIR") {
2013-03-16 19:13:01 +02:00
doing = DoingNone;
2016-07-09 11:21:54 +02:00
if (this->NamesPerDirAllowed) {
2013-03-16 19:13:01 +02:00
this->NamesPerDir = true;
2016-07-09 11:21:54 +02:00
} else {
2013-03-16 19:13:01 +02:00
this->SetError("does not support NAMES_PER_DIR");
return false;
}
2016-07-09 11:21:54 +02:00
} else if (args[j] == "NO_SYSTEM_PATH") {
doing = DoingNone;
this->NoDefaultPath = true;
2016-07-09 11:21:54 +02:00
} else if (this->CheckCommonArgument(args[j])) {
doing = DoingNone;
// 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.
2016-07-09 11:21:54 +02:00
} else if (doing == DoingNames) {
this->Names.push_back(args[j]);
2016-07-09 11:21:54 +02:00
} else if (doing == DoingPaths) {
2015-04-27 22:25:09 +02:00
this->UserGuessArgs.push_back(args[j]);
2016-07-09 11:21:54 +02:00
} else if (doing == DoingHints) {
2015-04-27 22:25:09 +02:00
this->UserHintsArgs.push_back(args[j]);
2016-07-09 11:21:54 +02:00
} else if (doing == DoingPathSuffixes) {
this->AddPathSuffix(args[j]);
}
2016-07-09 11:21:54 +02:00
}
2016-07-09 11:21:54 +02:00
if (this->VariableDocumentation.empty()) {
this->VariableDocumentation = "Where can ";
2016-07-09 11:21:54 +02:00
if (this->Names.empty()) {
this->VariableDocumentation += "the (unknown) library be found";
2016-07-09 11:21:54 +02:00
} else if (this->Names.size() == 1) {
this->VariableDocumentation +=
"the " + this->Names[0] + " library be found";
} else {
2015-08-17 11:37:30 +02:00
this->VariableDocumentation += "one of the ";
2015-11-17 17:22:37 +01:00
this->VariableDocumentation +=
cmJoin(cmMakeRange(this->Names).retreat(1), ", ");
2016-07-09 11:21:54 +02:00
this->VariableDocumentation +=
" or " + this->Names[this->Names.size() - 1] + " libraries be found";
}
2016-07-09 11:21:54 +02:00
}
// look for old style
// FIND_*(VAR name path1 path2 ...)
2016-07-09 11:21:54 +02:00
if (!newStyle) {
// All the short-hand arguments have been recorded as names.
std::vector<std::string> shortArgs = this->Names;
this->Names.clear(); // clear out any values in Names
this->Names.push_back(shortArgs[0]);
2015-04-27 22:25:09 +02:00
this->UserGuessArgs.insert(this->UserGuessArgs.end(),
shortArgs.begin() + 1, shortArgs.end());
2016-07-09 11:21:54 +02:00
}
this->ExpandPaths();
2012-02-18 12:40:36 +02:00
this->ComputeFinalPaths();
return true;
}
void cmFindBase::ExpandPaths()
{
2016-07-09 11:21:54 +02:00
if (!this->NoDefaultPath) {
2017-07-20 19:35:53 +02:00
if (!this->NoPackageRootPath) {
this->FillPackageRootPath();
}
2016-07-09 11:21:54 +02:00
if (!this->NoCMakePath) {
2015-04-27 22:25:09 +02:00
this->FillCMakeVariablePath();
2016-07-09 11:21:54 +02:00
}
if (!this->NoCMakeEnvironmentPath) {
2015-04-27 22:25:09 +02:00
this->FillCMakeEnvironmentPath();
2015-09-20 20:53:24 +02:00
}
2016-07-09 11:21:54 +02:00
}
2015-09-20 20:53:24 +02:00
this->FillUserHintsPath();
2016-07-09 11:21:54 +02:00
if (!this->NoDefaultPath) {
if (!this->NoSystemEnvironmentPath) {
2015-04-27 22:25:09 +02:00
this->FillSystemEnvironmentPath();
2016-07-09 11:21:54 +02:00
}
if (!this->NoCMakeSystemPath) {
2015-04-27 22:25:09 +02:00
this->FillCMakeSystemVariablePath();
}
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
this->FillUserGuessPath();
}
2015-04-27 22:25:09 +02:00
void cmFindBase::FillCMakeEnvironmentPath()
{
2016-07-09 11:21:54 +02:00
cmSearchPath& paths = this->LabeledPaths[PathLabel::CMakeEnvironment];
2015-04-27 22:25:09 +02:00
// Add CMAKE_*_PATH environment variables
std::string var = "CMAKE_";
var += this->CMakePathName;
var += "_PATH";
paths.AddEnvPrefixPath("CMAKE_PREFIX_PATH");
paths.AddEnvPath(var);
2016-07-09 11:21:54 +02:00
if (this->CMakePathName == "PROGRAM") {
2015-04-27 22:25:09 +02:00
paths.AddEnvPath("CMAKE_APPBUNDLE_PATH");
2016-07-09 11:21:54 +02:00
} else {
2015-04-27 22:25:09 +02:00
paths.AddEnvPath("CMAKE_FRAMEWORK_PATH");
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
paths.AddSuffixes(this->SearchPathSuffixes);
}
2017-07-20 19:35:53 +02:00
void cmFindBase::FillPackageRootPath()
{
cmSearchPath& paths = this->LabeledPaths[PathLabel::PackageRoot];
// Add package specific search prefixes
// NOTE: This should be using const_reverse_iterator but HP aCC and
// Oracle sunCC both currently have standard library issues
// with the reverse iterator APIs.
for (std::deque<std::string>::reverse_iterator pkg =
this->Makefile->FindPackageModuleStack.rbegin();
pkg != this->Makefile->FindPackageModuleStack.rend(); ++pkg) {
std::string varName = *pkg + "_ROOT";
paths.AddCMakePrefixPath(varName);
paths.AddEnvPrefixPath(varName);
}
paths.AddSuffixes(this->SearchPathSuffixes);
}
2015-04-27 22:25:09 +02:00
void cmFindBase::FillCMakeVariablePath()
{
2016-07-09 11:21:54 +02:00
cmSearchPath& paths = this->LabeledPaths[PathLabel::CMake];
2015-04-27 22:25:09 +02:00
// Add CMake varibles of the same name as the previous environment
// varibles CMAKE_*_PATH to be used most of the time with -D
// command line options
std::string var = "CMAKE_";
var += this->CMakePathName;
var += "_PATH";
paths.AddCMakePrefixPath("CMAKE_PREFIX_PATH");
paths.AddCMakePath(var);
2016-07-09 11:21:54 +02:00
if (this->CMakePathName == "PROGRAM") {
2015-04-27 22:25:09 +02:00
paths.AddCMakePath("CMAKE_APPBUNDLE_PATH");
2016-07-09 11:21:54 +02:00
} else {
2015-04-27 22:25:09 +02:00
paths.AddCMakePath("CMAKE_FRAMEWORK_PATH");
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
paths.AddSuffixes(this->SearchPathSuffixes);
}
2015-04-27 22:25:09 +02:00
void cmFindBase::FillSystemEnvironmentPath()
{
2016-07-09 11:21:54 +02:00
cmSearchPath& paths = this->LabeledPaths[PathLabel::SystemEnvironment];
2015-04-27 22:25:09 +02:00
// Add LIB or INCLUDE
2016-07-09 11:21:54 +02:00
if (!this->EnvironmentPath.empty()) {
2015-04-27 22:25:09 +02:00
paths.AddEnvPath(this->EnvironmentPath);
2016-07-09 11:21:54 +02:00
#if defined(_WIN32) || defined(__CYGWIN__)
2015-08-17 11:37:30 +02:00
paths.AddEnvPrefixPath("PATH", true);
2016-07-09 11:21:54 +02:00
#endif
}
2016-08-20 09:57:23 +02:00
// Add PATH
paths.AddEnvPath("PATH");
2015-04-27 22:25:09 +02:00
paths.AddSuffixes(this->SearchPathSuffixes);
}
2015-04-27 22:25:09 +02:00
void cmFindBase::FillCMakeSystemVariablePath()
{
2016-07-09 11:21:54 +02:00
cmSearchPath& paths = this->LabeledPaths[PathLabel::CMakeSystem];
2015-04-27 22:25:09 +02:00
std::string var = "CMAKE_SYSTEM_";
var += this->CMakePathName;
var += "_PATH";
paths.AddCMakePrefixPath("CMAKE_SYSTEM_PREFIX_PATH");
paths.AddCMakePath(var);
2016-07-09 11:21:54 +02:00
if (this->CMakePathName == "PROGRAM") {
2015-04-27 22:25:09 +02:00
paths.AddCMakePath("CMAKE_SYSTEM_APPBUNDLE_PATH");
2016-07-09 11:21:54 +02:00
} else {
2015-04-27 22:25:09 +02:00
paths.AddCMakePath("CMAKE_SYSTEM_FRAMEWORK_PATH");
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
paths.AddSuffixes(this->SearchPathSuffixes);
}
2015-04-27 22:25:09 +02:00
void cmFindBase::FillUserHintsPath()
{
2016-07-09 11:21:54 +02:00
cmSearchPath& paths = this->LabeledPaths[PathLabel::Hints];
2016-07-09 11:21:54 +02:00
for (std::vector<std::string>::const_iterator p =
this->UserHintsArgs.begin();
p != this->UserHintsArgs.end(); ++p) {
2015-04-27 22:25:09 +02:00
paths.AddUserPath(*p);
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
paths.AddSuffixes(this->SearchPathSuffixes);
}
2015-04-27 22:25:09 +02:00
void cmFindBase::FillUserGuessPath()
{
2016-07-09 11:21:54 +02:00
cmSearchPath& paths = this->LabeledPaths[PathLabel::Guess];
2015-04-27 22:25:09 +02:00
2016-07-09 11:21:54 +02:00
for (std::vector<std::string>::const_iterator p =
this->UserGuessArgs.begin();
p != this->UserGuessArgs.end(); ++p) {
2015-04-27 22:25:09 +02:00
paths.AddUserPath(*p);
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
paths.AddSuffixes(this->SearchPathSuffixes);
}
void cmFindBase::PrintFindStuff()
{
std::cerr << "SearchFrameworkLast: " << this->SearchFrameworkLast << "\n";
std::cerr << "SearchFrameworkOnly: " << this->SearchFrameworkOnly << "\n";
std::cerr << "SearchFrameworkFirst: " << this->SearchFrameworkFirst << "\n";
std::cerr << "SearchAppBundleLast: " << this->SearchAppBundleLast << "\n";
std::cerr << "SearchAppBundleOnly: " << this->SearchAppBundleOnly << "\n";
std::cerr << "SearchAppBundleFirst: " << this->SearchAppBundleFirst << "\n";
std::cerr << "VariableName " << this->VariableName << "\n";
2016-07-09 11:21:54 +02:00
std::cerr << "VariableDocumentation " << this->VariableDocumentation << "\n";
std::cerr << "NoDefaultPath " << this->NoDefaultPath << "\n";
2016-07-09 11:21:54 +02:00
std::cerr << "NoCMakeEnvironmentPath " << this->NoCMakeEnvironmentPath
<< "\n";
std::cerr << "NoCMakePath " << this->NoCMakePath << "\n";
2016-07-09 11:21:54 +02:00
std::cerr << "NoSystemEnvironmentPath " << this->NoSystemEnvironmentPath
<< "\n";
std::cerr << "NoCMakeSystemPath " << this->NoCMakeSystemPath << "\n";
std::cerr << "EnvironmentPath " << this->EnvironmentPath << "\n";
std::cerr << "CMakePathName " << this->CMakePathName << "\n";
2015-08-17 11:37:30 +02:00
std::cerr << "Names " << cmJoin(this->Names, " ") << "\n";
std::cerr << "\n";
std::cerr << "SearchPathSuffixes ";
2015-08-17 11:37:30 +02:00
std::cerr << cmJoin(this->SearchPathSuffixes, "\n") << "\n";
std::cerr << "SearchPaths\n";
2015-08-17 11:37:30 +02:00
std::cerr << cmWrap("[", this->SearchPaths, "]", "\n") << "\n";
}
bool cmFindBase::CheckForVariableInCache()
{
2016-07-09 11:21:54 +02:00
if (const char* cacheValue =
this->Makefile->GetDefinition(this->VariableName)) {
2015-08-17 11:37:30 +02:00
cmState* state = this->Makefile->GetState();
const char* cacheEntry = state->GetCacheEntryValue(this->VariableName);
bool found = !cmSystemTools::IsNOTFOUND(cacheValue);
2017-04-14 19:02:05 +02:00
bool cached = cacheEntry != CM_NULLPTR;
2016-07-09 11:21:54 +02:00
if (found) {
// 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.
2016-07-09 11:21:54 +02:00
if (cached &&
state->GetCacheEntryType(this->VariableName) ==
2017-04-14 19:02:05 +02:00
cmStateEnums::UNINITIALIZED) {
this->AlreadyInCacheWithoutMetaInfo = true;
}
2016-07-09 11:21:54 +02:00
return true;
2016-10-30 18:24:19 +01:00
}
if (cached) {
2016-07-09 11:21:54 +02:00
const char* hs =
state->GetCacheEntryProperty(this->VariableName, "HELPSTRING");
this->VariableDocumentation = hs ? hs : "(none)";
}
2016-07-09 11:21:54 +02:00
}
return false;
}