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