cmIfCommand.cxx 6.0 KB

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