cmIfCommand.cxx 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. cmMakefile* mf, std::vector<cmExpandedCommandArgument> const& args)
  18. {
  19. cmLocalGenerator* lg = mf->GetLocalGenerator();
  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 += lg->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. 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. // Place this call on the call stack.
  88. cmMakefileCall stack_manager(&mf, this->Functions[c], status);
  89. static_cast<void>(stack_manager);
  90. // if trace is enabled, print the evaluated "elseif" statement
  91. if(mf.GetCMakeInstance()->GetTrace())
  92. {
  93. mf.PrintCommandTrace(this->Functions[c]);
  94. }
  95. std::string errorString;
  96. std::vector<cmExpandedCommandArgument> expandedArguments;
  97. mf.ExpandArguments(this->Functions[c].Arguments,
  98. expandedArguments);
  99. cmake::MessageType messType;
  100. cmConditionEvaluator conditionEvaluator(mf);
  101. bool isTrue = conditionEvaluator.IsTrue(
  102. expandedArguments, errorString, messType);
  103. if (!errorString.empty())
  104. {
  105. std::string err = cmIfCommandError(&mf, expandedArguments);
  106. err += errorString;
  107. mf.IssueMessage(messType, err);
  108. if (messType == cmake::FATAL_ERROR)
  109. {
  110. cmSystemTools::SetFatalErrorOccured();
  111. return true;
  112. }
  113. }
  114. if (isTrue)
  115. {
  116. this->IsBlocking = false;
  117. this->HasRun = true;
  118. }
  119. }
  120. }
  121. // should we execute?
  122. else if (!this->IsBlocking)
  123. {
  124. status.Clear();
  125. mf.ExecuteCommand(this->Functions[c],status);
  126. if (status.GetReturnInvoked())
  127. {
  128. inStatus.SetReturnInvoked(true);
  129. return true;
  130. }
  131. if (status.GetBreakInvoked())
  132. {
  133. inStatus.SetBreakInvoked(true);
  134. return true;
  135. }
  136. if (status.GetContinueInvoked())
  137. {
  138. inStatus.SetContinueInvoked(true);
  139. return true;
  140. }
  141. }
  142. }
  143. return true;
  144. }
  145. }
  146. // record the command
  147. this->Functions.push_back(lff);
  148. // always return true
  149. return true;
  150. }
  151. //=========================================================================
  152. bool cmIfFunctionBlocker::ShouldRemove(const cmListFileFunction& lff,
  153. cmMakefile&)
  154. {
  155. if (!cmSystemTools::Strucmp(lff.Name.c_str(),"endif"))
  156. {
  157. // if the endif has arguments, then make sure
  158. // they match the arguments of the matching if
  159. if (lff.Arguments.empty() ||
  160. lff.Arguments == this->Args)
  161. {
  162. return true;
  163. }
  164. }
  165. return false;
  166. }
  167. //=========================================================================
  168. bool cmIfCommand
  169. ::InvokeInitialPass(const std::vector<cmListFileArgument>& args,
  170. cmExecutionStatus &)
  171. {
  172. std::string errorString;
  173. std::vector<cmExpandedCommandArgument> expandedArguments;
  174. this->Makefile->ExpandArguments(args, expandedArguments);
  175. cmake::MessageType status;
  176. cmConditionEvaluator conditionEvaluator(*(this->Makefile));
  177. bool isTrue = conditionEvaluator.IsTrue(
  178. expandedArguments, errorString, status);
  179. if (!errorString.empty())
  180. {
  181. std::string err = cmIfCommandError(this->Makefile, expandedArguments);
  182. err += errorString;
  183. if (status == cmake::FATAL_ERROR)
  184. {
  185. this->SetError(err);
  186. cmSystemTools::SetFatalErrorOccured();
  187. return false;
  188. }
  189. else
  190. {
  191. this->Makefile->IssueMessage(status, err);
  192. }
  193. }
  194. cmIfFunctionBlocker *f = new cmIfFunctionBlocker();
  195. // if is isn't true block the commands
  196. f->ScopeDepth = 1;
  197. f->IsBlocking = !isTrue;
  198. if (isTrue)
  199. {
  200. f->HasRun = true;
  201. }
  202. f->Args = args;
  203. this->Makefile->AddFunctionBlocker(f);
  204. return true;
  205. }