cmParseArgumentsCommand.cxx 7.0 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 "cmExecutionStatus.h"
  10. #include "cmMakefile.h"
  11. #include "cmMessageType.h"
  12. #include "cmRange.h"
  13. #include "cmStringAlgorithms.h"
  14. #include "cmSystemTools.h"
  15. #include "cm_string_view.hxx"
  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. using options_map = std::map<std::string, bool>;
  35. using single_map = std::map<std::string, std::string>;
  36. using multi_map = std::map<std::string, std::vector<std::string>>;
  37. using options_set = std::set<std::string>;
  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(std::vector<std::string> const& args,
  94. cmExecutionStatus& status)
  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. status.SetError("must be called with at least 4 arguments.");
  102. return false;
  103. }
  104. auto argIter = args.begin();
  105. auto argEnd = args.end();
  106. bool parseFromArgV = false;
  107. unsigned long argvStart = 0;
  108. if (*argIter == "PARSE_ARGV") {
  109. if (args.size() != 6) {
  110. status.GetMakefile().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. status.GetMakefile().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 = [&status](std::string const& key) {
  138. status.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 = cmExpandedList(*argIter++);
  143. parser.Bind(list, options, duplicateKey);
  144. // the third argument is a (cmake) list of single argument options
  145. list.clear();
  146. cmExpandList(*argIter++, list);
  147. parser.Bind(list, singleValArgs, duplicateKey);
  148. // the fourth argument is a (cmake) list of multi argument options
  149. list.clear();
  150. cmExpandList(*argIter++, list);
  151. parser.Bind(list, multiValArgs, duplicateKey);
  152. list.clear();
  153. if (!parseFromArgV) {
  154. // Flatten ;-lists in the arguments into a single list as was done
  155. // by the original function(CMAKE_PARSE_ARGUMENTS).
  156. for (; argIter != argEnd; ++argIter) {
  157. cmExpandList(*argIter, list);
  158. }
  159. } else {
  160. // in the PARSE_ARGV move read the arguments from ARGC and ARGV#
  161. std::string argc = status.GetMakefile().GetSafeDefinition("ARGC");
  162. unsigned long count;
  163. if (!cmStrToULong(argc, &count)) {
  164. status.GetMakefile().IssueMessage(MessageType::FATAL_ERROR,
  165. "PARSE_ARGV called with ARGC='" +
  166. 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 = status.GetMakefile().GetDefinition(argName.str());
  175. if (!arg) {
  176. status.GetMakefile().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, status.GetMakefile(), options, singleValArgs, multiValArgs,
  189. unparsed,
  190. options_set(keywordsMissingValues.begin(), keywordsMissingValues.end()),
  191. parseFromArgV);
  192. return true;
  193. }