cmIfCommand.cxx 6.1 KB

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