cmForEachCommand.cxx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. {
  17. // Prevent recusion and don't let this blobker block its own
  18. // commands.
  19. if (this->Executing)
  20. {
  21. return false;
  22. }
  23. if (!cmSystemTools::Strucmp(lff.Name.c_str(),"foreach"))
  24. {
  25. // record the number of nested foreach commands
  26. this->Depth++;
  27. }
  28. else if (!cmSystemTools::Strucmp(lff.Name.c_str(),"endforeach"))
  29. {
  30. // if this is the endofreach for this statement
  31. if (!this->Depth)
  32. {
  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. this->Executing = true;
  41. std::vector<std::string>::const_iterator j = this->Args.begin();
  42. ++j;
  43. std::string tmps;
  44. cmListFileArgument arg;
  45. for( ; j != this->Args.end(); ++j)
  46. {
  47. // set the variable to the loop value
  48. mf.AddDefinition(this->Args[0].c_str(),j->c_str());
  49. // Invoke all the functions that were collected in the block.
  50. for(unsigned int c = 0; c < this->Functions.size(); ++c)
  51. {
  52. mf.ExecuteCommand(this->Functions[c]);
  53. }
  54. }
  55. // restore the variable to its prior value
  56. mf.AddDefinition(this->Args[0].c_str(),oldDef.c_str());
  57. mf.RemoveFunctionBlocker(lff);
  58. return true;
  59. }
  60. else
  61. {
  62. // close out a nested foreach
  63. this->Depth--;
  64. }
  65. }
  66. // record the command
  67. this->Functions.push_back(lff);
  68. // always return true
  69. return true;
  70. }
  71. bool cmForEachFunctionBlocker::
  72. ShouldRemove(const cmListFileFunction& lff, cmMakefile& mf)
  73. {
  74. if(!cmSystemTools::Strucmp(lff.Name.c_str(),"endforeach"))
  75. {
  76. std::vector<std::string> expandedArguments;
  77. mf.ExpandArguments(lff.Arguments, expandedArguments);
  78. if ((!expandedArguments.empty() &&
  79. (expandedArguments[0] == this->Args[0]))
  80. || mf.IsOn("CMAKE_ALLOW_LOOSE_LOOP_CONSTRUCTS"))
  81. {
  82. return true;
  83. }
  84. }
  85. return false;
  86. }
  87. void cmForEachFunctionBlocker::
  88. ScopeEnded(cmMakefile &mf)
  89. {
  90. cmSystemTools::Error("The end of a CMakeLists file was reached with a "
  91. "FOREACH statement that was not closed properly. "
  92. "Within the directory: ",
  93. mf.GetCurrentDirectory());
  94. }
  95. bool cmForEachCommand::InitialPass(std::vector<std::string> const& args)
  96. {
  97. if(args.size() < 1)
  98. {
  99. this->SetError("called with incorrect number of arguments");
  100. return false;
  101. }
  102. // create a function blocker
  103. cmForEachFunctionBlocker *f = new cmForEachFunctionBlocker();
  104. if ( args.size() > 1 )
  105. {
  106. if ( args[1] == "RANGE" )
  107. {
  108. int start = 0;
  109. int stop = 0;
  110. int step = 0;
  111. if ( args.size() == 3 )
  112. {
  113. stop = atoi(args[2].c_str());
  114. }
  115. if ( args.size() == 4 )
  116. {
  117. start = atoi(args[2].c_str());
  118. stop = atoi(args[3].c_str());
  119. }
  120. if ( args.size() == 5 )
  121. {
  122. start = atoi(args[2].c_str());
  123. stop = atoi(args[3].c_str());
  124. step = atoi(args[4].c_str());
  125. }
  126. if ( step == 0 )
  127. {
  128. if ( start > stop )
  129. {
  130. step = -1;
  131. }
  132. else
  133. {
  134. step = 1;
  135. }
  136. }
  137. if (
  138. (start > stop && step > 0) ||
  139. (start < stop && step < 0) ||
  140. step == 0
  141. )
  142. {
  143. cmOStringStream str;
  144. str << "called with incorrect range specification: start ";
  145. str << start << ", stop " << stop << ", step " << step;
  146. this->SetError(str.str().c_str());
  147. return false;
  148. }
  149. std::vector<std::string> range;
  150. char buffer[100];
  151. range.push_back(args[0]);
  152. int cc;
  153. for ( cc = start; ; cc += step )
  154. {
  155. if ( (step > 0 && cc > stop) || (step < 0 && cc < stop) )
  156. {
  157. break;
  158. }
  159. sprintf(buffer, "%d", cc);
  160. range.push_back(buffer);
  161. if ( cc == stop )
  162. {
  163. break;
  164. }
  165. }
  166. f->Args = range;
  167. }
  168. else
  169. {
  170. f->Args = args;
  171. }
  172. }
  173. else
  174. {
  175. f->Args = args;
  176. }
  177. this->Makefile->AddFunctionBlocker(f);
  178. return true;
  179. }