cmParseArgumentsCommand.cxx 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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 EscapeArg(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. namespace {
  26. enum insideValues
  27. {
  28. NONE,
  29. SINGLE,
  30. MULTI
  31. };
  32. typedef std::map<std::string, bool> options_map;
  33. typedef std::map<std::string, std::string> single_map;
  34. typedef std::map<std::string, std::vector<std::string>> multi_map;
  35. typedef std::set<std::string> options_set;
  36. }
  37. // function to be called every time, a new key word was parsed or all
  38. // parameters where parsed.
  39. static void DetectKeywordsMissingValues(insideValues currentState,
  40. const std::string& currentArgName,
  41. int& argumentsFound,
  42. options_set& keywordsMissingValues)
  43. {
  44. if (currentState == SINGLE ||
  45. (currentState == MULTI && argumentsFound == 0)) {
  46. keywordsMissingValues.insert(currentArgName);
  47. }
  48. argumentsFound = 0;
  49. }
  50. static void PassParsedArguments(const std::string& prefix,
  51. cmMakefile& makefile,
  52. const options_map& options,
  53. const single_map& singleValArgs,
  54. const multi_map& multiValArgs,
  55. const std::vector<std::string>& unparsed,
  56. const options_set& keywordsMissingValues)
  57. {
  58. for (auto const& iter : options) {
  59. makefile.AddDefinition(prefix + iter.first,
  60. iter.second ? "TRUE" : "FALSE");
  61. }
  62. for (auto const& iter : singleValArgs) {
  63. if (!iter.second.empty()) {
  64. makefile.AddDefinition(prefix + iter.first, iter.second.c_str());
  65. } else {
  66. makefile.RemoveDefinition(prefix + iter.first);
  67. }
  68. }
  69. for (auto const& iter : multiValArgs) {
  70. if (!iter.second.empty()) {
  71. makefile.AddDefinition(prefix + iter.first,
  72. cmJoin(cmMakeRange(iter.second), ";").c_str());
  73. } else {
  74. makefile.RemoveDefinition(prefix + iter.first);
  75. }
  76. }
  77. if (!unparsed.empty()) {
  78. makefile.AddDefinition(prefix + "UNPARSED_ARGUMENTS",
  79. cmJoin(cmMakeRange(unparsed), ";").c_str());
  80. } else {
  81. makefile.RemoveDefinition(prefix + "UNPARSED_ARGUMENTS");
  82. }
  83. if (!keywordsMissingValues.empty()) {
  84. makefile.AddDefinition(
  85. prefix + "KEYWORDS_MISSING_VALUES",
  86. cmJoin(cmMakeRange(keywordsMissingValues), ";").c_str());
  87. } else {
  88. makefile.RemoveDefinition(prefix + "KEYWORDS_MISSING_VALUES");
  89. }
  90. }
  91. bool cmParseArgumentsCommand::InitialPass(std::vector<std::string> const& args,
  92. cmExecutionStatus&)
  93. {
  94. // cmake_parse_arguments(prefix options single multi <ARGN>)
  95. // 1 2 3 4
  96. // or
  97. // cmake_parse_arguments(PARSE_ARGV N prefix options single multi)
  98. if (args.size() < 4) {
  99. this->SetError("must be called with at least 4 arguments.");
  100. return false;
  101. }
  102. std::vector<std::string>::const_iterator argIter = args.begin(),
  103. argEnd = args.end();
  104. bool parseFromArgV = false;
  105. unsigned long argvStart = 0;
  106. if (*argIter == "PARSE_ARGV") {
  107. if (args.size() != 6) {
  108. this->Makefile->IssueMessage(
  109. MessageType::FATAL_ERROR,
  110. "PARSE_ARGV must be called with exactly 6 arguments.");
  111. cmSystemTools::SetFatalErrorOccured();
  112. return true;
  113. }
  114. parseFromArgV = true;
  115. argIter++; // move past PARSE_ARGV
  116. if (!cmSystemTools::StringToULong(argIter->c_str(), &argvStart)) {
  117. this->Makefile->IssueMessage(MessageType::FATAL_ERROR,
  118. "PARSE_ARGV index '" + *argIter +
  119. "' is not an unsigned integer");
  120. cmSystemTools::SetFatalErrorOccured();
  121. return true;
  122. }
  123. argIter++; // move past N
  124. }
  125. // the first argument is the prefix
  126. const std::string prefix = (*argIter++) + "_";
  127. // define the result maps holding key/value pairs for
  128. // options, single values and multi values
  129. options_map options;
  130. single_map singleValArgs;
  131. multi_map multiValArgs;
  132. // anything else is put into a vector of unparsed strings
  133. std::vector<std::string> unparsed;
  134. // remember already defined keywords
  135. std::set<std::string> used_keywords;
  136. const std::string dup_warning = "keyword defined more than once: ";
  137. // the second argument is a (cmake) list of options without argument
  138. std::vector<std::string> list;
  139. cmSystemTools::ExpandListArgument(*argIter++, list);
  140. for (std::string const& iter : list) {
  141. if (!used_keywords.insert(iter).second) {
  142. this->GetMakefile()->IssueMessage(MessageType::WARNING,
  143. dup_warning + iter);
  144. }
  145. options[iter]; // default initialize
  146. }
  147. // the third argument is a (cmake) list of single argument options
  148. list.clear();
  149. cmSystemTools::ExpandListArgument(*argIter++, list);
  150. for (std::string const& iter : list) {
  151. if (!used_keywords.insert(iter).second) {
  152. this->GetMakefile()->IssueMessage(MessageType::WARNING,
  153. dup_warning + iter);
  154. }
  155. singleValArgs[iter]; // default initialize
  156. }
  157. // the fourth argument is a (cmake) list of multi argument options
  158. list.clear();
  159. cmSystemTools::ExpandListArgument(*argIter++, list);
  160. for (std::string const& iter : list) {
  161. if (!used_keywords.insert(iter).second) {
  162. this->GetMakefile()->IssueMessage(MessageType::WARNING,
  163. dup_warning + iter);
  164. }
  165. multiValArgs[iter]; // default initialize
  166. }
  167. insideValues insideValues = NONE;
  168. std::string currentArgName;
  169. list.clear();
  170. if (!parseFromArgV) {
  171. // Flatten ;-lists in the arguments into a single list as was done
  172. // by the original function(CMAKE_PARSE_ARGUMENTS).
  173. for (; argIter != argEnd; ++argIter) {
  174. cmSystemTools::ExpandListArgument(*argIter, list);
  175. }
  176. } else {
  177. // in the PARSE_ARGV move read the arguments from ARGC and ARGV#
  178. std::string argc = this->Makefile->GetSafeDefinition("ARGC");
  179. unsigned long count;
  180. if (!cmSystemTools::StringToULong(argc.c_str(), &count)) {
  181. this->Makefile->IssueMessage(MessageType::FATAL_ERROR,
  182. "PARSE_ARGV called with ARGC='" + argc +
  183. "' that is not an unsigned integer");
  184. cmSystemTools::SetFatalErrorOccured();
  185. return true;
  186. }
  187. for (unsigned long i = argvStart; i < count; ++i) {
  188. std::ostringstream argName;
  189. argName << "ARGV" << i;
  190. const char* arg = this->Makefile->GetDefinition(argName.str());
  191. if (!arg) {
  192. this->Makefile->IssueMessage(MessageType::FATAL_ERROR,
  193. "PARSE_ARGV called with " +
  194. argName.str() + " not set");
  195. cmSystemTools::SetFatalErrorOccured();
  196. return true;
  197. }
  198. list.emplace_back(arg);
  199. }
  200. }
  201. options_set keywordsMissingValues;
  202. int multiArgumentsFound = 0;
  203. // iterate over the arguments list and fill in the values where applicable
  204. for (std::string const& arg : list) {
  205. const options_map::iterator optIter = options.find(arg);
  206. if (optIter != options.end()) {
  207. DetectKeywordsMissingValues(insideValues, currentArgName,
  208. multiArgumentsFound, keywordsMissingValues);
  209. insideValues = NONE;
  210. optIter->second = true;
  211. continue;
  212. }
  213. const single_map::iterator singleIter = singleValArgs.find(arg);
  214. if (singleIter != singleValArgs.end()) {
  215. DetectKeywordsMissingValues(insideValues, currentArgName,
  216. multiArgumentsFound, keywordsMissingValues);
  217. insideValues = SINGLE;
  218. currentArgName = arg;
  219. continue;
  220. }
  221. const multi_map::iterator multiIter = multiValArgs.find(arg);
  222. if (multiIter != multiValArgs.end()) {
  223. DetectKeywordsMissingValues(insideValues, currentArgName,
  224. multiArgumentsFound, keywordsMissingValues);
  225. insideValues = MULTI;
  226. currentArgName = arg;
  227. continue;
  228. }
  229. switch (insideValues) {
  230. case SINGLE:
  231. singleValArgs[currentArgName] = arg;
  232. insideValues = NONE;
  233. break;
  234. case MULTI:
  235. ++multiArgumentsFound;
  236. if (parseFromArgV) {
  237. multiValArgs[currentArgName].push_back(EscapeArg(arg));
  238. } else {
  239. multiValArgs[currentArgName].push_back(arg);
  240. }
  241. break;
  242. default:
  243. multiArgumentsFound = 0;
  244. if (parseFromArgV) {
  245. unparsed.push_back(EscapeArg(arg));
  246. } else {
  247. unparsed.push_back(arg);
  248. }
  249. break;
  250. }
  251. }
  252. DetectKeywordsMissingValues(insideValues, currentArgName,
  253. multiArgumentsFound, keywordsMissingValues);
  254. PassParsedArguments(prefix, *this->Makefile, options, singleValArgs,
  255. multiValArgs, unparsed, keywordsMissingValues);
  256. return true;
  257. }