cmParseArgumentsCommand.cxx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. // the second argument is a (cmake) list of options without argument
  39. std::vector<std::string> list;
  40. cmSystemTools::ExpandListArgument(*argIter++, list);
  41. for (std::vector<std::string>::const_iterator iter = list.begin(),
  42. end = list.end();
  43. iter != end; ++iter)
  44. {
  45. options[*iter]; // default initialize
  46. }
  47. // the third argument is a (cmake) list of single argument options
  48. list.clear();
  49. cmSystemTools::ExpandListArgument(*argIter++, list);
  50. for (std::vector<std::string>::const_iterator iter = list.begin(),
  51. end = list.end();
  52. iter != end; ++iter)
  53. {
  54. single[*iter]; // default initialize
  55. }
  56. // the fourth argument is a (cmake) list of multi argument options
  57. list.clear();
  58. cmSystemTools::ExpandListArgument(*argIter++, list);
  59. for (std::vector<std::string>::const_iterator iter = list.begin(),
  60. end = list.end();
  61. iter != end; ++iter)
  62. {
  63. multi[*iter]; // default initialize
  64. }
  65. enum insideValues
  66. {
  67. NONE,
  68. SINGLE,
  69. MULTI
  70. } insideValues = NONE;
  71. std::string currentArgName;
  72. // now iterate over the remaining arguments
  73. // and fill in the values where applicable
  74. for(; argIter != argEnd; ++argIter)
  75. {
  76. const options_map::iterator optIter = options.find(*argIter);
  77. if (optIter != options.end())
  78. {
  79. insideValues = NONE;
  80. optIter->second = true;
  81. continue;
  82. }
  83. const single_map::iterator singleIter = single.find(*argIter);
  84. if (singleIter != single.end())
  85. {
  86. insideValues = SINGLE;
  87. currentArgName = *argIter;
  88. continue;
  89. }
  90. const multi_map::iterator multiIter = multi.find(*argIter);
  91. if (multiIter != multi.end())
  92. {
  93. insideValues = MULTI;
  94. currentArgName = *argIter;
  95. continue;
  96. }
  97. switch(insideValues)
  98. {
  99. case SINGLE:
  100. single[currentArgName] = *argIter;
  101. insideValues = NONE;
  102. break;
  103. case MULTI:
  104. multi[currentArgName].push_back(*argIter);
  105. break;
  106. default:
  107. unparsed.push_back(*argIter);
  108. break;
  109. }
  110. }
  111. // now iterate over the collected values and update their definition
  112. // within the current scope. undefine if necessary.
  113. for (options_map::const_iterator iter = options.begin(), end = options.end();
  114. iter != end; ++iter)
  115. {
  116. this->Makefile->AddDefinition(prefix + iter->first,
  117. iter->second? "TRUE": "FALSE");
  118. }
  119. for (single_map::const_iterator iter = single.begin(), end = single.end();
  120. iter != end; ++iter)
  121. {
  122. if (!iter->second.empty())
  123. {
  124. this->Makefile->AddDefinition(prefix + iter->first,
  125. iter->second.c_str());
  126. }
  127. else
  128. {
  129. this->Makefile->RemoveDefinition(prefix + iter->first);
  130. }
  131. }
  132. for (multi_map::const_iterator iter = multi.begin(), end = multi.end();
  133. iter != end; ++iter)
  134. {
  135. if (!iter->second.empty())
  136. {
  137. this->Makefile->AddDefinition(prefix + iter->first,
  138. cmJoin(cmMakeRange(iter->second), ";")
  139. .c_str());
  140. }
  141. else
  142. {
  143. this->Makefile->RemoveDefinition(prefix + iter->first);
  144. }
  145. }
  146. if (!unparsed.empty())
  147. {
  148. this->Makefile->AddDefinition(prefix + "UNPARSED_ARGUMENTS",
  149. cmJoin(cmMakeRange(unparsed), ";").c_str());
  150. }
  151. else
  152. {
  153. this->Makefile->RemoveDefinition(prefix + "UNPARSED_ARGUMENTS");
  154. }
  155. return true;
  156. }