cmIfCommand.cxx 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. std::string err = "An IF command had incorrect arguments: ";
  99. unsigned int i;
  100. for(i =0; i < args.size(); ++i)
  101. {
  102. err += args[i];
  103. err += " ";
  104. }
  105. this->SetError(err.c_str());
  106. return false;
  107. }
  108. cmIfFunctionBlocker *f = new cmIfFunctionBlocker();
  109. // if is isn't true block the commands
  110. f->m_IsBlocking = !isTrue;
  111. for(std::vector<std::string>::const_iterator j = args.begin();
  112. j != args.end(); ++j)
  113. {
  114. f->m_Args.push_back(*j);
  115. }
  116. m_Makefile->AddFunctionBlocker(f);
  117. return true;
  118. }
  119. bool cmIfCommand::IsTrue(const std::vector<std::string> &args, bool &isValid,
  120. const cmMakefile *makefile)
  121. {
  122. // check for the different signatures
  123. bool isTrue = true;
  124. isValid = false;
  125. const char *def;
  126. const char *def2;
  127. if(args.size() < 1 )
  128. {
  129. isValid = true;
  130. return false;
  131. }
  132. if (args.size() == 1)
  133. {
  134. def = makefile->GetDefinition(args[0].c_str());
  135. if(cmSystemTools::IsOff(def))
  136. {
  137. isTrue = false;
  138. }
  139. isValid = true;
  140. }
  141. if (args.size() == 2 && (args[0] == "NOT"))
  142. {
  143. def = makefile->GetDefinition(args[1].c_str());
  144. if(!cmSystemTools::IsOff(def))
  145. {
  146. isTrue = false;
  147. }
  148. isValid = true;
  149. }
  150. if (args.size() == 2 && (args[0] == "COMMAND"))
  151. {
  152. if(!makefile->CommandExists(args[1].c_str()))
  153. {
  154. isTrue = false;
  155. }
  156. isValid = true;
  157. }
  158. if (args.size() == 2 && (args[0] == "EXISTS"))
  159. {
  160. if(!cmSystemTools::FileExists(args[1].c_str()))
  161. {
  162. isTrue = false;
  163. }
  164. isValid = true;
  165. }
  166. if (args.size() == 2 && (args[0] == "MATCHES"))
  167. {
  168. if(!cmSystemTools::FileExists(args[1].c_str()))
  169. {
  170. isTrue = false;
  171. }
  172. isValid = true;
  173. }
  174. if (args.size() == 3 && (args[1] == "AND"))
  175. {
  176. def = makefile->GetDefinition(args[0].c_str());
  177. def2 = makefile->GetDefinition(args[2].c_str());
  178. if(cmSystemTools::IsOff(def) || cmSystemTools::IsOff(def2))
  179. {
  180. isTrue = false;
  181. }
  182. isValid = true;
  183. }
  184. if (args.size() == 3 && (args[1] == "OR"))
  185. {
  186. def = makefile->GetDefinition(args[0].c_str());
  187. def2 = makefile->GetDefinition(args[2].c_str());
  188. if(cmSystemTools::IsOff(def) && cmSystemTools::IsOff(def2))
  189. {
  190. isTrue = false;
  191. }
  192. isValid = true;
  193. }
  194. if (args.size() == 3 && (args[1] == "MATCHES"))
  195. {
  196. def = cmIfCommand::GetVariableOrString(args[0].c_str(), makefile);
  197. cmRegularExpression regEntry(args[2].c_str());
  198. // check for black line or comment
  199. if (!regEntry.find(def))
  200. {
  201. isTrue = false;
  202. }
  203. isValid = true;
  204. }
  205. if (args.size() == 3 && (args[1] == "LESS"))
  206. {
  207. def = cmIfCommand::GetVariableOrString(args[0].c_str(), makefile);
  208. def2 = cmIfCommand::GetVariableOrString(args[2].c_str(), makefile);
  209. if (!def)
  210. {
  211. def = args[0].c_str();
  212. }
  213. if (!def2)
  214. {
  215. def2 = args[2].c_str();
  216. }
  217. if(atof(def) >= atof(def2))
  218. {
  219. isTrue = false;
  220. }
  221. isValid = true;
  222. }
  223. if (args.size() == 3 && (args[1] == "GREATER"))
  224. {
  225. def = cmIfCommand::GetVariableOrString(args[0].c_str(), makefile);
  226. def2 = cmIfCommand::GetVariableOrString(args[2].c_str(), makefile);
  227. if(atof(def) <= atof(def2))
  228. {
  229. isTrue = false;
  230. }
  231. isValid = true;
  232. }
  233. if (args.size() == 3 && (args[1] == "STRLESS"))
  234. {
  235. def = cmIfCommand::GetVariableOrString(args[0].c_str(), makefile);
  236. def2 = cmIfCommand::GetVariableOrString(args[2].c_str(), makefile);
  237. if(strcmp(def,def2) >= 0)
  238. {
  239. isTrue = false;
  240. }
  241. isValid = true;
  242. }
  243. if (args.size() == 3 && (args[1] == "STRGREATER"))
  244. {
  245. def = cmIfCommand::GetVariableOrString(args[0].c_str(), makefile);
  246. def2 = cmIfCommand::GetVariableOrString(args[2].c_str(), makefile);
  247. if(strcmp(def,def2) <= 0)
  248. {
  249. isTrue = false;
  250. }
  251. isValid = true;
  252. }
  253. return isTrue;
  254. }
  255. const char* cmIfCommand::GetVariableOrString(const char* str,
  256. const cmMakefile* mf)
  257. {
  258. const char* def = mf->GetDefinition(str);
  259. if(!def)
  260. {
  261. def = str;
  262. }
  263. return def;
  264. }