cmIfCommand.cxx 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. cmListFileBacktrace elseBT = mf.GetBacktrace().Push(
  67. cmListFileContext{ func.OriginalName(),
  68. this->GetStartingContext().FilePath, func.Line() });
  69. if (this->ElseSeen) {
  70. mf.GetCMakeInstance()->IssueMessage(
  71. MessageType::FATAL_ERROR,
  72. "A duplicate ELSE command was found inside an IF block.", elseBT);
  73. cmSystemTools::SetFatalErrorOccurred();
  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, elseBT,
  83. cmMakefile::CommandMissingFromStack::Yes);
  84. }
  85. } else if (scopeDepth == 0 && func.LowerCaseName() == "elseif") {
  86. cmListFileBacktrace elseifBT = mf.GetBacktrace().Push(
  87. cmListFileContext{ func.OriginalName(),
  88. this->GetStartingContext().FilePath, func.Line() });
  89. if (this->ElseSeen) {
  90. mf.GetCMakeInstance()->IssueMessage(
  91. MessageType::FATAL_ERROR,
  92. "An ELSEIF command was found after an ELSE command.", elseifBT);
  93. cmSystemTools::SetFatalErrorOccurred();
  94. return true;
  95. }
  96. if (this->HasRun) {
  97. this->IsBlocking = true;
  98. } else {
  99. // if trace is enabled, print the evaluated "elseif" statement
  100. if (mf.GetCMakeInstance()->GetTrace()) {
  101. mf.PrintCommandTrace(func, elseifBT,
  102. cmMakefile::CommandMissingFromStack::Yes);
  103. }
  104. std::string errorString;
  105. std::vector<cmExpandedCommandArgument> expandedArguments;
  106. mf.ExpandArguments(func.Arguments(), expandedArguments);
  107. MessageType messType;
  108. cmConditionEvaluator conditionEvaluator(mf, elseifBT);
  109. bool isTrue =
  110. conditionEvaluator.IsTrue(expandedArguments, errorString, messType);
  111. if (!errorString.empty()) {
  112. std::string err =
  113. cmStrCat(cmIfCommandError(expandedArguments), errorString);
  114. mf.GetCMakeInstance()->IssueMessage(messType, err, elseifBT);
  115. if (messType == MessageType::FATAL_ERROR) {
  116. cmSystemTools::SetFatalErrorOccurred();
  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. cmExecutionStatus status(mf);
  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(makefile, 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::SetFatalErrorOccurred();
  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. }