1
0

cmParseArgumentsCommand.cxx 7.8 KB

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