cmWhileCommand.cxx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. // Prevent recusion and don't let this blocker block its own
  20. // commands.
  21. if (this->Executing)
  22. {
  23. return false;
  24. }
  25. // at end of for each execute recorded commands
  26. if (!cmSystemTools::Strucmp(lff.Name.c_str(),"while"))
  27. {
  28. // record the number of while commands past this one
  29. this->Depth++;
  30. }
  31. else if (!cmSystemTools::Strucmp(lff.Name.c_str(),"endwhile"))
  32. {
  33. // if this is the endwhile for this while loop then execute
  34. if (!this->Depth)
  35. {
  36. char* errorString = 0;
  37. std::vector<std::string> expandedArguments;
  38. mf.ExpandArguments(this->Args, expandedArguments);
  39. bool isTrue =
  40. cmIfCommand::IsTrue(expandedArguments,&errorString,&mf);
  41. this->Executing = true;
  42. while (isTrue)
  43. {
  44. // Invoke all the functions that were collected in the block.
  45. for(unsigned int c = 0; c < this->Functions.size(); ++c)
  46. {
  47. cmExecutionStatus status;
  48. mf.ExecuteCommand(this->Functions[c],status);
  49. if (status.GetReturnInvoked())
  50. {
  51. inStatus.SetReturnInvoked(true);
  52. mf.RemoveFunctionBlocker(lff);
  53. return true;
  54. }
  55. if (status.GetBreakInvoked())
  56. {
  57. mf.RemoveFunctionBlocker(lff);
  58. return true;
  59. }
  60. }
  61. expandedArguments.clear();
  62. mf.ExpandArguments(this->Args, expandedArguments);
  63. isTrue =
  64. cmIfCommand::IsTrue(expandedArguments,&errorString,&mf);
  65. }
  66. mf.RemoveFunctionBlocker(lff);
  67. return true;
  68. }
  69. else
  70. {
  71. // decrement for each nested while that ends
  72. this->Depth--;
  73. }
  74. }
  75. // record the command
  76. this->Functions.push_back(lff);
  77. // always return true
  78. return true;
  79. }
  80. bool cmWhileFunctionBlocker::
  81. ShouldRemove(const cmListFileFunction& lff, cmMakefile& mf)
  82. {
  83. if(!cmSystemTools::Strucmp(lff.Name.c_str(),"endwhile"))
  84. {
  85. // if the endwhile has arguments, then make sure
  86. // they match the arguments of the matching while
  87. if (lff.Arguments.size() == 0 ||
  88. lff.Arguments == this->Args)
  89. {
  90. return true;
  91. }
  92. }
  93. return false;
  94. }
  95. void cmWhileFunctionBlocker::
  96. ScopeEnded(cmMakefile &mf)
  97. {
  98. cmSystemTools::Error(
  99. "The end of a CMakeLists file was reached with a WHILE statement that "
  100. "was not closed properly. Within the directory: ",
  101. mf.GetCurrentDirectory());
  102. }
  103. bool cmWhileCommand
  104. ::InvokeInitialPass(const std::vector<cmListFileArgument>& args,
  105. cmExecutionStatus &)
  106. {
  107. if(args.size() < 1)
  108. {
  109. this->SetError("called with incorrect number of arguments");
  110. return false;
  111. }
  112. // create a function blocker
  113. cmWhileFunctionBlocker *f = new cmWhileFunctionBlocker();
  114. f->Args = args;
  115. this->Makefile->AddFunctionBlocker(f);
  116. return true;
  117. }