cmake/Source/cmAddSubDirectoryCommand.cxx

119 lines
3.8 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"
2020-02-01 23:06:01 +01:00
#include <cstring>
2017-04-14 19:02:05 +02:00
2020-08-30 11:54:41 +02:00
#include <cm/string_view>
2020-02-01 23:06:01 +01:00
#include "cmExecutionStatus.h"
2017-04-14 19:02:05 +02:00
#include "cmMakefile.h"
2019-11-11 23:01:05 +01:00
#include "cmRange.h"
2020-02-01 23:06:01 +01:00
#include "cmStringAlgorithms.h"
2017-04-14 19:02:05 +02:00
#include "cmSystemTools.h"
2020-02-01 23:06:01 +01:00
bool cmAddSubDirectoryCommand(std::vector<std::string> const& args,
cmExecutionStatus& status)
{
2016-10-30 18:24:19 +01:00
if (args.empty()) {
2020-02-01 23:06:01 +01:00
status.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
2020-02-01 23:06:01 +01:00
cmMakefile& mf = status.GetMakefile();
// store the binpath
2019-11-11 23:01:05 +01:00
std::string const& srcArg = args.front();
std::string binArg;
2013-03-16 19:13:01 +02:00
bool excludeFromAll = false;
2022-11-16 20:14:03 +01:00
bool system = false;
// process the rest of the arguments looking for optional args
2019-11-11 23:01:05 +01:00
for (std::string const& arg : cmMakeRange(args).advance(1)) {
if (arg == "EXCLUDE_FROM_ALL") {
excludeFromAll = true;
continue;
2017-07-20 19:35:53 +02:00
}
2022-11-16 20:14:03 +01:00
if (arg == "SYSTEM") {
system = true;
continue;
}
2017-07-20 19:35:53 +02:00
if (binArg.empty()) {
2019-11-11 23:01:05 +01:00
binArg = arg;
2016-07-09 11:21:54 +02:00
} else {
2020-02-01 23:06:01 +01:00
status.SetError("called with incorrect number of arguments");
return false;
}
2016-07-09 11:21:54 +02:00
}
2022-11-16 20:14:03 +01:00
// "SYSTEM" directory property should also affects targets in nested
// subdirectories.
if (mf.GetPropertyAsBool("SYSTEM")) {
system = true;
}
// Compute the full path to the specified source directory.
// Interpret a relative path with respect to the current source directory.
std::string srcPath;
2018-04-23 21:13:27 +02:00
if (cmSystemTools::FileIsFullPath(srcArg)) {
srcPath = srcArg;
2016-07-09 11:21:54 +02:00
} else {
2020-02-01 23:06:01 +01:00
srcPath = cmStrCat(mf.GetCurrentSourceDirectory(), '/', srcArg);
2016-07-09 11:21:54 +02:00
}
if (!cmSystemTools::FileIsDirectory(srcPath)) {
2020-02-01 23:06:01 +01:00
std::string error = cmStrCat("given source \"", srcArg,
"\" which is not an existing directory.");
status.SetError(error);
return false;
2016-07-09 11:21:54 +02:00
}
2020-08-30 11:54:41 +02:00
srcPath =
cmSystemTools::CollapseFullPath(srcPath, mf.GetHomeOutputDirectory());
// 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.
2020-02-01 23:06:01 +01:00
if (!cmSystemTools::IsSubDirectory(srcPath,
mf.GetCurrentSourceDirectory())) {
status.SetError(
cmStrCat("not given a binary directory but the given source ",
"directory \"", srcPath, "\" is not a subdirectory of \"",
mf.GetCurrentSourceDirectory(),
"\". When specifying an "
"out-of-tree source a binary directory must be explicitly "
"specified."));
return false;
2016-07-09 11:21:54 +02:00
}
// Remove the CurrentDirectory from the srcPath and replace it
// with the CurrentOutputDirectory.
2020-02-01 23:06:01 +01:00
const std::string& src = mf.GetCurrentSourceDirectory();
const std::string& bin = mf.GetCurrentBinaryDirectory();
2018-10-28 12:09:07 +01:00
size_t srcLen = src.length();
size_t binLen = bin.length();
2019-11-11 23:01:05 +01:00
if (srcLen > 0 && src.back() == '/') {
2016-07-09 11:21:54 +02:00
--srcLen;
}
2019-11-11 23:01:05 +01:00
if (binLen > 0 && bin.back() == '/') {
2016-07-09 11:21:54 +02:00
--binLen;
}
2020-08-30 11:54:41 +02:00
binPath = cmStrCat(cm::string_view(bin).substr(0, binLen),
cm::string_view(srcPath).substr(srcLen));
2016-07-09 11:21:54 +02:00
} else {
// Use the binary directory specified.
// Interpret a relative path with respect to the current binary directory.
2018-04-23 21:13:27 +02:00
if (cmSystemTools::FileIsFullPath(binArg)) {
binPath = binArg;
2016-07-09 11:21:54 +02:00
} else {
2020-02-01 23:06:01 +01:00
binPath = cmStrCat(mf.GetCurrentBinaryDirectory(), '/', 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.
2022-11-16 20:14:03 +01:00
mf.AddSubDirectory(srcPath, binPath, excludeFromAll, true, system);
return true;
}