cmParseArgumentsCommand.cxx 9.4 KB

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