cmIfCommand.cxx 7.3 KB

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