cmWhileCommand.cxx 3.9 KB

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