cmWhileCommand.cxx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmWhileCommand.h"
  11. #include "cmConditionEvaluator.h"
  12. cmWhileFunctionBlocker::cmWhileFunctionBlocker(cmMakefile* mf):
  13. Makefile(mf), Depth(0)
  14. {
  15. this->Makefile->PushLoopBlock();
  16. }
  17. cmWhileFunctionBlocker::~cmWhileFunctionBlocker()
  18. {
  19. this->Makefile->PopLoopBlock();
  20. }
  21. bool cmWhileFunctionBlocker::
  22. IsFunctionBlocked(const cmListFileFunction& lff, cmMakefile &mf,
  23. cmExecutionStatus &inStatus)
  24. {
  25. // at end of for each execute recorded commands
  26. if (!cmSystemTools::Strucmp(lff.Name.c_str(),"while"))
  27. {
  28. // record the number of while commands past this one
  29. this->Depth++;
  30. }
  31. else if (!cmSystemTools::Strucmp(lff.Name.c_str(),"endwhile"))
  32. {
  33. // if this is the endwhile for this while loop then execute
  34. if (!this->Depth)
  35. {
  36. // Remove the function blocker for this scope or bail.
  37. cmsys::auto_ptr<cmFunctionBlocker>
  38. fb(mf.RemoveFunctionBlocker(this, lff));
  39. if(!fb.get()) { return false; }
  40. std::string errorString;
  41. std::vector<cmExpandedCommandArgument> expandedArguments;
  42. mf.ExpandArguments(this->Args, expandedArguments);
  43. cmake::MessageType messageType;
  44. cmConditionEvaluator conditionEvaluator(mf);
  45. bool isTrue = conditionEvaluator.IsTrue(
  46. expandedArguments, errorString, messageType);
  47. while (isTrue)
  48. {
  49. if (!errorString.empty())
  50. {
  51. std::string err = "had incorrect arguments: ";
  52. unsigned int i;
  53. for(i =0; i < this->Args.size(); ++i)
  54. {
  55. err += (this->Args[i].Delim?"\"":"");
  56. err += this->Args[i].Value;
  57. err += (this->Args[i].Delim?"\"":"");
  58. err += " ";
  59. }
  60. err += "(";
  61. err += errorString;
  62. err += ").";
  63. mf.IssueMessage(messageType, err);
  64. if (messageType == cmake::FATAL_ERROR)
  65. {
  66. cmSystemTools::SetFatalErrorOccured();
  67. return true;
  68. }
  69. }
  70. // Invoke all the functions that were collected in the block.
  71. for(unsigned int c = 0; c < this->Functions.size(); ++c)
  72. {
  73. cmExecutionStatus status;
  74. mf.ExecuteCommand(this->Functions[c],status);
  75. if (status.GetReturnInvoked())
  76. {
  77. inStatus.SetReturnInvoked(true);
  78. return true;
  79. }
  80. if (status.GetBreakInvoked())
  81. {
  82. return true;
  83. }
  84. if (status.GetContinueInvoked())
  85. {
  86. break;
  87. }
  88. if(cmSystemTools::GetFatalErrorOccured() )
  89. {
  90. return true;
  91. }
  92. }
  93. expandedArguments.clear();
  94. mf.ExpandArguments(this->Args, expandedArguments);
  95. isTrue = conditionEvaluator.IsTrue(
  96. expandedArguments, errorString, messageType);
  97. }
  98. return true;
  99. }
  100. else
  101. {
  102. // decrement for each nested while that ends
  103. this->Depth--;
  104. }
  105. }
  106. // record the command
  107. this->Functions.push_back(lff);
  108. // always return true
  109. return true;
  110. }
  111. bool cmWhileFunctionBlocker::
  112. ShouldRemove(const cmListFileFunction& lff, cmMakefile& )
  113. {
  114. if(!cmSystemTools::Strucmp(lff.Name.c_str(),"endwhile"))
  115. {
  116. // if the endwhile has arguments, then make sure
  117. // they match the arguments of the matching while
  118. if (lff.Arguments.empty() ||
  119. lff.Arguments == this->Args)
  120. {
  121. return true;
  122. }
  123. }
  124. return false;
  125. }
  126. bool cmWhileCommand
  127. ::InvokeInitialPass(const std::vector<cmListFileArgument>& args,
  128. cmExecutionStatus &)
  129. {
  130. if(args.size() < 1)
  131. {
  132. this->SetError("called with incorrect number of arguments");
  133. return false;
  134. }
  135. // create a function blocker
  136. cmWhileFunctionBlocker *f = new cmWhileFunctionBlocker(this->Makefile);
  137. f->Args = args;
  138. this->Makefile->AddFunctionBlocker(f);
  139. return true;
  140. }