cmWhileCommand.cxx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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)
  14. , Depth(0)
  15. {
  16. this->Makefile->PushLoopBlock();
  17. }
  18. cmWhileFunctionBlocker::~cmWhileFunctionBlocker()
  19. {
  20. this->Makefile->PopLoopBlock();
  21. }
  22. bool cmWhileFunctionBlocker::IsFunctionBlocked(const cmListFileFunction& lff,
  23. cmMakefile& mf,
  24. cmExecutionStatus& inStatus)
  25. {
  26. // at end of for each execute recorded commands
  27. if (!cmSystemTools::Strucmp(lff.Name.c_str(), "while")) {
  28. // record the number of while commands past this one
  29. this->Depth++;
  30. } else if (!cmSystemTools::Strucmp(lff.Name.c_str(), "endwhile")) {
  31. // if this is the endwhile for this while loop then execute
  32. if (!this->Depth) {
  33. // Remove the function blocker for this scope or bail.
  34. CM_AUTO_PTR<cmFunctionBlocker> fb(mf.RemoveFunctionBlocker(this, lff));
  35. if (!fb.get()) {
  36. return false;
  37. }
  38. std::string errorString;
  39. std::vector<cmExpandedCommandArgument> expandedArguments;
  40. mf.ExpandArguments(this->Args, expandedArguments);
  41. cmake::MessageType messageType;
  42. cmListFileContext execContext = this->GetStartingContext();
  43. cmCommandContext commandContext;
  44. commandContext.Line = execContext.Line;
  45. commandContext.Name = execContext.Name;
  46. cmConditionEvaluator conditionEvaluator(mf, this->GetStartingContext(),
  47. mf.GetBacktrace(commandContext));
  48. bool isTrue =
  49. conditionEvaluator.IsTrue(expandedArguments, errorString, messageType);
  50. while (isTrue) {
  51. if (!errorString.empty()) {
  52. std::string err = "had incorrect arguments: ";
  53. unsigned int i;
  54. for (i = 0; i < this->Args.size(); ++i) {
  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. cmSystemTools::SetFatalErrorOccured();
  66. return true;
  67. }
  68. }
  69. // Invoke all the functions that were collected in the block.
  70. for (unsigned int c = 0; c < this->Functions.size(); ++c) {
  71. cmExecutionStatus status;
  72. mf.ExecuteCommand(this->Functions[c], status);
  73. if (status.GetReturnInvoked()) {
  74. inStatus.SetReturnInvoked(true);
  75. return true;
  76. }
  77. if (status.GetBreakInvoked()) {
  78. return true;
  79. }
  80. if (status.GetContinueInvoked()) {
  81. break;
  82. }
  83. if (cmSystemTools::GetFatalErrorOccured()) {
  84. return true;
  85. }
  86. }
  87. expandedArguments.clear();
  88. mf.ExpandArguments(this->Args, expandedArguments);
  89. isTrue = conditionEvaluator.IsTrue(expandedArguments, errorString,
  90. messageType);
  91. }
  92. return true;
  93. } else {
  94. // decrement for each nested while that ends
  95. this->Depth--;
  96. }
  97. }
  98. // record the command
  99. this->Functions.push_back(lff);
  100. // always return true
  101. return true;
  102. }
  103. bool cmWhileFunctionBlocker::ShouldRemove(const cmListFileFunction& lff,
  104. cmMakefile&)
  105. {
  106. if (!cmSystemTools::Strucmp(lff.Name.c_str(), "endwhile")) {
  107. // if the endwhile has arguments, then make sure
  108. // they match the arguments of the matching while
  109. if (lff.Arguments.empty() || lff.Arguments == this->Args) {
  110. return true;
  111. }
  112. }
  113. return false;
  114. }
  115. bool cmWhileCommand::InvokeInitialPass(
  116. const std::vector<cmListFileArgument>& args, cmExecutionStatus&)
  117. {
  118. if (args.empty()) {
  119. this->SetError("called with incorrect number of arguments");
  120. return false;
  121. }
  122. // create a function blocker
  123. cmWhileFunctionBlocker* f = new cmWhileFunctionBlocker(this->Makefile);
  124. f->Args = args;
  125. this->Makefile->AddFunctionBlocker(f);
  126. return true;
  127. }