cmIfCommand.cxx 7.3 KB

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