cmForEachCommand.cxx 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 (m_Executing)
  20. {
  21. return false;
  22. }
  23. // at end of for each execute recorded commands
  24. if (lff.m_Name == "ENDFOREACH")
  25. {
  26. std::vector<std::string> expandedArguments;
  27. mf.ExpandArguments(lff.m_Arguments, expandedArguments);
  28. if(!expandedArguments.empty() && (expandedArguments[0] == m_Args[0]))
  29. {
  30. m_Executing = true;
  31. std::string variable = "${";
  32. variable += m_Args[0];
  33. variable += "}";
  34. std::vector<std::string>::const_iterator j = m_Args.begin();
  35. ++j;
  36. std::string tmps;
  37. cmListFileArgument arg;
  38. for( ; j != m_Args.end(); ++j)
  39. {
  40. // Invoke all the functions that were collected in the block.
  41. for(unsigned int c = 0; c < m_Functions.size(); ++c)
  42. {
  43. // Replace the loop variable and then invoke the command.
  44. cmListFileFunction newLFF;
  45. newLFF.m_Name = m_Functions[c].m_Name;
  46. newLFF.m_FilePath = m_Functions[c].m_FilePath;
  47. newLFF.m_Line = m_Functions[c].m_Line;
  48. for (std::vector<cmListFileArgument>::const_iterator k =
  49. m_Functions[c].m_Arguments.begin();
  50. k != m_Functions[c].m_Arguments.end(); ++k)
  51. {
  52. tmps = k->Value;
  53. cmSystemTools::ReplaceString(tmps, variable.c_str(), j->c_str());
  54. arg.Value = tmps;
  55. arg.Quoted = k->Quoted;
  56. arg.FilePath = k->FilePath;
  57. arg.Line = k->Line;
  58. newLFF.m_Arguments.push_back(arg);
  59. }
  60. mf.ExecuteCommand(newLFF);
  61. }
  62. }
  63. mf.RemoveFunctionBlocker(lff);
  64. return true;
  65. }
  66. }
  67. // record the command
  68. m_Functions.push_back(lff);
  69. // always return true
  70. return true;
  71. }
  72. bool cmForEachFunctionBlocker::
  73. ShouldRemove(const cmListFileFunction& lff, cmMakefile& mf)
  74. {
  75. if(lff.m_Name == "ENDFOREACH")
  76. {
  77. std::vector<std::string> expandedArguments;
  78. mf.ExpandArguments(lff.m_Arguments, expandedArguments);
  79. if(!expandedArguments.empty() && (expandedArguments[0] == m_Args[0]))
  80. {
  81. return true;
  82. }
  83. }
  84. return false;
  85. }
  86. void cmForEachFunctionBlocker::
  87. ScopeEnded(cmMakefile &mf)
  88. {
  89. cmSystemTools::Error("The end of a CMakeLists file was reached with a FOREACH statement that was not closed properly. Within the directory: ",
  90. mf.GetCurrentDirectory());
  91. }
  92. bool cmForEachCommand::InitialPass(std::vector<std::string> const& args)
  93. {
  94. if(args.size() < 1)
  95. {
  96. this->SetError("called with incorrect number of arguments");
  97. return false;
  98. }
  99. // create a function blocker
  100. cmForEachFunctionBlocker *f = new cmForEachFunctionBlocker();
  101. if ( args.size() > 1 )
  102. {
  103. if ( args[1] == "RANGE" )
  104. {
  105. int start = 0;
  106. int stop = 0;
  107. int step = 0;
  108. if ( args.size() == 3 )
  109. {
  110. stop = atoi(args[2].c_str());
  111. }
  112. if ( args.size() == 4 )
  113. {
  114. start = atoi(args[2].c_str());
  115. stop = atoi(args[3].c_str());
  116. }
  117. if ( args.size() == 5 )
  118. {
  119. start = atoi(args[2].c_str());
  120. stop = atoi(args[3].c_str());
  121. step = atoi(args[4].c_str());
  122. }
  123. if ( step == 0 )
  124. {
  125. if ( start > stop )
  126. {
  127. step = -1;
  128. }
  129. else
  130. {
  131. step = 1;
  132. }
  133. }
  134. if (
  135. (start > stop && step > 0) ||
  136. (start < stop && step < 0) ||
  137. step == 0
  138. )
  139. {
  140. cmOStringStream str;
  141. str << "called with incorrect range specification: start ";
  142. str << start << ", stop " << stop << ", step " << step;
  143. this->SetError(str.str().c_str());
  144. return false;
  145. }
  146. std::vector<std::string> range;
  147. char buffer[100];
  148. range.push_back(args[0]);
  149. int cc;
  150. for ( cc = start; ; cc += step )
  151. {
  152. if ( (step > 0 && cc > stop) || (step < 0 && cc < stop) )
  153. {
  154. break;
  155. }
  156. sprintf(buffer, "%d", cc);
  157. range.push_back(buffer);
  158. if ( cc == stop )
  159. {
  160. break;
  161. }
  162. }
  163. f->m_Args = range;
  164. }
  165. else
  166. {
  167. f->m_Args = args;
  168. }
  169. }
  170. else
  171. {
  172. f->m_Args = args;
  173. }
  174. m_Makefile->AddFunctionBlocker(f);
  175. return true;
  176. }