cmForEachCommand.cxx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 FOREACH statement that was not closed properly. Within the directory: ",
  80. mf.GetCurrentDirectory());
  81. }
  82. bool cmForEachCommand::InitialPass(std::vector<std::string> const& args)
  83. {
  84. if(args.size() < 1)
  85. {
  86. this->SetError("called with incorrect number of arguments");
  87. return false;
  88. }
  89. // create a function blocker
  90. cmForEachFunctionBlocker *f = new cmForEachFunctionBlocker();
  91. if ( args.size() > 1 )
  92. {
  93. if ( args[1] == "RANGE" )
  94. {
  95. int start = 0;
  96. int stop = 0;
  97. int step = 0;
  98. if ( args.size() == 3 )
  99. {
  100. stop = atoi(args[2].c_str());
  101. }
  102. if ( args.size() == 4 )
  103. {
  104. start = atoi(args[2].c_str());
  105. stop = atoi(args[3].c_str());
  106. }
  107. if ( args.size() == 5 )
  108. {
  109. start = atoi(args[2].c_str());
  110. stop = atoi(args[3].c_str());
  111. step = atoi(args[4].c_str());
  112. }
  113. if ( step == 0 )
  114. {
  115. if ( start > stop )
  116. {
  117. step = -1;
  118. }
  119. else
  120. {
  121. step = 1;
  122. }
  123. }
  124. if (
  125. (start > stop && step > 0) ||
  126. (start < stop && step < 0) ||
  127. step == 0
  128. )
  129. {
  130. cmOStringStream str;
  131. str << "called with incorrect range specification: start ";
  132. str << start << ", stop " << stop << ", step " << step;
  133. this->SetError(str.str().c_str());
  134. return false;
  135. }
  136. std::vector<std::string> range;
  137. char buffer[100];
  138. range.push_back(args[0]);
  139. int cc;
  140. for ( cc = start; ; cc += step )
  141. {
  142. if ( (step > 0 && cc > stop) || (step < 0 && cc < stop) )
  143. {
  144. break;
  145. }
  146. sprintf(buffer, "%d", cc);
  147. range.push_back(buffer);
  148. if ( cc == stop )
  149. {
  150. break;
  151. }
  152. }
  153. f->Args = range;
  154. }
  155. else
  156. {
  157. f->Args = args;
  158. }
  159. }
  160. else
  161. {
  162. f->Args = args;
  163. }
  164. this->Makefile->AddFunctionBlocker(f);
  165. return true;
  166. }