cmForEachCommand.cxx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 "cmForEachCommand.h"
  14. bool cmForEachFunctionBlocker::
  15. IsFunctionBlocked(const cmListFileFunction& lff, cmMakefile &mf)
  16. {
  17. // Prevent recusion and don't let this blobker block its own
  18. // commands.
  19. if (m_Executing)
  20. {
  21. return false;
  22. }
  23. // at end of for each execute recorded commands
  24. if (lff.m_Name == "ENDFOREACH")
  25. {
  26. std::vector<std::string> expandedArguments;
  27. mf.ExpandArguments(lff.m_Arguments, expandedArguments);
  28. if(!expandedArguments.empty() && (expandedArguments[0] == m_Args[0]))
  29. {
  30. m_Executing = true;
  31. std::string variable = "${";
  32. variable += m_Args[0];
  33. variable += "}";
  34. std::vector<std::string>::const_iterator j = m_Args.begin();
  35. ++j;
  36. for( ; j != m_Args.end(); ++j)
  37. {
  38. // Invoke all the functions that were collected in the block.
  39. for(unsigned int c = 0; c < m_Functions.size(); ++c)
  40. {
  41. // Replace the loop variable and then invoke the command.
  42. cmListFileFunction newLFF;
  43. newLFF.m_Name = m_Functions[c].m_Name;
  44. for (std::vector<cmListFileArgument>::const_iterator k =
  45. m_Functions[c].m_Arguments.begin();
  46. k != m_Functions[c].m_Arguments.end(); ++k)
  47. {
  48. std::string tmps = k->Value;
  49. cmSystemTools::ReplaceString(tmps, variable.c_str(), j->c_str());
  50. cmListFileArgument arg(tmps, k->Quoted);
  51. newLFF.m_Arguments.push_back(arg);
  52. }
  53. mf.ExecuteCommand(newLFF);
  54. }
  55. }
  56. return false;
  57. }
  58. }
  59. // record the command
  60. m_Functions.push_back(lff);
  61. // always return true
  62. return true;
  63. }
  64. bool cmForEachFunctionBlocker::
  65. ShouldRemove(const cmListFileFunction& lff, cmMakefile& mf)
  66. {
  67. if(lff.m_Name == "ENDFOREACH")
  68. {
  69. std::vector<std::string> expandedArguments;
  70. mf.ExpandArguments(lff.m_Arguments, expandedArguments);
  71. if(!expandedArguments.empty() && (expandedArguments[0] == m_Args[0]))
  72. {
  73. return true;
  74. }
  75. }
  76. return false;
  77. }
  78. void cmForEachFunctionBlocker::
  79. ScopeEnded(cmMakefile &mf)
  80. {
  81. cmSystemTools::Error("The end of a CMakeLists file was reached with a FOREACH statement that was not closed properly. Within the directory: ",
  82. mf.GetCurrentDirectory());
  83. }
  84. bool cmForEachCommand::InitialPass(std::vector<std::string> const& args)
  85. {
  86. if(args.size() < 1)
  87. {
  88. this->SetError("called with incorrect number of arguments");
  89. return false;
  90. }
  91. // create a function blocker
  92. cmForEachFunctionBlocker *f = new cmForEachFunctionBlocker();
  93. f->m_Args = args;
  94. m_Makefile->AddFunctionBlocker(f);
  95. return true;
  96. }