cmForEachCommand.cxx 4.8 KB

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