cmMacroCommand.cxx 3.6 KB

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