cmIfCommand.cxx 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 "cmIfCommand.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 "cmOutputConverter.h"
  15. #include "cmSystemTools.h"
  16. #include "cmake.h"
  17. #include <string>
  18. #include <utility>
  19. static std::string cmIfCommandError(
  20. std::vector<cmExpandedCommandArgument> const& args)
  21. {
  22. std::string err = "given arguments:\n ";
  23. for (cmExpandedCommandArgument const& i : args) {
  24. err += " ";
  25. err += cmOutputConverter::EscapeForCMake(i.GetValue());
  26. }
  27. err += "\n";
  28. return err;
  29. }
  30. class cmIfFunctionBlocker : public cmFunctionBlocker
  31. {
  32. public:
  33. cm::string_view StartCommandName() const override { return "if"_s; }
  34. cm::string_view EndCommandName() const override { return "endif"_s; }
  35. bool ArgumentsMatch(cmListFileFunction const& lff,
  36. cmMakefile&) const override;
  37. bool Replay(std::vector<cmListFileFunction> const& functions,
  38. cmExecutionStatus& inStatus) override;
  39. std::vector<cmListFileArgument> Args;
  40. bool IsBlocking;
  41. bool HasRun = false;
  42. bool ElseSeen = false;
  43. };
  44. bool cmIfFunctionBlocker::ArgumentsMatch(cmListFileFunction const& lff,
  45. cmMakefile&) const
  46. {
  47. return lff.Arguments.empty() || lff.Arguments == this->Args;
  48. }
  49. bool cmIfFunctionBlocker::Replay(
  50. std::vector<cmListFileFunction> const& functions,
  51. cmExecutionStatus& inStatus)
  52. {
  53. cmMakefile& mf = inStatus.GetMakefile();
  54. // execute the functions for the true parts of the if statement
  55. cmExecutionStatus status(mf);
  56. int scopeDepth = 0;
  57. for (cmListFileFunction const& func : functions) {
  58. // keep track of scope depth
  59. if (func.Name.Lower == "if") {
  60. scopeDepth++;
  61. }
  62. if (func.Name.Lower == "endif") {
  63. scopeDepth--;
  64. }
  65. // watch for our state change
  66. if (scopeDepth == 0 && func.Name.Lower == "else") {
  67. if (this->ElseSeen) {
  68. cmListFileBacktrace bt = mf.GetBacktrace(func);
  69. mf.GetCMakeInstance()->IssueMessage(
  70. MessageType::FATAL_ERROR,
  71. "A duplicate ELSE command was found inside an IF block.", bt);
  72. cmSystemTools::SetFatalErrorOccured();
  73. return true;
  74. }
  75. this->IsBlocking = this->HasRun;
  76. this->HasRun = true;
  77. this->ElseSeen = true;
  78. // if trace is enabled, print a (trivially) evaluated "else"
  79. // statement
  80. if (!this->IsBlocking && mf.GetCMakeInstance()->GetTrace()) {
  81. mf.PrintCommandTrace(func);
  82. }
  83. } else if (scopeDepth == 0 && func.Name.Lower == "elseif") {
  84. if (this->ElseSeen) {
  85. cmListFileBacktrace bt = mf.GetBacktrace(func);
  86. mf.GetCMakeInstance()->IssueMessage(
  87. MessageType::FATAL_ERROR,
  88. "An ELSEIF command was found after an ELSE command.", bt);
  89. cmSystemTools::SetFatalErrorOccured();
  90. return true;
  91. }
  92. if (this->HasRun) {
  93. this->IsBlocking = true;
  94. } else {
  95. // if trace is enabled, print the evaluated "elseif" statement
  96. if (mf.GetCMakeInstance()->GetTrace()) {
  97. mf.PrintCommandTrace(func);
  98. }
  99. std::string errorString;
  100. std::vector<cmExpandedCommandArgument> expandedArguments;
  101. mf.ExpandArguments(func.Arguments, expandedArguments);
  102. MessageType messType;
  103. cmListFileContext conditionContext =
  104. cmListFileContext::FromCommandContext(
  105. func, this->GetStartingContext().FilePath);
  106. cmConditionEvaluator conditionEvaluator(mf, conditionContext,
  107. mf.GetBacktrace(func));
  108. bool isTrue =
  109. conditionEvaluator.IsTrue(expandedArguments, errorString, messType);
  110. if (!errorString.empty()) {
  111. std::string err = cmIfCommandError(expandedArguments);
  112. err += errorString;
  113. cmListFileBacktrace bt = mf.GetBacktrace(func);
  114. mf.GetCMakeInstance()->IssueMessage(messType, err, bt);
  115. if (messType == MessageType::FATAL_ERROR) {
  116. cmSystemTools::SetFatalErrorOccured();
  117. return true;
  118. }
  119. }
  120. if (isTrue) {
  121. this->IsBlocking = false;
  122. this->HasRun = true;
  123. }
  124. }
  125. }
  126. // should we execute?
  127. else if (!this->IsBlocking) {
  128. status.Clear();
  129. mf.ExecuteCommand(func, status);
  130. if (status.GetReturnInvoked()) {
  131. inStatus.SetReturnInvoked();
  132. return true;
  133. }
  134. if (status.GetBreakInvoked()) {
  135. inStatus.SetBreakInvoked();
  136. return true;
  137. }
  138. if (status.GetContinueInvoked()) {
  139. inStatus.SetContinueInvoked();
  140. return true;
  141. }
  142. }
  143. }
  144. return true;
  145. }
  146. //=========================================================================
  147. bool cmIfCommand(std::vector<cmListFileArgument> const& args,
  148. cmExecutionStatus& inStatus)
  149. {
  150. cmMakefile& makefile = inStatus.GetMakefile();
  151. std::string errorString;
  152. std::vector<cmExpandedCommandArgument> expandedArguments;
  153. makefile.ExpandArguments(args, expandedArguments);
  154. MessageType status;
  155. cmConditionEvaluator conditionEvaluator(
  156. makefile, makefile.GetExecutionContext(), makefile.GetBacktrace());
  157. bool isTrue =
  158. conditionEvaluator.IsTrue(expandedArguments, errorString, status);
  159. if (!errorString.empty()) {
  160. std::string err = "if " + cmIfCommandError(expandedArguments);
  161. err += errorString;
  162. if (status == MessageType::FATAL_ERROR) {
  163. makefile.IssueMessage(MessageType::FATAL_ERROR, err);
  164. cmSystemTools::SetFatalErrorOccured();
  165. return true;
  166. }
  167. makefile.IssueMessage(status, err);
  168. }
  169. {
  170. auto fb = cm::make_unique<cmIfFunctionBlocker>();
  171. // if is isn't true block the commands
  172. fb->IsBlocking = !isTrue;
  173. if (isTrue) {
  174. fb->HasRun = true;
  175. }
  176. fb->Args = args;
  177. makefile.AddFunctionBlocker(std::move(fb));
  178. }
  179. return true;
  180. }