cmParseArgumentsCommand.cxx 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmParseArgumentsCommand.h"
  4. #include "cmAlgorithms.h"
  5. static std::string escape_arg(const std::string& arg)
  6. {
  7. // replace ";" with "\;" so output argument lists will split correctly
  8. std::string escapedArg;
  9. for (size_t i = 0; i < arg.size(); ++i) {
  10. if (arg[i] == ';') {
  11. escapedArg += '\\';
  12. }
  13. escapedArg += arg[i];
  14. }
  15. return escapedArg;
  16. }
  17. bool cmParseArgumentsCommand::InitialPass(std::vector<std::string> const& args,
  18. cmExecutionStatus&)
  19. {
  20. // cmake_parse_arguments(prefix options single multi <ARGN>)
  21. // 1 2 3 4
  22. // or
  23. // cmake_parse_arguments(PARSE_ARGV N prefix options single multi)
  24. if (args.size() < 4) {
  25. this->SetError("must be called with at least 4 arguments.");
  26. return false;
  27. }
  28. std::vector<std::string>::const_iterator argIter = args.begin(),
  29. argEnd = args.end();
  30. bool parseFromArgV = false;
  31. unsigned long argvStart = 0;
  32. if (*argIter == "PARSE_ARGV") {
  33. if (args.size() != 6) {
  34. this->Makefile->IssueMessage(
  35. cmake::FATAL_ERROR,
  36. "PARSE_ARGV must be called with exactly 6 arguments.");
  37. cmSystemTools::SetFatalErrorOccured();
  38. return true;
  39. }
  40. parseFromArgV = true;
  41. argIter++; // move past PARSE_ARGV
  42. if (!cmSystemTools::StringToULong(argIter->c_str(), &argvStart)) {
  43. this->Makefile->IssueMessage(cmake::FATAL_ERROR, "PARSE_ARGV index '" +
  44. *argIter +
  45. "' is not an unsigned integer");
  46. cmSystemTools::SetFatalErrorOccured();
  47. return true;
  48. }
  49. argIter++; // move past N
  50. }
  51. // the first argument is the prefix
  52. const std::string prefix = (*argIter++) + "_";
  53. // define the result maps holding key/value pairs for
  54. // options, single values and multi values
  55. typedef std::map<std::string, bool> options_map;
  56. typedef std::map<std::string, std::string> single_map;
  57. typedef std::map<std::string, std::vector<std::string> > multi_map;
  58. options_map options;
  59. single_map single;
  60. multi_map multi;
  61. // anything else is put into a vector of unparsed strings
  62. std::vector<std::string> unparsed;
  63. // remember already defined keywords
  64. std::set<std::string> used_keywords;
  65. const std::string dup_warning = "keyword defined more than once: ";
  66. // the second argument is a (cmake) list of options without argument
  67. std::vector<std::string> list;
  68. cmSystemTools::ExpandListArgument(*argIter++, list);
  69. for (std::vector<std::string>::const_iterator iter = list.begin(),
  70. end = list.end();
  71. iter != end; ++iter) {
  72. if (!used_keywords.insert(*iter).second) {
  73. this->GetMakefile()->IssueMessage(cmake::WARNING, dup_warning + *iter);
  74. }
  75. options[*iter]; // default initialize
  76. }
  77. // the third argument is a (cmake) list of single argument options
  78. list.clear();
  79. cmSystemTools::ExpandListArgument(*argIter++, list);
  80. for (std::vector<std::string>::const_iterator iter = list.begin(),
  81. end = list.end();
  82. iter != end; ++iter) {
  83. if (!used_keywords.insert(*iter).second) {
  84. this->GetMakefile()->IssueMessage(cmake::WARNING, dup_warning + *iter);
  85. }
  86. single[*iter]; // default initialize
  87. }
  88. // the fourth argument is a (cmake) list of multi argument options
  89. list.clear();
  90. cmSystemTools::ExpandListArgument(*argIter++, list);
  91. for (std::vector<std::string>::const_iterator iter = list.begin(),
  92. end = list.end();
  93. iter != end; ++iter) {
  94. if (!used_keywords.insert(*iter).second) {
  95. this->GetMakefile()->IssueMessage(cmake::WARNING, dup_warning + *iter);
  96. }
  97. multi[*iter]; // default initialize
  98. }
  99. enum insideValues
  100. {
  101. NONE,
  102. SINGLE,
  103. MULTI
  104. } insideValues = NONE;
  105. std::string currentArgName;
  106. list.clear();
  107. if (!parseFromArgV) {
  108. // Flatten ;-lists in the arguments into a single list as was done
  109. // by the original function(CMAKE_PARSE_ARGUMENTS).
  110. for (; argIter != argEnd; ++argIter) {
  111. cmSystemTools::ExpandListArgument(*argIter, list);
  112. }
  113. } else {
  114. // in the PARSE_ARGV move read the arguments from ARGC and ARGV#
  115. std::string argc = this->Makefile->GetSafeDefinition("ARGC");
  116. unsigned long count;
  117. if (!cmSystemTools::StringToULong(argc.c_str(), &count)) {
  118. this->Makefile->IssueMessage(cmake::FATAL_ERROR,
  119. "PARSE_ARGV called with ARGC='" + argc +
  120. "' that is not an unsigned integer");
  121. cmSystemTools::SetFatalErrorOccured();
  122. return true;
  123. }
  124. for (unsigned long i = argvStart; i < count; ++i) {
  125. std::ostringstream argName;
  126. argName << "ARGV" << i;
  127. const char* arg = this->Makefile->GetDefinition(argName.str());
  128. if (!arg) {
  129. this->Makefile->IssueMessage(cmake::FATAL_ERROR,
  130. "PARSE_ARGV called with " +
  131. argName.str() + " not set");
  132. cmSystemTools::SetFatalErrorOccured();
  133. return true;
  134. }
  135. list.push_back(arg);
  136. }
  137. }
  138. // iterate over the arguments list and fill in the values where applicable
  139. for (argIter = list.begin(), argEnd = list.end(); argIter != argEnd;
  140. ++argIter) {
  141. const options_map::iterator optIter = options.find(*argIter);
  142. if (optIter != options.end()) {
  143. insideValues = NONE;
  144. optIter->second = true;
  145. continue;
  146. }
  147. const single_map::iterator singleIter = single.find(*argIter);
  148. if (singleIter != single.end()) {
  149. insideValues = SINGLE;
  150. currentArgName = *argIter;
  151. continue;
  152. }
  153. const multi_map::iterator multiIter = multi.find(*argIter);
  154. if (multiIter != multi.end()) {
  155. insideValues = MULTI;
  156. currentArgName = *argIter;
  157. continue;
  158. }
  159. switch (insideValues) {
  160. case SINGLE:
  161. single[currentArgName] = *argIter;
  162. insideValues = NONE;
  163. break;
  164. case MULTI:
  165. if (parseFromArgV) {
  166. multi[currentArgName].push_back(escape_arg(*argIter));
  167. } else {
  168. multi[currentArgName].push_back(*argIter);
  169. }
  170. break;
  171. default:
  172. if (parseFromArgV) {
  173. unparsed.push_back(escape_arg(*argIter));
  174. } else {
  175. unparsed.push_back(*argIter);
  176. }
  177. break;
  178. }
  179. }
  180. // now iterate over the collected values and update their definition
  181. // within the current scope. undefine if necessary.
  182. for (options_map::const_iterator iter = options.begin(), end = options.end();
  183. iter != end; ++iter) {
  184. this->Makefile->AddDefinition(prefix + iter->first,
  185. iter->second ? "TRUE" : "FALSE");
  186. }
  187. for (single_map::const_iterator iter = single.begin(), end = single.end();
  188. iter != end; ++iter) {
  189. if (!iter->second.empty()) {
  190. this->Makefile->AddDefinition(prefix + iter->first,
  191. iter->second.c_str());
  192. } else {
  193. this->Makefile->RemoveDefinition(prefix + iter->first);
  194. }
  195. }
  196. for (multi_map::const_iterator iter = multi.begin(), end = multi.end();
  197. iter != end; ++iter) {
  198. if (!iter->second.empty()) {
  199. this->Makefile->AddDefinition(
  200. prefix + iter->first, cmJoin(cmMakeRange(iter->second), ";").c_str());
  201. } else {
  202. this->Makefile->RemoveDefinition(prefix + iter->first);
  203. }
  204. }
  205. if (!unparsed.empty()) {
  206. this->Makefile->AddDefinition(prefix + "UNPARSED_ARGUMENTS",
  207. cmJoin(cmMakeRange(unparsed), ";").c_str());
  208. } else {
  209. this->Makefile->RemoveDefinition(prefix + "UNPARSED_ARGUMENTS");
  210. }
  211. return true;
  212. }