cmForEachCommand.cxx 4.6 KB

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