cmIfCommand.cxx 6.7 KB

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