cmForEachCommand.cxx 6.5 KB

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