2016-09-27 22:01:08 +03:00
|
|
|
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
|
|
|
|
file Copyright.txt or https://cmake.org/licensing for details. */
|
2015-12-05 21:01:12 +03:00
|
|
|
#include "cmParseArgumentsCommand.h"
|
2016-04-29 16:40:20 +03:00
|
|
|
|
2015-12-05 21:01:12 +03:00
|
|
|
#include "cmAlgorithms.h"
|
|
|
|
|
2016-09-28 19:07:39 +03:00
|
|
|
static std::string escape_arg(const std::string& arg)
|
|
|
|
{
|
|
|
|
// replace ";" with "\;" so output argument lists will split correctly
|
|
|
|
std::string escapedArg;
|
|
|
|
for (size_t i = 0; i < arg.size(); ++i) {
|
|
|
|
if (arg[i] == ';') {
|
|
|
|
escapedArg += '\\';
|
|
|
|
}
|
|
|
|
escapedArg += arg[i];
|
|
|
|
}
|
|
|
|
return escapedArg;
|
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
bool cmParseArgumentsCommand::InitialPass(std::vector<std::string> const& args,
|
|
|
|
cmExecutionStatus&)
|
2015-12-05 21:01:12 +03:00
|
|
|
{
|
|
|
|
// cmake_parse_arguments(prefix options single multi <ARGN>)
|
|
|
|
// 1 2 3 4
|
2016-09-07 23:47:23 +03:00
|
|
|
// or
|
|
|
|
// cmake_parse_arguments(PARSE_ARGV N prefix options single multi)
|
2016-05-16 17:34:04 +03:00
|
|
|
if (args.size() < 4) {
|
2015-12-05 21:01:12 +03:00
|
|
|
this->SetError("must be called with at least 4 arguments.");
|
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-12-05 21:01:12 +03:00
|
|
|
|
|
|
|
std::vector<std::string>::const_iterator argIter = args.begin(),
|
2016-05-16 17:34:04 +03:00
|
|
|
argEnd = args.end();
|
2016-09-07 23:47:23 +03:00
|
|
|
bool parseFromArgV = false;
|
|
|
|
unsigned long argvStart = 0;
|
|
|
|
if (*argIter == "PARSE_ARGV") {
|
|
|
|
if (args.size() != 6) {
|
|
|
|
this->Makefile->IssueMessage(
|
|
|
|
cmake::FATAL_ERROR,
|
|
|
|
"PARSE_ARGV must be called with exactly 6 arguments.");
|
|
|
|
cmSystemTools::SetFatalErrorOccured();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
parseFromArgV = true;
|
|
|
|
argIter++; // move past PARSE_ARGV
|
|
|
|
if (!cmSystemTools::StringToULong(argIter->c_str(), &argvStart)) {
|
|
|
|
this->Makefile->IssueMessage(cmake::FATAL_ERROR, "PARSE_ARGV index '" +
|
|
|
|
*argIter +
|
|
|
|
"' is not an unsigned integer");
|
|
|
|
cmSystemTools::SetFatalErrorOccured();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
argIter++; // move past N
|
|
|
|
}
|
2015-12-05 21:01:12 +03:00
|
|
|
// the first argument is the prefix
|
|
|
|
const std::string prefix = (*argIter++) + "_";
|
|
|
|
|
|
|
|
// define the result maps holding key/value pairs for
|
|
|
|
// options, single values and multi values
|
|
|
|
typedef std::map<std::string, bool> options_map;
|
|
|
|
typedef std::map<std::string, std::string> single_map;
|
|
|
|
typedef std::map<std::string, std::vector<std::string> > multi_map;
|
|
|
|
options_map options;
|
|
|
|
single_map single;
|
|
|
|
multi_map multi;
|
|
|
|
|
|
|
|
// anything else is put into a vector of unparsed strings
|
|
|
|
std::vector<std::string> unparsed;
|
|
|
|
|
2015-12-05 21:02:19 +03:00
|
|
|
// remember already defined keywords
|
|
|
|
std::set<std::string> used_keywords;
|
|
|
|
const std::string dup_warning = "keyword defined more than once: ";
|
|
|
|
|
2015-12-05 21:01:12 +03:00
|
|
|
// the second argument is a (cmake) list of options without argument
|
|
|
|
std::vector<std::string> list;
|
|
|
|
cmSystemTools::ExpandListArgument(*argIter++, list);
|
2016-05-16 17:34:04 +03:00
|
|
|
for (std::vector<std::string>::const_iterator iter = list.begin(),
|
|
|
|
end = list.end();
|
|
|
|
iter != end; ++iter) {
|
|
|
|
if (!used_keywords.insert(*iter).second) {
|
2015-12-05 21:02:19 +03:00
|
|
|
this->GetMakefile()->IssueMessage(cmake::WARNING, dup_warning + *iter);
|
2015-12-05 21:01:12 +03:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
options[*iter]; // default initialize
|
|
|
|
}
|
2015-12-05 21:01:12 +03:00
|
|
|
|
|
|
|
// the third argument is a (cmake) list of single argument options
|
|
|
|
list.clear();
|
|
|
|
cmSystemTools::ExpandListArgument(*argIter++, list);
|
2016-05-16 17:34:04 +03:00
|
|
|
for (std::vector<std::string>::const_iterator iter = list.begin(),
|
|
|
|
end = list.end();
|
|
|
|
iter != end; ++iter) {
|
|
|
|
if (!used_keywords.insert(*iter).second) {
|
2015-12-05 21:02:19 +03:00
|
|
|
this->GetMakefile()->IssueMessage(cmake::WARNING, dup_warning + *iter);
|
2015-12-05 21:01:12 +03:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
single[*iter]; // default initialize
|
|
|
|
}
|
2015-12-05 21:01:12 +03:00
|
|
|
|
|
|
|
// the fourth argument is a (cmake) list of multi argument options
|
|
|
|
list.clear();
|
|
|
|
cmSystemTools::ExpandListArgument(*argIter++, list);
|
2016-05-16 17:34:04 +03:00
|
|
|
for (std::vector<std::string>::const_iterator iter = list.begin(),
|
|
|
|
end = list.end();
|
|
|
|
iter != end; ++iter) {
|
|
|
|
if (!used_keywords.insert(*iter).second) {
|
2015-12-05 21:02:19 +03:00
|
|
|
this->GetMakefile()->IssueMessage(cmake::WARNING, dup_warning + *iter);
|
2015-12-05 21:01:12 +03:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
multi[*iter]; // default initialize
|
|
|
|
}
|
2015-12-05 21:01:12 +03:00
|
|
|
|
|
|
|
enum insideValues
|
|
|
|
{
|
|
|
|
NONE,
|
|
|
|
SINGLE,
|
|
|
|
MULTI
|
|
|
|
} insideValues = NONE;
|
|
|
|
std::string currentArgName;
|
|
|
|
|
2016-02-12 15:21:12 +03:00
|
|
|
list.clear();
|
2016-09-07 23:47:23 +03:00
|
|
|
if (!parseFromArgV) {
|
|
|
|
// Flatten ;-lists in the arguments into a single list as was done
|
|
|
|
// by the original function(CMAKE_PARSE_ARGUMENTS).
|
|
|
|
for (; argIter != argEnd; ++argIter) {
|
|
|
|
cmSystemTools::ExpandListArgument(*argIter, list);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// in the PARSE_ARGV move read the arguments from ARGC and ARGV#
|
|
|
|
std::string argc = this->Makefile->GetSafeDefinition("ARGC");
|
|
|
|
unsigned long count;
|
|
|
|
if (!cmSystemTools::StringToULong(argc.c_str(), &count)) {
|
|
|
|
this->Makefile->IssueMessage(cmake::FATAL_ERROR,
|
|
|
|
"PARSE_ARGV called with ARGC='" + argc +
|
|
|
|
"' that is not an unsigned integer");
|
|
|
|
cmSystemTools::SetFatalErrorOccured();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
for (unsigned long i = argvStart; i < count; ++i) {
|
|
|
|
std::ostringstream argName;
|
|
|
|
argName << "ARGV" << i;
|
|
|
|
const char* arg = this->Makefile->GetDefinition(argName.str());
|
|
|
|
if (!arg) {
|
|
|
|
this->Makefile->IssueMessage(cmake::FATAL_ERROR,
|
|
|
|
"PARSE_ARGV called with " +
|
|
|
|
argName.str() + " not set");
|
|
|
|
cmSystemTools::SetFatalErrorOccured();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
list.push_back(arg);
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2016-02-12 15:21:12 +03:00
|
|
|
|
|
|
|
// iterate over the arguments list and fill in the values where applicable
|
2016-05-16 17:34:04 +03:00
|
|
|
for (argIter = list.begin(), argEnd = list.end(); argIter != argEnd;
|
|
|
|
++argIter) {
|
2015-12-05 21:01:12 +03:00
|
|
|
const options_map::iterator optIter = options.find(*argIter);
|
2016-05-16 17:34:04 +03:00
|
|
|
if (optIter != options.end()) {
|
2015-12-05 21:01:12 +03:00
|
|
|
insideValues = NONE;
|
|
|
|
optIter->second = true;
|
|
|
|
continue;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-12-05 21:01:12 +03:00
|
|
|
|
|
|
|
const single_map::iterator singleIter = single.find(*argIter);
|
2016-05-16 17:34:04 +03:00
|
|
|
if (singleIter != single.end()) {
|
2015-12-05 21:01:12 +03:00
|
|
|
insideValues = SINGLE;
|
|
|
|
currentArgName = *argIter;
|
|
|
|
continue;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-12-05 21:01:12 +03:00
|
|
|
|
|
|
|
const multi_map::iterator multiIter = multi.find(*argIter);
|
2016-05-16 17:34:04 +03:00
|
|
|
if (multiIter != multi.end()) {
|
2015-12-05 21:01:12 +03:00
|
|
|
insideValues = MULTI;
|
|
|
|
currentArgName = *argIter;
|
|
|
|
continue;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-12-05 21:01:12 +03:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
switch (insideValues) {
|
2015-12-05 21:01:12 +03:00
|
|
|
case SINGLE:
|
|
|
|
single[currentArgName] = *argIter;
|
|
|
|
insideValues = NONE;
|
|
|
|
break;
|
|
|
|
case MULTI:
|
2016-09-28 19:07:39 +03:00
|
|
|
if (parseFromArgV) {
|
|
|
|
multi[currentArgName].push_back(escape_arg(*argIter));
|
|
|
|
} else {
|
|
|
|
multi[currentArgName].push_back(*argIter);
|
|
|
|
}
|
2015-12-05 21:01:12 +03:00
|
|
|
break;
|
|
|
|
default:
|
2016-09-28 19:07:39 +03:00
|
|
|
if (parseFromArgV) {
|
|
|
|
unparsed.push_back(escape_arg(*argIter));
|
|
|
|
} else {
|
|
|
|
unparsed.push_back(*argIter);
|
|
|
|
}
|
2015-12-05 21:01:12 +03:00
|
|
|
break;
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-12-05 21:01:12 +03:00
|
|
|
|
|
|
|
// now iterate over the collected values and update their definition
|
|
|
|
// within the current scope. undefine if necessary.
|
|
|
|
|
|
|
|
for (options_map::const_iterator iter = options.begin(), end = options.end();
|
2016-05-16 17:34:04 +03:00
|
|
|
iter != end; ++iter) {
|
2015-12-05 21:01:12 +03:00
|
|
|
this->Makefile->AddDefinition(prefix + iter->first,
|
2016-05-16 17:34:04 +03:00
|
|
|
iter->second ? "TRUE" : "FALSE");
|
|
|
|
}
|
2015-12-05 21:01:12 +03:00
|
|
|
for (single_map::const_iterator iter = single.begin(), end = single.end();
|
2016-05-16 17:34:04 +03:00
|
|
|
iter != end; ++iter) {
|
|
|
|
if (!iter->second.empty()) {
|
2015-12-05 21:01:12 +03:00
|
|
|
this->Makefile->AddDefinition(prefix + iter->first,
|
|
|
|
iter->second.c_str());
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2015-12-05 21:01:12 +03:00
|
|
|
this->Makefile->RemoveDefinition(prefix + iter->first);
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-12-05 21:01:12 +03:00
|
|
|
|
|
|
|
for (multi_map::const_iterator iter = multi.begin(), end = multi.end();
|
2016-05-16 17:34:04 +03:00
|
|
|
iter != end; ++iter) {
|
|
|
|
if (!iter->second.empty()) {
|
|
|
|
this->Makefile->AddDefinition(
|
|
|
|
prefix + iter->first, cmJoin(cmMakeRange(iter->second), ";").c_str());
|
|
|
|
} else {
|
2015-12-05 21:01:12 +03:00
|
|
|
this->Makefile->RemoveDefinition(prefix + iter->first);
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-12-05 21:01:12 +03:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!unparsed.empty()) {
|
2015-12-05 21:01:12 +03:00
|
|
|
this->Makefile->AddDefinition(prefix + "UNPARSED_ARGUMENTS",
|
|
|
|
cmJoin(cmMakeRange(unparsed), ";").c_str());
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2015-12-05 21:01:12 +03:00
|
|
|
this->Makefile->RemoveDefinition(prefix + "UNPARSED_ARGUMENTS");
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-12-05 21:01:12 +03:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|