cmake/Source/cmConfigureFileCommand.cxx

215 lines
6.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 "cmConfigureFileCommand.h"
2020-08-30 11:54:41 +02:00
#include <set>
2021-09-14 00:13:48 +02:00
#include <sstream>
2020-08-30 11:54:41 +02:00
#include <cm/string_view>
#include <cmext/string_view>
2021-09-14 00:13:48 +02:00
#include <sys/types.h>
2020-02-01 23:06:01 +01:00
#include "cmExecutionStatus.h"
2021-09-14 00:13:48 +02:00
#include "cmFSPermissions.h"
2017-04-14 19:02:05 +02:00
#include "cmMakefile.h"
2019-11-11 23:01:05 +01:00
#include "cmMessageType.h"
2020-02-01 23:06:01 +01:00
#include "cmNewLineStyle.h"
#include "cmStringAlgorithms.h"
2017-04-14 19:02:05 +02:00
#include "cmSystemTools.h"
// cmConfigureFileCommand
2020-02-01 23:06:01 +01:00
bool cmConfigureFileCommand(std::vector<std::string> const& args,
cmExecutionStatus& status)
{
2016-07-09 11:21:54 +02:00
if (args.size() < 2) {
2020-02-01 23:06:01 +01:00
status.SetError("called with incorrect number of arguments, expected 2");
return false;
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
2017-07-20 19:35:53 +02:00
std::string const& inFile = args[0];
2020-02-01 23:06:01 +01:00
const std::string inputFile = cmSystemTools::CollapseFullPath(
inFile, status.GetMakefile().GetCurrentSourceDirectory());
2009-10-04 10:30:41 +03:00
// If the input location is a directory, error out.
2020-02-01 23:06:01 +01:00
if (cmSystemTools::FileIsDirectory(inputFile)) {
status.SetError(cmStrCat("input location\n ", inputFile,
"\n"
"is a directory but a file was expected."));
2009-10-04 10:30:41 +03:00
return false;
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
2017-07-20 19:35:53 +02:00
std::string const& outFile = args[1];
2020-02-01 23:06:01 +01:00
std::string outputFile = cmSystemTools::CollapseFullPath(
outFile, status.GetMakefile().GetCurrentBinaryDirectory());
2009-10-04 10:30:41 +03:00
// If the output location is already a directory put the file in it.
2020-02-01 23:06:01 +01:00
if (cmSystemTools::FileIsDirectory(outputFile)) {
outputFile += "/";
outputFile += cmSystemTools::GetFilenameName(inFile);
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
2020-02-01 23:06:01 +01:00
if (!status.GetMakefile().CanIWriteThisFile(outputFile)) {
std::string e = "attempted to configure a file: " + outputFile +
2016-07-09 11:21:54 +02:00
" into a source directory.";
2020-02-01 23:06:01 +01:00
status.SetError(e);
2022-08-04 22:12:04 +02:00
cmSystemTools::SetFatalErrorOccurred();
return false;
2016-07-09 11:21:54 +02:00
}
2012-02-18 12:40:36 +02:00
std::string errorMessage;
2020-02-01 23:06:01 +01:00
cmNewLineStyle newLineStyle;
if (!newLineStyle.ReadFromArguments(args, errorMessage)) {
status.SetError(errorMessage);
2012-02-18 12:40:36 +02:00
return false;
2016-07-09 11:21:54 +02:00
}
2020-02-01 23:06:01 +01:00
bool copyOnly = false;
bool escapeQuotes = false;
2021-09-14 00:13:48 +02:00
bool useSourcePermissions = false;
bool noSourcePermissions = false;
bool filePermissions = false;
std::vector<std::string> filePermissionOptions;
enum class Doing
{
DoingNone,
DoingFilePermissions,
DoneFilePermissions
};
Doing doing = Doing::DoingNone;
2020-08-30 11:54:41 +02:00
static std::set<cm::string_view> noopOptions = {
/* Legacy. */
"IMMEDIATE"_s,
/* Handled by NewLineStyle member. */
"NEWLINE_STYLE"_s,
"LF"_s,
"UNIX"_s,
"CRLF"_s,
"WIN32"_s,
"DOS"_s,
};
2015-04-27 22:25:09 +02:00
std::string unknown_args;
2020-02-01 23:06:01 +01:00
bool atOnly = false;
2016-07-09 11:21:54 +02:00
for (unsigned int i = 2; i < args.size(); ++i) {
if (args[i] == "COPYONLY") {
2021-09-14 00:13:48 +02:00
if (doing == Doing::DoingFilePermissions) {
doing = Doing::DoneFilePermissions;
}
2020-02-01 23:06:01 +01:00
copyOnly = true;
if (newLineStyle.IsValid()) {
status.SetError("COPYONLY could not be used in combination "
"with NEWLINE_STYLE");
2012-02-18 12:40:36 +02:00
return false;
}
2016-07-09 11:21:54 +02:00
} else if (args[i] == "ESCAPE_QUOTES") {
2021-09-14 00:13:48 +02:00
if (doing == Doing::DoingFilePermissions) {
doing = Doing::DoneFilePermissions;
}
2020-02-01 23:06:01 +01:00
escapeQuotes = true;
2016-07-09 11:21:54 +02:00
} else if (args[i] == "@ONLY") {
2021-09-14 00:13:48 +02:00
if (doing == Doing::DoingFilePermissions) {
doing = Doing::DoneFilePermissions;
}
2020-02-01 23:06:01 +01:00
atOnly = true;
2021-09-14 00:13:48 +02:00
} else if (args[i] == "NO_SOURCE_PERMISSIONS") {
if (doing == Doing::DoingFilePermissions) {
status.SetError(" given both FILE_PERMISSIONS and "
"NO_SOURCE_PERMISSIONS. Only one option allowed.");
return false;
}
noSourcePermissions = true;
} else if (args[i] == "USE_SOURCE_PERMISSIONS") {
if (doing == Doing::DoingFilePermissions) {
status.SetError(" given both FILE_PERMISSIONS and "
"USE_SOURCE_PERMISSIONS. Only one option allowed.");
return false;
}
useSourcePermissions = true;
} else if (args[i] == "FILE_PERMISSIONS") {
if (useSourcePermissions) {
status.SetError(" given both FILE_PERMISSIONS and "
"USE_SOURCE_PERMISSIONS. Only one option allowed.");
return false;
}
if (noSourcePermissions) {
status.SetError(" given both FILE_PERMISSIONS and "
"NO_SOURCE_PERMISSIONS. Only one option allowed.");
return false;
}
if (doing == Doing::DoingNone) {
doing = Doing::DoingFilePermissions;
filePermissions = true;
}
2020-08-30 11:54:41 +02:00
} else if (noopOptions.find(args[i]) != noopOptions.end()) {
/* Ignore no-op options. */
2021-09-14 00:13:48 +02:00
} else if (doing == Doing::DoingFilePermissions) {
filePermissionOptions.push_back(args[i]);
2016-07-09 11:21:54 +02:00
} else {
2015-04-27 22:25:09 +02:00
unknown_args += " ";
unknown_args += args[i];
unknown_args += "\n";
}
2016-07-09 11:21:54 +02:00
}
if (!unknown_args.empty()) {
2020-02-01 23:06:01 +01:00
std::string msg = cmStrCat(
"configure_file called with unknown argument(s):\n", unknown_args);
status.GetMakefile().IssueMessage(MessageType::AUTHOR_WARNING, msg);
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
2021-09-14 00:13:48 +02:00
if (useSourcePermissions && noSourcePermissions) {
status.SetError(" given both USE_SOURCE_PERMISSIONS and "
"NO_SOURCE_PERMISSIONS. Only one option allowed.");
return false;
}
mode_t permissions = 0;
if (filePermissions) {
if (filePermissionOptions.empty()) {
status.SetError(" given FILE_PERMISSIONS without any options.");
return false;
}
std::vector<std::string> invalidOptions;
for (auto const& e : filePermissionOptions) {
if (!cmFSPermissions::stringToModeT(e, permissions)) {
invalidOptions.push_back(e);
}
}
if (!invalidOptions.empty()) {
std::ostringstream oss;
oss << " given invalid permission ";
for (auto i = 0u; i < invalidOptions.size(); i++) {
if (i == 0u) {
oss << "\"" << invalidOptions[i] << "\"";
} else {
oss << ",\"" << invalidOptions[i] << "\"";
}
}
oss << ".";
status.SetError(oss.str());
return false;
}
}
if (noSourcePermissions) {
permissions |= cmFSPermissions::mode_owner_read;
permissions |= cmFSPermissions::mode_owner_write;
permissions |= cmFSPermissions::mode_group_read;
permissions |= cmFSPermissions::mode_world_read;
}
if (!status.GetMakefile().ConfigureFile(inputFile, outputFile, copyOnly,
atOnly, escapeQuotes, permissions,
newLineStyle)) {
2020-02-01 23:06:01 +01:00
status.SetError("Problem configuring file");
2014-08-03 19:52:23 +02:00
return false;
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
return true;
}