cmForEachCommand.cxx 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmForEachCommand.h"
  11. #include <cm_auto_ptr.hxx>
  12. cmForEachFunctionBlocker::cmForEachFunctionBlocker(cmMakefile* mf)
  13. : Makefile(mf)
  14. , Depth(0)
  15. {
  16. this->Makefile->PushLoopBlock();
  17. }
  18. cmForEachFunctionBlocker::~cmForEachFunctionBlocker()
  19. {
  20. this->Makefile->PopLoopBlock();
  21. }
  22. bool cmForEachFunctionBlocker::IsFunctionBlocked(const cmListFileFunction& lff,
  23. cmMakefile& mf,
  24. cmExecutionStatus& inStatus)
  25. {
  26. if (!cmSystemTools::Strucmp(lff.Name.c_str(), "foreach")) {
  27. // record the number of nested foreach commands
  28. this->Depth++;
  29. } else if (!cmSystemTools::Strucmp(lff.Name.c_str(), "endforeach")) {
  30. // if this is the endofreach for this statement
  31. if (!this->Depth) {
  32. // Remove the function blocker for this scope or bail.
  33. CM_AUTO_PTR<cmFunctionBlocker> fb(mf.RemoveFunctionBlocker(this, lff));
  34. if (!fb.get()) {
  35. return false;
  36. }
  37. // at end of for each execute recorded commands
  38. // store the old value
  39. std::string oldDef;
  40. if (mf.GetDefinition(this->Args[0])) {
  41. oldDef = mf.GetDefinition(this->Args[0]);
  42. }
  43. std::vector<std::string>::const_iterator j = this->Args.begin();
  44. ++j;
  45. for (; j != this->Args.end(); ++j) {
  46. // set the variable to the loop value
  47. mf.AddDefinition(this->Args[0], j->c_str());
  48. // Invoke all the functions that were collected in the block.
  49. cmExecutionStatus status;
  50. for (unsigned int c = 0; c < this->Functions.size(); ++c) {
  51. status.Clear();
  52. mf.ExecuteCommand(this->Functions[c], status);
  53. if (status.GetReturnInvoked()) {
  54. inStatus.SetReturnInvoked(true);
  55. // restore the variable to its prior value
  56. mf.AddDefinition(this->Args[0], oldDef.c_str());
  57. return true;
  58. }
  59. if (status.GetBreakInvoked()) {
  60. // restore the variable to its prior value
  61. mf.AddDefinition(this->Args[0], oldDef.c_str());
  62. return true;
  63. }
  64. if (status.GetContinueInvoked()) {
  65. break;
  66. }
  67. if (cmSystemTools::GetFatalErrorOccured()) {
  68. return true;
  69. }
  70. }
  71. }
  72. // restore the variable to its prior value
  73. mf.AddDefinition(this->Args[0], oldDef.c_str());
  74. return true;
  75. } else {
  76. // close out a nested foreach
  77. this->Depth--;
  78. }
  79. }
  80. // record the command
  81. this->Functions.push_back(lff);
  82. // always return true
  83. return true;
  84. }
  85. bool cmForEachFunctionBlocker::ShouldRemove(const cmListFileFunction& lff,
  86. cmMakefile& mf)
  87. {
  88. if (!cmSystemTools::Strucmp(lff.Name.c_str(), "endforeach")) {
  89. std::vector<std::string> expandedArguments;
  90. mf.ExpandArguments(lff.Arguments, expandedArguments);
  91. // if the endforeach has arguments then make sure
  92. // they match the begin foreach arguments
  93. if ((expandedArguments.empty() ||
  94. (expandedArguments[0] == this->Args[0]))) {
  95. return true;
  96. }
  97. }
  98. return false;
  99. }
  100. bool cmForEachCommand::InitialPass(std::vector<std::string> const& args,
  101. cmExecutionStatus&)
  102. {
  103. if (args.size() < 1) {
  104. this->SetError("called with incorrect number of arguments");
  105. return false;
  106. }
  107. if (args.size() > 1 && args[1] == "IN") {
  108. return this->HandleInMode(args);
  109. }
  110. // create a function blocker
  111. cmForEachFunctionBlocker* f = new cmForEachFunctionBlocker(this->Makefile);
  112. if (args.size() > 1) {
  113. if (args[1] == "RANGE") {
  114. int start = 0;
  115. int stop = 0;
  116. int step = 0;
  117. if (args.size() == 3) {
  118. stop = atoi(args[2].c_str());
  119. }
  120. if (args.size() == 4) {
  121. start = atoi(args[2].c_str());
  122. stop = atoi(args[3].c_str());
  123. }
  124. if (args.size() == 5) {
  125. start = atoi(args[2].c_str());
  126. stop = atoi(args[3].c_str());
  127. step = atoi(args[4].c_str());
  128. }
  129. if (step == 0) {
  130. if (start > stop) {
  131. step = -1;
  132. } else {
  133. step = 1;
  134. }
  135. }
  136. if ((start > stop && step > 0) || (start < stop && step < 0) ||
  137. step == 0) {
  138. std::ostringstream str;
  139. str << "called with incorrect range specification: start ";
  140. str << start << ", stop " << stop << ", step " << step;
  141. this->SetError(str.str());
  142. return false;
  143. }
  144. std::vector<std::string> range;
  145. char buffer[100];
  146. range.push_back(args[0]);
  147. int cc;
  148. for (cc = start;; cc += step) {
  149. if ((step > 0 && cc > stop) || (step < 0 && cc < stop)) {
  150. break;
  151. }
  152. sprintf(buffer, "%d", cc);
  153. range.push_back(buffer);
  154. if (cc == stop) {
  155. break;
  156. }
  157. }
  158. f->Args = range;
  159. } else {
  160. f->Args = args;
  161. }
  162. } else {
  163. f->Args = args;
  164. }
  165. this->Makefile->AddFunctionBlocker(f);
  166. return true;
  167. }
  168. bool cmForEachCommand::HandleInMode(std::vector<std::string> const& args)
  169. {
  170. CM_AUTO_PTR<cmForEachFunctionBlocker> f(
  171. new cmForEachFunctionBlocker(this->Makefile));
  172. f->Args.push_back(args[0]);
  173. enum Doing
  174. {
  175. DoingNone,
  176. DoingLists,
  177. DoingItems
  178. };
  179. Doing doing = DoingNone;
  180. for (unsigned int i = 2; i < args.size(); ++i) {
  181. if (doing == DoingItems) {
  182. f->Args.push_back(args[i]);
  183. } else if (args[i] == "LISTS") {
  184. doing = DoingLists;
  185. } else if (args[i] == "ITEMS") {
  186. doing = DoingItems;
  187. } else if (doing == DoingLists) {
  188. const char* value = this->Makefile->GetDefinition(args[i]);
  189. if (value && *value) {
  190. cmSystemTools::ExpandListArgument(value, f->Args, true);
  191. }
  192. } else {
  193. std::ostringstream e;
  194. e << "Unknown argument:\n"
  195. << " " << args[i] << "\n";
  196. this->Makefile->IssueMessage(cmake::FATAL_ERROR, e.str());
  197. return true;
  198. }
  199. }
  200. this->Makefile->AddFunctionBlocker(f.release()); // TODO: pass auto_ptr
  201. return true;
  202. }