1
0

cmIfCommand.cxx 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. #include <cmsys/RegularExpression.hxx>
  16. bool cmIfFunctionBlocker::
  17. IsFunctionBlocked(const cmListFileFunction& lff, cmMakefile &mf)
  18. {
  19. const char* name = lff.m_Name.c_str();
  20. const std::vector<cmListFileArgument>& args = lff.m_Arguments;
  21. // always let if statements through
  22. if (!strcmp(name,"IF"))
  23. {
  24. return false;
  25. }
  26. // watch for our ELSE or ENDIF
  27. if (!strcmp(name,"ELSE") || !strcmp(name,"ENDIF"))
  28. {
  29. if (args == m_Args)
  30. {
  31. // if it was an else statement then we should change state
  32. // and block this Else Command
  33. if (!strcmp(name,"ELSE"))
  34. {
  35. m_IsBlocking = !m_IsBlocking;
  36. return true;
  37. }
  38. // otherwise it must be an ENDIF statement, in that case remove the
  39. // function blocker
  40. mf.RemoveFunctionBlocker(lff);
  41. return true;
  42. }
  43. else if(args.empty())
  44. {
  45. std::string err = "Empty arguments for ";
  46. err += name;
  47. err += ". Did you mean ";
  48. err += name;
  49. err += "( ";
  50. for(std::vector<cmListFileArgument>::const_iterator a = m_Args.begin();
  51. a != m_Args.end();++a)
  52. {
  53. err += (a->Quoted?"\"":"");
  54. err += a->Value;
  55. err += (a->Quoted?"\"":"");
  56. err += " ";
  57. }
  58. err += ")?";
  59. cmSystemTools::Error(err.c_str());
  60. }
  61. }
  62. return m_IsBlocking;
  63. }
  64. bool cmIfFunctionBlocker::ShouldRemove(const cmListFileFunction& lff,
  65. cmMakefile&)
  66. {
  67. if (lff.m_Name == "ENDIF")
  68. {
  69. if (lff.m_Arguments == m_Args)
  70. {
  71. return true;
  72. }
  73. }
  74. return false;
  75. }
  76. void cmIfFunctionBlocker::
  77. ScopeEnded(cmMakefile &mf)
  78. {
  79. std::string errmsg = "The end of a CMakeLists file was reached with an IF statement that was not closed properly.\nWithin the directory: ";
  80. errmsg += mf.GetCurrentDirectory();
  81. errmsg += "\nThe arguments are: ";
  82. for(std::vector<cmListFileArgument>::const_iterator j = m_Args.begin();
  83. j != m_Args.end(); ++j)
  84. {
  85. errmsg += (j->Quoted?"\"":"");
  86. errmsg += j->Value;
  87. errmsg += (j->Quoted?"\"":"");
  88. errmsg += " ";
  89. }
  90. cmSystemTools::Message(errmsg.c_str(), "Warning");
  91. }
  92. bool cmIfCommand::InvokeInitialPass(const std::vector<cmListFileArgument>& args)
  93. {
  94. bool isValid;
  95. std::vector<std::string> expandedArguments;
  96. m_Makefile->ExpandArguments(args, expandedArguments);
  97. bool isTrue = cmIfCommand::IsTrue(expandedArguments,isValid,m_Makefile);
  98. if (!isValid)
  99. {
  100. std::string err = "An IF command had incorrect arguments: ";
  101. unsigned int i;
  102. for(i =0; i < args.size(); ++i)
  103. {
  104. err += (args[i].Quoted?"\"":"");
  105. err += args[i].Value;
  106. err += (args[i].Quoted?"\"":"");
  107. err += " ";
  108. }
  109. this->SetError(err.c_str());
  110. return false;
  111. }
  112. cmIfFunctionBlocker *f = new cmIfFunctionBlocker();
  113. // if is isn't true block the commands
  114. f->m_IsBlocking = !isTrue;
  115. f->m_Args = args;
  116. m_Makefile->AddFunctionBlocker(f);
  117. return true;
  118. }
  119. bool cmIfCommand::IsTrue(const std::vector<std::string> &args,
  120. bool &isValid, 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. cmsys::RegularExpression 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. }