cmParseArgumentsCommand.cxx 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2015 Matthias Maennich <[email protected]>
  4. Copyright 2010 Alexander Neundorf <[email protected]>
  5. Distributed under the OSI-approved BSD License (the "License");
  6. see accompanying file Copyright.txt for details.
  7. This software is distributed WITHOUT ANY WARRANTY; without even the
  8. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. See the License for more information.
  10. ============================================================================*/
  11. #include "cmParseArgumentsCommand.h"
  12. #include "cmAlgorithms.h"
  13. //----------------------------------------------------------------------------
  14. bool cmParseArgumentsCommand
  15. ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
  16. {
  17. // cmake_parse_arguments(prefix options single multi <ARGN>)
  18. // 1 2 3 4
  19. if (args.size() < 4)
  20. {
  21. this->SetError("must be called with at least 4 arguments.");
  22. return false;
  23. }
  24. std::vector<std::string>::const_iterator argIter = args.begin(),
  25. argEnd = args.end();
  26. // the first argument is the prefix
  27. const std::string prefix = (*argIter++) + "_";
  28. // define the result maps holding key/value pairs for
  29. // options, single values and multi values
  30. typedef std::map<std::string, bool> options_map;
  31. typedef std::map<std::string, std::string> single_map;
  32. typedef std::map<std::string, std::vector<std::string> > multi_map;
  33. options_map options;
  34. single_map single;
  35. multi_map multi;
  36. // anything else is put into a vector of unparsed strings
  37. std::vector<std::string> unparsed;
  38. // remember already defined keywords
  39. std::set<std::string> used_keywords;
  40. const std::string dup_warning = "keyword defined more than once: ";
  41. // the second argument is a (cmake) list of options without argument
  42. std::vector<std::string> list;
  43. cmSystemTools::ExpandListArgument(*argIter++, list);
  44. for (std::vector<std::string>::const_iterator iter = list.begin(),
  45. end = list.end();
  46. iter != end; ++iter)
  47. {
  48. if (!used_keywords.insert(*iter).second)
  49. {
  50. this->GetMakefile()->IssueMessage(cmake::WARNING, dup_warning + *iter);
  51. }
  52. options[*iter]; // default initialize
  53. }
  54. // the third argument is a (cmake) list of single argument options
  55. list.clear();
  56. cmSystemTools::ExpandListArgument(*argIter++, list);
  57. for (std::vector<std::string>::const_iterator iter = list.begin(),
  58. end = list.end();
  59. iter != end; ++iter)
  60. {
  61. if (!used_keywords.insert(*iter).second)
  62. {
  63. this->GetMakefile()->IssueMessage(cmake::WARNING, dup_warning + *iter);
  64. }
  65. single[*iter]; // default initialize
  66. }
  67. // the fourth argument is a (cmake) list of multi argument options
  68. list.clear();
  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. {
  74. if (!used_keywords.insert(*iter).second)
  75. {
  76. this->GetMakefile()->IssueMessage(cmake::WARNING, dup_warning + *iter);
  77. }
  78. multi[*iter]; // default initialize
  79. }
  80. enum insideValues
  81. {
  82. NONE,
  83. SINGLE,
  84. MULTI
  85. } insideValues = NONE;
  86. std::string currentArgName;
  87. // now iterate over the remaining arguments
  88. // and fill in the values where applicable
  89. for(; argIter != argEnd; ++argIter)
  90. {
  91. const options_map::iterator optIter = options.find(*argIter);
  92. if (optIter != options.end())
  93. {
  94. insideValues = NONE;
  95. optIter->second = true;
  96. continue;
  97. }
  98. const single_map::iterator singleIter = single.find(*argIter);
  99. if (singleIter != single.end())
  100. {
  101. insideValues = SINGLE;
  102. currentArgName = *argIter;
  103. continue;
  104. }
  105. const multi_map::iterator multiIter = multi.find(*argIter);
  106. if (multiIter != multi.end())
  107. {
  108. insideValues = MULTI;
  109. currentArgName = *argIter;
  110. continue;
  111. }
  112. switch(insideValues)
  113. {
  114. case SINGLE:
  115. single[currentArgName] = *argIter;
  116. insideValues = NONE;
  117. break;
  118. case MULTI:
  119. multi[currentArgName].push_back(*argIter);
  120. break;
  121. default:
  122. unparsed.push_back(*argIter);
  123. break;
  124. }
  125. }
  126. // now iterate over the collected values and update their definition
  127. // within the current scope. undefine if necessary.
  128. for (options_map::const_iterator iter = options.begin(), end = options.end();
  129. iter != end; ++iter)
  130. {
  131. this->Makefile->AddDefinition(prefix + iter->first,
  132. iter->second? "TRUE": "FALSE");
  133. }
  134. for (single_map::const_iterator iter = single.begin(), end = single.end();
  135. iter != end; ++iter)
  136. {
  137. if (!iter->second.empty())
  138. {
  139. this->Makefile->AddDefinition(prefix + iter->first,
  140. iter->second.c_str());
  141. }
  142. else
  143. {
  144. this->Makefile->RemoveDefinition(prefix + iter->first);
  145. }
  146. }
  147. for (multi_map::const_iterator iter = multi.begin(), end = multi.end();
  148. iter != end; ++iter)
  149. {
  150. if (!iter->second.empty())
  151. {
  152. this->Makefile->AddDefinition(prefix + iter->first,
  153. cmJoin(cmMakeRange(iter->second), ";")
  154. .c_str());
  155. }
  156. else
  157. {
  158. this->Makefile->RemoveDefinition(prefix + iter->first);
  159. }
  160. }
  161. if (!unparsed.empty())
  162. {
  163. this->Makefile->AddDefinition(prefix + "UNPARSED_ARGUMENTS",
  164. cmJoin(cmMakeRange(unparsed), ";").c_str());
  165. }
  166. else
  167. {
  168. this->Makefile->RemoveDefinition(prefix + "UNPARSED_ARGUMENTS");
  169. }
  170. return true;
  171. }