cmIfCommand.cxx 6.7 KB

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