cmWhileCommand.cxx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. cmListFileContext execContext = this->GetStartingContext();
  45. cmCommandContext commandContext;
  46. commandContext.Line = execContext.Line;
  47. commandContext.Name = execContext.Name;
  48. cmConditionEvaluator conditionEvaluator(
  49. mf,
  50. this->GetStartingContext(),
  51. mf.GetBacktrace(commandContext));
  52. bool isTrue = conditionEvaluator.IsTrue(
  53. expandedArguments, errorString, messageType);
  54. while (isTrue)
  55. {
  56. if (!errorString.empty())
  57. {
  58. std::string err = "had incorrect arguments: ";
  59. unsigned int i;
  60. for(i =0; i < this->Args.size(); ++i)
  61. {
  62. err += (this->Args[i].Delim?"\"":"");
  63. err += this->Args[i].Value;
  64. err += (this->Args[i].Delim?"\"":"");
  65. err += " ";
  66. }
  67. err += "(";
  68. err += errorString;
  69. err += ").";
  70. mf.IssueMessage(messageType, err);
  71. if (messageType == cmake::FATAL_ERROR)
  72. {
  73. cmSystemTools::SetFatalErrorOccured();
  74. return true;
  75. }
  76. }
  77. // Invoke all the functions that were collected in the block.
  78. for(unsigned int c = 0; c < this->Functions.size(); ++c)
  79. {
  80. cmExecutionStatus status;
  81. mf.ExecuteCommand(this->Functions[c],status);
  82. if (status.GetReturnInvoked())
  83. {
  84. inStatus.SetReturnInvoked(true);
  85. return true;
  86. }
  87. if (status.GetBreakInvoked())
  88. {
  89. return true;
  90. }
  91. if (status.GetContinueInvoked())
  92. {
  93. break;
  94. }
  95. if(cmSystemTools::GetFatalErrorOccured() )
  96. {
  97. return true;
  98. }
  99. }
  100. expandedArguments.clear();
  101. mf.ExpandArguments(this->Args, expandedArguments);
  102. isTrue = conditionEvaluator.IsTrue(
  103. expandedArguments, errorString, messageType);
  104. }
  105. return true;
  106. }
  107. else
  108. {
  109. // decrement for each nested while that ends
  110. this->Depth--;
  111. }
  112. }
  113. // record the command
  114. this->Functions.push_back(lff);
  115. // always return true
  116. return true;
  117. }
  118. bool cmWhileFunctionBlocker::
  119. ShouldRemove(const cmListFileFunction& lff, cmMakefile& )
  120. {
  121. if(!cmSystemTools::Strucmp(lff.Name.c_str(),"endwhile"))
  122. {
  123. // if the endwhile has arguments, then make sure
  124. // they match the arguments of the matching while
  125. if (lff.Arguments.empty() ||
  126. lff.Arguments == this->Args)
  127. {
  128. return true;
  129. }
  130. }
  131. return false;
  132. }
  133. bool cmWhileCommand
  134. ::InvokeInitialPass(const std::vector<cmListFileArgument>& args,
  135. cmExecutionStatus &)
  136. {
  137. if(args.size() < 1)
  138. {
  139. this->SetError("called with incorrect number of arguments");
  140. return false;
  141. }
  142. // create a function blocker
  143. cmWhileFunctionBlocker *f = new cmWhileFunctionBlocker(this->Makefile);
  144. f->Args = args;
  145. this->Makefile->AddFunctionBlocker(f);
  146. return true;
  147. }