cmForEachCommand.cxx 3.2 KB

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