cmIfCommand.cxx 6.7 KB

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