cmWhileCommand.cxx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmWhileCommand.h"
  4. #include <string>
  5. #include <utility>
  6. #include <cm/memory>
  7. #include <cm/string_view>
  8. #include <cmext/string_view>
  9. #include "cmConditionEvaluator.h"
  10. #include "cmExecutionStatus.h"
  11. #include "cmExpandedCommandArgument.h"
  12. #include "cmFunctionBlocker.h"
  13. #include "cmListFileCache.h"
  14. #include "cmMakefile.h"
  15. #include "cmMessageType.h"
  16. #include "cmSystemTools.h"
  17. #include "cmake.h"
  18. class cmWhileFunctionBlocker : public cmFunctionBlocker
  19. {
  20. public:
  21. cmWhileFunctionBlocker(cmMakefile* mf);
  22. ~cmWhileFunctionBlocker() override;
  23. cm::string_view StartCommandName() const override { return "while"_s; }
  24. cm::string_view EndCommandName() const override { return "endwhile"_s; }
  25. bool ArgumentsMatch(cmListFileFunction const& lff,
  26. cmMakefile& mf) const override;
  27. bool Replay(std::vector<cmListFileFunction> functions,
  28. cmExecutionStatus& inStatus) override;
  29. std::vector<cmListFileArgument> Args;
  30. private:
  31. cmMakefile* Makefile;
  32. };
  33. cmWhileFunctionBlocker::cmWhileFunctionBlocker(cmMakefile* mf)
  34. : Makefile(mf)
  35. {
  36. this->Makefile->PushLoopBlock();
  37. }
  38. cmWhileFunctionBlocker::~cmWhileFunctionBlocker()
  39. {
  40. this->Makefile->PopLoopBlock();
  41. }
  42. bool cmWhileFunctionBlocker::ArgumentsMatch(cmListFileFunction const& lff,
  43. cmMakefile&) const
  44. {
  45. return lff.Arguments().empty() || lff.Arguments() == this->Args;
  46. }
  47. bool cmWhileFunctionBlocker::Replay(std::vector<cmListFileFunction> functions,
  48. cmExecutionStatus& inStatus)
  49. {
  50. cmMakefile& mf = inStatus.GetMakefile();
  51. std::string errorString;
  52. std::vector<cmExpandedCommandArgument> expandedArguments;
  53. mf.ExpandArguments(this->Args, expandedArguments);
  54. MessageType messageType;
  55. cmListFileBacktrace whileBT =
  56. mf.GetBacktrace().Push(this->GetStartingContext());
  57. cmConditionEvaluator conditionEvaluator(mf, whileBT);
  58. bool isTrue =
  59. conditionEvaluator.IsTrue(expandedArguments, errorString, messageType);
  60. while (isTrue) {
  61. if (!errorString.empty()) {
  62. std::string err = "had incorrect arguments: ";
  63. for (cmListFileArgument const& arg : this->Args) {
  64. err += (arg.Delim ? "\"" : "");
  65. err += arg.Value;
  66. err += (arg.Delim ? "\"" : "");
  67. err += " ";
  68. }
  69. err += "(";
  70. err += errorString;
  71. err += ").";
  72. mf.GetCMakeInstance()->IssueMessage(messageType, err, whileBT);
  73. if (messageType == MessageType::FATAL_ERROR) {
  74. cmSystemTools::SetFatalErrorOccured();
  75. return true;
  76. }
  77. }
  78. // Invoke all the functions that were collected in the block.
  79. for (cmListFileFunction const& fn : functions) {
  80. cmExecutionStatus status(mf);
  81. mf.ExecuteCommand(fn, status);
  82. if (status.GetReturnInvoked()) {
  83. inStatus.SetReturnInvoked();
  84. return true;
  85. }
  86. if (status.GetBreakInvoked()) {
  87. return true;
  88. }
  89. if (status.GetContinueInvoked()) {
  90. break;
  91. }
  92. if (cmSystemTools::GetFatalErrorOccured()) {
  93. return true;
  94. }
  95. }
  96. expandedArguments.clear();
  97. mf.ExpandArguments(this->Args, expandedArguments);
  98. isTrue =
  99. conditionEvaluator.IsTrue(expandedArguments, errorString, messageType);
  100. }
  101. return true;
  102. }
  103. bool cmWhileCommand(std::vector<cmListFileArgument> const& args,
  104. cmExecutionStatus& status)
  105. {
  106. if (args.empty()) {
  107. status.SetError("called with incorrect number of arguments");
  108. return false;
  109. }
  110. // create a function blocker
  111. {
  112. cmMakefile& makefile = status.GetMakefile();
  113. auto fb = cm::make_unique<cmWhileFunctionBlocker>(&makefile);
  114. fb->Args = args;
  115. makefile.AddFunctionBlocker(std::move(fb));
  116. }
  117. return true;
  118. }