cmForEachCommand.cxx 6.8 KB

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