cmake/Source/cmSeparateArgumentsCommand.cxx

105 lines
2.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 "cmSeparateArgumentsCommand.h"
2017-04-14 19:02:05 +02:00
#include <algorithm>
#include <sstream>
#include "cmMakefile.h"
#include "cmSystemTools.h"
class cmExecutionStatus;
// cmSeparateArgumentsCommand
2016-07-09 11:21:54 +02:00
bool cmSeparateArgumentsCommand::InitialPass(
std::vector<std::string> const& args, cmExecutionStatus&)
{
2016-07-09 11:21:54 +02:00
if (args.empty()) {
2009-10-04 10:30:41 +03:00
this->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;
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 {
2015-04-27 22:25:09 +02:00
std::ostringstream e;
2018-01-26 17:06:56 +01:00
e << "given unknown argument " << arg;
2015-04-27 22:25:09 +02:00
this->SetError(e.str());
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.
2016-07-09 11:21:54 +02:00
if (const char* def = this->Makefile->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(), ' ', ';');
2015-04-27 22:25:09 +02:00
this->Makefile->AddDefinition(var, value.c_str());
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
}
}
2016-07-09 11:21:54 +02:00
this->Makefile->AddDefinition(var, value.c_str());
}
2009-10-04 10:30:41 +03:00
return true;
}