cmParseArgumentsCommand.cxx 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 <map>
  5. #include <set>
  6. #include <sstream>
  7. #include <utility>
  8. #include "cmArgumentParser.h"
  9. #include "cmMakefile.h"
  10. #include "cmMessageType.h"
  11. #include "cmRange.h"
  12. #include "cmStringAlgorithms.h"
  13. #include "cmSystemTools.h"
  14. #include "cm_string_view.hxx"
  15. class cmExecutionStatus;
  16. static std::string EscapeArg(const std::string& arg)
  17. {
  18. // replace ";" with "\;" so output argument lists will split correctly
  19. std::string escapedArg;
  20. for (char i : arg) {
  21. if (i == ';') {
  22. escapedArg += '\\';
  23. }
  24. escapedArg += i;
  25. }
  26. return escapedArg;
  27. }
  28. static std::string JoinList(std::vector<std::string> const& arg, bool escape)
  29. {
  30. return escape ? cmJoin(cmMakeRange(arg).transform(EscapeArg), ";")
  31. : cmJoin(cmMakeRange(arg), ";");
  32. }
  33. namespace {
  34. typedef std::map<std::string, bool> options_map;
  35. typedef std::map<std::string, std::string> single_map;
  36. typedef std::map<std::string, std::vector<std::string>> multi_map;
  37. typedef std::set<std::string> options_set;
  38. struct UserArgumentParser : public cmArgumentParser<void>
  39. {
  40. template <typename T, typename H>
  41. void Bind(std::vector<std::string> const& names,
  42. std::map<std::string, T>& ref, H duplicateKey)
  43. {
  44. for (std::string const& key : names) {
  45. auto const it = ref.emplace(key, T{}).first;
  46. bool const inserted = this->cmArgumentParser<void>::Bind(
  47. cm::string_view(it->first), it->second);
  48. if (!inserted) {
  49. duplicateKey(key);
  50. }
  51. }
  52. }
  53. };
  54. } // namespace
  55. static void PassParsedArguments(
  56. const std::string& prefix, cmMakefile& makefile, const options_map& options,
  57. const single_map& singleValArgs, const multi_map& multiValArgs,
  58. const std::vector<std::string>& unparsed,
  59. const options_set& keywordsMissingValues, bool parseFromArgV)
  60. {
  61. for (auto const& iter : options) {
  62. makefile.AddDefinition(prefix + iter.first,
  63. iter.second ? "TRUE" : "FALSE");
  64. }
  65. for (auto const& iter : singleValArgs) {
  66. if (!iter.second.empty()) {
  67. makefile.AddDefinition(prefix + iter.first, iter.second);
  68. } else {
  69. makefile.RemoveDefinition(prefix + iter.first);
  70. }
  71. }
  72. for (auto const& iter : multiValArgs) {
  73. if (!iter.second.empty()) {
  74. makefile.AddDefinition(prefix + iter.first,
  75. JoinList(iter.second, parseFromArgV));
  76. } else {
  77. makefile.RemoveDefinition(prefix + iter.first);
  78. }
  79. }
  80. if (!unparsed.empty()) {
  81. makefile.AddDefinition(prefix + "UNPARSED_ARGUMENTS",
  82. JoinList(unparsed, parseFromArgV));
  83. } else {
  84. makefile.RemoveDefinition(prefix + "UNPARSED_ARGUMENTS");
  85. }
  86. if (!keywordsMissingValues.empty()) {
  87. makefile.AddDefinition(prefix + "KEYWORDS_MISSING_VALUES",
  88. cmJoin(cmMakeRange(keywordsMissingValues), ";"));
  89. } else {
  90. makefile.RemoveDefinition(prefix + "KEYWORDS_MISSING_VALUES");
  91. }
  92. }
  93. bool cmParseArgumentsCommand::InitialPass(std::vector<std::string> const& args,
  94. cmExecutionStatus&)
  95. {
  96. // cmake_parse_arguments(prefix options single multi <ARGN>)
  97. // 1 2 3 4
  98. // or
  99. // cmake_parse_arguments(PARSE_ARGV N prefix options single multi)
  100. if (args.size() < 4) {
  101. this->SetError("must be called with at least 4 arguments.");
  102. return false;
  103. }
  104. std::vector<std::string>::const_iterator argIter = args.begin();
  105. std::vector<std::string>::const_iterator argEnd = args.end();
  106. bool parseFromArgV = false;
  107. unsigned long argvStart = 0;
  108. if (*argIter == "PARSE_ARGV") {
  109. if (args.size() != 6) {
  110. this->Makefile->IssueMessage(
  111. MessageType::FATAL_ERROR,
  112. "PARSE_ARGV must be called with exactly 6 arguments.");
  113. cmSystemTools::SetFatalErrorOccured();
  114. return true;
  115. }
  116. parseFromArgV = true;
  117. argIter++; // move past PARSE_ARGV
  118. if (!cmStrToULong(*argIter, &argvStart)) {
  119. this->Makefile->IssueMessage(MessageType::FATAL_ERROR,
  120. "PARSE_ARGV index '" + *argIter +
  121. "' is not an unsigned integer");
  122. cmSystemTools::SetFatalErrorOccured();
  123. return true;
  124. }
  125. argIter++; // move past N
  126. }
  127. // the first argument is the prefix
  128. const std::string prefix = (*argIter++) + "_";
  129. UserArgumentParser parser;
  130. // define the result maps holding key/value pairs for
  131. // options, single values and multi values
  132. options_map options;
  133. single_map singleValArgs;
  134. multi_map multiValArgs;
  135. // anything else is put into a vector of unparsed strings
  136. std::vector<std::string> unparsed;
  137. auto const duplicateKey = [this](std::string const& key) {
  138. this->GetMakefile()->IssueMessage(
  139. MessageType::WARNING, "keyword defined more than once: " + key);
  140. };
  141. // the second argument is a (cmake) list of options without argument
  142. std::vector<std::string> list;
  143. cmExpandList(*argIter++, list);
  144. parser.Bind(list, options, duplicateKey);
  145. // the third argument is a (cmake) list of single argument options
  146. list.clear();
  147. cmExpandList(*argIter++, list);
  148. parser.Bind(list, singleValArgs, duplicateKey);
  149. // the fourth argument is a (cmake) list of multi argument options
  150. list.clear();
  151. cmExpandList(*argIter++, list);
  152. parser.Bind(list, multiValArgs, duplicateKey);
  153. list.clear();
  154. if (!parseFromArgV) {
  155. // Flatten ;-lists in the arguments into a single list as was done
  156. // by the original function(CMAKE_PARSE_ARGUMENTS).
  157. for (; argIter != argEnd; ++argIter) {
  158. cmExpandList(*argIter, list);
  159. }
  160. } else {
  161. // in the PARSE_ARGV move read the arguments from ARGC and ARGV#
  162. std::string argc = this->Makefile->GetSafeDefinition("ARGC");
  163. unsigned long count;
  164. if (!cmStrToULong(argc, &count)) {
  165. this->Makefile->IssueMessage(MessageType::FATAL_ERROR,
  166. "PARSE_ARGV called with ARGC='" + argc +
  167. "' that is not an unsigned integer");
  168. cmSystemTools::SetFatalErrorOccured();
  169. return true;
  170. }
  171. for (unsigned long i = argvStart; i < count; ++i) {
  172. std::ostringstream argName;
  173. argName << "ARGV" << i;
  174. const char* arg = this->Makefile->GetDefinition(argName.str());
  175. if (!arg) {
  176. this->Makefile->IssueMessage(MessageType::FATAL_ERROR,
  177. "PARSE_ARGV called with " +
  178. argName.str() + " not set");
  179. cmSystemTools::SetFatalErrorOccured();
  180. return true;
  181. }
  182. list.emplace_back(arg);
  183. }
  184. }
  185. std::vector<std::string> keywordsMissingValues;
  186. parser.Parse(list, &unparsed, &keywordsMissingValues);
  187. PassParsedArguments(
  188. prefix, *this->Makefile, options, singleValArgs, multiValArgs, unparsed,
  189. options_set(keywordsMissingValues.begin(), keywordsMissingValues.end()),
  190. parseFromArgV);
  191. return true;
  192. }