cmIfCommand.cxx 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. cmListFileContext lfc =
  104. cmListFileContext::FromCommandContext(
  105. this->Functions[c], this->GetStartingContext().FilePath);
  106. cmListFileBacktrace bt = mf.GetBacktrace(lfc);
  107. mf.GetCMakeInstance()->IssueMessage(messType, err, bt);
  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(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. }