cmIfCommand.cxx 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. #include <stdlib.h> // required for atof
  15. bool cmIfFunctionBlocker::
  16. IsFunctionBlocked(const cmListFileFunction& lff, cmMakefile &mf)
  17. {
  18. const char* name = lff.m_Name.c_str();
  19. const std::vector<cmListFileArgument>& args = lff.m_Arguments;
  20. // always let if statements through
  21. if (!strcmp(name,"IF"))
  22. {
  23. return false;
  24. }
  25. // watch for our ELSE or ENDIF
  26. if (!strcmp(name,"ELSE") || !strcmp(name,"ENDIF"))
  27. {
  28. if (args == m_Args)
  29. {
  30. // if it was an else statement then we should change state
  31. // and block this Else Command
  32. if (!strcmp(name,"ELSE"))
  33. {
  34. m_IsBlocking = !m_IsBlocking;
  35. return true;
  36. }
  37. // otherwise it must be an ENDIF statement, in that case remove the
  38. // function blocker
  39. mf.RemoveFunctionBlocker(lff);
  40. return true;
  41. }
  42. else if(args.empty())
  43. {
  44. std::string err = "Empty arguments for ";
  45. err += name;
  46. err += ". Did you mean ";
  47. err += name;
  48. err += "( ";
  49. for(std::vector<cmListFileArgument>::const_iterator a = m_Args.begin();
  50. a != m_Args.end();++a)
  51. {
  52. err += (a->Quoted?"\"":"");
  53. err += a->Value;
  54. err += (a->Quoted?"\"":"");
  55. err += " ";
  56. }
  57. err += ")?";
  58. cmSystemTools::Error(err.c_str());
  59. }
  60. }
  61. return m_IsBlocking;
  62. }
  63. bool cmIfFunctionBlocker::ShouldRemove(const cmListFileFunction& lff,
  64. cmMakefile&)
  65. {
  66. if (lff.m_Name == "ENDIF")
  67. {
  68. if (lff.m_Arguments == m_Args)
  69. {
  70. return true;
  71. }
  72. }
  73. return false;
  74. }
  75. void cmIfFunctionBlocker::
  76. ScopeEnded(cmMakefile &mf)
  77. {
  78. std::string errmsg = "The end of a CMakeLists file was reached with an IF statement that was not closed properly.\nWithin the directory: ";
  79. errmsg += mf.GetCurrentDirectory();
  80. errmsg += "\nThe arguments are: ";
  81. for(std::vector<cmListFileArgument>::const_iterator j = m_Args.begin();
  82. j != m_Args.end(); ++j)
  83. {
  84. errmsg += (j->Quoted?"\"":"");
  85. errmsg += j->Value;
  86. errmsg += (j->Quoted?"\"":"");
  87. errmsg += " ";
  88. }
  89. cmSystemTools::Message(errmsg.c_str(), "Warning");
  90. }
  91. bool cmIfCommand::InvokeInitialPass(const std::vector<cmListFileArgument>& args)
  92. {
  93. bool isValid;
  94. std::vector<std::string> expandedArguments;
  95. m_Makefile->ExpandArguments(args, expandedArguments);
  96. bool isTrue = cmIfCommand::IsTrue(expandedArguments,isValid,m_Makefile);
  97. if (!isValid)
  98. {
  99. std::string err = "An IF command had incorrect arguments: ";
  100. unsigned int i;
  101. for(i =0; i < args.size(); ++i)
  102. {
  103. err += (args[i].Quoted?"\"":"");
  104. err += args[i].Value;
  105. err += (args[i].Quoted?"\"":"");
  106. err += " ";
  107. }
  108. this->SetError(err.c_str());
  109. return false;
  110. }
  111. cmIfFunctionBlocker *f = new cmIfFunctionBlocker();
  112. // if is isn't true block the commands
  113. f->m_IsBlocking = !isTrue;
  114. f->m_Args = args;
  115. m_Makefile->AddFunctionBlocker(f);
  116. return true;
  117. }
  118. bool cmIfCommand::IsTrue(const std::vector<std::string> &args,
  119. bool &isValid, const cmMakefile *makefile)
  120. {
  121. // check for the different signatures
  122. bool isTrue = true;
  123. isValid = false;
  124. const char *def;
  125. const char *def2;
  126. if(args.size() < 1 )
  127. {
  128. isValid = true;
  129. return false;
  130. }
  131. if (args.size() == 1)
  132. {
  133. def = makefile->GetDefinition(args[0].c_str());
  134. if(cmSystemTools::IsOff(def))
  135. {
  136. isTrue = false;
  137. }
  138. isValid = true;
  139. }
  140. if (args.size() == 2 && (args[0] == "NOT"))
  141. {
  142. def = makefile->GetDefinition(args[1].c_str());
  143. if(!cmSystemTools::IsOff(def))
  144. {
  145. isTrue = false;
  146. }
  147. isValid = true;
  148. }
  149. if (args.size() == 2 && (args[0] == "COMMAND"))
  150. {
  151. if(!makefile->CommandExists(args[1].c_str()))
  152. {
  153. isTrue = false;
  154. }
  155. isValid = true;
  156. }
  157. if (args.size() == 2 && (args[0] == "EXISTS"))
  158. {
  159. if(!cmSystemTools::FileExists(args[1].c_str()))
  160. {
  161. isTrue = false;
  162. }
  163. isValid = true;
  164. }
  165. if (args.size() == 2 && (args[0] == "MATCHES"))
  166. {
  167. if(!cmSystemTools::FileExists(args[1].c_str()))
  168. {
  169. isTrue = false;
  170. }
  171. isValid = true;
  172. }
  173. if (args.size() == 3 && (args[1] == "AND"))
  174. {
  175. def = makefile->GetDefinition(args[0].c_str());
  176. def2 = makefile->GetDefinition(args[2].c_str());
  177. if(cmSystemTools::IsOff(def) || cmSystemTools::IsOff(def2))
  178. {
  179. isTrue = false;
  180. }
  181. isValid = true;
  182. }
  183. if (args.size() == 3 && (args[1] == "OR"))
  184. {
  185. def = makefile->GetDefinition(args[0].c_str());
  186. def2 = makefile->GetDefinition(args[2].c_str());
  187. if(cmSystemTools::IsOff(def) && cmSystemTools::IsOff(def2))
  188. {
  189. isTrue = false;
  190. }
  191. isValid = true;
  192. }
  193. if (args.size() == 3 && (args[1] == "MATCHES"))
  194. {
  195. def = cmIfCommand::GetVariableOrString(args[0].c_str(), makefile);
  196. cmRegularExpression regEntry(args[2].c_str());
  197. // check for black line or comment
  198. if (!regEntry.find(def))
  199. {
  200. isTrue = false;
  201. }
  202. isValid = true;
  203. }
  204. if (args.size() == 3 && (args[1] == "LESS"))
  205. {
  206. def = cmIfCommand::GetVariableOrString(args[0].c_str(), makefile);
  207. def2 = cmIfCommand::GetVariableOrString(args[2].c_str(), makefile);
  208. if (!def)
  209. {
  210. def = args[0].c_str();
  211. }
  212. if (!def2)
  213. {
  214. def2 = args[2].c_str();
  215. }
  216. if(atof(def) >= atof(def2))
  217. {
  218. isTrue = false;
  219. }
  220. isValid = true;
  221. }
  222. if (args.size() == 3 && (args[1] == "GREATER"))
  223. {
  224. def = cmIfCommand::GetVariableOrString(args[0].c_str(), makefile);
  225. def2 = cmIfCommand::GetVariableOrString(args[2].c_str(), makefile);
  226. if(atof(def) <= atof(def2))
  227. {
  228. isTrue = false;
  229. }
  230. isValid = true;
  231. }
  232. if (args.size() == 3 && (args[1] == "STRLESS"))
  233. {
  234. def = cmIfCommand::GetVariableOrString(args[0].c_str(), makefile);
  235. def2 = cmIfCommand::GetVariableOrString(args[2].c_str(), makefile);
  236. if(strcmp(def,def2) >= 0)
  237. {
  238. isTrue = false;
  239. }
  240. isValid = true;
  241. }
  242. if (args.size() == 3 && (args[1] == "STRGREATER"))
  243. {
  244. def = cmIfCommand::GetVariableOrString(args[0].c_str(), makefile);
  245. def2 = cmIfCommand::GetVariableOrString(args[2].c_str(), makefile);
  246. if(strcmp(def,def2) <= 0)
  247. {
  248. isTrue = false;
  249. }
  250. isValid = true;
  251. }
  252. return isTrue;
  253. }
  254. const char* cmIfCommand::GetVariableOrString(const char* str,
  255. const cmMakefile* mf)
  256. {
  257. const char* def = mf->GetDefinition(str);
  258. if(!def)
  259. {
  260. def = str;
  261. }
  262. return def;
  263. }