cmIfCommand.cxx 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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.size())
  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. }
  137. }
  138. return true;
  139. }
  140. }
  141. // record the command
  142. this->Functions.push_back(lff);
  143. // always return true
  144. return true;
  145. }
  146. //=========================================================================
  147. bool cmIfFunctionBlocker::ShouldRemove(const cmListFileFunction& lff,
  148. cmMakefile&)
  149. {
  150. if (!cmSystemTools::Strucmp(lff.Name.c_str(),"endif"))
  151. {
  152. // if the endif has arguments, then make sure
  153. // they match the arguments of the matching if
  154. if (lff.Arguments.size() == 0 ||
  155. lff.Arguments == this->Args)
  156. {
  157. return true;
  158. }
  159. }
  160. return false;
  161. }
  162. //=========================================================================
  163. bool cmIfCommand
  164. ::InvokeInitialPass(const std::vector<cmListFileArgument>& args,
  165. cmExecutionStatus &)
  166. {
  167. std::string errorString;
  168. std::vector<cmExpandedCommandArgument> expandedArguments;
  169. this->Makefile->ExpandArguments(args, expandedArguments);
  170. cmake::MessageType status;
  171. cmConditionEvaluator conditionEvaluator(*(this->Makefile));
  172. bool isTrue = conditionEvaluator.IsTrue(
  173. expandedArguments, errorString, status);
  174. if (errorString.size())
  175. {
  176. std::string err = cmIfCommandError(this->Makefile, expandedArguments);
  177. err += errorString;
  178. if (status == cmake::FATAL_ERROR)
  179. {
  180. this->SetError(err);
  181. cmSystemTools::SetFatalErrorOccured();
  182. return false;
  183. }
  184. else
  185. {
  186. this->Makefile->IssueMessage(status, err);
  187. }
  188. }
  189. cmIfFunctionBlocker *f = new cmIfFunctionBlocker();
  190. // if is isn't true block the commands
  191. f->ScopeDepth = 1;
  192. f->IsBlocking = !isTrue;
  193. if (isTrue)
  194. {
  195. f->HasRun = true;
  196. }
  197. f->Args = args;
  198. this->Makefile->AddFunctionBlocker(f);
  199. return true;
  200. }