1
0

cmWhileCommand.cxx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 "cmIfCommand.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. bool isTrue =
  36. cmIfCommand::IsTrue(expandedArguments,errorString,
  37. &mf, 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 =
  83. cmIfCommand::IsTrue(expandedArguments,errorString,
  84. &mf, 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. return true;
  128. }