cmIfCommand.cxx 6.2 KB

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