cmIfCommand.cxx 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 "cmOutputConverter.h"
  5. #include "cmStringCommand.h"
  6. #include "cmSystemTools.h"
  7. #include "cmConditionEvaluator.h"
  8. #include <cmsys/RegularExpression.hxx>
  9. #include <list>
  10. #include <stdlib.h> // required for atof
  11. static std::string cmIfCommandError(
  12. std::vector<cmExpandedCommandArgument> const& args)
  13. {
  14. std::string err = "given arguments:\n ";
  15. for (std::vector<cmExpandedCommandArgument>::const_iterator i = args.begin();
  16. i != args.end(); ++i) {
  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 (!cmSystemTools::Strucmp(lff.Name.c_str(), "if")) {
  30. this->ScopeDepth++;
  31. } else if (!cmSystemTools::Strucmp(lff.Name.c_str(), "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. CM_AUTO_PTR<cmFunctionBlocker> fb(mf.RemoveFunctionBlocker(this, lff));
  37. if (!fb.get()) {
  38. return false;
  39. }
  40. // execute the functions for the true parts of the if statement
  41. cmExecutionStatus status;
  42. int scopeDepth = 0;
  43. for (unsigned int c = 0; c < this->Functions.size(); ++c) {
  44. // keep track of scope depth
  45. if (!cmSystemTools::Strucmp(this->Functions[c].Name.c_str(), "if")) {
  46. scopeDepth++;
  47. }
  48. if (!cmSystemTools::Strucmp(this->Functions[c].Name.c_str(),
  49. "endif")) {
  50. scopeDepth--;
  51. }
  52. // watch for our state change
  53. if (scopeDepth == 0 &&
  54. !cmSystemTools::Strucmp(this->Functions[c].Name.c_str(), "else")) {
  55. this->IsBlocking = this->HasRun;
  56. this->HasRun = true;
  57. // if trace is enabled, print a (trivially) evaluated "else"
  58. // statement
  59. if (!this->IsBlocking && mf.GetCMakeInstance()->GetTrace()) {
  60. mf.PrintCommandTrace(this->Functions[c]);
  61. }
  62. } else if (scopeDepth == 0 &&
  63. !cmSystemTools::Strucmp(this->Functions[c].Name.c_str(),
  64. "elseif")) {
  65. if (this->HasRun) {
  66. this->IsBlocking = true;
  67. } else {
  68. // if trace is enabled, print the evaluated "elseif" statement
  69. if (mf.GetCMakeInstance()->GetTrace()) {
  70. mf.PrintCommandTrace(this->Functions[c]);
  71. }
  72. std::string errorString;
  73. std::vector<cmExpandedCommandArgument> expandedArguments;
  74. mf.ExpandArguments(this->Functions[c].Arguments,
  75. expandedArguments);
  76. cmake::MessageType messType;
  77. cmListFileContext conditionContext =
  78. cmListFileContext::FromCommandContext(
  79. this->Functions[c], this->GetStartingContext().FilePath);
  80. cmConditionEvaluator conditionEvaluator(
  81. mf, conditionContext, mf.GetBacktrace(this->Functions[c]));
  82. bool isTrue = conditionEvaluator.IsTrue(expandedArguments,
  83. errorString, messType);
  84. if (!errorString.empty()) {
  85. std::string err = cmIfCommandError(expandedArguments);
  86. err += errorString;
  87. cmListFileBacktrace bt = mf.GetBacktrace(this->Functions[c]);
  88. mf.GetCMakeInstance()->IssueMessage(messType, err, bt);
  89. if (messType == cmake::FATAL_ERROR) {
  90. cmSystemTools::SetFatalErrorOccured();
  91. return true;
  92. }
  93. }
  94. if (isTrue) {
  95. this->IsBlocking = false;
  96. this->HasRun = true;
  97. }
  98. }
  99. }
  100. // should we execute?
  101. else if (!this->IsBlocking) {
  102. status.Clear();
  103. mf.ExecuteCommand(this->Functions[c], status);
  104. if (status.GetReturnInvoked()) {
  105. inStatus.SetReturnInvoked(true);
  106. return true;
  107. }
  108. if (status.GetBreakInvoked()) {
  109. inStatus.SetBreakInvoked(true);
  110. return true;
  111. }
  112. if (status.GetContinueInvoked()) {
  113. inStatus.SetContinueInvoked(true);
  114. return true;
  115. }
  116. }
  117. }
  118. return true;
  119. }
  120. }
  121. // record the command
  122. this->Functions.push_back(lff);
  123. // always return true
  124. return true;
  125. }
  126. //=========================================================================
  127. bool cmIfFunctionBlocker::ShouldRemove(const cmListFileFunction& lff,
  128. cmMakefile&)
  129. {
  130. if (!cmSystemTools::Strucmp(lff.Name.c_str(), "endif")) {
  131. // if the endif has arguments, then make sure
  132. // they match the arguments of the matching if
  133. if (lff.Arguments.empty() || lff.Arguments == this->Args) {
  134. return true;
  135. }
  136. }
  137. return false;
  138. }
  139. //=========================================================================
  140. bool cmIfCommand::InvokeInitialPass(
  141. const std::vector<cmListFileArgument>& args, cmExecutionStatus&)
  142. {
  143. std::string errorString;
  144. std::vector<cmExpandedCommandArgument> expandedArguments;
  145. this->Makefile->ExpandArguments(args, expandedArguments);
  146. cmake::MessageType status;
  147. cmConditionEvaluator conditionEvaluator(
  148. *(this->Makefile), this->Makefile->GetExecutionContext(),
  149. this->Makefile->GetBacktrace());
  150. bool isTrue =
  151. conditionEvaluator.IsTrue(expandedArguments, errorString, status);
  152. if (!errorString.empty()) {
  153. std::string err = "if " + cmIfCommandError(expandedArguments);
  154. err += errorString;
  155. if (status == cmake::FATAL_ERROR) {
  156. this->Makefile->IssueMessage(cmake::FATAL_ERROR, err);
  157. cmSystemTools::SetFatalErrorOccured();
  158. return true;
  159. }
  160. this->Makefile->IssueMessage(status, err);
  161. }
  162. cmIfFunctionBlocker* f = new cmIfFunctionBlocker();
  163. // if is isn't true block the commands
  164. f->ScopeDepth = 1;
  165. f->IsBlocking = !isTrue;
  166. if (isTrue) {
  167. f->HasRun = true;
  168. }
  169. f->Args = args;
  170. this->Makefile->AddFunctionBlocker(f);
  171. return true;
  172. }