cmParseArgumentsCommand.cxx 7.3 KB

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