cmIfCommand.cxx 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmIfCommand.h"
  11. #include "cmStringCommand.h"
  12. #include "cmConditionEvaluator.h"
  13. #include <stdlib.h> // required for atof
  14. #include <list>
  15. #include <cmsys/RegularExpression.hxx>
  16. static std::string cmIfCommandError(
  17. std::vector<cmExpandedCommandArgument> const& args)
  18. {
  19. std::string err = "given arguments:\n ";
  20. for(std::vector<cmExpandedCommandArgument>::const_iterator i = args.begin();
  21. i != args.end(); ++i)
  22. {
  23. err += " ";
  24. err += cmOutputConverter::EscapeForCMake(i->GetValue());
  25. }
  26. err += "\n";
  27. return err;
  28. }
  29. //=========================================================================
  30. bool cmIfFunctionBlocker::
  31. IsFunctionBlocked(const cmListFileFunction& lff,
  32. cmMakefile &mf,
  33. cmExecutionStatus &inStatus)
  34. {
  35. // we start by recording all the functions
  36. if (!cmSystemTools::Strucmp(lff.Name.c_str(),"if"))
  37. {
  38. this->ScopeDepth++;
  39. }
  40. else if (!cmSystemTools::Strucmp(lff.Name.c_str(),"endif"))
  41. {
  42. this->ScopeDepth--;
  43. // if this is the endif for this if statement, then start executing
  44. if (!this->ScopeDepth)
  45. {
  46. // Remove the function blocker for this scope or bail.
  47. cmsys::auto_ptr<cmFunctionBlocker>
  48. fb(mf.RemoveFunctionBlocker(this, lff));
  49. if(!fb.get()) { return false; }
  50. // execute the functions for the true parts of the if statement
  51. cmExecutionStatus status;
  52. int scopeDepth = 0;
  53. for(unsigned int c = 0; c < this->Functions.size(); ++c)
  54. {
  55. // keep track of scope depth
  56. if (!cmSystemTools::Strucmp(this->Functions[c].Name.c_str(),"if"))
  57. {
  58. scopeDepth++;
  59. }
  60. if (!cmSystemTools::Strucmp(this->Functions[c].Name.c_str(),"endif"))
  61. {
  62. scopeDepth--;
  63. }
  64. // watch for our state change
  65. if (scopeDepth == 0 &&
  66. !cmSystemTools::Strucmp(this->Functions[c].Name.c_str(),"else"))
  67. {
  68. this->IsBlocking = this->HasRun;
  69. this->HasRun = true;
  70. // if trace is enabled, print a (trivially) evaluated "else"
  71. // statement
  72. if(!this->IsBlocking && mf.GetCMakeInstance()->GetTrace())
  73. {
  74. mf.PrintCommandTrace(this->Functions[c]);
  75. }
  76. }
  77. else if (scopeDepth == 0 && !cmSystemTools::Strucmp
  78. (this->Functions[c].Name.c_str(),"elseif"))
  79. {
  80. if (this->HasRun)
  81. {
  82. this->IsBlocking = true;
  83. }
  84. else
  85. {
  86. // if trace is enabled, print the evaluated "elseif" statement
  87. if(mf.GetCMakeInstance()->GetTrace())
  88. {
  89. mf.PrintCommandTrace(this->Functions[c]);
  90. }
  91. std::string errorString;
  92. std::vector<cmExpandedCommandArgument> expandedArguments;
  93. mf.ExpandArguments(this->Functions[c].Arguments,
  94. expandedArguments);
  95. cmake::MessageType messType;
  96. cmConditionEvaluator conditionEvaluator(mf);
  97. bool isTrue = conditionEvaluator.IsTrue(
  98. expandedArguments, errorString, messType);
  99. if (!errorString.empty())
  100. {
  101. std::string err = cmIfCommandError(expandedArguments);
  102. err += errorString;
  103. cmListFileBacktrace bt = mf.GetBacktrace(this->Functions[c]);
  104. mf.GetCMakeInstance()->IssueMessage(messType, err, bt);
  105. if (messType == cmake::FATAL_ERROR)
  106. {
  107. cmSystemTools::SetFatalErrorOccured();
  108. return true;
  109. }
  110. }
  111. if (isTrue)
  112. {
  113. this->IsBlocking = false;
  114. this->HasRun = true;
  115. }
  116. }
  117. }
  118. // should we execute?
  119. else if (!this->IsBlocking)
  120. {
  121. status.Clear();
  122. mf.ExecuteCommand(this->Functions[c],status);
  123. if (status.GetReturnInvoked())
  124. {
  125. inStatus.SetReturnInvoked(true);
  126. return true;
  127. }
  128. if (status.GetBreakInvoked())
  129. {
  130. inStatus.SetBreakInvoked(true);
  131. return true;
  132. }
  133. if (status.GetContinueInvoked())
  134. {
  135. inStatus.SetContinueInvoked(true);
  136. return true;
  137. }
  138. }
  139. }
  140. return true;
  141. }
  142. }
  143. // record the command
  144. this->Functions.push_back(lff);
  145. // always return true
  146. return true;
  147. }
  148. //=========================================================================
  149. bool cmIfFunctionBlocker::ShouldRemove(const cmListFileFunction& lff,
  150. cmMakefile&)
  151. {
  152. if (!cmSystemTools::Strucmp(lff.Name.c_str(),"endif"))
  153. {
  154. // if the endif has arguments, then make sure
  155. // they match the arguments of the matching if
  156. if (lff.Arguments.empty() ||
  157. lff.Arguments == this->Args)
  158. {
  159. return true;
  160. }
  161. }
  162. return false;
  163. }
  164. //=========================================================================
  165. bool cmIfCommand
  166. ::InvokeInitialPass(const std::vector<cmListFileArgument>& args,
  167. cmExecutionStatus &)
  168. {
  169. std::string errorString;
  170. std::vector<cmExpandedCommandArgument> expandedArguments;
  171. this->Makefile->ExpandArguments(args, expandedArguments);
  172. cmake::MessageType status;
  173. cmConditionEvaluator conditionEvaluator(*(this->Makefile));
  174. bool isTrue = conditionEvaluator.IsTrue(
  175. expandedArguments, errorString, status);
  176. if (!errorString.empty())
  177. {
  178. std::string err = cmIfCommandError(expandedArguments);
  179. err += errorString;
  180. if (status == cmake::FATAL_ERROR)
  181. {
  182. this->SetError(err);
  183. cmSystemTools::SetFatalErrorOccured();
  184. return false;
  185. }
  186. else
  187. {
  188. this->Makefile->IssueMessage(status, err);
  189. }
  190. }
  191. cmIfFunctionBlocker *f = new cmIfFunctionBlocker();
  192. // if is isn't true block the commands
  193. f->ScopeDepth = 1;
  194. f->IsBlocking = !isTrue;
  195. if (isTrue)
  196. {
  197. f->HasRun = true;
  198. }
  199. f->Args = args;
  200. this->Makefile->AddFunctionBlocker(f);
  201. return true;
  202. }