cmWhileCommand.cxx 3.1 KB

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