cmMacroCommand.cxx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 "cmMacroCommand.h"
  14. bool cmMacroFunctionBlocker::
  15. IsFunctionBlocked(const char *name, const std::vector<std::string> &args,
  16. cmMakefile &mf)
  17. {
  18. // record commands until we hit the ENDMACRO
  19. // at the ENDMACRO call we shift gears and start looking for invocations
  20. if (!strcmp(name,"ENDMACRO") && args[0] == m_Args[0])
  21. {
  22. m_Executing = true;
  23. return true;
  24. }
  25. if (!m_Executing)
  26. {
  27. // if it wasn't an endmacro and we are not executing then we must be
  28. // recording
  29. m_Commands.push_back(name);
  30. std::vector<std::string> newArgs;
  31. for(std::vector<std::string>::const_iterator j = args.begin();
  32. j != args.end(); ++j)
  33. {
  34. newArgs.push_back(*j);
  35. }
  36. m_CommandArguments.push_back(newArgs);
  37. return true;
  38. }
  39. // otherwise the macro has been recorded and we are executing
  40. // so we look for macro invocations
  41. if (!strcmp(name,m_Args[0].c_str()))
  42. {
  43. // make sure the number of arguments matches
  44. if (args.size() != m_Args.size() - 1)
  45. {
  46. cmSystemTools::Error("A macro was invoked without the correct number of arguments. The macro name was: ", m_Args[0].c_str());
  47. }
  48. // for each recorded command
  49. for(unsigned int c = 0; c < m_Commands.size(); ++c)
  50. {
  51. // perform argument replacement
  52. std::vector<std::string> newArgs;
  53. // for each argument of this command
  54. for (std::vector<std::string>::const_iterator k =
  55. m_CommandArguments[c].begin();
  56. k != m_CommandArguments[c].end(); ++k)
  57. {
  58. // replace any matches with the formal arguments
  59. std::string tmps = *k;
  60. // for each formal macro argument
  61. for (unsigned int j = 1; j < m_Args.size(); ++j)
  62. {
  63. std::string variable = "${";
  64. variable += m_Args[j];
  65. variable += "}";
  66. cmSystemTools::ReplaceString(tmps, variable.c_str(),
  67. args[j-1].c_str());
  68. }
  69. newArgs.push_back(tmps);
  70. }
  71. // execute command
  72. mf.ExecuteCommand(m_Commands[c],newArgs);
  73. }
  74. return true;
  75. }
  76. // if not an invocation then it is just an ordinary line
  77. return false;
  78. }
  79. bool cmMacroFunctionBlocker::
  80. ShouldRemove(const char *, const std::vector<std::string> &,
  81. cmMakefile &)
  82. {
  83. return false;
  84. }
  85. void cmMacroFunctionBlocker::
  86. ScopeEnded(cmMakefile &)
  87. {
  88. // macros never leave scope
  89. }
  90. bool cmMacroCommand::InitialPass(std::vector<std::string> const& argsIn)
  91. {
  92. std::vector<std::string> args;
  93. cmSystemTools::ExpandListArguments(argsIn, args);
  94. if(args.size() < 1)
  95. {
  96. this->SetError("called with incorrect number of arguments");
  97. return false;
  98. }
  99. // create a function blocker
  100. cmMacroFunctionBlocker *f = new cmMacroFunctionBlocker();
  101. for(std::vector<std::string>::const_iterator j = args.begin();
  102. j != args.end(); ++j)
  103. {
  104. f->m_Args.push_back(*j);
  105. }
  106. m_Makefile->AddFunctionBlocker(f);
  107. return true;
  108. }