cmIfCommand.cxx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. std::string tmp = args[1];
  102. m_Makefile->ExpandVariablesInString(tmp);
  103. if(!cmSystemTools::FileExists(tmp.c_str()))
  104. {
  105. f = new cmIfFunctionBlocker();
  106. }
  107. }
  108. if (args.size() == 3 && (args[1] == "AND"))
  109. {
  110. def = m_Makefile->GetDefinition(args[0].c_str());
  111. def2 = m_Makefile->GetDefinition(args[2].c_str());
  112. if(cmSystemTools::IsOff(def) || cmSystemTools::IsOff(def2))
  113. {
  114. f = new cmIfFunctionBlocker();
  115. }
  116. }
  117. if (args.size() == 3 && (args[1] == "OR"))
  118. {
  119. def = m_Makefile->GetDefinition(args[0].c_str());
  120. def2 = m_Makefile->GetDefinition(args[2].c_str());
  121. if(cmSystemTools::IsOff(def) && cmSystemTools::IsOff(def2))
  122. {
  123. f = new cmIfFunctionBlocker();
  124. }
  125. }
  126. if (args.size() == 3 && (args[1] == "MATCHES"))
  127. {
  128. def = m_Makefile->GetDefinition(args[0].c_str());
  129. cmRegularExpression regEntry(args[2].c_str());
  130. // check for black line or comment
  131. if (!def || !regEntry.find(def))
  132. {
  133. f = new cmIfFunctionBlocker();
  134. }
  135. }
  136. // if we created a function blocker then set its args
  137. if (f)
  138. {
  139. for(std::vector<std::string>::const_iterator j = args.begin();
  140. j != args.end(); ++j)
  141. {
  142. f->m_Args.push_back(*j);
  143. }
  144. m_Makefile->AddFunctionBlocker(f);
  145. }
  146. return true;
  147. }