cmForEachCommand.cxx 3.2 KB

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