cmParseArgumentsCommand.cxx 7.3 KB

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