cmWhileCommand.cxx 4.1 KB

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