cmIfCommand.cxx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 true;
  43. }
  44. bool cmIfFunctionBlocker::
  45. ShouldRemove(const char *name, const std::vector<std::string> &args,
  46. cmMakefile &mf)
  47. {
  48. return !this->IsFunctionBlocked(name,args,mf);
  49. }
  50. void cmIfFunctionBlocker::
  51. ScopeEnded(cmMakefile &mf)
  52. {
  53. std::string errmsg = "The end of a CMakeLists file was reached with an IF statement that was not closed properly.\nWithin the directory: ";
  54. errmsg += mf.GetCurrentDirectory();
  55. errmsg += "\nThe arguments are: ";
  56. for(std::vector<std::string>::const_iterator j = m_Args.begin();
  57. j != m_Args.end(); ++j)
  58. {
  59. errmsg += *j;
  60. errmsg += " ";
  61. }
  62. cmSystemTools::Error(errmsg.c_str());
  63. }
  64. bool cmIfCommand::InitialPass(std::vector<std::string> const& args)
  65. {
  66. if(args.size() < 1 )
  67. {
  68. this->SetError("called with incorrect number of arguments");
  69. return false;
  70. }
  71. // create a function blocker
  72. cmIfFunctionBlocker *f = NULL;
  73. // check for the different signatures
  74. const char *def;
  75. const char *def2;
  76. if (args.size() == 1)
  77. {
  78. def = m_Makefile->GetDefinition(args[0].c_str());
  79. if(cmSystemTools::IsOff(def))
  80. {
  81. f = new cmIfFunctionBlocker();
  82. }
  83. }
  84. if (args.size() == 2 && (args[0] == "NOT"))
  85. {
  86. def = m_Makefile->GetDefinition(args[1].c_str());
  87. if(!cmSystemTools::IsOff(def))
  88. {
  89. f = new cmIfFunctionBlocker();
  90. }
  91. }
  92. if (args.size() == 2 && (args[0] == "COMMAND"))
  93. {
  94. if(!m_Makefile->CommandExists(args[1].c_str()))
  95. {
  96. f = new cmIfFunctionBlocker();
  97. }
  98. }
  99. if (args.size() == 2 && (args[0] == "EXISTS"))
  100. {
  101. if(!cmSystemTools::FileExists(args[1].c_str()))
  102. {
  103. f = new cmIfFunctionBlocker();
  104. }
  105. }
  106. if (args.size() == 3 && (args[1] == "AND"))
  107. {
  108. def = m_Makefile->GetDefinition(args[0].c_str());
  109. def2 = m_Makefile->GetDefinition(args[2].c_str());
  110. if(cmSystemTools::IsOff(def) || cmSystemTools::IsOff(def2))
  111. {
  112. f = new cmIfFunctionBlocker();
  113. }
  114. }
  115. if (args.size() == 3 && (args[1] == "OR"))
  116. {
  117. def = m_Makefile->GetDefinition(args[0].c_str());
  118. def2 = m_Makefile->GetDefinition(args[2].c_str());
  119. if(cmSystemTools::IsOff(def) && cmSystemTools::IsOff(def2))
  120. {
  121. f = new cmIfFunctionBlocker();
  122. }
  123. }
  124. if (args.size() == 3 && (args[1] == "MATCHES"))
  125. {
  126. def = m_Makefile->GetDefinition(args[0].c_str());
  127. cmRegularExpression regEntry(args[2].c_str());
  128. // check for black line or comment
  129. if (!def || !regEntry.find(def))
  130. {
  131. f = new cmIfFunctionBlocker();
  132. }
  133. }
  134. if (args.size() == 3 && (args[1] == "LESS"))
  135. {
  136. def = m_Makefile->GetDefinition(args[0].c_str());
  137. def2 = m_Makefile->GetDefinition(args[2].c_str());
  138. if (!def)
  139. {
  140. def = args[0].c_str();
  141. }
  142. if (!def2)
  143. {
  144. def2 = args[2].c_str();
  145. }
  146. if(atof(def) >= atof(def2))
  147. {
  148. f = new cmIfFunctionBlocker();
  149. }
  150. }
  151. if (args.size() == 3 && (args[1] == "GREATER"))
  152. {
  153. def = m_Makefile->GetDefinition(args[0].c_str());
  154. def2 = m_Makefile->GetDefinition(args[2].c_str());
  155. if (!def)
  156. {
  157. def = args[0].c_str();
  158. }
  159. if (!def2)
  160. {
  161. def2 = args[2].c_str();
  162. }
  163. if(atof(def) <= atof(def2))
  164. {
  165. f = new cmIfFunctionBlocker();
  166. }
  167. }
  168. if (args.size() == 3 && (args[1] == "STRLESS"))
  169. {
  170. def = m_Makefile->GetDefinition(args[0].c_str());
  171. def2 = m_Makefile->GetDefinition(args[2].c_str());
  172. if(strcmp(def,def2) >= 0)
  173. {
  174. f = new cmIfFunctionBlocker();
  175. }
  176. }
  177. if (args.size() == 3 && (args[1] == "STRGREATER"))
  178. {
  179. def = m_Makefile->GetDefinition(args[0].c_str());
  180. def2 = m_Makefile->GetDefinition(args[2].c_str());
  181. if(strcmp(def,def2) <= 0)
  182. {
  183. f = new cmIfFunctionBlocker();
  184. }
  185. }
  186. // if we created a function blocker then set its args
  187. if (f)
  188. {
  189. for(std::vector<std::string>::const_iterator j = args.begin();
  190. j != args.end(); ++j)
  191. {
  192. f->m_Args.push_back(*j);
  193. }
  194. m_Makefile->AddFunctionBlocker(f);
  195. }
  196. return true;
  197. }