cmIfCommand.cxx 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmIfCommand.h"
  14. bool cmIfFunctionBlocker::
  15. IsFunctionBlocked(const char *name, const std::vector<std::string> &args,
  16. cmMakefile &mf)
  17. {
  18. // always let if statements through
  19. if (!strcmp(name,"IF"))
  20. {
  21. return false;
  22. }
  23. // watch for our ELSE or ENDIF
  24. if (!strcmp(name,"ELSE") || !strcmp(name,"ENDIF"))
  25. {
  26. if (args == m_Args)
  27. {
  28. // if it was an else statement then we should change state
  29. // and block this Else Command
  30. if (!strcmp(name,"ELSE"))
  31. {
  32. m_IsBlocking = !m_IsBlocking;
  33. return true;
  34. }
  35. // otherwise it must be an ENDIF statement, in that case remove the
  36. // function blocker
  37. mf.RemoveFunctionBlocker("ENDIF",args);
  38. return true;
  39. }
  40. else if(args.empty())
  41. {
  42. std::string err = "Empty arguments for ";
  43. err += name;
  44. err += ". Did you mean ";
  45. err += name;
  46. err += "( ";
  47. for(std::vector<std::string>::const_iterator a = m_Args.begin();
  48. a != m_Args.end();++a)
  49. {
  50. err += *a;
  51. err += " ";
  52. }
  53. err += ")?";
  54. cmSystemTools::Error(err.c_str());
  55. }
  56. }
  57. return m_IsBlocking;
  58. }
  59. bool cmIfFunctionBlocker::
  60. ShouldRemove(const char *name, const std::vector<std::string> &args,
  61. cmMakefile &)
  62. {
  63. if (!strcmp(name,"ELSE") || !strcmp(name,"ENDIF"))
  64. {
  65. if (args == m_Args)
  66. {
  67. return true;
  68. }
  69. }
  70. return false;
  71. }
  72. void cmIfFunctionBlocker::
  73. ScopeEnded(cmMakefile &mf)
  74. {
  75. const char* versionValue
  76. = mf.GetDefinition("CMAKE_MINIMUM_REQUIRED_VERSION");
  77. if (!versionValue || (atof(versionValue) <= 1.4))
  78. {
  79. return;
  80. }
  81. std::string errmsg = "The end of a CMakeLists file was reached with an IF statement that was not closed properly.\nWithin the directory: ";
  82. errmsg += mf.GetCurrentDirectory();
  83. errmsg += "\nThe arguments are: ";
  84. for(std::vector<std::string>::const_iterator j = m_Args.begin();
  85. j != m_Args.end(); ++j)
  86. {
  87. errmsg += *j;
  88. errmsg += " ";
  89. }
  90. cmSystemTools::Error(errmsg.c_str());
  91. }
  92. bool cmIfCommand::InitialPass(std::vector<std::string> const& args)
  93. {
  94. bool isValid;
  95. bool isTrue = cmIfCommand::IsTrue(args,isValid,m_Makefile);
  96. if (!isValid)
  97. {
  98. this->SetError("An IF command had incorrect arguments");
  99. return false;
  100. }
  101. cmIfFunctionBlocker *f = new cmIfFunctionBlocker();
  102. // if is isn't true block the commands
  103. f->m_IsBlocking = !isTrue;
  104. for(std::vector<std::string>::const_iterator j = args.begin();
  105. j != args.end(); ++j)
  106. {
  107. f->m_Args.push_back(*j);
  108. }
  109. m_Makefile->AddFunctionBlocker(f);
  110. return true;
  111. }
  112. bool cmIfCommand::IsTrue(const std::vector<std::string> &args, bool &isValid,
  113. const cmMakefile *makefile)
  114. {
  115. // check for the different signatures
  116. bool isTrue = true;
  117. isValid = false;
  118. const char *def;
  119. const char *def2;
  120. if(args.size() < 1 )
  121. {
  122. isValid = true;
  123. return false;
  124. }
  125. if (args.size() == 1)
  126. {
  127. def = makefile->GetDefinition(args[0].c_str());
  128. if(cmSystemTools::IsOff(def))
  129. {
  130. isTrue = false;
  131. }
  132. isValid = true;
  133. }
  134. if (args.size() == 2 && (args[0] == "NOT"))
  135. {
  136. def = makefile->GetDefinition(args[1].c_str());
  137. if(!cmSystemTools::IsOff(def))
  138. {
  139. isTrue = false;
  140. }
  141. isValid = true;
  142. }
  143. if (args.size() == 2 && (args[0] == "COMMAND"))
  144. {
  145. if(!makefile->CommandExists(args[1].c_str()))
  146. {
  147. isTrue = false;
  148. }
  149. isValid = true;
  150. }
  151. if (args.size() == 2 && (args[0] == "EXISTS"))
  152. {
  153. if(!cmSystemTools::FileExists(args[1].c_str()))
  154. {
  155. isTrue = false;
  156. }
  157. isValid = true;
  158. }
  159. if (args.size() == 3 && (args[1] == "AND"))
  160. {
  161. def = makefile->GetDefinition(args[0].c_str());
  162. def2 = makefile->GetDefinition(args[2].c_str());
  163. if(cmSystemTools::IsOff(def) || cmSystemTools::IsOff(def2))
  164. {
  165. isTrue = false;
  166. }
  167. isValid = true;
  168. }
  169. if (args.size() == 3 && (args[1] == "OR"))
  170. {
  171. def = makefile->GetDefinition(args[0].c_str());
  172. def2 = makefile->GetDefinition(args[2].c_str());
  173. if(cmSystemTools::IsOff(def) && cmSystemTools::IsOff(def2))
  174. {
  175. isTrue = false;
  176. }
  177. isValid = true;
  178. }
  179. if (args.size() == 3 && (args[1] == "MATCHES"))
  180. {
  181. def = cmIfCommand::GetVariableOrString(args[0].c_str(), makefile);
  182. cmRegularExpression regEntry(args[2].c_str());
  183. // check for black line or comment
  184. if (!regEntry.find(def))
  185. {
  186. isTrue = false;
  187. }
  188. isValid = true;
  189. }
  190. if (args.size() == 3 && (args[1] == "LESS"))
  191. {
  192. def = cmIfCommand::GetVariableOrString(args[0].c_str(), makefile);
  193. def2 = cmIfCommand::GetVariableOrString(args[2].c_str(), makefile);
  194. if (!def)
  195. {
  196. def = args[0].c_str();
  197. }
  198. if (!def2)
  199. {
  200. def2 = args[2].c_str();
  201. }
  202. if(atof(def) >= atof(def2))
  203. {
  204. isTrue = false;
  205. }
  206. isValid = true;
  207. }
  208. if (args.size() == 3 && (args[1] == "GREATER"))
  209. {
  210. def = cmIfCommand::GetVariableOrString(args[0].c_str(), makefile);
  211. def2 = cmIfCommand::GetVariableOrString(args[2].c_str(), makefile);
  212. if(atof(def) <= atof(def2))
  213. {
  214. isTrue = false;
  215. }
  216. isValid = true;
  217. }
  218. if (args.size() == 3 && (args[1] == "STRLESS"))
  219. {
  220. def = cmIfCommand::GetVariableOrString(args[0].c_str(), makefile);
  221. def2 = cmIfCommand::GetVariableOrString(args[2].c_str(), makefile);
  222. if(strcmp(def,def2) >= 0)
  223. {
  224. isTrue = false;
  225. }
  226. isValid = true;
  227. }
  228. if (args.size() == 3 && (args[1] == "STRGREATER"))
  229. {
  230. def = cmIfCommand::GetVariableOrString(args[0].c_str(), makefile);
  231. def2 = cmIfCommand::GetVariableOrString(args[2].c_str(), makefile);
  232. if(strcmp(def,def2) <= 0)
  233. {
  234. isTrue = false;
  235. }
  236. isValid = true;
  237. }
  238. return isTrue;
  239. }
  240. const char* cmIfCommand::GetVariableOrString(const char* str,
  241. const cmMakefile* mf)
  242. {
  243. const char* def = mf->GetDefinition(str);
  244. if(!def)
  245. {
  246. def = str;
  247. }
  248. return def;
  249. }