cmMacroCommand.cxx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html 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 cmListFileFunction& lff, cmMakefile &mf)
  16. {
  17. // record commands until we hit the ENDMACRO
  18. // at the ENDMACRO call we shift gears and start looking for invocations
  19. if(lff.m_Name == "ENDMACRO")
  20. {
  21. std::vector<std::string> expandedArguments;
  22. mf.ExpandArguments(lff.m_Arguments, expandedArguments);
  23. if(!expandedArguments.empty() && (expandedArguments[0] == m_Args[0]))
  24. {
  25. m_Executing = true;
  26. return true;
  27. }
  28. }
  29. if (!m_Executing)
  30. {
  31. // if it wasn't an endmacro and we are not executing then we must be
  32. // recording
  33. m_Functions.push_back(lff);
  34. return true;
  35. }
  36. // otherwise the macro has been recorded and we are executing
  37. // so we look for macro invocations
  38. if(lff.m_Name == m_Args[0])
  39. {
  40. // Expand the argument list to the macro.
  41. std::vector<std::string> expandedArguments;
  42. mf.ExpandArguments(lff.m_Arguments, expandedArguments);
  43. // make sure the number of arguments matches
  44. if (expandedArguments.size() != m_Args.size() - 1)
  45. {
  46. cmOStringStream error;
  47. error << "Error in cmake code at\n"
  48. << lff.m_FilePath << ":" << lff.m_Line << ":\n"
  49. << "Invocation of macro \""
  50. << lff.m_Name.c_str() << "\" with incorrect number of arguments.";
  51. cmSystemTools::Error(error.str().c_str());
  52. return true;
  53. }
  54. // Invoke all the functions that were collected in the block.
  55. for(unsigned int c = 0; c < m_Functions.size(); ++c)
  56. {
  57. // Replace the formal arguments and then invoke the command.
  58. cmListFileFunction newLFF;
  59. newLFF.m_Name = m_Functions[c].m_Name;
  60. newLFF.m_FilePath = m_Functions[c].m_FilePath;
  61. newLFF.m_Line = m_Functions[c].m_Line;
  62. for (std::vector<cmListFileArgument>::const_iterator k =
  63. m_Functions[c].m_Arguments.begin();
  64. k != m_Functions[c].m_Arguments.end(); ++k)
  65. {
  66. std::string tmps = k->Value;
  67. for (unsigned int j = 1; j < m_Args.size(); ++j)
  68. {
  69. std::string variable = "${";
  70. variable += m_Args[j];
  71. variable += "}";
  72. cmSystemTools::ReplaceString(tmps, variable.c_str(),
  73. expandedArguments[j-1].c_str());
  74. }
  75. cmListFileArgument arg(tmps, k->Quoted);
  76. newLFF.m_Arguments.push_back(arg);
  77. }
  78. if(!mf.ExecuteCommand(newLFF))
  79. {
  80. cmOStringStream error;
  81. error << "Error in cmake code at\n"
  82. << lff.m_FilePath << ":" << lff.m_Line << ":\n"
  83. << "A command failed during the invocation of macro \""
  84. << lff.m_Name.c_str() << "\".";
  85. cmSystemTools::Error(error.str().c_str());
  86. }
  87. }
  88. return true;
  89. }
  90. // if not an invocation then it is just an ordinary line
  91. return false;
  92. }
  93. bool cmMacroFunctionBlocker::
  94. ShouldRemove(const cmListFileFunction&, cmMakefile &)
  95. {
  96. return false;
  97. }
  98. void cmMacroFunctionBlocker::
  99. ScopeEnded(cmMakefile &)
  100. {
  101. // macros never leave scope
  102. }
  103. bool cmMacroCommand::InitialPass(std::vector<std::string> const& args)
  104. {
  105. if(args.size() < 1)
  106. {
  107. this->SetError("called with incorrect number of arguments");
  108. return false;
  109. }
  110. // create a function blocker
  111. cmMacroFunctionBlocker *f = new cmMacroFunctionBlocker();
  112. for(std::vector<std::string>::const_iterator j = args.begin();
  113. j != args.end(); ++j)
  114. {
  115. f->m_Args.push_back(*j);
  116. }
  117. m_Makefile->AddFunctionBlocker(f);
  118. return true;
  119. }