cmParseArgumentsCommand.cxx 7.5 KB

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