cmParseArgumentsCommand.cxx 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. // Flatten ;-lists in the arguments into a single list as was done
  88. // by the original function(CMAKE_PARSE_ARGUMENTS).
  89. list.clear();
  90. for(; argIter != argEnd; ++argIter)
  91. {
  92. cmSystemTools::ExpandListArgument(*argIter, list);
  93. }
  94. // iterate over the arguments list and fill in the values where applicable
  95. for (argIter = list.begin(), argEnd = list.end();
  96. argIter != argEnd; ++argIter)
  97. {
  98. const options_map::iterator optIter = options.find(*argIter);
  99. if (optIter != options.end())
  100. {
  101. insideValues = NONE;
  102. optIter->second = true;
  103. continue;
  104. }
  105. const single_map::iterator singleIter = single.find(*argIter);
  106. if (singleIter != single.end())
  107. {
  108. insideValues = SINGLE;
  109. currentArgName = *argIter;
  110. continue;
  111. }
  112. const multi_map::iterator multiIter = multi.find(*argIter);
  113. if (multiIter != multi.end())
  114. {
  115. insideValues = MULTI;
  116. currentArgName = *argIter;
  117. continue;
  118. }
  119. switch(insideValues)
  120. {
  121. case SINGLE:
  122. single[currentArgName] = *argIter;
  123. insideValues = NONE;
  124. break;
  125. case MULTI:
  126. multi[currentArgName].push_back(*argIter);
  127. break;
  128. default:
  129. unparsed.push_back(*argIter);
  130. break;
  131. }
  132. }
  133. // now iterate over the collected values and update their definition
  134. // within the current scope. undefine if necessary.
  135. for (options_map::const_iterator iter = options.begin(), end = options.end();
  136. iter != end; ++iter)
  137. {
  138. this->Makefile->AddDefinition(prefix + iter->first,
  139. iter->second? "TRUE": "FALSE");
  140. }
  141. for (single_map::const_iterator iter = single.begin(), end = single.end();
  142. iter != end; ++iter)
  143. {
  144. if (!iter->second.empty())
  145. {
  146. this->Makefile->AddDefinition(prefix + iter->first,
  147. iter->second.c_str());
  148. }
  149. else
  150. {
  151. this->Makefile->RemoveDefinition(prefix + iter->first);
  152. }
  153. }
  154. for (multi_map::const_iterator iter = multi.begin(), end = multi.end();
  155. iter != end; ++iter)
  156. {
  157. if (!iter->second.empty())
  158. {
  159. this->Makefile->AddDefinition(prefix + iter->first,
  160. cmJoin(cmMakeRange(iter->second), ";")
  161. .c_str());
  162. }
  163. else
  164. {
  165. this->Makefile->RemoveDefinition(prefix + iter->first);
  166. }
  167. }
  168. if (!unparsed.empty())
  169. {
  170. this->Makefile->AddDefinition(prefix + "UNPARSED_ARGUMENTS",
  171. cmJoin(cmMakeRange(unparsed), ";").c_str());
  172. }
  173. else
  174. {
  175. this->Makefile->RemoveDefinition(prefix + "UNPARSED_ARGUMENTS");
  176. }
  177. return true;
  178. }