cmForEachCommand.cxx 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 <cmsys/auto_ptr.hxx>
  12. cmForEachFunctionBlocker::cmForEachFunctionBlocker(cmMakefile* mf):
  13. Makefile(mf), Depth(0)
  14. {
  15. this->Makefile->PushLoopBlock();
  16. }
  17. cmForEachFunctionBlocker::~cmForEachFunctionBlocker()
  18. {
  19. this->Makefile->PopLoopBlock();
  20. }
  21. bool cmForEachFunctionBlocker::
  22. IsFunctionBlocked(const cmListFileFunction& lff, cmMakefile &mf,
  23. cmExecutionStatus &inStatus)
  24. {
  25. if (!cmSystemTools::Strucmp(lff.Name.c_str(),"foreach"))
  26. {
  27. // record the number of nested foreach commands
  28. this->Depth++;
  29. }
  30. else if (!cmSystemTools::Strucmp(lff.Name.c_str(),"endforeach"))
  31. {
  32. // if this is the endofreach for this statement
  33. if (!this->Depth)
  34. {
  35. // Remove the function blocker for this scope or bail.
  36. cmsys::auto_ptr<cmFunctionBlocker>
  37. fb(mf.RemoveFunctionBlocker(this, lff));
  38. if(!fb.get()) { return false; }
  39. // at end of for each execute recorded commands
  40. // store the old value
  41. std::string oldDef;
  42. if (mf.GetDefinition(this->Args[0]))
  43. {
  44. oldDef = mf.GetDefinition(this->Args[0]);
  45. }
  46. std::vector<std::string>::const_iterator j = this->Args.begin();
  47. ++j;
  48. for( ; j != this->Args.end(); ++j)
  49. {
  50. // set the variable to the loop value
  51. mf.AddDefinition(this->Args[0],j->c_str());
  52. // Invoke all the functions that were collected in the block.
  53. cmExecutionStatus status;
  54. for(unsigned int c = 0; c < this->Functions.size(); ++c)
  55. {
  56. status.Clear();
  57. mf.ExecuteCommand(this->Functions[c],status);
  58. if (status.GetReturnInvoked())
  59. {
  60. inStatus.SetReturnInvoked(true);
  61. // restore the variable to its prior value
  62. mf.AddDefinition(this->Args[0],oldDef.c_str());
  63. return true;
  64. }
  65. if (status.GetBreakInvoked())
  66. {
  67. // restore the variable to its prior value
  68. mf.AddDefinition(this->Args[0],oldDef.c_str());
  69. return true;
  70. }
  71. if (status.GetContinueInvoked())
  72. {
  73. break;
  74. }
  75. if(cmSystemTools::GetFatalErrorOccured() )
  76. {
  77. return true;
  78. }
  79. }
  80. }
  81. // restore the variable to its prior value
  82. mf.AddDefinition(this->Args[0],oldDef.c_str());
  83. return true;
  84. }
  85. else
  86. {
  87. // close out a nested foreach
  88. this->Depth--;
  89. }
  90. }
  91. // record the command
  92. this->Functions.push_back(lff);
  93. // always return true
  94. return true;
  95. }
  96. bool cmForEachFunctionBlocker::
  97. ShouldRemove(const cmListFileFunction& lff, cmMakefile& mf)
  98. {
  99. if(!cmSystemTools::Strucmp(lff.Name.c_str(),"endforeach"))
  100. {
  101. std::vector<std::string> expandedArguments;
  102. mf.ExpandArguments(lff.Arguments, expandedArguments);
  103. // if the endforeach has arguments then make sure
  104. // they match the begin foreach arguments
  105. if ((expandedArguments.empty() ||
  106. (expandedArguments[0] == this->Args[0])))
  107. {
  108. return true;
  109. }
  110. }
  111. return false;
  112. }
  113. bool cmForEachCommand
  114. ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
  115. {
  116. if(args.size() < 1)
  117. {
  118. this->SetError("called with incorrect number of arguments");
  119. return false;
  120. }
  121. if(args.size() > 1 && args[1] == "IN")
  122. {
  123. return this->HandleInMode(args);
  124. }
  125. // create a function blocker
  126. cmForEachFunctionBlocker *f = new cmForEachFunctionBlocker(this->Makefile);
  127. if ( args.size() > 1 )
  128. {
  129. if ( args[1] == "RANGE" )
  130. {
  131. int start = 0;
  132. int stop = 0;
  133. int step = 0;
  134. if ( args.size() == 3 )
  135. {
  136. stop = atoi(args[2].c_str());
  137. }
  138. if ( args.size() == 4 )
  139. {
  140. start = atoi(args[2].c_str());
  141. stop = atoi(args[3].c_str());
  142. }
  143. if ( args.size() == 5 )
  144. {
  145. start = atoi(args[2].c_str());
  146. stop = atoi(args[3].c_str());
  147. step = atoi(args[4].c_str());
  148. }
  149. if ( step == 0 )
  150. {
  151. if ( start > stop )
  152. {
  153. step = -1;
  154. }
  155. else
  156. {
  157. step = 1;
  158. }
  159. }
  160. if (
  161. (start > stop && step > 0) ||
  162. (start < stop && step < 0) ||
  163. step == 0
  164. )
  165. {
  166. std::ostringstream str;
  167. str << "called with incorrect range specification: start ";
  168. str << start << ", stop " << stop << ", step " << step;
  169. this->SetError(str.str());
  170. return false;
  171. }
  172. std::vector<std::string> range;
  173. char buffer[100];
  174. range.push_back(args[0]);
  175. int cc;
  176. for ( cc = start; ; cc += step )
  177. {
  178. if ( (step > 0 && cc > stop) || (step < 0 && cc < stop) )
  179. {
  180. break;
  181. }
  182. sprintf(buffer, "%d", cc);
  183. range.push_back(buffer);
  184. if ( cc == stop )
  185. {
  186. break;
  187. }
  188. }
  189. f->Args = range;
  190. }
  191. else
  192. {
  193. f->Args = args;
  194. }
  195. }
  196. else
  197. {
  198. f->Args = args;
  199. }
  200. this->Makefile->AddFunctionBlocker(f);
  201. return true;
  202. }
  203. //----------------------------------------------------------------------------
  204. bool cmForEachCommand::HandleInMode(std::vector<std::string> const& args)
  205. {
  206. cmsys::auto_ptr<cmForEachFunctionBlocker>
  207. f(new cmForEachFunctionBlocker(this->Makefile));
  208. f->Args.push_back(args[0]);
  209. enum Doing { DoingNone, DoingLists, DoingItems };
  210. Doing doing = DoingNone;
  211. for(unsigned int i=2; i < args.size(); ++i)
  212. {
  213. if(doing == DoingItems)
  214. {
  215. f->Args.push_back(args[i]);
  216. }
  217. else if(args[i] == "LISTS")
  218. {
  219. doing = DoingLists;
  220. }
  221. else if(args[i] == "ITEMS")
  222. {
  223. doing = DoingItems;
  224. }
  225. else if(doing == DoingLists)
  226. {
  227. const char* value = this->Makefile->GetDefinition(args[i]);
  228. if(value && *value)
  229. {
  230. cmSystemTools::ExpandListArgument(value, f->Args, true);
  231. }
  232. }
  233. else
  234. {
  235. std::ostringstream e;
  236. e << "Unknown argument:\n" << " " << args[i] << "\n";
  237. this->Makefile->IssueMessage(cmake::FATAL_ERROR, e.str());
  238. return true;
  239. }
  240. }
  241. this->Makefile->AddFunctionBlocker(f.release()); // TODO: pass auto_ptr
  242. return true;
  243. }