cmForEachCommand.cxx 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmForEachCommand.h"
  14. bool cmForEachFunctionBlocker::
  15. IsFunctionBlocked(const cmListFileFunction& lff, cmMakefile &mf,
  16. cmExecutionStatus &inStatus)
  17. {
  18. // Prevent recusion and don't let this blocker block its own
  19. // commands.
  20. if (this->Executing)
  21. {
  22. return false;
  23. }
  24. if (!cmSystemTools::Strucmp(lff.Name.c_str(),"foreach"))
  25. {
  26. // record the number of nested foreach commands
  27. this->Depth++;
  28. }
  29. else if (!cmSystemTools::Strucmp(lff.Name.c_str(),"endforeach"))
  30. {
  31. // if this is the endofreach for this statement
  32. if (!this->Depth)
  33. {
  34. // at end of for each execute recorded commands
  35. // store the old value
  36. std::string oldDef;
  37. if (mf.GetDefinition(this->Args[0].c_str()))
  38. {
  39. oldDef = mf.GetDefinition(this->Args[0].c_str());
  40. }
  41. this->Executing = true;
  42. std::vector<std::string>::const_iterator j = this->Args.begin();
  43. ++j;
  44. std::string tmps;
  45. cmListFileArgument arg;
  46. for( ; j != this->Args.end(); ++j)
  47. {
  48. // set the variable to the loop value
  49. mf.AddDefinition(this->Args[0].c_str(),j->c_str());
  50. // Invoke all the functions that were collected in the block.
  51. cmExecutionStatus status;
  52. for(unsigned int c = 0; c < this->Functions.size(); ++c)
  53. {
  54. status.Clear();
  55. mf.ExecuteCommand(this->Functions[c],status);
  56. if (status.GetReturnInvoked())
  57. {
  58. inStatus.SetReturnInvoked(true);
  59. // restore the variable to its prior value
  60. mf.AddDefinition(this->Args[0].c_str(),oldDef.c_str());
  61. mf.RemoveFunctionBlocker(lff);
  62. return true;
  63. }
  64. if (status.GetBreakInvoked())
  65. {
  66. // restore the variable to its prior value
  67. mf.AddDefinition(this->Args[0].c_str(),oldDef.c_str());
  68. mf.RemoveFunctionBlocker(lff);
  69. return true;
  70. }
  71. }
  72. }
  73. // restore the variable to its prior value
  74. mf.AddDefinition(this->Args[0].c_str(),oldDef.c_str());
  75. mf.RemoveFunctionBlocker(lff);
  76. return true;
  77. }
  78. else
  79. {
  80. // close out a nested foreach
  81. this->Depth--;
  82. }
  83. }
  84. // record the command
  85. this->Functions.push_back(lff);
  86. // always return true
  87. return true;
  88. }
  89. bool cmForEachFunctionBlocker::
  90. ShouldRemove(const cmListFileFunction& lff, cmMakefile& mf)
  91. {
  92. if(!cmSystemTools::Strucmp(lff.Name.c_str(),"endforeach"))
  93. {
  94. std::vector<std::string> expandedArguments;
  95. mf.ExpandArguments(lff.Arguments, expandedArguments);
  96. // if the endforeach has arguments then make sure
  97. // they match the begin foreach arguments
  98. if ((expandedArguments.empty() ||
  99. (expandedArguments[0] == this->Args[0])))
  100. {
  101. return true;
  102. }
  103. }
  104. return false;
  105. }
  106. void cmForEachFunctionBlocker::
  107. ScopeEnded(cmMakefile &mf)
  108. {
  109. cmSystemTools::Error("The end of a CMakeLists file was reached with a "
  110. "FOREACH statement that was not closed properly. "
  111. "Within the directory: ",
  112. mf.GetCurrentDirectory());
  113. }
  114. bool cmForEachCommand
  115. ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
  116. {
  117. if(args.size() < 1)
  118. {
  119. this->SetError("called with incorrect number of arguments");
  120. return false;
  121. }
  122. // create a function blocker
  123. cmForEachFunctionBlocker *f = new cmForEachFunctionBlocker();
  124. if ( args.size() > 1 )
  125. {
  126. if ( args[1] == "RANGE" )
  127. {
  128. int start = 0;
  129. int stop = 0;
  130. int step = 0;
  131. if ( args.size() == 3 )
  132. {
  133. stop = atoi(args[2].c_str());
  134. }
  135. if ( args.size() == 4 )
  136. {
  137. start = atoi(args[2].c_str());
  138. stop = atoi(args[3].c_str());
  139. }
  140. if ( args.size() == 5 )
  141. {
  142. start = atoi(args[2].c_str());
  143. stop = atoi(args[3].c_str());
  144. step = atoi(args[4].c_str());
  145. }
  146. if ( step == 0 )
  147. {
  148. if ( start > stop )
  149. {
  150. step = -1;
  151. }
  152. else
  153. {
  154. step = 1;
  155. }
  156. }
  157. if (
  158. (start > stop && step > 0) ||
  159. (start < stop && step < 0) ||
  160. step == 0
  161. )
  162. {
  163. cmOStringStream str;
  164. str << "called with incorrect range specification: start ";
  165. str << start << ", stop " << stop << ", step " << step;
  166. this->SetError(str.str().c_str());
  167. return false;
  168. }
  169. std::vector<std::string> range;
  170. char buffer[100];
  171. range.push_back(args[0]);
  172. int cc;
  173. for ( cc = start; ; cc += step )
  174. {
  175. if ( (step > 0 && cc > stop) || (step < 0 && cc < stop) )
  176. {
  177. break;
  178. }
  179. sprintf(buffer, "%d", cc);
  180. range.push_back(buffer);
  181. if ( cc == stop )
  182. {
  183. break;
  184. }
  185. }
  186. f->Args = range;
  187. }
  188. else
  189. {
  190. f->Args = args;
  191. }
  192. }
  193. else
  194. {
  195. f->Args = args;
  196. }
  197. this->Makefile->AddFunctionBlocker(f);
  198. return true;
  199. }