cmake/Source/cmFindPathCommand.cxx

154 lines
4.5 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 "cmFindPathCommand.h"
2017-07-20 19:35:53 +02:00
#include "cmsys/Glob.hxx"
2017-04-14 19:02:05 +02:00
#include "cmMakefile.h"
#include "cmStateTypes.h"
#include "cmSystemTools.h"
class cmExecutionStatus;
cmFindPathCommand::cmFindPathCommand()
{
this->EnvironmentPath = "INCLUDE";
this->IncludeFileInPath = false;
2011-01-16 11:35:12 +01:00
}
// cmFindPathCommand
2016-07-09 11:21:54 +02:00
bool cmFindPathCommand::InitialPass(std::vector<std::string> const& argsIn,
cmExecutionStatus&)
{
this->VariableDocumentation = "Path to a file.";
this->CMakePathName = "INCLUDE";
2016-07-09 11:21:54 +02:00
if (!this->ParseArguments(argsIn)) {
return false;
2016-07-09 11:21:54 +02:00
}
if (this->AlreadyInCache) {
// 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.
2016-07-09 11:21:54 +02:00
if (this->AlreadyInCacheWithoutMetaInfo) {
this->Makefile->AddCacheDefinition(
2016-07-09 11:21:54 +02:00
this->VariableName, "", this->VariableDocumentation.c_str(),
2017-04-14 19:02:05 +02:00
(this->IncludeFileInPath ? cmStateEnums::FILEPATH
: cmStateEnums::PATH));
}
2016-07-09 11:21:54 +02:00
return true;
}
std::string result = this->FindHeader();
2016-07-09 11:21:54 +02:00
if (!result.empty()) {
this->Makefile->AddCacheDefinition(
this->VariableName, result.c_str(), this->VariableDocumentation.c_str(),
2017-04-14 19:02:05 +02:00
(this->IncludeFileInPath) ? cmStateEnums::FILEPATH : cmStateEnums::PATH);
return true;
2016-07-09 11:21:54 +02:00
}
this->Makefile->AddCacheDefinition(
this->VariableName, (this->VariableName + "-NOTFOUND").c_str(),
this->VariableDocumentation.c_str(),
2017-04-14 19:02:05 +02:00
(this->IncludeFileInPath) ? cmStateEnums::FILEPATH : cmStateEnums::PATH);
return true;
}
std::string cmFindPathCommand::FindHeader()
{
std::string header;
2016-07-09 11:21:54 +02:00
if (this->SearchFrameworkFirst || this->SearchFrameworkOnly) {
header = this->FindFrameworkHeader();
2016-07-09 11:21:54 +02:00
}
if (header.empty() && !this->SearchFrameworkOnly) {
header = this->FindNormalHeader();
2016-07-09 11:21:54 +02:00
}
if (header.empty() && this->SearchFrameworkLast) {
header = this->FindFrameworkHeader();
2016-07-09 11:21:54 +02:00
}
return header;
}
2016-07-09 11:21:54 +02:00
std::string cmFindPathCommand::FindHeaderInFramework(std::string const& file,
std::string const& dir)
{
2015-04-27 22:25:09 +02:00
std::string fileName = file;
std::string frameWorkName;
2017-04-14 19:02:05 +02:00
std::string::size_type pos = fileName.find('/');
// if there is a / in the name try to find the header as a framework
// For example bar/foo.h would look for:
// bar.framework/Headers/foo.h
2017-07-20 19:35:53 +02:00
if (pos != std::string::npos) {
// remove the name from the slash;
2016-07-09 11:21:54 +02:00
fileName = fileName.substr(pos + 1);
frameWorkName = file;
2013-03-16 19:13:01 +02:00
frameWorkName =
2016-07-09 11:21:54 +02:00
frameWorkName.substr(0, frameWorkName.size() - fileName.size() - 1);
// if the framework has a path in it then just use the filename
2017-07-20 19:35:53 +02:00
if (frameWorkName.find('/') != std::string::npos) {
fileName = file;
2018-01-26 17:06:56 +01:00
frameWorkName.clear();
2016-07-09 11:21:54 +02:00
}
if (!frameWorkName.empty()) {
std::string fpath = dir;
fpath += frameWorkName;
fpath += ".framework";
std::string intPath = fpath;
intPath += "/Headers/";
intPath += fileName;
2018-04-23 21:13:27 +02:00
if (cmSystemTools::FileExists(intPath)) {
2016-07-09 11:21:54 +02:00
if (this->IncludeFileInPath) {
return intPath;
}
2016-07-09 11:21:54 +02:00
return fpath;
}
}
2016-07-09 11:21:54 +02:00
}
// if it is not found yet or not a framework header, then do a glob search
// for all frameworks in the directory: dir/*.framework/Headers/<file>
2015-04-27 22:25:09 +02:00
std::string glob = dir;
glob += "*.framework/Headers/";
glob += file;
cmsys::Glob globIt;
globIt.FindFiles(glob);
std::vector<std::string> files = globIt.GetFiles();
2016-07-09 11:21:54 +02:00
if (!files.empty()) {
2015-04-27 22:25:09 +02:00
std::string fheader = cmSystemTools::CollapseFullPath(files[0]);
2016-07-09 11:21:54 +02:00
if (this->IncludeFileInPath) {
return fheader;
2016-07-09 11:21:54 +02:00
}
2016-10-30 18:24:19 +01:00
fheader.resize(fheader.size() - file.size());
return fheader;
2016-07-09 11:21:54 +02:00
}
return "";
}
std::string cmFindPathCommand::FindNormalHeader()
{
std::string tryPath;
2018-01-26 17:06:56 +01:00
for (std::string const& n : this->Names) {
for (std::string const& sp : this->SearchPaths) {
tryPath = sp;
tryPath += n;
2018-04-23 21:13:27 +02:00
if (cmSystemTools::FileExists(tryPath)) {
2016-07-09 11:21:54 +02:00
if (this->IncludeFileInPath) {
return tryPath;
}
2018-01-26 17:06:56 +01:00
return sp;
}
}
2016-07-09 11:21:54 +02:00
}
return "";
}
std::string cmFindPathCommand::FindFrameworkHeader()
{
2018-01-26 17:06:56 +01:00
for (std::string const& n : this->Names) {
for (std::string const& sp : this->SearchPaths) {
std::string fwPath = this->FindHeaderInFramework(n, sp);
2016-07-09 11:21:54 +02:00
if (!fwPath.empty()) {
return fwPath;
}
}
2016-07-09 11:21:54 +02:00
}
return "";
}