cmIfCommand.cxx 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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.LowerCaseName() == "if") {
  59. scopeDepth++;
  60. }
  61. if (func.LowerCaseName() == "endif") {
  62. scopeDepth--;
  63. }
  64. // watch for our state change
  65. if (scopeDepth == 0 && func.LowerCaseName() == "else") {
  66. if (this->ElseSeen) {
  67. cmListFileBacktrace elseBT = mf.GetBacktrace().Push(cmListFileContext{
  68. func.OriginalName(), this->GetStartingContext().FilePath,
  69. func.Line() });
  70. mf.GetCMakeInstance()->IssueMessage(
  71. MessageType::FATAL_ERROR,
  72. "A duplicate ELSE command was found inside an IF block.", elseBT);
  73. cmSystemTools::SetFatalErrorOccured();
  74. return true;
  75. }
  76. this->IsBlocking = this->HasRun;
  77. this->HasRun = true;
  78. this->ElseSeen = true;
  79. // if trace is enabled, print a (trivially) evaluated "else"
  80. // statement
  81. if (!this->IsBlocking && mf.GetCMakeInstance()->GetTrace()) {
  82. mf.PrintCommandTrace(func);
  83. }
  84. } else if (scopeDepth == 0 && func.LowerCaseName() == "elseif") {
  85. cmListFileBacktrace elseifBT = mf.GetBacktrace().Push(
  86. cmListFileContext{ func.OriginalName(),
  87. this->GetStartingContext().FilePath, func.Line() });
  88. if (this->ElseSeen) {
  89. mf.GetCMakeInstance()->IssueMessage(
  90. MessageType::FATAL_ERROR,
  91. "An ELSEIF command was found after an ELSE command.", elseifBT);
  92. cmSystemTools::SetFatalErrorOccured();
  93. return true;
  94. }
  95. if (this->HasRun) {
  96. this->IsBlocking = true;
  97. } else {
  98. // if trace is enabled, print the evaluated "elseif" statement
  99. if (mf.GetCMakeInstance()->GetTrace()) {
  100. mf.PrintCommandTrace(func);
  101. }
  102. std::string errorString;
  103. std::vector<cmExpandedCommandArgument> expandedArguments;
  104. mf.ExpandArguments(func.Arguments(), expandedArguments);
  105. MessageType messType;
  106. cmConditionEvaluator conditionEvaluator(mf, elseifBT);
  107. bool isTrue =
  108. conditionEvaluator.IsTrue(expandedArguments, errorString, messType);
  109. if (!errorString.empty()) {
  110. std::string err =
  111. cmStrCat(cmIfCommandError(expandedArguments), errorString);
  112. mf.GetCMakeInstance()->IssueMessage(messType, err, elseifBT);
  113. if (messType == MessageType::FATAL_ERROR) {
  114. cmSystemTools::SetFatalErrorOccured();
  115. return true;
  116. }
  117. }
  118. if (isTrue) {
  119. this->IsBlocking = false;
  120. this->HasRun = true;
  121. }
  122. }
  123. }
  124. // should we execute?
  125. else if (!this->IsBlocking) {
  126. cmExecutionStatus status(mf);
  127. mf.ExecuteCommand(func, status);
  128. if (status.GetReturnInvoked()) {
  129. inStatus.SetReturnInvoked();
  130. return true;
  131. }
  132. if (status.GetBreakInvoked()) {
  133. inStatus.SetBreakInvoked();
  134. return true;
  135. }
  136. if (status.GetContinueInvoked()) {
  137. inStatus.SetContinueInvoked();
  138. return true;
  139. }
  140. }
  141. }
  142. return true;
  143. }
  144. //=========================================================================
  145. bool cmIfCommand(std::vector<cmListFileArgument> const& args,
  146. cmExecutionStatus& inStatus)
  147. {
  148. cmMakefile& makefile = inStatus.GetMakefile();
  149. std::string errorString;
  150. std::vector<cmExpandedCommandArgument> expandedArguments;
  151. makefile.ExpandArguments(args, expandedArguments);
  152. MessageType status;
  153. cmConditionEvaluator conditionEvaluator(makefile, makefile.GetBacktrace());
  154. bool isTrue =
  155. conditionEvaluator.IsTrue(expandedArguments, errorString, status);
  156. if (!errorString.empty()) {
  157. std::string err =
  158. cmStrCat("if ", cmIfCommandError(expandedArguments), errorString);
  159. if (status == MessageType::FATAL_ERROR) {
  160. makefile.IssueMessage(MessageType::FATAL_ERROR, err);
  161. cmSystemTools::SetFatalErrorOccured();
  162. return true;
  163. }
  164. makefile.IssueMessage(status, err);
  165. }
  166. {
  167. auto fb = cm::make_unique<cmIfFunctionBlocker>();
  168. // if is isn't true block the commands
  169. fb->IsBlocking = !isTrue;
  170. if (isTrue) {
  171. fb->HasRun = true;
  172. }
  173. fb->Args = args;
  174. makefile.AddFunctionBlocker(std::move(fb));
  175. }
  176. return true;
  177. }