cmForEachCommand.cxx 6.6 KB

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