cmIfCommand.cxx 5.8 KB

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