cmake/Source/cmAddSubDirectoryCommand.cxx

112 lines
3.4 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 "cmAddSubDirectoryCommand.h"
2017-04-14 19:02:05 +02:00
#include <sstream>
#include <string.h>
#include "cmMakefile.h"
#include "cmSystemTools.h"
class cmExecutionStatus;
// cmAddSubDirectoryCommand
2016-07-09 11:21:54 +02:00
bool cmAddSubDirectoryCommand::InitialPass(
std::vector<std::string> const& args, cmExecutionStatus&)
{
2016-10-30 18:24:19 +01:00
if (args.empty()) {
this->SetError("called with incorrect number of arguments");
return false;
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
// store the binpath
2017-07-20 19:35:53 +02:00
std::string const& srcArg = args[0];
std::string binArg;
2013-03-16 19:13:01 +02:00
bool excludeFromAll = false;
// process the rest of the arguments looking for optional args
std::vector<std::string>::const_iterator i = args.begin();
++i;
2016-07-09 11:21:54 +02:00
for (; i != args.end(); ++i) {
if (*i == "EXCLUDE_FROM_ALL") {
excludeFromAll = true;
continue;
2017-07-20 19:35:53 +02:00
}
if (binArg.empty()) {
binArg = *i;
2016-07-09 11:21:54 +02:00
} else {
this->SetError("called with incorrect number of arguments");
return false;
}
2016-07-09 11:21:54 +02:00
}
// Compute the full path to the specified source directory.
// Interpret a relative path with respect to the current source directory.
std::string srcPath;
2016-07-09 11:21:54 +02:00
if (cmSystemTools::FileIsFullPath(srcArg.c_str())) {
srcPath = srcArg;
2016-07-09 11:21:54 +02:00
} else {
2015-08-17 11:37:30 +02:00
srcPath = this->Makefile->GetCurrentSourceDirectory();
srcPath += "/";
srcPath += srcArg;
2016-07-09 11:21:54 +02:00
}
if (!cmSystemTools::FileIsDirectory(srcPath)) {
std::string error = "given source \"";
error += srcArg;
error += "\" which is not an existing directory.";
2015-04-27 22:25:09 +02:00
this->SetError(error);
return false;
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
srcPath = cmSystemTools::CollapseFullPath(srcPath);
// Compute the full path to the binary directory.
std::string binPath;
2016-07-09 11:21:54 +02:00
if (binArg.empty()) {
// No binary directory was specified. If the source directory is
// not a subdirectory of the current directory then it is an
// error.
2016-07-09 11:21:54 +02:00
if (!cmSystemTools::IsSubDirectory(
srcPath, this->Makefile->GetCurrentSourceDirectory())) {
2015-04-27 22:25:09 +02:00
std::ostringstream e;
e << "not given a binary directory but the given source directory "
<< "\"" << srcPath << "\" is not a subdirectory of \""
2015-08-17 11:37:30 +02:00
<< this->Makefile->GetCurrentSourceDirectory() << "\". "
<< "When specifying an out-of-tree source a binary directory "
<< "must be explicitly specified.";
2015-04-27 22:25:09 +02:00
this->SetError(e.str());
return false;
2016-07-09 11:21:54 +02:00
}
// Remove the CurrentDirectory from the srcPath and replace it
// with the CurrentOutputDirectory.
2015-08-17 11:37:30 +02:00
const char* src = this->Makefile->GetCurrentSourceDirectory();
const char* bin = this->Makefile->GetCurrentBinaryDirectory();
2013-03-16 19:13:01 +02:00
size_t srcLen = strlen(src);
size_t binLen = strlen(bin);
2016-07-09 11:21:54 +02:00
if (srcLen > 0 && src[srcLen - 1] == '/') {
--srcLen;
}
2016-07-09 11:21:54 +02:00
if (binLen > 0 && bin[binLen - 1] == '/') {
--binLen;
}
binPath = std::string(bin, binLen) + srcPath.substr(srcLen);
} else {
// Use the binary directory specified.
// Interpret a relative path with respect to the current binary directory.
2016-07-09 11:21:54 +02:00
if (cmSystemTools::FileIsFullPath(binArg.c_str())) {
binPath = binArg;
2016-07-09 11:21:54 +02:00
} else {
2015-08-17 11:37:30 +02:00
binPath = this->Makefile->GetCurrentBinaryDirectory();
binPath += "/";
binPath += binArg;
}
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
binPath = cmSystemTools::CollapseFullPath(binPath);
// Add the subdirectory using the computed full paths.
2016-07-09 11:21:54 +02:00
this->Makefile->AddSubDirectory(srcPath, binPath, excludeFromAll, true);
return true;
}