cmake/Source/cmIncludeDirectoryCommand.cxx

136 lines
3.7 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 "cmIncludeDirectoryCommand.h"
2017-04-14 19:02:05 +02:00
#include <algorithm>
#include <set>
#include "cmMakefile.h"
#include "cmSystemTools.h"
class cmExecutionStatus;
// cmIncludeDirectoryCommand
2016-07-09 11:21:54 +02:00
bool cmIncludeDirectoryCommand::InitialPass(
std::vector<std::string> const& args, cmExecutionStatus&)
{
2016-10-30 18:24:19 +01:00
if (args.empty()) {
return true;
2016-07-09 11:21:54 +02:00
}
std::vector<std::string>::const_iterator i = args.begin();
bool before = this->Makefile->IsOn("CMAKE_INCLUDE_DIRECTORIES_BEFORE");
bool system = false;
2016-07-09 11:21:54 +02:00
if ((*i) == "BEFORE") {
before = true;
++i;
2016-07-09 11:21:54 +02:00
} else if ((*i) == "AFTER") {
before = false;
++i;
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
std::vector<std::string> beforeIncludes;
std::vector<std::string> afterIncludes;
2015-04-27 22:25:09 +02:00
std::set<std::string> systemIncludes;
2013-03-16 19:13:01 +02:00
2016-07-09 11:21:54 +02:00
for (; i != args.end(); ++i) {
if (*i == "SYSTEM") {
system = true;
continue;
2016-07-09 11:21:54 +02:00
}
if (i->empty()) {
this->SetError("given empty-string as include directory.");
return false;
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
std::vector<std::string> includes;
2014-08-03 19:52:23 +02:00
this->GetIncludes(*i, includes);
2016-07-09 11:21:54 +02:00
if (before) {
beforeIncludes.insert(beforeIncludes.end(), includes.begin(),
2013-03-16 19:13:01 +02:00
includes.end());
2016-07-09 11:21:54 +02:00
} else {
afterIncludes.insert(afterIncludes.end(), includes.begin(),
2013-03-16 19:13:01 +02:00
includes.end());
2016-07-09 11:21:54 +02:00
}
if (system) {
2015-04-27 22:25:09 +02:00
systemIncludes.insert(includes.begin(), includes.end());
}
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
std::reverse(beforeIncludes.begin(), beforeIncludes.end());
this->Makefile->AddIncludeDirectories(afterIncludes);
this->Makefile->AddIncludeDirectories(beforeIncludes, before);
this->Makefile->AddSystemIncludeDirectories(systemIncludes);
return true;
}
2016-07-09 11:21:54 +02:00
static bool StartsWithGeneratorExpression(const std::string& input)
2013-03-16 19:13:01 +02:00
{
return input[0] == '$' && input[1] == '<';
}
// do a lot of cleanup on the arguments because this is one place where folks
// sometimes take the output of a program and pass it directly into this
// command not thinking that a single argument could be filled with spaces
// and newlines etc liek below:
//
// " /foo/bar
// /boo/hoo /dingle/berry "
//
2010-11-13 01:00:53 +02:00
// ideally that should be three separate arguments but when sucking the
// output from a program and passing it into a command the cleanup doesn't
// always happen
//
2016-07-09 11:21:54 +02:00
void cmIncludeDirectoryCommand::GetIncludes(const std::string& arg,
std::vector<std::string>& incs)
{
// break apart any line feed arguments
std::string::size_type pos = 0;
2013-03-16 19:13:01 +02:00
std::string::size_type lastPos = 0;
2016-07-09 11:21:54 +02:00
while ((pos = arg.find('\n', lastPos)) != std::string::npos) {
if (pos) {
std::string inc = arg.substr(lastPos, pos);
2013-03-16 19:13:01 +02:00
this->NormalizeInclude(inc);
2016-07-09 11:21:54 +02:00
if (!inc.empty()) {
2013-06-16 00:40:25 +03:00
incs.push_back(inc);
}
}
2016-07-09 11:21:54 +02:00
lastPos = pos + 1;
}
2013-03-16 19:13:01 +02:00
std::string inc = arg.substr(lastPos);
this->NormalizeInclude(inc);
2016-07-09 11:21:54 +02:00
if (!inc.empty()) {
2013-06-16 00:40:25 +03:00
incs.push_back(inc);
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
}
2016-07-09 11:21:54 +02:00
void cmIncludeDirectoryCommand::NormalizeInclude(std::string& inc)
2013-03-16 19:13:01 +02:00
{
std::string::size_type b = inc.find_first_not_of(" \r");
std::string::size_type e = inc.find_last_not_of(" \r");
2017-07-20 19:35:53 +02:00
if ((b != std::string::npos) && (e != std::string::npos)) {
2016-07-09 11:21:54 +02:00
inc.assign(inc, b, 1 + e - b); // copy the remaining substring
} else {
2013-06-16 00:40:25 +03:00
inc = "";
return;
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
2016-07-09 11:21:54 +02:00
if (!cmSystemTools::IsOff(inc.c_str())) {
2013-03-16 19:13:01 +02:00
cmSystemTools::ConvertToUnixSlashes(inc);
2016-07-09 11:21:54 +02:00
if (!cmSystemTools::FileIsFullPath(inc.c_str())) {
if (!StartsWithGeneratorExpression(inc)) {
2015-08-17 11:37:30 +02:00
std::string tmp = this->Makefile->GetCurrentSourceDirectory();
2013-03-16 19:13:01 +02:00
tmp += "/";
tmp += inc;
inc = tmp;
}
}
2016-07-09 11:21:54 +02:00
}
}