cmake/Source/cmSeparateArgumentsCommand.cxx

104 lines
2.6 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 "cmSeparateArgumentsCommand.h"
2017-04-14 19:02:05 +02:00
#include <algorithm>
2020-02-01 23:06:01 +01:00
#include "cmExecutionStatus.h"
2017-04-14 19:02:05 +02:00
#include "cmMakefile.h"
2020-02-01 23:06:01 +01:00
#include "cmStringAlgorithms.h"
2017-04-14 19:02:05 +02:00
#include "cmSystemTools.h"
// cmSeparateArgumentsCommand
2020-02-01 23:06:01 +01:00
bool cmSeparateArgumentsCommand(std::vector<std::string> const& args,
cmExecutionStatus& status)
{
2016-07-09 11:21:54 +02:00
if (args.empty()) {
2020-02-01 23:06:01 +01:00
status.SetError("must be given at least one argument.");
return false;
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
std::string var;
std::string command;
2016-07-09 11:21:54 +02:00
enum Mode
{
ModeOld,
ModeUnix,
ModeWindows
};
2009-10-04 10:30:41 +03:00
Mode mode = ModeOld;
2016-07-09 11:21:54 +02:00
enum Doing
{
DoingNone,
DoingVariable,
DoingMode,
DoingCommand
};
2009-10-04 10:30:41 +03:00
Doing doing = DoingVariable;
2018-01-26 17:06:56 +01:00
for (std::string const& arg : args) {
2016-07-09 11:21:54 +02:00
if (doing == DoingVariable) {
2018-01-26 17:06:56 +01:00
var = arg;
2009-10-04 10:30:41 +03:00
doing = DoingMode;
2020-08-30 11:54:41 +02:00
// This will always clone one of the other blocks.
// NOLINTNEXTLINE(bugprone-branch-clone)
2018-01-26 17:06:56 +01:00
} else if (doing == DoingMode && arg == "NATIVE_COMMAND") {
2017-07-20 19:35:53 +02:00
#ifdef _WIN32
mode = ModeWindows;
#else
mode = ModeUnix;
#endif
doing = DoingCommand;
2018-01-26 17:06:56 +01:00
} else if (doing == DoingMode && arg == "UNIX_COMMAND") {
2009-10-04 10:30:41 +03:00
mode = ModeUnix;
doing = DoingCommand;
2018-01-26 17:06:56 +01:00
} else if (doing == DoingMode && arg == "WINDOWS_COMMAND") {
2009-10-04 10:30:41 +03:00
mode = ModeWindows;
doing = DoingCommand;
2016-07-09 11:21:54 +02:00
} else if (doing == DoingCommand) {
2018-01-26 17:06:56 +01:00
command = arg;
2009-10-04 10:30:41 +03:00
doing = DoingNone;
2016-07-09 11:21:54 +02:00
} else {
2020-02-01 23:06:01 +01:00
status.SetError(cmStrCat("given unknown argument ", arg));
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
2016-07-09 11:21:54 +02:00
if (mode == ModeOld) {
2009-10-04 10:30:41 +03:00
// Original space-replacement version of command.
2020-02-01 23:06:01 +01:00
if (const char* def = status.GetMakefile().GetDefinition(var)) {
2009-10-04 10:30:41 +03:00
std::string value = def;
2016-07-09 11:21:54 +02:00
std::replace(value.begin(), value.end(), ' ', ';');
2020-02-01 23:06:01 +01:00
status.GetMakefile().AddDefinition(var, value);
2009-10-04 10:30:41 +03:00
}
2016-07-09 11:21:54 +02:00
} else {
2009-10-04 10:30:41 +03:00
// Parse the command line.
std::vector<std::string> vec;
2016-07-09 11:21:54 +02:00
if (mode == ModeUnix) {
2009-10-04 10:30:41 +03:00
cmSystemTools::ParseUnixCommandLine(command.c_str(), vec);
2016-07-09 11:21:54 +02:00
} else // if(mode == ModeWindows)
{
2009-10-04 10:30:41 +03:00
cmSystemTools::ParseWindowsCommandLine(command.c_str(), vec);
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
// Construct the result list value.
std::string value;
const char* sep = "";
2018-01-26 17:06:56 +01:00
for (std::string const& vi : vec) {
2009-10-04 10:30:41 +03:00
// Separate from the previous argument.
value += sep;
sep = ";";
// Preserve semicolons.
2018-01-26 17:06:56 +01:00
for (char si : vi) {
if (si == ';') {
2009-10-04 10:30:41 +03:00
value += '\\';
}
2018-01-26 17:06:56 +01:00
value += si;
2009-10-04 10:30:41 +03:00
}
}
2020-02-01 23:06:01 +01:00
status.GetMakefile().AddDefinition(var, value);
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
return true;
}