1
0

cmIfCommand.cxx 7.2 KB

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