cmCustomCommandGenerator.cxx 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2010 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmCustomCommandGenerator.h"
  11. #include "cmMakefile.h"
  12. #include "cmCustomCommand.h"
  13. #include "cmOutputConverter.h"
  14. #include "cmGeneratorExpression.h"
  15. //----------------------------------------------------------------------------
  16. cmCustomCommandGenerator::cmCustomCommandGenerator(
  17. cmCustomCommand const& cc, const std::string& config, cmMakefile* mf):
  18. CC(cc), Config(config), Makefile(mf),
  19. OldStyle(cc.GetEscapeOldStyle()), MakeVars(cc.GetEscapeAllowMakeVars()),
  20. GE(new cmGeneratorExpression(&cc.GetBacktrace())), DependsDone(false)
  21. {
  22. }
  23. //----------------------------------------------------------------------------
  24. cmCustomCommandGenerator::~cmCustomCommandGenerator()
  25. {
  26. delete this->GE;
  27. }
  28. //----------------------------------------------------------------------------
  29. unsigned int cmCustomCommandGenerator::GetNumberOfCommands() const
  30. {
  31. return static_cast<unsigned int>(this->CC.GetCommandLines().size());
  32. }
  33. //----------------------------------------------------------------------------
  34. std::string cmCustomCommandGenerator::GetCommand(unsigned int c) const
  35. {
  36. std::string const& argv0 = this->CC.GetCommandLines()[c][0];
  37. cmTarget* target = this->Makefile->FindTargetToUse(argv0);
  38. if(target && target->GetType() == cmTarget::EXECUTABLE &&
  39. (target->IsImported() || !this->Makefile->IsOn("CMAKE_CROSSCOMPILING")))
  40. {
  41. return target->GetLocation(this->Config);
  42. }
  43. return this->GE->Parse(argv0)->Evaluate(this->Makefile, this->Config);
  44. }
  45. //----------------------------------------------------------------------------
  46. std::string escapeForShellOldStyle(const std::string& str)
  47. {
  48. std::string result;
  49. #if defined(_WIN32) && !defined(__CYGWIN__)
  50. // if there are spaces
  51. std::string temp = str;
  52. if (temp.find(" ") != std::string::npos &&
  53. temp.find("\"")==std::string::npos)
  54. {
  55. result = "\"";
  56. result += str;
  57. result += "\"";
  58. return result;
  59. }
  60. return str;
  61. #else
  62. for(const char* ch = str.c_str(); *ch != '\0'; ++ch)
  63. {
  64. if(*ch == ' ')
  65. {
  66. result += '\\';
  67. }
  68. result += *ch;
  69. }
  70. return result;
  71. #endif
  72. }
  73. //----------------------------------------------------------------------------
  74. void
  75. cmCustomCommandGenerator
  76. ::AppendArguments(unsigned int c, std::string& cmd) const
  77. {
  78. cmCustomCommandLine const& commandLine = this->CC.GetCommandLines()[c];
  79. for(unsigned int j=1;j < commandLine.size(); ++j)
  80. {
  81. std::string arg = this->GE->Parse(commandLine[j])->Evaluate(this->Makefile,
  82. this->Config);
  83. cmd += " ";
  84. if(this->OldStyle)
  85. {
  86. cmd += escapeForShellOldStyle(arg);
  87. }
  88. else
  89. {
  90. cmOutputConverter converter(this->Makefile->GetStateSnapshot());
  91. cmd += converter.EscapeForShell(arg, this->MakeVars);
  92. }
  93. }
  94. }
  95. //----------------------------------------------------------------------------
  96. const char* cmCustomCommandGenerator::GetComment() const
  97. {
  98. return this->CC.GetComment();
  99. }
  100. //----------------------------------------------------------------------------
  101. std::string cmCustomCommandGenerator::GetWorkingDirectory() const
  102. {
  103. return this->CC.GetWorkingDirectory();
  104. }
  105. //----------------------------------------------------------------------------
  106. std::vector<std::string> const& cmCustomCommandGenerator::GetOutputs() const
  107. {
  108. return this->CC.GetOutputs();
  109. }
  110. //----------------------------------------------------------------------------
  111. std::vector<std::string> const& cmCustomCommandGenerator::GetByproducts() const
  112. {
  113. return this->CC.GetByproducts();
  114. }
  115. //----------------------------------------------------------------------------
  116. std::vector<std::string> const& cmCustomCommandGenerator::GetDepends() const
  117. {
  118. if (!this->DependsDone)
  119. {
  120. this->DependsDone = true;
  121. std::vector<std::string> depends = this->CC.GetDepends();
  122. for(std::vector<std::string>::const_iterator
  123. i = depends.begin();
  124. i != depends.end(); ++i)
  125. {
  126. cmsys::auto_ptr<cmCompiledGeneratorExpression> cge
  127. = this->GE->Parse(*i);
  128. std::vector<std::string> result;
  129. cmSystemTools::ExpandListArgument(
  130. cge->Evaluate(this->Makefile, this->Config), result);
  131. for (std::vector<std::string>::iterator it = result.begin();
  132. it != result.end(); ++it)
  133. {
  134. if (cmSystemTools::FileIsFullPath(it->c_str()))
  135. {
  136. *it = cmSystemTools::CollapseFullPath(*it);
  137. }
  138. }
  139. this->Depends.insert(this->Depends.end(), result.begin(), result.end());
  140. }
  141. }
  142. return this->Depends;
  143. }