cmForEachCommand.cxx 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. if (!cmSystemTools::Strucmp(lff.Name.c_str(),"foreach"))
  19. {
  20. // record the number of nested foreach commands
  21. this->Depth++;
  22. }
  23. else if (!cmSystemTools::Strucmp(lff.Name.c_str(),"endforeach"))
  24. {
  25. // if this is the endofreach for this statement
  26. if (!this->Depth)
  27. {
  28. // Remove the function blocker for this scope or bail.
  29. cmsys::auto_ptr<cmFunctionBlocker>
  30. fb(mf.RemoveFunctionBlocker(this, lff));
  31. if(!fb.get()) { return false; }
  32. // at end of for each execute recorded commands
  33. // store the old value
  34. std::string oldDef;
  35. if (mf.GetDefinition(this->Args[0].c_str()))
  36. {
  37. oldDef = mf.GetDefinition(this->Args[0].c_str());
  38. }
  39. std::vector<std::string>::const_iterator j = this->Args.begin();
  40. ++j;
  41. std::string tmps;
  42. cmListFileArgument arg;
  43. for( ; j != this->Args.end(); ++j)
  44. {
  45. // set the variable to the loop value
  46. mf.AddDefinition(this->Args[0].c_str(),j->c_str());
  47. // Invoke all the functions that were collected in the block.
  48. cmExecutionStatus status;
  49. for(unsigned int c = 0; c < this->Functions.size(); ++c)
  50. {
  51. status.Clear();
  52. mf.ExecuteCommand(this->Functions[c],status);
  53. if (status.GetReturnInvoked())
  54. {
  55. inStatus.SetReturnInvoked(true);
  56. // restore the variable to its prior value
  57. mf.AddDefinition(this->Args[0].c_str(),oldDef.c_str());
  58. return true;
  59. }
  60. if (status.GetBreakInvoked())
  61. {
  62. // restore the variable to its prior value
  63. mf.AddDefinition(this->Args[0].c_str(),oldDef.c_str());
  64. return true;
  65. }
  66. }
  67. }
  68. // restore the variable to its prior value
  69. mf.AddDefinition(this->Args[0].c_str(),oldDef.c_str());
  70. return true;
  71. }
  72. else
  73. {
  74. // close out a nested foreach
  75. this->Depth--;
  76. }
  77. }
  78. // record the command
  79. this->Functions.push_back(lff);
  80. // always return true
  81. return true;
  82. }
  83. bool cmForEachFunctionBlocker::
  84. ShouldRemove(const cmListFileFunction& lff, cmMakefile& mf)
  85. {
  86. if(!cmSystemTools::Strucmp(lff.Name.c_str(),"endforeach"))
  87. {
  88. std::vector<std::string> expandedArguments;
  89. mf.ExpandArguments(lff.Arguments, expandedArguments);
  90. // if the endforeach has arguments then make sure
  91. // they match the begin foreach arguments
  92. if ((expandedArguments.empty() ||
  93. (expandedArguments[0] == this->Args[0])))
  94. {
  95. return true;
  96. }
  97. }
  98. return false;
  99. }
  100. bool cmForEachCommand
  101. ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
  102. {
  103. if(args.size() < 1)
  104. {
  105. this->SetError("called with incorrect number of arguments");
  106. return false;
  107. }
  108. // create a function blocker
  109. cmForEachFunctionBlocker *f = new cmForEachFunctionBlocker();
  110. if ( args.size() > 1 )
  111. {
  112. if ( args[1] == "RANGE" )
  113. {
  114. int start = 0;
  115. int stop = 0;
  116. int step = 0;
  117. if ( args.size() == 3 )
  118. {
  119. stop = atoi(args[2].c_str());
  120. }
  121. if ( args.size() == 4 )
  122. {
  123. start = atoi(args[2].c_str());
  124. stop = atoi(args[3].c_str());
  125. }
  126. if ( args.size() == 5 )
  127. {
  128. start = atoi(args[2].c_str());
  129. stop = atoi(args[3].c_str());
  130. step = atoi(args[4].c_str());
  131. }
  132. if ( step == 0 )
  133. {
  134. if ( start > stop )
  135. {
  136. step = -1;
  137. }
  138. else
  139. {
  140. step = 1;
  141. }
  142. }
  143. if (
  144. (start > stop && step > 0) ||
  145. (start < stop && step < 0) ||
  146. step == 0
  147. )
  148. {
  149. cmOStringStream str;
  150. str << "called with incorrect range specification: start ";
  151. str << start << ", stop " << stop << ", step " << step;
  152. this->SetError(str.str().c_str());
  153. return false;
  154. }
  155. std::vector<std::string> range;
  156. char buffer[100];
  157. range.push_back(args[0]);
  158. int cc;
  159. for ( cc = start; ; cc += step )
  160. {
  161. if ( (step > 0 && cc > stop) || (step < 0 && cc < stop) )
  162. {
  163. break;
  164. }
  165. sprintf(buffer, "%d", cc);
  166. range.push_back(buffer);
  167. if ( cc == stop )
  168. {
  169. break;
  170. }
  171. }
  172. f->Args = range;
  173. }
  174. else
  175. {
  176. f->Args = args;
  177. }
  178. }
  179. else
  180. {
  181. f->Args = args;
  182. }
  183. this->Makefile->AddFunctionBlocker(f);
  184. return true;
  185. }