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 "cmAlgorithms.h"
  9. #include "cmMakefile.h"
  10. #include "cmSystemTools.h"
  11. #include "cmake.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. cmake::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(cmake::FATAL_ERROR, "PARSE_ARGV index '" +
  52. *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(cmake::WARNING, dup_warning + iter);
  80. }
  81. options[iter]; // default initialize
  82. }
  83. // the third argument is a (cmake) list of single argument options
  84. list.clear();
  85. cmSystemTools::ExpandListArgument(*argIter++, list);
  86. for (std::string const& iter : list) {
  87. if (!used_keywords.insert(iter).second) {
  88. this->GetMakefile()->IssueMessage(cmake::WARNING, dup_warning + iter);
  89. }
  90. singleValArgs[iter]; // default initialize
  91. }
  92. // the fourth argument is a (cmake) list of multi argument options
  93. list.clear();
  94. cmSystemTools::ExpandListArgument(*argIter++, list);
  95. for (std::string const& iter : list) {
  96. if (!used_keywords.insert(iter).second) {
  97. this->GetMakefile()->IssueMessage(cmake::WARNING, dup_warning + iter);
  98. }
  99. multiValArgs[iter]; // default initialize
  100. }
  101. enum insideValues
  102. {
  103. NONE,
  104. SINGLE,
  105. MULTI
  106. } insideValues = NONE;
  107. std::string currentArgName;
  108. list.clear();
  109. if (!parseFromArgV) {
  110. // Flatten ;-lists in the arguments into a single list as was done
  111. // by the original function(CMAKE_PARSE_ARGUMENTS).
  112. for (; argIter != argEnd; ++argIter) {
  113. cmSystemTools::ExpandListArgument(*argIter, list);
  114. }
  115. } else {
  116. // in the PARSE_ARGV move read the arguments from ARGC and ARGV#
  117. std::string argc = this->Makefile->GetSafeDefinition("ARGC");
  118. unsigned long count;
  119. if (!cmSystemTools::StringToULong(argc.c_str(), &count)) {
  120. this->Makefile->IssueMessage(cmake::FATAL_ERROR,
  121. "PARSE_ARGV called with ARGC='" + argc +
  122. "' that is not an unsigned integer");
  123. cmSystemTools::SetFatalErrorOccured();
  124. return true;
  125. }
  126. for (unsigned long i = argvStart; i < count; ++i) {
  127. std::ostringstream argName;
  128. argName << "ARGV" << i;
  129. const char* arg = this->Makefile->GetDefinition(argName.str());
  130. if (!arg) {
  131. this->Makefile->IssueMessage(cmake::FATAL_ERROR,
  132. "PARSE_ARGV called with " +
  133. argName.str() + " not set");
  134. cmSystemTools::SetFatalErrorOccured();
  135. return true;
  136. }
  137. list.push_back(arg);
  138. }
  139. }
  140. // iterate over the arguments list and fill in the values where applicable
  141. for (std::string const& arg : list) {
  142. const options_map::iterator optIter = options.find(arg);
  143. if (optIter != options.end()) {
  144. insideValues = NONE;
  145. optIter->second = true;
  146. continue;
  147. }
  148. const single_map::iterator singleIter = singleValArgs.find(arg);
  149. if (singleIter != singleValArgs.end()) {
  150. insideValues = SINGLE;
  151. currentArgName = arg;
  152. continue;
  153. }
  154. const multi_map::iterator multiIter = multiValArgs.find(arg);
  155. if (multiIter != multiValArgs.end()) {
  156. insideValues = MULTI;
  157. currentArgName = arg;
  158. continue;
  159. }
  160. switch (insideValues) {
  161. case SINGLE:
  162. singleValArgs[currentArgName] = arg;
  163. insideValues = NONE;
  164. break;
  165. case MULTI:
  166. if (parseFromArgV) {
  167. multiValArgs[currentArgName].push_back(escape_arg(arg));
  168. } else {
  169. multiValArgs[currentArgName].push_back(arg);
  170. }
  171. break;
  172. default:
  173. if (parseFromArgV) {
  174. unparsed.push_back(escape_arg(arg));
  175. } else {
  176. unparsed.push_back(arg);
  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 (auto const& iter : options) {
  184. this->Makefile->AddDefinition(prefix + iter.first,
  185. iter.second ? "TRUE" : "FALSE");
  186. }
  187. for (auto const& iter : singleValArgs) {
  188. if (!iter.second.empty()) {
  189. this->Makefile->AddDefinition(prefix + iter.first, iter.second.c_str());
  190. } else {
  191. this->Makefile->RemoveDefinition(prefix + iter.first);
  192. }
  193. }
  194. for (auto const& iter : multiValArgs) {
  195. if (!iter.second.empty()) {
  196. this->Makefile->AddDefinition(
  197. prefix + iter.first, cmJoin(cmMakeRange(iter.second), ";").c_str());
  198. } else {
  199. this->Makefile->RemoveDefinition(prefix + iter.first);
  200. }
  201. }
  202. if (!unparsed.empty()) {
  203. this->Makefile->AddDefinition(prefix + "UNPARSED_ARGUMENTS",
  204. cmJoin(cmMakeRange(unparsed), ";").c_str());
  205. } else {
  206. this->Makefile->RemoveDefinition(prefix + "UNPARSED_ARGUMENTS");
  207. }
  208. return true;
  209. }