cmWhileCommand.cxx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. bool isTrue =
  38. cmIfCommand::IsTrue(expandedArguments,errorString,&mf);
  39. while (isTrue)
  40. {
  41. // Invoke all the functions that were collected in the block.
  42. for(unsigned int c = 0; c < this->Functions.size(); ++c)
  43. {
  44. cmExecutionStatus status;
  45. mf.ExecuteCommand(this->Functions[c],status);
  46. if (status.GetReturnInvoked())
  47. {
  48. inStatus.SetReturnInvoked(true);
  49. return true;
  50. }
  51. if (status.GetBreakInvoked())
  52. {
  53. return true;
  54. }
  55. }
  56. expandedArguments.clear();
  57. mf.ExpandArguments(this->Args, expandedArguments);
  58. isTrue =
  59. cmIfCommand::IsTrue(expandedArguments,errorString,&mf);
  60. }
  61. return true;
  62. }
  63. else
  64. {
  65. // decrement for each nested while that ends
  66. this->Depth--;
  67. }
  68. }
  69. // record the command
  70. this->Functions.push_back(lff);
  71. // always return true
  72. return true;
  73. }
  74. bool cmWhileFunctionBlocker::
  75. ShouldRemove(const cmListFileFunction& lff, cmMakefile& )
  76. {
  77. if(!cmSystemTools::Strucmp(lff.Name.c_str(),"endwhile"))
  78. {
  79. // if the endwhile has arguments, then make sure
  80. // they match the arguments of the matching while
  81. if (lff.Arguments.size() == 0 ||
  82. lff.Arguments == this->Args)
  83. {
  84. return true;
  85. }
  86. }
  87. return false;
  88. }
  89. bool cmWhileCommand
  90. ::InvokeInitialPass(const std::vector<cmListFileArgument>& args,
  91. cmExecutionStatus &)
  92. {
  93. if(args.size() < 1)
  94. {
  95. this->SetError("called with incorrect number of arguments");
  96. return false;
  97. }
  98. // create a function blocker
  99. cmWhileFunctionBlocker *f = new cmWhileFunctionBlocker();
  100. f->Args = args;
  101. this->Makefile->AddFunctionBlocker(f);
  102. return true;
  103. }