cmWhileCommand.cxx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmWhileCommand.h"
  14. #include "cmIfCommand.h"
  15. bool cmWhileFunctionBlocker::
  16. IsFunctionBlocked(const cmListFileFunction& lff, cmMakefile &mf,
  17. cmExecutionStatus &inStatus)
  18. {
  19. // at end of for each execute recorded commands
  20. if (!cmSystemTools::Strucmp(lff.Name.c_str(),"while"))
  21. {
  22. // record the number of while commands past this one
  23. this->Depth++;
  24. }
  25. else if (!cmSystemTools::Strucmp(lff.Name.c_str(),"endwhile"))
  26. {
  27. // if this is the endwhile for this while loop then execute
  28. if (!this->Depth)
  29. {
  30. // Remove the function blocker for this scope or bail.
  31. cmsys::auto_ptr<cmFunctionBlocker>
  32. fb(mf.RemoveFunctionBlocker(this, lff));
  33. if(!fb.get()) { return false; }
  34. std::string errorString;
  35. std::vector<std::string> expandedArguments;
  36. mf.ExpandArguments(this->Args, expandedArguments);
  37. cmake::MessageType messageType;
  38. bool isTrue =
  39. cmIfCommand::IsTrue(expandedArguments,errorString,
  40. &mf, messageType);
  41. while (isTrue)
  42. {
  43. if (errorString.size())
  44. {
  45. std::string err = "had incorrect arguments: ";
  46. unsigned int i;
  47. for(i =0; i < this->Args.size(); ++i)
  48. {
  49. err += (this->Args[i].Quoted?"\"":"");
  50. err += this->Args[i].Value;
  51. err += (this->Args[i].Quoted?"\"":"");
  52. err += " ";
  53. }
  54. err += "(";
  55. err += errorString;
  56. err += ").";
  57. mf.IssueMessage(messageType, err);
  58. if (messageType == cmake::FATAL_ERROR)
  59. {
  60. cmSystemTools::SetFatalErrorOccured();
  61. return true;
  62. }
  63. }
  64. // Invoke all the functions that were collected in the block.
  65. for(unsigned int c = 0; c < this->Functions.size(); ++c)
  66. {
  67. cmExecutionStatus status;
  68. mf.ExecuteCommand(this->Functions[c],status);
  69. if (status.GetReturnInvoked())
  70. {
  71. inStatus.SetReturnInvoked(true);
  72. return true;
  73. }
  74. if (status.GetBreakInvoked())
  75. {
  76. return true;
  77. }
  78. if(cmSystemTools::GetFatalErrorOccured() )
  79. {
  80. return true;
  81. }
  82. }
  83. expandedArguments.clear();
  84. mf.ExpandArguments(this->Args, expandedArguments);
  85. isTrue =
  86. cmIfCommand::IsTrue(expandedArguments,errorString,
  87. &mf, messageType);
  88. }
  89. return true;
  90. }
  91. else
  92. {
  93. // decrement for each nested while that ends
  94. this->Depth--;
  95. }
  96. }
  97. // record the command
  98. this->Functions.push_back(lff);
  99. // always return true
  100. return true;
  101. }
  102. bool cmWhileFunctionBlocker::
  103. ShouldRemove(const cmListFileFunction& lff, cmMakefile& )
  104. {
  105. if(!cmSystemTools::Strucmp(lff.Name.c_str(),"endwhile"))
  106. {
  107. // if the endwhile has arguments, then make sure
  108. // they match the arguments of the matching while
  109. if (lff.Arguments.size() == 0 ||
  110. lff.Arguments == this->Args)
  111. {
  112. return true;
  113. }
  114. }
  115. return false;
  116. }
  117. bool cmWhileCommand
  118. ::InvokeInitialPass(const std::vector<cmListFileArgument>& args,
  119. cmExecutionStatus &)
  120. {
  121. if(args.size() < 1)
  122. {
  123. this->SetError("called with incorrect number of arguments");
  124. return false;
  125. }
  126. // create a function blocker
  127. cmWhileFunctionBlocker *f = new cmWhileFunctionBlocker();
  128. f->Args = args;
  129. this->Makefile->AddFunctionBlocker(f);
  130. return true;
  131. }