cmIfCommand.cxx 7.3 KB

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