cmWhileCommand.cxx 4.9 KB

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